标签:des blog http io ar os sp for strong
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 14105 | Accepted: 6964 | Special Judge |
Description
Input
Output
Sample Input
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
Source
#include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<cstdio> using namespace std; const int n = 9; int board[9][9]; char ch[10]; bool ok(int x, int y) { for (int i = x / 3 * 3; i < x / 3 * 3 + 3; ++i) { for (int j = y / 3 * 3; j < y / 3 * 3 + 3; ++j) { if (x == i && y == j) continue; if (board[x][y] == board[i][j]) // the number has been used return false; } } int temp = board[x][y]; for (int j = 0; j < n; ++j) { if (j == y) continue; if (board[x][j] == temp) return false; } for (int i = 0; i < n; ++i) { if (i == x) continue; if (board[i][y] == temp) return false; } return true; } int dfs(int location) { if (location == -1) return 1; if (board[location / n][location % n] != 0) return dfs(location - 1); else { for (int i = 1; i <= n; ++i) { board[location / n][location % n] = i; if (ok(location / n, location % n)) { if (dfs(location - 1)) return 1; } board[location / n][location % n] = 0; } } return 0; } int main() { int nCases; scanf("%d", &nCases); while (nCases--) { for (int i = 0; i < n; ++i) { scanf("%s", ch); for (int j = 0; j < n; ++j) { board[i][j] = ch[j] - ‘0‘; } } dfs(80); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { printf("%d", board[i][j]); } printf("\n"); } } return 0; }
标签:des blog http io ar os sp for strong
原文地址:http://www.cnblogs.com/a972290869/p/4101043.html