题目地址 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2764
就是让你写一个计算数独的程序 这个程序有几个条件
1 输入o处填奇数
2 输入e处天偶数
3 输入0处随便填
4 输入相同小写字符的地方要写相同的数
5 每一个3*3的小矩阵里1-9每个数都有 每一行每一列1-9都有
计算一共有多少种符合条件的数独种类数量
解法:暴力搜索 分析每次搜索其实就是条件哪几种情况分别处理就行
注意可以把每一个小矩阵可以按照对应下表去搞一个标记矩阵
bool bok[i][j] 表示 第i个矩阵是否标记过j
对于下标 x y 对应就是第(x/3*3 + y/3 )个矩阵
还有一个地方不好处理 就是对于第4个条件 这里我们需要在搞一个aph数组
记录某字符下存的数是多少 第一次遇到通常放
下次遇到相同字符直接拿出来判断 可行就放
不可行回溯回去修改原来的数
code:
//1 数字非0->跳过
//2 0 进入模式搜索
//3 o 进入奇数搜索
//4 e 进入偶数搜索
//5 其他alp字符 找之前记录的数字 如果没有重新搜索一个新数字
#include<bits/stdc++.h>
using namespace std;
char p[12][12];
int ans[12][12],c;// 结果矩阵
bool bok[12][10];//9 个正方形 每个里面有1-9
int aph[30];
void dfs(int x,int y);
void next(int x,int y)
{
if(y<8&&x<=8)dfs(x,y+1);
else if(y==8&&x<8)dfs(x+1,0);
else if(y==8&&x==8){
c++;
return;
}
}
bool check(int x,int y,int n)
{
for(int i=0;i<9;i++)
if(ans[x][i]==n)return 0;
for(int i=0;i<9;i++)
if(ans[i][y]==n)return 0;
return 1;
}
void dfs(int x,int y)
{
int d = x/3*3+y/3;
if(p[x][y]=='0'){
for(int i=1;i<10;i++){
if(check(x,y,i)&&!bok[d][i]){// 检查同行同列 和矩阵标记信息
bok[d][i]=1;
ans[x][y]=i;
next(x,y);
bok[d][i]=0;
ans[x][y]=0;
}
}
}
else if(p[x][y]=='e'){
for(int i=2;i<10;i+=2){
if(check(x,y,i)&&!bok[d][i])
{
bok[d][i]=1;
ans[x][y]=i;
next(x,y);
bok[d][i]=0;
ans[x][y]=0;
}
}
}
else if(p[x][y]=='o'){
for(int i=1;i<10;i+=2){
if(check(x,y,i)&&!bok[d][i]){
bok[d][i]=1;
ans[x][y]=i;
next(x,y);
bok[d][i]=0;
ans[x][y]=0;
}
}
}
else if(islower(p[x][y])){
int t = p[x][y]-'a';
if(aph[t]==0)
{// 如果没放过
for(int i=1;i<10;i++)
if(!bok[d][i]&&check(x,y,i)){
bok[d][i]=1;
ans[x][y] = i;
aph[t]=i;
next(x,y);
aph[t]=0;
bok[d][i]=0;
ans[x][y] = 0;
}
}
else {//放过
if(!bok[d][aph[t]]&&check(x,y,aph[t])){//可行
ans[x][y]=aph[t];
bok[d][aph[t]]=1;
next(x,y);
bok[d][aph[t]]=0;
ans[x][y] = 0;
}
else return;//不可行
}
}
else next(x,y);
}
int main()
{
int t;
cin>>t;
getchar();
while(t--)
{
c=0;
memset(bok,0,sizeof(bok));
memset(ans,0,sizeof(ans));
memset(aph,0,sizeof(aph));
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
scanf("%c",&p[i][j]);
int d = i/3*3+j/3;
if(p[i][j]!='0'&&isdigit(p[i][j])){
ans[i][j]=p[i][j]-'0';
bok[d][p[i][j]-'0']=1;
}
}
getchar();
}
dfs(0,0);
cout<<c<<endl;
}
return 0;
}