poj2488DFS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 30
struct g
{
int x;
int y;
}path[MAX+100];
const int d[8][2] = {{-1, -2}, {1, -2}, {-2, -1}, {2, -1}, {-2, 1}, {2, 1}, {-1, 2}, {1, 2}};
//这里就处理好了字典序的问题,为什么?
int map[MAX][MAX];
int color[MAX][MAX];
int p,q,ok;
int out(int x,int y)
{
if(x<0||x>=p||y<0||y>=q)
return 1;//out
else
return 0;
}//出界
void dfs(int x,int y,int len)
{
int i;
int tx,ty;
if(len==(p*q))
{ok=1;return;}
for (i = 0; i < 8;i++)
{
tx = x + d[i][0];
ty = y + d[i][1];
if(color[tx][ty]==0&&out(tx,ty)==0&&ok==0)//没有出界并且没有走过
{
color[tx][ty]=1;
path[len].x=tx;
path[len].y=ty;
dfs(tx,ty,len+1);
color[tx][ty]=0;//dfs(tx,ty,len+1)失败了
}
}
}
int main()
{
int i,j,t;
scanf("%d",&t);
for(j=0;j<t;j++)
{
scanf("%d%d",&p,&q);
memset(map,0,sizeof(map));
memset(color,0,sizeof(color));
ok=0;//not success
path[0].x=0;
path[0].y=0;
color[0][0]=1;
dfs(0,0,1);
printf("Scenario #%d:\n",j+1);
if(ok==1)//1 success
{
for(i=0;i<p*q;i++)
printf("%c%d",(path[i].y)+'A',(path[i].x)+1);
printf("\n");
}
else
printf("impossible\n");
printf("\n");
}
return 0;
}
keep moving...

浙公网安备 33010602011771号