1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include<bits/stdc++.h> using namespace std; int n; string s[15]; int fc[15][2], fr[15][2]; bool chk(int x, int y){ if(x > 2&&s[x-1][y]==s[x][y]&&s[x-2][y]==s[x][y]) return false; if(y > 2&&s[x][y-1]==s[x][y]&&s[x][y-2]==s[x][y]) return false; return true; } int flag = 0; int A[1050], B[1050]; void dfs(int x, int y){ if(flag) return; if(x == n+1 && y == 1){ int res = 1; for(int i = 1; i <= n; i++){ if(fr[i][0]!=n/2 || fr[i][1]!=n/2) res = 0; if(fc[i][0]!=n/2 || fc[i][1]!=n/2) res = 0; } if(res == 0) return; memset(A, 0, sizeof(A)); for(int i = 1; i <= n; i++){ int tmp = 0; for(int j = 1; j <= n; j++){ tmp = tmp * 2 + s[i][j]-'0'; } if(A[tmp]!=0) res = 0; A[tmp]++; } memset(B, 0, sizeof(B)); for(int i = 1; i <= n; i++){ int tmp = 0; for(int j = 1; j <= n; j++){ tmp = tmp * 2 + s[j][i]-'0'; } if(B[tmp]!=0) res = 0; B[tmp]++; } if(res == 0) return; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(i + 2 <= n){ if(s[i][j] == s[i+1][j]&&s[i][j] == s[i+2][j]) res = 0; } if(j + 2 <= n){ if(s[i][j] == s[i][j+1]&&s[i][j] == s[i][j+2]) res = 0; } } } if(res == 1){ flag = 1; for(int i = 1; i <=n; i++){ for(int j = 1; j <= n; j++){ cout<<s[i][j]; } cout<<endl; } } return; } int nx = x, ny = y + 1; if(ny == n+1){ nx = x + 1; ny = 1; }
if(s[x][y] != '_'){ dfs(nx, ny); return; } s[x][y] = '0'; fr[x][0]++; fc[y][0]++; if(fr[x][0]<=n/2&&fc[y][0]<=n/2&&chk(x, y)) dfs(nx, ny); fr[x][0]--; fc[y][0]--; s[x][y] = '1'; fr[x][1]++; fc[y][1]++; if(fr[x][1]<=n/2&&fc[y][1]<=n/2&&chk(x, y)) dfs(nx, ny); fr[x][1]--; fc[y][1]--; s[x][y] = '_'; return; } int main() { cin>>n; for(int i = 1; i <= n; i++){ cin>>s[i]; s[i] = ' '+ s[i]; for(int j = 1; j <= n; j++){ if(s[i][j] == '0'){ fr[i][0]++; fc[j][0]++; }else if(s[i][j] == '1'){ fr[i][1]++; fc[j][1]++; } } } dfs(1, 1); return 0; }
|