A knight's journey
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:15815 |
|
Accepted:5295 |
Description
Background
The knight is getting bored of seeing the same black and white squares again and has decided to make a journey
Around the world. whenever a knight moves, it is two squares in one direction and one square perpendicular to this. the world of a knight is the chessboard he is living on. our knight lives on a chessboard that has a smaller area than a regular 8*8 board, but it is still rectangular. can you help this adventurous knight to make travel plans?
Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the Board.
Input
The input begins with a positive integer N in the first line. the following lines contain N test cases. each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. this represents a p * q chessboard, where p describes how between different square numbers 1 ,..., P exist, Q describes how many different square letters exist. these are the first Q letters of the Latin alphabet: ,...
Output
The output for every scenario begins with a line ining "Scenario # I:", where I is the number of the scenario starting at 1. then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. the path shoshould be given on a single line by concatenating the names of the visited squares. each square name consists of a capital letter followed by a number.
If no such path exist, you shoshould output impossible on a single line.
Sample Input
31 12 34 3
Sample output
Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4
The question is to give a board with a specification less than 8x8, and judge whether a knight who can only go through the "day" can travel the entire board without repeating it. If yes, the path is output in the Lexicographic Order, otherwise, "impossible" is output"
This is a search question that can be directly solved using DFS. Search from left to right each time, from top to bottom, and mark the searched place;
Reference code:
#include<iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct prog {char a ;int b ;}tra;struct proglu{char str[900];}map;bool hash[900];bool found=false;int p , q ;int number=0;void DFS(prog tmp , int n ,proglu map ,int k){if(found)return ;if(n==1){int i;cout<<"Scenario #"<<number<<":"<<endl;for(i=0;i<k;i++){cout<<map.str[i];}cout<<endl;found=true;}int t=(tmp.a-'A')*30+tmp.b;hash[t]=true;prog tmp2;tmp2=tmp;tmp2.a-=2;tmp2.b--;if(tmp2.a>='A'&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}tmp2=tmp;tmp2.a-=2;tmp2.b++;if(tmp2.a>='A'&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}tmp2=tmp;tmp2.a-=1;tmp2.b-=2;if(tmp2.a>='A'&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}tmp2=tmp;tmp2.a-=1;tmp2.b+=2;if(tmp2.a>='A'&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}tmp2=tmp;tmp2.a+=1;tmp2.b-=2;if(tmp2.a<='A'+p-1&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}tmp2=tmp;tmp2.a+=1;tmp2.b+=2;if(tmp2.a<='A'+p-1&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}tmp2=tmp;tmp2.a+=2;tmp2.b-=1;if(tmp2.a<='A'+p-1&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}tmp2=tmp;tmp2.a+=2;tmp2.b+=1;if(tmp2.a<='A'+p-1&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false){map.str[k]=tmp2.a;map.str[k+1]=tmp2.b+'0';DFS(tmp2,n-1,map,k+2);hash[(tmp2.a-'A')*30+tmp2.b]=false;}}int main(){int k = 1 ;int t;cin>>t;while(t--){number++;cin >> q >> p ;if(number!=1)cout<<endl;memset(hash,false,sizeof(hash)) ;tra.a='A';tra.b=1;memset(map.str,0,sizeof(map.str));map.str[0]='A';map.str[1]='1';found=false;DFS(tra,p*q,map,2); if(!found){cout<<"Scenario #"<<number<<":"<<endl;cout<<"impossible"<<endl;}}return 0;}