Description
Input
Output
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
Source
#include<cstdio> #include<cstring> using namespace std; const int inf = 27; int visit[inf][inf]; int dir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; int flag ,p, q,ans; struct edge{ char s; int num; }a[inf*inf]; void dfs(int x,int y,int step) { if(flag == 0 ) return ; if(visit[x][y]) return ; visit[x][y] = 1; if(step == ans){ flag = 0; for(int i = 1; i <= ans;i++) printf("%c%d",a[i].s,a[i].num); printf("\n"); return ; } for(int i = 0; i < 8 ; i++){ int x1 = x + dir[i][0]; int y1 = y +dir[i][1]; if( x1 < 1||y1 < 1||x1 >p||y1 > q||visit[x1][y1] == 1) continue; a[step+1].s = 'A' + y1 - 1; a[step+1].num = x1; dfs(x1,y1,step+1); visit[x1][y1] = 0; } } int main() { int T,count = 1; scanf("%d",&T); while(T--){ memset(visit,0,sizeof(visit)); flag = 1; a[1].s = 'A',a[1].num = 1; scanf("%d%d",&p,&q); ans = p*q; printf("Scenario #%d:\n",count++); dfs(1,1,1); if(flag == 1) printf("impossible\n"); if(T!=0) printf("\n"); } return 0; }