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 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.
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
这道题目:给定长和宽,求哈密顿通路(按照字典序)。
易错点在字典序,还有 Presentation Error , 除了最后一行,中间的数据输出要再加一个换行。
详情见注释:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std ;
int Gragh[27][27] ;
int book[27][27] ;
int row , line , ans , top ;
int dx[8] = {-1 , 1 , -2 , 2 , -2 , 2 , -1 , 1 } ; // 注意输出是字典序
int dy[8] = {-2 , -2 , -1 , -1 , 1 , 1 , 2 , 2 } ;
struct Node{
int x , y ;
} Path[27] ;
void Go_Ahead( int &ans , int x , int y ){ // 往前走
ans++ ; Path[++top].x = x , Path[top].y = y ; book[x][y] = 1 ;
}
void Go_Back( int &ans , int x , int y ){ // 往回走,回溯
ans-- ; top-- ; book[x][y] = 0 ;
}
int DFS( int x , int y , int &ans ){
if( x < 1 || x > row || y < 1 || y > line ) // 如果越界
return 0 ;
Go_Ahead( ans , x , y ) ; // 往前走一步
if( ans == row*line ) // 如果马把所有的点都走了一遍
return 1 ;
for( int i = 0 ; i < 8 ; ++i ){
int curx = x + dx[i] , cury = y + dy[i] ;
if( curx < 1 || curx > row || cury < 1 || cury > line|| book[curx][cury] ) // 如果越界或者这个点走过了,不走这个点
continue ;
if( DFS( curx , cury , ans ) ) // 如果走这里可以完成旅行,返回 1
return 1 ;
Go_Back( ans , curx , cury ) ; // 走这步完不成旅行,就往回走,回溯
}
return 0 ;
}
int main(){
int cases ;
cin >> cases ;
for( int i = 1 ; i <= cases ; ++i ){
cin >> row >> line ;
printf( "Scenario #%d:\n" , i ) ;
memset( book , 0 , sizeof( book ) ) ;
book[1][1] = 1 ; // 标记起点走过了
top = 0 ;
if( !DFS( 1 , 1 , ans = 0 ) )
printf( "impossible" ) ;
else{
for( int i = 1 ; i <= top ; ++i )
printf( "%c%d" , 'A' + Path[i].y - 1 , Path[i].x ) ;
}
printf( "\n" ) ;
if( i != cases ) printf( "\n" ) ; // 这里要注意,除了中间任意两个 cases 之间,最后一行是没有这个再换行的
}
return 0 ;
}