省赛的D题。。。。一年前的题目了。。。思路早就问过侯神了,就是讨论各种情况。。。早就想写一下,一直拖着。。今天乱写了一下。。终于过了。。写的太繁琐了,而且出现BUG了过不了样例,又加了一个判断,过了样例就AC了。。。
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 using namespace std; 6 int o[51][51],n,m,z; 7 int a[4] = {0,0,1,-1}; 8 int b[4] = {1,-1,0,0}; 9 char p[31][31]; 10 int judge(int x,int y) 11 { 12 int i,t = 0; 13 if(o[x][y]) 14 t ++; 15 for(i = 0;i < 4;i ++) 16 { 17 if(x+a[i] >= 0&&x+a[i] < n&&y+b[i] >= 0&&y+b[i] < m) 18 { 19 if(o[x+a[i]][y+b[i]]) 20 t ++; 21 } 22 } 23 if(t == p[x][y]-'0') 24 return 1; 25 else 26 return 0; 27 } 28 void dfs(int x,int y) 29 { 30 int i,j,r,c,num,temp; 31 if(x-2 >= 0)//判断一下已知的点是否符合 32 { 33 if(!judge(x-2,y)) 34 return ; 35 } 36 if(x == n) 37 { 38 z = 1; 39 for(i = 0;i < n;i ++) 40 { 41 for(j = 0;j < m;j ++) 42 { 43 if(o[i][j]) 44 printf("*"); 45 else 46 printf("."); 47 } 48 printf("\n"); 49 } 50 } 51 if(z) return ; 52 if(y+1 == m) 53 { 54 r = x+1; 55 c = 0; 56 } 57 else 58 { 59 r = x; 60 c = y + 1; 61 } 62 if(x == n-1) 63 { 64 if(judge(x,y)) 65 dfs(r,c); 66 } 67 else if(o[x][y] != -1) 68 { 69 num = 0; 70 temp = p[x][y] - '0'; 71 if(x-1 >= 0) 72 num += o[x-1][y]; 73 if(y-1 >= 0) 74 num += o[x][y-1]; 75 if(o[x][y]) 76 num ++; 77 if(num > temp) 78 return ; 79 else if(num == temp) 80 { 81 o[x][y+1] = 0; 82 o[x+1][y] = 0; 83 if(judge(x,y)) 84 dfs(r,c); 85 } 86 else if(temp - num == 1) 87 { 88 o[x][y+1] = 1; 89 o[x+1][y] = 0; 90 if(judge(x,y)) 91 dfs(r,c); 92 o[x][y+1] = 0; 93 o[x+1][y] = 1; 94 if(judge(x,y)) 95 dfs(r,c); 96 } 97 else if(temp - num == 2) 98 { 99 o[x][y+1] = 1; 100 o[x+1][y] = 1; 101 if(judge(x,y)) 102 dfs(r,c); 103 } 104 } 105 else 106 { 107 num = 0; 108 temp = p[x][y] - '0'; 109 if(x-1 >= 0) 110 num += o[x-1][y]; 111 if(y-1 >= 0) 112 num += o[x][y-1]; 113 if(num > temp) 114 return ; 115 else if(num == temp) 116 { 117 o[x][y] = 0; 118 o[x][y+1] = 0; 119 o[x+1][y] = 0; 120 if(judge(x,y)) 121 dfs(r,c); 122 } 123 else if(temp - num == 1) 124 { 125 o[x][y] = 1; 126 o[x][y+1] = 0; 127 o[x+1][y] = 0; 128 if(judge(x,y)) 129 dfs(r,c); 130 o[x][y] = 0; 131 o[x][y+1] = 1; 132 o[x+1][y] = 0; 133 if(judge(x,y)) 134 dfs(r,c); 135 o[x][y] = 0; 136 o[x][y+1] = 0; 137 o[x+1][y] = 1; 138 if(judge(x,y)) 139 dfs(r,c); 140 } 141 else if(temp - num == 2) 142 { 143 o[x][y] = 1; 144 o[x][y+1] = 1; 145 o[x+1][y] = 0; 146 if(judge(x,y)) 147 dfs(r,c); 148 o[x][y] = 1; 149 o[x][y+1] = 0; 150 o[x+1][y] = 1; 151 if(judge(x,y)) 152 dfs(r,c); 153 o[x][y] = 0; 154 o[x][y+1] = 1; 155 o[x+1][y] = 1; 156 if(judge(x,y)) 157 dfs(r,c); 158 } 159 else if(temp - num == 3) 160 { 161 o[x][y] = 1; 162 o[x][y+1] = 1; 163 o[x+1][y] = 1; 164 if(judge(x,y)) 165 dfs(r,c); 166 } 167 } 168 return ; 169 } 170 int main() 171 { 172 173 int t,i,num = 0; 174 scanf("%d",&t); 175 while(t--) 176 { 177 z = 0; 178 num ++; 179 scanf("%d%d",&n,&m); 180 memset(o,-1,sizeof(o)); 181 for(i = 0; i < n; i ++) 182 scanf("%s",p[i]); 183 printf("Case %d:\n",num); 184 dfs(0,0); 185 } 186 return 0; 187 } 188 /* 189 3 190 2 2 191 21 192 21 193 */