http://poj.org/problem?id=2488

问从8*8的矩阵上某一点,以某一方向两格,而另一个方向一格的方式前进,出发能否遍历整个矩阵,若能则输出遍历的路线。

采用深搜遍历,但要注意此题有一个隐性要求遍历路线要字典序最小。

1 #include<stdio.h>
2  int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1}; /*注意此处的数组数据,
3 为了保证每次的探索都是
4 符合字典序的*/
5 int g,a,b;
6 int vist[26][26],path[26][2];
7 void find(int i,int j,int k)
8 {
9 if(k==a*b)
10 {
11 for(int i=0;i<k;i++)
12 printf("%c%d",path[i][0]+'A',path[i][1]+1);
13 printf("\n");
14 g=1;
15 }
16 else
17 for(int x=0;x<8;x++)
18 {
19 int n=i+dir[x][0];
20 int m=j+dir[x][1];
21 if(n>=0&&n<b&&m>=0&&m<a&&!vist[n][m]&&!g)
22 {
23 vist[n][m]=1;
24 path[k][0]=n,path[k][1]=m;
25 find(n,m,k+1);
26 vist[n][m]=0;
27 }
28 }
29 }
30 int main()
31 {
32 int n;
33 scanf("%d",&n);
34 for(int m=0;m<n;m++)
35 {
36 g=0;
37 scanf("%d %d",&a,&b);
38 for(int i=0;i<a;i++)
39 for(int j=0;j<b;j++)
40 vist[i][j]=0;
41 vist[0][0]=1;
42 path[0][0]=0,path[0][1]=0;
43 printf("Scenario #%d:\n",m+1);
44 find(0,0,1);
45 if(!g) printf("impossible\n");
46 printf("\n");
47 }
48 return 0;
49 }

相关文章:

  • 2021-10-25
  • 2022-12-23
  • 2021-08-22
  • 2021-10-30
  • 2021-08-09
  • 2022-12-23
猜你喜欢
  • 2021-08-04
  • 2021-08-21
  • 2022-12-23
  • 2021-08-10
  • 2022-12-23
  • 2021-11-26
相关资源
相似解决方案