深搜水题poj2488
来源:互联网 发布:国际网络安全形势 编辑:程序博客网 时间:2024/06/05 20:02
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.
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
31 12 34 3
Sample Output
Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4
题意:要求从一个点出发,向中国象棋里马那样只能走“日”形对角,问是否能走遍全场,如果不行,输出“impossible”,否则按照字典序输出路径。
题解:首先审题,明显是深搜,既然深搜,那就简单了,dfs函数,每次调用给出现在所在的点以及到达的点的个数,如果已经全部到达,标记,结束递归。
注意字典序。那个字典序必须决定于移动数组move1和move2,所以一开始没a,就是字典序搞错了。代码如下
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <sstream>#include <fstream>#include <set>#include <map>#define INF 1e9#define M_PI 3.14159265358979323846using namespace std;//ifstream infile;int n,m,flag;int maze[30][30];int path1[30][30],path2[30][30];int move1[8] = {-1,1,-2,2,-2,2,-1,1},move2[8] = {-2,-2,-1,-1,1,1,2,2};void dfs(int x,int y,int cnt){ if(cnt == n*m){ flag = 1; return; } for (int i = 0;i < 8;i++) { int nx = x+move1[i],ny = y+move2[i]; if((nx>=0&&nx<n)&&(ny>=0&&ny<m)&&maze[nx][ny]==0) { maze[nx][ny] = 1; path1[x][y] = nx,path2[x][y] = ny; dfs(nx,ny,cnt+1); if(flag) { return; } path1[x][y] = -1,path2[x][y] = -1; maze[nx][ny] = 0; } }}void print(int x,int y){ if(path1[x][y]==-1&&path2[x][y]==-1){ cout<<(char)(y+'A')<<x+1; return; } cout<<(char)(y+'A')<<x+1; print(path1[x][y],path2[x][y]);}int main(void){ //infile.open("1234.txt",ios::in); int t,k = 1; cin>>t; while (t--) { flag = 0; cin>>n>>m; memset(path1,-1,sizeof(path1)); memset(path2,-1,sizeof(path2)); memset(maze,0,sizeof(maze)); maze[0][0] = 1; dfs(0,0,1); printf("Scenario #%d:\n",k++); if(!flag){ cout<<"impossible"<<endl<<endl; continue; } print(0,0); cout<<endl<<endl; } return 0;}
1 0
- 深搜水题poj2488
- poj2488
- POJ2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- Poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- 【学习笔记】吝啬的国度 JAVA
- java 拔河比赛
- Android 动画初识篇
- Java编程易犯错误
- 14个简单有用的android源码,适合初学者
- 深搜水题poj2488
- [操作系统概念]第五部分——死锁
- 递归习题—排队游戏(C程序设计进阶 第3周)
- 51NOD1785 数据流中的算法 【水】
- 图片添加水印及打印文件
- 递归习题—扩号匹配问题(C程序设计进阶 第3周)
- linux相较于windows作为服务器端操作系统的优点
- MFC基于对话框添加背景图
- Maven实战 eclipse下载依赖jar包出问题