POJ 2488A Knight's Journey

来源:互联网 发布:网络感情骗术 台湾 编辑:程序博客网 时间:2024/06/03 19:23

A - A Knight's Journey
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2488

Description

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.

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.

Sample Input

31 12 34 3

Sample Output

Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4

题意:一个矩形国际象棋棋盘,规格小于8*8(可以不是正方形但必须是矩形),一个骑士(移动方式相当于中国象棋的马)可以从任一点出发,给定一种规格的棋盘,要求判定骑士能否遍历棋盘的所有格子而不重复经过某一格,并输出字典序最小的路径。骑士可以最终停止在任意位置。


这是我新人训练的第一个题,深度优先搜索。所谓深搜即从一个出发点开始选择一个节点逐层向下推进(本题中就是骑士选择了一条路就一直走下去),直到这条“单线”达到了目的(本题中就是骑士遍历了所有点)或者肯定达不到目的;碰到后者的情况就回溯——返回上一层找其他的父节点。


这里的最小字典序当时把我问懵了,半天没明白到底要干嘛。后来我的理解是我们就把a1作为第一个出发点,这是保证字典序的第一个做法(第二个稍后);

对于骑士的步伐,这个处理就比较常见了,数组模拟。

同时在代码中,遍历步子的时的次序设置也间接保证了字典序。。

再一个就是初始化的问题,由于是多组测试数据,一些数据一定要及时更新,比如check,还有got,测试新数据时这些变量都要重新初始化。作为彻头彻尾的新手。。这个错误也在犯,代码风格不严谨,what a sigh..


代码如下:

#include <stdio.h>#include <string.h>//#include <stdlib.h>      //x对应q,y对应p//int p,q;int x[50],y[50];int got[50][50];int check;int step[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};//数组模拟骑士的步子int allowed(int x,int y)//这个函数是用来判定骑士迈出的步子是否越过棋盘边界的{if(x>=1&&x<=q&&y>=1&&y<=p)return 1;return 0;}void dfs(int a,int b,int cell)//dfs一般是写成一个函数{int i;int x1,y1;x[cell]=a;y[cell]=b;if(cell==p*q)//遍历结束{check=1;return;}for(i=0;i<8;i++){x1=a+step[i][0];y1=b+step[i][1];if(allowed(x1,y1)&&!got[x1][y1]&&!check){got[x1][y1]=1;dfs(x1,y1,cell+1);got[x1][y1]=0;}}}int main(){int i,j,n;scanf("%d",&n);for(j=1;j<=n;j++){scanf("%d%d",&p,&q);memset(got,0,sizeof(got));got[1][1]=1;check=0;dfs(1,1,1);printf("Scenario #%d:\n",j);if(check){for(i=1;i<=p*q;i++)printf("%c%d",x[i]+'A'-1,y[i]);printf("\n");}else{printf("impossible\n");}printf(j==n?"":"\n");}return 0;}




0 0
原创粉丝点击