poj 2488 -- A Knight's Journey
A Knight's Journey
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 30388 | Accepted: 10404 |
Description

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.
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 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, . . .
Output
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.
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
题目大意: 让骑士以字典序遍历整个图,每个点只能到一次。
思路:数据不大,直接搜索即可。注意每个测试数据后面都有一个空行。

1 /*====================================================================== 2 * Author : kevin 3 * Filename : AKnightsJourney.cpp 4 * Creat time : 2014-07-31 16:22 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 30 15 using namespace std; 16 struct Node 17 { 18 int x,y; 19 }steps[M*M]; 20 bool vis[M][M]; 21 int p,q; 22 int dir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; 23 int DFS(int x,int y,int cnt) 24 { 25 if(vis[x][y]){ 26 return -1; 27 } 28 vis[x][y] = true; 29 steps[cnt].x = x; steps[cnt].y = y; 30 if(cnt == p*q-1) return 1; 31 for(int i = 0; i < 8; i++){ 32 int xx = x+dir[i][0]; 33 int yy = y+dir[i][1]; 34 if(xx >= 1 && xx <= p && yy >= 1 && yy <= q){ 35 int ans = DFS(xx,yy,cnt+1); 36 if(ans == 1){ 37 return true; 38 } 39 else if(ans == 0){ 40 vis[xx][yy] = false; 41 } 42 } 43 } 44 return 0; 45 } 46 int main(int argc,char *argv[]) 47 { 48 int n,t = 1; 49 scanf("%d",&n); 50 while(n--){ 51 clr(steps,0); 52 scanf("%d%d",&p,&q); 53 int cnt = 0; 54 printf("Scenario #%d:\n",t++); 55 int flag = 0; 56 for(int i = 1; i <= q; i++){ 57 for(int j = 1; j <= p; j++){ 58 clr(vis,0); 59 if(DFS(j,i,cnt) == 1){ //因为i与j位置反了,贡献了几次wr 60 flag = 1; 61 break; 62 } 63 } 64 if(flag) break; 65 } 66 if(!flag){ 67 printf("impossible"); 68 } 69 else{ 70 for(int i = 0; i < q*p; i++){ 71 printf("%c%d",steps[i].y-1+'A',steps[i].x); 72 } 73 } 74 printf("\n\n"); 75 } 76 return 0; 77 }
Do one thing , and do it well !
分类:
acm--搜索
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?