poj 2488
1 #include<iostream> 2 #include <vector> 3 #include<string> 4 using namespace std; 5 6 int n, p, q, total; 7 int area[26][26]; 8 int dirX[8] = {-1, 1, -2, 2, -2, 2, -1, 1}; 9 int dirY[8] = {-2, -2, -1, -1, 1, 1, 2, 2}; 10 vector<int> orderX; 11 vector<int> orderY; 12 int dfs(int x, int y, int sum) 13 { 14 //Èç¹ûÊÇ×îºóÒ»¸öÇÒÒѾ³É¹¦; 15 if(sum == total) 16 { 17 orderX.push_back(x); 18 orderY.push_back(y); 19 return 1; 20 } 21 22 area[x][y] = 1; 23 int res = 0; 24 for(int i = 0; i < 8; ++i) 25 { 26 int nx = x+dirX[i]; 27 int ny = y+dirY[i]; 28 if(nx >= 0 && nx < p && ny >= 0 && ny < q && area[nx][ny] == -1) 29 { 30 int temp = sum+1; 31 res = dfs(nx, ny, temp); 32 if(res) 33 { 34 orderX.push_back(x); 35 orderY.push_back(y); 36 break; 37 } 38 } 39 } 40 if(!res) 41 area[x][y] = -1; 42 return res; 43 } 44 45 void solver(int no) 46 { 47 memset(area, -1, sizeof(int)*676); 48 cout << "Scenario #"<< no <<":" << endl; 49 orderX.clear(); 50 orderY.clear(); 51 if(dfs(0, 0, 1)) 52 { 53 54 for(int i = orderX.size()-1; i >= 0; --i) 55 { 56 char c = 65 + orderY[i]; 57 cout << c << orderX[i]+1; 58 } 59 } 60 else 61 { 62 cout << "impossible"; 63 } 64 cout << endl << endl;; 65 } 66 67 int main() 68 { 69 cin >> n; 70 for(int i = 0; i < n; ++i) 71 { 72 cin >> p >> q; 73 total = p * q; 74 solver(i+1); 75 } 76 return 0; 77 }