-
Notifications
You must be signed in to change notification settings - Fork 12
/
2253.cpp
77 lines (73 loc) · 1.31 KB
/
2253.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;
int go[][2]=
{
-1,-2,
1 ,-2 ,
-2,-1,
2 ,-1,
-2, 1,
2 , 1,
-1, 2,
1 , 2
};
bool vis[30][30];
int path[30][2];//记录坐标
int sum;
int p,q;
bool isput;
void dfs(int x,int y,int step)
{// 1~p 1~q
if(isput)
return;
if(step>=sum)
{
for(int i=1;i<=sum;i++)
{
printf("%c%d",path[i][1]+'A'-1,path[i][0]);
}
printf("\n");
isput=true;
return;
}
for(int i=0;i<8;i++)
{
int nx=x+go[i][0];
int ny=y+go[i][1];
if(1<=nx&&nx<=p&&1<=ny&&ny<=q&&!vis[nx][ny])
{
vis[nx][ny]=true;
path[step+1][0]=nx;
path[step+1][1]=ny;
dfs(nx,ny,step+1);
vis[nx][ny]=false;
}
}
}
int main ()
{
int cas;
scanf("%d",&cas);
int cnt=1;
while(cas--)
{
scanf("%d %d",&p,&q);
memset(vis,false,sizeof(vis));
sum=q*p;
vis[1][1]=true;
path[1][0]=1;
path[1][1]=1;
isput=false;
printf("Scenario #%d:\n",cnt++);
dfs(1,1,1);
if(!isput)
{
printf("impossible\n");
}
printf("\n");
}
return 0;
}//Parsed in 0.137 seconds