题目描述
题目大意
解数独。。。
样例输入
见代码垃圾markdown编辑器
样例输出
5 3 9 4 6 8 2 1 7
2 4 8 1 9 7 3 5 6
1 6 7 2 3 5 9 8 4
6 8 1 7 4 2 5 9 3
3 7 5 9 1 6 8 4 2
9 2 4 5 8 3 7 6 1
7 9 6 8 2 1 4 3 5
4 1 2 3 5 9 6 7 8
8 5 3 6 7 4 1 2 9
解题思路
搜索搜索
代码
/*
> < < < > <
v ^ v v ^ v ^ ^ v
< < < > < <
v ^ v ^ v v ^ ^ v
< < < < > >
< > > > < >
v v ^ ^ v ^ ^ v v
< > > < > >
^ v v v ^ v v ^ v
> < < > > >
< > > > > <
v v v v ^ ^ ^ ^ ^
> < < < < <
^ ^ ^ ^ ^ v v v ^
> > < > < <
*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
char Map[20][20];
bool vish[10][10];
bool viss[10][10];
bool visk[10][10];
int judg[10][10];//1:比左大,2:比上大
int vl[10][10];
int shoudonglisan[16]={0,1,2,2,3,3,4,5,5,6,6,7,8,8,9,9};
int shoudonglisanyzuobiao[7]={0,2,3,5,6,8,9};
void Init(){
for(int i=1;i<=15;i++){
for(int j=1;j<=((i%5==2||(i%5==4))?9:6);j++){
cin>>Map[i][j];
if(Map[i][j]=='^')
judg[shoudonglisan[i]][j]+=2;
if(Map[i][j]=='<')
judg[shoudonglisan[i]][shoudonglisanyzuobiao[j]]+=1;
}
}
}
inline int ff(int x,int y){int delta=3*((x-1)/3);return delta+(y-1)/3+1;}
inline int hh(int x,int y){x=x%3;if(!x)x+=3;y=y%3;if(!y)y+=3;return (x-1)*3+y;}
void dfs(int x,int y){
if(x==10){
for(int i=1;i<=9;i++,cout<<"\n")
for(int j=1;j<=9;j++)
cout<<vl[i][j]<<" ";
exit(0);
}
if(y==10){
dfs(x+1,1);
return;
}
for(int i=1;i<=9;i++){
if(vish[x][i])continue;
if(viss[y][i])continue;
if(visk[ff(x,y)][i])continue;
if(hh(x,y)==1);
else if(hh(x,y)==2||hh(x,y)==3){
if((judg[x][y]&1)==1&&i<vl[x][y-1])continue;
if((judg[x][y]&1)==0&&i>vl[x][y-1])continue;
}
else if(hh(x,y)==4||hh(x,y)==7){
if((judg[x][y]&2)==2&&i<vl[x-1][y])continue;
if((judg[x][y]&2)==0&&i>vl[x-1][y])continue;
}
else {
if((judg[x][y]&1)==1&&i<vl[x][y-1])continue;
if((judg[x][y]&1)==0&&i>vl[x][y-1])continue;
if((judg[x][y]&2)==2&&i<vl[x-1][y])continue;
if((judg[x][y]&2)==0&&i>vl[x-1][y])continue;
}
if(i==1&&hh(x,y)<=6&&(judg[x+1][y]&2)==0)continue;
if(i==1&&hh(x,y)!=3&&hh(x,y)!=6&&hh(x,y)!=9&&(judg[x][y+1]&1)==0)continue;
if(i==9&&hh(x,y)<=6&&(judg[x+1][y]&2)==2)continue;
if(i==9&&hh(x,y)!=3&&hh(x,y)!=6&&hh(x,y)!=9&&(judg[x][y+1]&1)==1)continue;
vish[x][i]=true;
viss[y][i]=true;
visk[ff(x,y)][i]=true;
vl[x][y]=i;
dfs(x,y+1);
vish[x][i]=false;
viss[y][i]=false;
visk[ff(x,y)][i]=false;
}
}
int main(){
memset(vish,0,sizeof(vish));
memset(viss,0,sizeof(viss));
memset(visk,0,sizeof(visk));
memset(judg,0,sizeof(judg));
Init();
dfs(1,1);
return 0;
}