标签:
Description
Input
Output
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
1 #include <stdio.h> 2 #include <string.h> 3 4 int main() 5 { 6 int T; 7 int i,j,k,p,q,z,flg,u,v,cas=0; 8 int x[100],y[100],t[100],d[10][10]; 9 int a[9]={0,-1,1,-2,2,-2,2,-1,1}; 10 int b[9]={0,-2,-2,-1,-1,1,1,2,2}; 11 12 scanf("%d",&T); 13 while(T--) 14 { 15 memset(t,0,sizeof(t)); 16 memset(d,0,sizeof(d)); 17 scanf("%d %d",&p,&q); 18 i=1;z=0; 19 x[i]=1,y[i]=1,d[i][i]=1; 20 while(i>0) 21 { 22 flg=0; 23 for(k=t[i]+1;k<=8;k++) 24 { 25 u=x[i]+a[k],v=y[i]+b[k]; 26 if(u>0 && u<=p && v>0 && v<=q && d[u][v]==0) 27 { 28 x[i+1]=u,y[i+1]=v,d[u][v]=1; 29 t[i]=k; 30 flg=1; 31 break; 32 } 33 } 34 35 if(flg==1 && i==p*q-1) 36 { 37 z=1; 38 break; 39 } 40 41 else if(flg==1) 42 i++; 43 else 44 { 45 t[i]=d[x[i]][y[i]]=0; 46 i--; 47 } 48 } 49 if(p==1 && q==1) 50 z=1; 51 printf("Scenario #%d:\n",++cas); 52 if(z==1) 53 { 54 for(i=1;i<=p*q;i++) 55 { 56 printf("%c%d",‘A‘+y[i]-1,x[i]); 57 } 58 printf("\n"); 59 } 60 else 61 { 62 printf("impossible\n"); 63 } 64 printf("\n"); 65 } 66 return 0; 67 }
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4771404.html