SDAU 搜索专题 15 Knight Moves
来源:互联网 发布:qt ros 显示界面编程 编辑:程序博客网 时间:2024/05/16 03:08
1:问题描述
Problem Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying “To get from xx to yy takes n knight moves.”.
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
2:大致题意
给出两个坐标,让马从第一个坐标跳到第二哥坐标上。求出最短步数。
3:思路
马走日。
用BFS,只是将方向数组变了一下,直接套模板就行了。很简单的。不过当时错了好多次,是因为用了next,和关键字重复了。当时找了半天。好像new什么的都是关键字,不能用做自定义的变量。
4:感想
这个题老师上课的时候也讲了。嘿嘿。
快12点了。。。
〒_〒
5:ac代码
#include<iostream>#include<string.h>#include<queue>#include<stdio.h>using namespace std;int visit[20][20];string s1,s2;struct ans{ int x; int y; int ste;}now,n2;int dix[10]={-2,-2,-1,-1,1,1,2,2};int diy[10]={-1,1,-2,2,-2,2,-1,1};int ww(int a,int b){ if(a>0&&a<=8&&b>0&&b<=8) return 1; return 0;}int BFS( ){ now.ste=0; queue<ans> Q; Q.push(now); while(!Q.empty()) { now=Q.front(); Q.pop(); if(now.x==s2[1]-'0'&&now.y==s2[0]-'a'+1) return now.ste; for(int i=0;i<8;i++) { n2.x=now.x+dix[i]; n2.y=now.y+diy[i]; if(ww(n2.x,n2.y)&&!visit[n2.x][n2.y]) { visit[n2.x][n2.y]=1; n2.ste=now.ste+1; Q.push(n2); } } } return -1;}int main(){ //freopen("a.txt","r",stdin); while(cin>>s1>>s2) { int i,j; now.x=s1[1]-'0'; now.y=s1[0]-'a'+1; for(i=0;i<20;i++) for(j=0;j<20;j++) visit[i][j]=0; visit[now.x][now.y]=1; int t=BFS(); cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<t<<" knight moves."<<endl; } return 0;}//为什么一直ce啊!!!//把next换掉就行了。可能是和关键字重复吧。
- SDAU 搜索专题 15 Knight Moves
- UVa 439 - Knight Moves 搜索专题
- hdu1372-Knight Moves--搜索
- SDAU 搜索专题 总结
- Knight Moves--广度优先搜索
- Knight Moves - UVa 439 搜索
- 搜索—Problem_1015-Knight Moves
- 搜索专题: HDU1372Knight Moves
- 15Knight Moves
- SDAU 搜索专题 03 pie
- SDAU 搜索专题 04 射箭
- SDAU 搜索专题 12 Rescue
- 宽度优先搜索1185: Knight Moves
- poj1915 Knight Moves 双向广度搜索
- 搜索简单题——Knight Moves
- hdu 1372 Knight Moves(搜索)
- POJ 1915 Knight Moves 双向搜索
- POJ 1915 Knight Moves (广度搜索)
- IOS开发之OC学习笔记(下)
- mFC + ffmpeg
- CF 620 C. Pearls in a Row 贪心
- n x n矩阵的转置矩阵,并计算对角线元素之和
- 4.6日学习记录
- SDAU 搜索专题 15 Knight Moves
- [IOS]tableView的操作。
- 项目:信息交流平台系统
- Android后台定时提醒功能实现
- angularjs 前端页面之用户修改密码
- JAVA语言规范(官方文档)一
- LeetCode 110 Balanced Binary Tree
- Nginx
- no such file or directory