poj2488
来源:互联网 发布:阿里云机房在哪里 编辑:程序博客网 时间:2024/06/05 11:41
A Knight's Journey
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 45323 Accepted: 15414
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
Source
TUD Programming Contest 2005, Darmstadt, Germany
题目大意:现在有一个n*m大小的棋盘,马的开始位置任意,如果马能跳完整个棋盘,则按照字典序输出路径,否则输出impossible。
题目分析:刚看到这个题目不怎么会做,难点就在怎么按照字典序输出路径,其他用dfs即可解决,最后贴一下大神的代码吧。
代码:
#include <iostream>#include <string.h>using namespace std;#define size 30int k=1,n,m,flag;int visit[size][size];int dx[10]={-2,-2,-1,-1, 1,1, 2,2};int dy[10]={-1, 1,-2, 2,-2,2,-1,1};struct node{ int x,y;}str[size];void dfs(int row,int col,int step){ if(flag==1){ return; } visit[row][col]=1; str[step].x=row; str[step].y=col; if(step==n*m){ flag=1; cout<<"Scenario #"<<k++<<":"<<endl; for(int i=1;i<=n*m;i++){ cout<<char(str[i].x-1+'A')<<str[i].y; } cout<<endl<<endl; return; } for(int i=0;i<8;i++){ int nex=row+dx[i]; int ney=col+dy[i]; if(nex<=m&&nex>=1 &&ney<=n&&ney>=1 &&!visit[nex][ney]){ dfs(nex,ney,step+1); } } visit[row][col]=0;}int main(){ int T; cin>>T; while(T--) { memset(visit,0,sizeof(visit)); flag=0; cin>>n>>m; dfs(1,1,1); if(!flag){ cout<<"Scenario #"<<k++<<":"<<endl; cout<<"impossible"<<endl<<endl; } } return 0;}
大神代码:传送门阅读全文
0 0
- poj2488
- POJ2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- Poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- POJ2488
- Matlab:入门知识小总结(Dir函数)
- Linux与Unix究竟有什么区别呢,孰优孰劣?
- PAT甲级1015
- zookeeper
- 高级网络配置及ipv6网络的管理
- poj2488
- 为了学习,找些好伙伴吧
- Python3教程--协程(转自廖雪峰的官方网站)
- Hibernate的主键生成策略
- lintcode 85 在二叉树中插入节点
- 跟我学TCP/IP系列4
- Imu数据均值滤波分析
- 什么是优先级?
- mvcc 数据库相关