一开始,没敢写,感觉会超时。。。其实就是暴力搜索。DFS
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #define clc(a,b) memset(a,b,sizeof(a)) 8 #define N 100 9 #define eps 1e-6 10 #include<stack> 11 #include<deque> 12 #include<vector> 13 using namespace std; 14 const int MAXD = 1e5+10; 15 const int MAXM = 51; 16 const int INF = 0x3f3f3f3f; 17 typedef long long ll; 18 19 char G[10][10]; 20 int row[10][10],coloum[10][10],cr[10][10],row1[26]; 21 int t; 22 int sum; 23 void dfs(int x,int y) 24 { 25 if(x==9) 26 { 27 sum++; 28 return; 29 } 30 if(G[x][y]>=‘1‘&&G[x][y]<=‘9‘) 31 { 32 if(y==8) 33 dfs(x+1,0); 34 else 35 dfs(x,y+1); 36 return ; 37 } 38 else if(G[x][y]==‘e‘) 39 { 40 int flag=0; 41 for(int i=2; i<10; i=i+2) 42 { 43 if(!row[x][i]&&!coloum[y][i]&&!cr[(x/3)*3+y/3+1][i]) 44 { 45 flag=1; 46 G[x][y]=i+‘0‘; 47 row[x][i]=1; 48 coloum[y][i]=1; 49 cr[(x/3)*3+y/3+1][i]=1; 50 if(y==8) 51 dfs(x+1,0); 52 else 53 dfs(x,y+1); 54 G[x][y]=‘e‘; 55 row[x][i]=0; 56 coloum[y][i]=0; 57 cr[(x/3)*3+y/3+1][i]=0; 58 } 59 } 60 if(!flag) 61 return; 62 } 63 else if(G[x][y]==‘o‘) 64 { 65 int flag=0; 66 for(int i=1; i<=9; i=i+2) 67 { 68 if(!row[x][i]&&!coloum[y][i]&&!cr[(x/3)*3+y/3+1][i]) 69 { 70 flag=1; 71 G[x][y]=i+‘0‘; 72 row[x][i]=1; 73 coloum[y][i]=1; 74 cr[(x/3)*3+y/3+1][i]=1; 75 if(y==8) 76 dfs(x+1,0); 77 else 78 dfs(x,y+1); 79 G[x][y]=‘o‘; 80 row[x][i]=0; 81 coloum[y][i]=0; 82 cr[(x/3)*3+y/3+1][i]=0; 83 } 84 } 85 if(!flag) 86 return ; 87 } 88 else if(G[x][y]>=‘a‘&&G[x][y]<=‘z‘&&G[x][y]!=‘e‘&&G[x][y]!=‘o‘) 89 { 90 int num=G[x][y]-‘a‘; 91 if(row1[num]) 92 { 93 if(!row[x][row1[num]]&&!coloum[y][row1[num]]&&!cr[(x/3)*3+y/3+1][row1[num]]) 94 { 95 G[x][y]=row1[num]+‘0‘; 96 row[x][row1[num]]=1; 97 coloum[y][row1[num]]=1; 98 cr[(x/3)*3+y/3+1][row1[num]]=1; 99 if(y==8) 100 dfs(x+1,0); 101 else 102 dfs(x,y+1); 103 G[x][y]=num+‘a‘; 104 row[x][row1[num]]=0; 105 coloum[y][row1[num]]=0; 106 cr[(x/3)*3+y/3+1][row1[num]]=0; 107 } 108 else 109 return ; 110 } 111 else 112 { 113 int flag=0; 114 for(int i=1; i<=9; i++) 115 { 116 if(row[x][i]==0&&coloum[y][i]==0&&cr[(x/3)*3+y/3+1][i]==0) 117 { 118 flag=1; 119 row1[num]=i; 120 G[x][y]=i+‘0‘; 121 row[x][i]=1; 122 coloum[y][i]=1; 123 cr[(x/3)*3+y/3+1][i]=1; 124 if(y==8) 125 dfs(x+1,0); 126 else 127 dfs(x,y+1); 128 G[x][y]=num+‘a‘; 129 row1[num]=0; 130 row[x][i]=0; 131 coloum[y][i]=0; 132 cr[(x/3)*3+y/3+1][i]=0; 133 } 134 } 135 if(flag==0) 136 return ; 137 } 138 } 139 else if(G[x][y]==‘0‘) 140 { 141 int flag=0; 142 for(int i=1; i<=9; i++) 143 { 144 if(!row[x][i]&&!coloum[y][i]&&cr[(x/3)*3+y/3+1][i]==0) 145 { 146 flag=1; 147 G[x][y]=i+‘0‘; 148 row[x][i]=1; 149 coloum[y][i]=1; 150 cr[(x/3)*3+y/3+1][i]=1; 151 if(y==8) 152 dfs(x+1,0); 153 else 154 dfs(x,y+1); 155 G[x][y]=‘0‘; 156 row[x][i]=0; 157 coloum[y][i]=0; 158 cr[(x/3)*3+y/3+1][i]=0; 159 } 160 } 161 if(flag==0) 162 return; 163 } 164 } 165 166 int main() 167 { 168 //freopen("in.txt","r",stdin); 169 scanf("%d",&t); 170 while(t--) 171 { 172 sum=0; 173 clc(row,0); 174 clc(coloum,0); 175 clc(cr,0); 176 clc(row1,0); 177 for(int i=0;i<9;i++) 178 { 179 scanf("%s",G[i]); 180 } 181 for(int i=0; i<9; i++) 182 { 183 for(int j=0; j<9; j++) 184 { 185 186 if(G[i][j]>=‘1‘&&G[i][j]<=‘9‘) 187 { 188 row[i][G[i][j]-‘0‘]=1; 189 coloum[j][G[i][j]-‘0‘]=1; 190 cr[(i/3)*3+j/3+1][G[i][j]-‘0‘]=1; 191 } 192 } 193 } 194 dfs(0,0); 195 printf("%d\n",sum); 196 } 197 return 0; 198 }
时间: 2024-10-04 09:02:20