标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1064 Accepted Submission(s): 362
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; char s[5][5]; int ans[5][5]; int l[5][5]; int r[5][5]; int p[5][5]; int judge(int x,int y) { if(x<2&&y<2) return 1; if(x>=2&&y<2) return 3; if(x<2&&y>=2) return 2; if(x>=2&&y>=2) return 4; } void solve() { for(int i = 0; i<4; i++) for(int j = 0; j<4; j++) { l[i][ans[i][j]] = 1; } for(int j = 0; j<4; j++) for(int i = 0; i<4; i++) { r[j][ans[i][j]] = 1; } for(int i = 0; i<4; i++) { for(int j = 0; j<4; j++) { p[judge(i,j)][ans[i][j]] = 1; } } int flag = 1; int count = 0; while(flag) { flag = 0; for(int i = 0; i<4; i++) { for(int j = 0; j<4; j++) { count = 0; if(ans[i][j]) continue; for(int k = 1; k<=4; k++) { if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0) count++; } if(count == 1) { flag = 1; for(int k = 1; k<=4; k++) { if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0) { ans[i][j] = k; l[i][k] = 1; r[j][k] = 1; p[judge(i,j)][k] = 1; } } } } } } } int main() { int t,kase = 0; scanf("%d",&t); while(t--) { memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); memset(ans,0,sizeof(ans)); memset(p,0,sizeof(p)); for(int i = 0; i<4; i++) { scanf("%s",s[i]); } for(int i = 0; i<4; i++) { for(int j = 0; j<4; j++) { if(s[i][j] == ‘*‘) ans[i][j] = 0; else ans[i][j] = s[i][j] - ‘0‘; } } solve(); printf("Case #%d:\n",++kase); for(int i = 0; i<4; i++) { for(int j = 0; j<4; j++) { printf("%d",ans[i][j]); } printf("\n"); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/littlepear/p/5669174.html