标签:
Description
Input
Output
Sample Input
3
1 1
2 3
4 3
Sample Output
Scenario #1:
A1
Scenario #2:
impossible
Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
Source
要求字典序最小,那么暴力枚举起点然后DFS就行。沿途存下路径,最后转化成字符输出。
1 /**/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 using namespace std; 8 const int mxn=125; 9 int vis[mxn][mxn]; 10 bool flag; 11 int mx[9]={0,-2,-2,-1,-1,1,1,2,2}, 12 my[9]={0,-1,1,-2,2,-2,2,-1,1}; 13 int n,m; 14 int qx[mxn],qy[mxn]; 15 void dfs(int x,int y,int cnt){ 16 qx[cnt]=x;qy[cnt]=y; 17 vis[x][y]=1; 18 if(cnt==m*n){ 19 flag=1; 20 return; 21 } 22 for(int i=1;i<=8;i++){ 23 int nx=mx[i]+x; 24 int ny=my[i]+y; 25 if(nx<1 || nx>m || ny<1 || ny>n)continue; 26 if(vis[nx][ny])continue; 27 dfs(nx,ny,cnt+1); 28 if(flag)return; 29 } 30 vis[x][y]=0; 31 return; 32 } 33 int main(){ 34 int T; 35 scanf("%d",&T); 36 int cas=0; 37 for(cas=1;cas<=T;cas++){ 38 memset(vis,0,sizeof vis); 39 flag=0; 40 int i,j; 41 scanf("%d%d",&n,&m); 42 for(i=1;i<=m;i++){ 43 if(flag)break; 44 for(j=1;j<=n;j++){ 45 dfs(i,j,1); 46 if(flag)break; 47 } 48 } 49 printf("Scenario #%d:\n",cas); 50 if(flag){ 51 for(i=1;i<=n*m;i++)printf("%c%d",qx[i]+‘A‘-1,qy[i]); 52 } 53 else printf("impossible"); 54 printf("\n"); 55 if(cas<T)printf("\n"); 56 } 57 return 0; 58 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5719432.html