1015 of search
来源:互联网 发布:苹果mac系统安装教程 编辑:程序博客网 时间:2024/05/22 03:03
Knight Moves
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 36 Accepted Submission(s) : 16
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.<br>Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. <br><br>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. <br>
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. <br>
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.". <br>
Sample Input
e2 e4<br>a1 b2<br>b2 c3<br>a1 h8<br>a1 h7<br>h8 a1<br>b1 c3<br>f6 f6<br>
Sample Output
To get from e2 to e4 takes 2 knight moves.<br>To get from a1 to b2 takes 4 knight moves.<br>To get from b2 to c3 takes 2 knight moves.<br>To get from a1 to h8 takes 6 knight moves.<br>To get from a1 to h7 takes 5 knight moves.<br>To get from h8 to a1 takes 6 knight moves.<br>To get from b1 to c3 takes 1 knight moves.<br>To get from f6 to f6 takes 0 knight moves.<br>
Source
University of Ulm Local Contest 1996
题目要求:告诉你马的起始位置和要到达的位置,输出最短到达该点的步数。
解题思路:一是看懂国际象棋的坐标,若玩过的人应该会看,然后用bfs进行搜索最短路径,需要对走过的路进行标记。但是不知道为什么没有跳出,后来经过一番思考后得到发现马能跳过全部的位置。
#include<iostream>#include<cstdio>#include<queue>#include<string.h>using namespace std;struct point{ int x,y,count;};int dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};int Map[9][9],visit[9][9];bool board(point a){ if(a.x>=0&&a.x<=7&&a.y>=0&&a.y<=7)return true; else return false;}void bfs(char m,char n,char c,char d){ queue<point>q; point tem,pre; if(m!=c||n!=d) {pre.x=m-'a'+0; pre.y=n-'1'; pre.count=0; q.push(pre); while(!q.empty()) { pre=q.front(); q.pop(); if(pre.x==c-'a'+0&&pre.y==d-'1'){break;} for(int i=0;i<8;i++) { tem.x=pre.x+dir[i][0]; tem.y=pre.y+dir[i][1]; if(board(tem)&&!visit[tem.x][tem.y]) { tem.count=pre.count+1; q.push(tem); visit[tem.x][tem.y]=1; } } } } else pre.count=0; printf("To get from %c%c to %c%c takes %d knight moves.\n",m,n,c,d,pre.count);}int main(){ char x2,y2,x1,y1; while(cin>>x1>>y1>>x2>>y2) { memset(visit,0,sizeof(visit)); bfs(x1,y1,x2,y2); }}
0 0
- 1015 of search
- 1003 of search
- 1004 of search
- 1005 of search
- 1006 of search
- 1007 of search
- 1008 of search
- 1009 of search
- 1010 of search
- 1011 of search
- 1012 of search
- 1003 of search
- 1016 of search
- 1018 of search
- 1019 of search
- 1020 of search
- the summary of search
- Role of Internal Search
- 关于AndroidMainfest中配置的知识
- Activity任务栈
- 保证分布式系统数据一致性的6种方案
- Redis入门
- java并发-Volatile关键字
- 1015 of search
- hadoop运行环境安装与配置+hadoop开发环境配置(一)
- 汇编-判断两个字符串是否相等
- Android studio 如何导入并引用Library工程
- C++重载括号和重载类型探索
- Swift中文教程(四) 集合类型
- 电子相册系统(七)查看原图
- day01 C语言基础
- Next Permutation