ACM-搜索-02
来源:互联网 发布:程序员面试简历模板 编辑:程序博客网 时间:2024/06/07 23:39
(欢迎阅读我的博客,如发现错误或有建议请评论留言,谢谢。)
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
题目思路:
马的遍历问题,使用dfs,可以跳跃八个方向
x[8]={-1, 1, -2, 2, -2, 2, -1, 1},y[8]={-2, -2, -1, -1, 1, 1, 2, 2};
这个方向的顺序是固定的,因为题目中说输出的这一条路径是以字典为基础的,其实就是跳越的顺序是确定的,第一次没有注意到这一点,以为随便输出一条j就ok了,记过显示错误答案。
每次跳跃时要进行判断,是否到边界,是否要跳到的点已经使用过,如果能跳,则改变坐标,并标记已经走过。当移动步数等于方格数p*q时说明已经遍历了,则输出;
代码如下:
#include<string.h>#include<cstdio>#include<iostream>#include<cstdlib>using namespace std;int a[100][2],b[9][9],t=0,p,q,flag,n;int x[8]={-1, 1, -2, 2, -2, 2, -1, 1},y[8]={-2, -2, -1, -1, 1, 1, 2, 2};char A='A';void search(int ax,int ay,int i){ a[i][0]=ax; a[i][1]=ay; if(i==p*q){flag=1;return;} for(int j=0;j<8;j++) if(ax+x[j]>=1&&ax+x[j]<=p&&ay+y[j]>=1&&ay+y[j]<=q&&b[ax+x[j]][ay+y[j]]!=0&&flag==0) { int newax=ax+x[j]; int neway=ay+y[j]; b[newax][neway]=0; search(newax,neway,i+1); b[newax][neway]=1; }}int main(){ cin>>n; while(n--) { cin>>p>>q; t++;flag=0; memset(b,1,sizeof(b)); memset(a,0,sizeof(a)); b[1][1]=0; search(1,1,1); cout<<"Scenario #"<<t<<":"<<endl; if(flag) { for(int j=1;j<=p*q;j++) { A='A'; A=A+-1+a[j][1]; cout<<A<<a[j][0]; } cout<<endl; } else cout<<"impossible"<<endl; cout<<endl; }}
阅读全文
0 0
- ACM-搜索-02
- ACM搜索
- ACM搜索题目总结
- PKU ACM 搜索总结
- ACM搜索算法总结
- ACM搜索总结
- ACM搜索算法总结
- ACM搜索算法总结
- ACM搜索题目总结
- ACM搜索算法总结
- acm,关于搜索题目
- ACM:蚕宝宝:搜索
- ACM搜索算法总结
- ACM-三分搜索
- ACM搜索算法总结
- acm 搜索总结
- ACM 搜索 hdu1240 Asteroids!
- ACM-搜索-04
- C语言数据结构——数组顺序存储结构的实现和表示
- 信号匹配(代码填空)
- MySQL目录结构
- CentOS 通过yum安装web环境
- 1132: 演讲大赛评分
- ACM-搜索-02
- C语言字符串操作函数
- 莫队算法讲解 (详尽版)
- IDEA 开发常用快捷键
- Solr客户端查询Java
- 异常:Cannot create ConcurrentHashMap
- 1501 二叉树最大宽度和高度
- AttributeError: 'FeedExporter' object has no attribute 'slot'
- string,wstring,CString