搜索,没什么好说的。要注意读入。
Code:
1 #include<cstdio> 2 #include<cstdlib> 3 using namespace std; 4 const int num[10][10]= 5 {{0,0,0,0,0,0,0,0,0,0}, 6 {0,1,1,1,2,2,2,3,3,3}, 7 {0,1,1,1,2,2,2,3,3,3}, 8 {0,1,1,1,2,2,2,3,3,3}, 9 {0,4,4,4,5,5,5,6,6,6}, 10 {0,4,4,4,5,5,5,6,6,6}, 11 {0,4,4,4,5,5,5,6,6,6}, 12 {0,7,7,7,8,8,8,9,9,9}, 13 {0,7,7,7,8,8,8,9,9,9}, 14 {0,7,7,7,8,8,8,9,9,9}}; 15 int map[10][10][10][10],vish[10][10],visl[10][10],visn[10][10],chess[10][10],h=1,l=2; 16 char s[5]; 17 void Read() 18 { 19 for(int k=1;k<=3;k++) 20 { 21 for(int j=1;j<=2;j++) 22 { 23 for(int i=1;i<=3;i++) 24 { 25 scanf("%s",s); 26 map[h][l][h][l-1]= ( s[0]=='>' ? -1 : 1 ); 27 scanf("%s",s); 28 map[h][l+1][h][l]= ( s[0]=='<' ? 1 : -1 ); 29 l+=3; 30 } 31 for(int i=1;i<=9;i++) 32 { 33 scanf("%s",s); 34 map[h+1][i][h][i]= ( s[0]=='v' ? -1 : 1 ); 35 } 36 h++;l=2; 37 } 38 for(int i=1;i<=3;i++) 39 { 40 scanf("%s",s); 41 map[h][l][h][l-1]= ( s[0]=='>' ? -1 : 1 ); 42 scanf("%s",s); 43 map[h][l+1][h][l]= ( s[0]=='<' ? 1 : -1 ); 44 l+=3; 45 } 46 h++;l=2; 47 } 48 } 49 void print() 50 { 51 for(int i=1;i<=9;i++) 52 { 53 for(int j=1;j<9;j++) 54 printf("%d ",chess[i][j]); 55 printf("%d ",chess[i][9]); 56 } 57 } 58 bool check(const int &X,const int &Y,const int &Val) 59 { 60 if(map[X][Y][X][Y-1]==1) 61 if(Val<=chess[X][Y-1])return false; 62 if(map[X][Y][X][Y-1]==-1) 63 if(Val>=chess[X][Y-1])return false; 64 if(map[X][Y][X-1][Y]==1) 65 if(Val<=chess[X-1][Y])return false; 66 if(map[X][Y][X-1][Y]==-1) 67 if(Val>=chess[X-1][Y])return false; 68 return true; 69 } 70 void dfs(int x,int y) 71 { 72 if(x==10) 73 { 74 print(); 75 exit(0); 76 } 77 for(int i=1;i<=9;i++) 78 if(!vish[x][i]&&!visl[y][i]&&!visn[num[x][y]][i]) 79 if(check(x,y,i)) 80 { 81 chess[x][y]=i; 82 vish[x][i]=visl[y][i]=visn[num[x][y]][i]=true; 83 if(y==9) 84 dfs(x+1,1); 85 else 86 dfs(x,y+1); 87 vish[x][i]=visl[y][i]=visn[num[x][y]][i]=false; 88 } 89 } 90 int main() 91 { 92 Read(); 93 dfs(1,1); 94 return 0; 95 }