poj 2488:A Knight's Journey

總時間限制: 
1000ms 
內存限制: 
65536kB
描述
Background
The knight is getting bored of seeing the same black and white squares again and 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.
輸入
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 many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .
輸出
The output for every scenario begins with a line containing "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 should 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 should output impossible on a single line.
樣例輸入
3
1 1
2 3
4 3
樣例輸出
Scenario #1:
A1

Scenario #2:
impossible

Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4


思路:這是一個簡單的搜索題,馬是按國際象棋的走法,即“兩橫一豎”或“兩豎一橫”,則馬一共有八種走法,在代碼中用move函數表示。move的走法,按照題中給出的字典順序進行搜索,即b-2,b-1……。只要按字典順序搜索,則找到的第一條路徑就是符合題目要求的輸出,不需要按照回溯方法遍歷整個圖。遞歸時,首先判斷是否已經找到路徑,再判斷該路徑是否已經走過,然後判斷馬是否走出邊界。然後根據上未走的格子數判斷馬是否已經走完全部格子,若全部走完,則輸出返回,否則將走過的該格子填入vector中,並進行相應處理,然後繼續遞歸,最後根據回溯的方法,返回操作。

注意:

1.這裏要注意輸出是需要按字典順序的,即不需要遍歷搜索整個圖,只需要找到按字典順序輸出的第一條記錄即可,這就需要安排好搜索的路徑,否則還要對輸出進行排序,增加時間。

2.進行數組操作的時候注意不要產生runtime error。


代碼如下:

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
char qletter[27]={'0','A','B','C','D','E','F','G','H','I','J',
<span style="white-space:pre">	</span>'K','L','M','N','O','P','Q','R','S','T','U',
<span style="white-space:pre">		</span>'V','W','X','Y','Z'};
int CHESS[27][27];
int totalN,p,q;
char buf[10];
vector<string> result;
bool isfind;

void clear(int ix, int jy){
<span style="white-space:pre">	</span>++ix;
<span style="white-space:pre">	</span>++jy;
<span style="white-space:pre">	</span>for (int i = 0; i < ix; ++i)
<span style="white-space:pre">		</span>for(int j = 0; j < jy; ++j)
<span style="white-space:pre">			</span>CHESS[i][j] = 0;
}

void move(int a, int b){
<span style="white-space:pre">	</span>if (isfind)
<span style="white-space:pre">		</span>return;
<span style="white-space:pre">	</span>if (CHESS[a][b])
<span style="white-space:pre">		</span>return;
<span style="white-space:pre">	</span>if (a < 1 || b < 1 || a > p || b > q)
<span style="white-space:pre">		</span>return;
<span style="white-space:pre">	</span>CHESS[a][b] = 1;
<span style="white-space:pre">	</span>--totalN;
<span style="white-space:pre">	</span>sprintf(buf, "%c%d", qletter[b], a);
<span style="white-space:pre">	</span>string string_buf(buf);
<span style="white-space:pre">	</span>result.push_back(string_buf);
<span style="white-space:pre">	</span>if (totalN == 0){
<span style="white-space:pre">		</span>isfind = true;
<span style="white-space:pre">		</span>for (vector<string>::iterator it = result.begin(); it != result.end(); ++it){
<span style="white-space:pre">			</span>cout << *it;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>cout << endl;<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>//result.pop_back();
<span style="white-space:pre">		</span>//CHESS[a][b] = 0;
<span style="white-space:pre">		</span>//++totalN;
<span style="white-space:pre">		</span>return;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>move(a-1, b-2);
<span style="white-space:pre">	</span>move(a+1, b-2);
<span style="white-space:pre">	</span>move(a-2, b-1);
<span style="white-space:pre">	</span>move(a+2, b-1);
<span style="white-space:pre">	</span>move(a-2, b+1);
<span style="white-space:pre">	</span>move(a+2, b+1);
<span style="white-space:pre">	</span>move(a-1, b+2);
<span style="white-space:pre">	</span>move(a+1, b+2);
<span style="white-space:pre">	</span>result.pop_back();
<span style="white-space:pre">	</span>CHESS[a][b] = 0;
<span style="white-space:pre">	</span>++totalN;
}

void search(int p, int q){
<span style="white-space:pre">	</span>move(1,1);
<span style="white-space:pre">	</span>if (isfind == false)
<span style="white-space:pre">		</span>cout << "impossible" << endl;
}

int main(){
<span style="white-space:pre">	</span>int n;
<span style="white-space:pre">	</span>int num = 1;
<span style="white-space:pre">	</span>cin >> n;
<span style="white-space:pre">	</span>while (n--){
<span style="white-space:pre">		</span>cin >> p >> q;
<span style="white-space:pre">		</span>isfind = false;
<span style="white-space:pre">		</span>totalN = p*q;
<span style="white-space:pre">		</span>clear(p, q);
<span style="white-space:pre">		</span>cout << "Scenario #"<<num<<":\n";
<span style="white-space:pre">		</span>search(p, q);
<span style="white-space:pre">		</span>result.clear();
<span style="white-space:pre">		</span>++num;
<span style="white-space:pre">		</span>cout << "\n";
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return 0;
}
發佈了53 篇原創文章 · 獲贊 5 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章