Knight Moves(BFS,走’日‘字)

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8831    Accepted Submission(s): 5202

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.".

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.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <cstring>
 5 using namespace std;
 6 int dx[]={1,-1,1,-1,2,-2,2,-2};
 7 int dy[]={2,-2,-2,2,1,-1,-1,1};
 8 int vis[9][9];
 9 struct point
10 {
11     int x;
12     int y;
13     int t;
14 }st,tem,nex;
15 int sx,sy,ex,ey,tim;
16 char a,b,c,d;
17 void bfs()
18 {
19     queue<point> s;
20     st.x=sx,st.y=sy;
21     st.t=0;
22     s.push(st);
23     memset(vis,0,sizeof(vis));
24     while(!s.empty())
25     {
26         tem=s.front();
27         s.pop();
28
29         if(tem.x==ex&&tem.y==ey)
30         {
31             tim=tem.t;
32             return;
33         }
34         if(vis[tem.x][tem.y]||tem.x<=0||tem.y<=0||tem.x>8||tem.y>8)
35             continue;
36         vis[tem.x][tem.y]=1;
37         for(int i=0;i<8;i++)
38         {
39             int nx=tem.x+dx[i];
40             int ny=tem.y+dy[i];
41             int nt=tem.t+1;
42             nex.x=nx,nex.y=ny,nex.t=nt;
43             s.push(nex);
44         }
45     }
46 }
47 int main()
48 {
49     freopen("in.txt","r",stdin);
50     while(scanf("%c%c %c%c",&a,&b,&c,&d)!=EOF)
51     {
52         getchar();
53         tim=0;
54         sy=a-‘a‘+1,sx=b-‘0‘;
55         ey=c-‘a‘+1,ex=d-‘0‘;
56         bfs();
57         printf("To get from %c%c to %c%c takes %d knight moves.\n",a,b,c,d,tim);
58     }
59 }
时间: 2024-10-12 18:52:26

Knight Moves(BFS,走’日‘字)的相关文章

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 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 visit

POJ 1915 Knight Moves(BFS+STL)

 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fa

hdu5794 A Simple Chess 容斥+Lucas 从(1,1)开始出发,每一步从(x1,y1)到达(x2,y2)满足(x2?x1)^2+(y2?y1)^2=5, x2&gt;x1,y2&gt;y1; 其实就是走日字。而且是往(n,m)方向走的日字。还有r个障碍物,障碍物不可以到达。求(1,1)到(n,m)的路径条数。

A Simple Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2597    Accepted Submission(s): 691 Problem Description There is a n×m board, a chess want to go to the position (n,m) from the pos

POJ 2243:Knight Moves(BFS)

可以理解为象棋中的马走“日”字形,从第一个位置到第二个位置所需的最短步数,简单的BFS 每走一步需判断一次是否到达目标点. 由于BFS写得不多,一直用DFS的思维理解,递归写一直溢出.超时~~ #include"cstdio" #include"iostream" #include"cstring" #include"queue" using namespace std; int dx[8]={-1,1,-2,2,-2,2,-

POJ 1915 Knight Moves [BFS]

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26844   Accepted: 12663 Description Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fas

UVa439 Knight Moves (BFS求最短路)

链接:http://acm.hust.edu.cn/vjudge/problem/19436分析:BFS跑一次最短路,状态转移有8个. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 struct Point { 7 char r, c; 8 Point(char r = ' ', char c = ' '): r(r), c(c) {}; 9

UVa 439 Knight Moves(BFS应用)

题意  求国际象棋中骑士从一个位置移东到另一个位置所需最少步数 基础的BFS应用 #include <bits/stdc++.h> using namespace std; int x[] = { -2, -1, -2, -1, 1, 2, 1, 2}; int y[] = { -1, -2, 1, 2, -2, -1, 2, 1}; int d[15][15], sx, sy, ex, ey; pair<int, int> q[105], t; int bfs() { int c

HDU1372:Knight Moves(BFS)

Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 21   Accepted Submission(s) : 17 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description A friend of you is doing

Sicily Knight Moves(BFS)

1000. Knight Moves                       Time Limit: 1sec    Memory Limit:32MB 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