HDU 1372 Knight Moves(bfs)

嗯...

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372

这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向,然后从起点到终点跑一遍最典型的bfs即可...注意HDU的坑爹输入和输出...

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<queue>
 4 #include<cstring>
 5
 6 using namespace std;
 7
 8 int ex, ey, sx, sy, step, g[10][10];
 9
10 struct node{
11     int x, y, step;
12 };
13
14 int dir[8][2] = {{2, 1}, {1, 2}, {-1, 2}, {2, -1}, {-2, 1}, {1, -2}, {-1, -2}, {-2, -1}};
15 //方向
16 inline void bfs(){
17     memset(g, 0, sizeof(g));
18     queue<node> q;
19     node now, next;
20     now.x = sx;
21     now.y = sy;
22     now.step = 0;
23     g[now.x][now.y] = 1;
24     q.push(now);
25     while(!q.empty()){
26         now = q.front();
27         q.pop();
28         if(now.x == ex && now.y == ey){
29             step = now.step;
30             return;//找到终点
31         }
32         for(int i = 0; i < 8; i++){
33             next.x = now.x + dir[i][0];
34             next.y = now.y + dir[i][1];
35             if(next.x >= 1 && next.x <= 8 && next.y >= 1 && next.y <= 8 && !g[next.x][next.y]){
36                 next.step = now.step + 1;
37                 g[next.x][next.y] = 1;
38                 q.push(next);
39             }
40         }
41     }
42 }
43
44
45 int main(){
46     char c1, c2;
47     int s, t;
48     while(~scanf("%c%d %c%d", &c1, &s, &c2, &t)){
49         getchar();
50         sx = c1 - ‘a‘ + 1;
51         sy = s;
52         ex = c2 - ‘a‘ + 1;
53         ey = t;//起终点
54         bfs();
55         printf("To get from %c%d to %c%d takes %d knight moves.\n", c1, s, c2, t, step);
56     }
57     return 0;
58 }

AC代码

原文地址:https://www.cnblogs.com/New-ljx/p/11334853.html

时间: 2024-08-04 00:40:27

HDU 1372 Knight Moves(bfs)的相关文章

HDU 1372 Knight Moves (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10372    Accepted Submission(s): 6105 Problem Description A friend of you is doin

HDU - 1372 Knight Moves(bfs入门)

HDU - 1372 Knight Moves 题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567 题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你.由于火山是一匹马,他的移动方式将会遵守国际象棋马的走法. 输入: 输入包含一个或多个输入样例.每个测试样例将会有两个坐标,表示现在的位置和将要到达的地

HDOJ 题目1372 Knight Moves(BFS)

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7551    Accepted Submission(s): 4513 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe

poj2243&amp;&amp;hdu1372 Knight Moves(BFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

poj2243 &amp;amp;&amp;amp; hdu1372 Knight Moves(BFS)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: pid=1372">http://acm.hdu.edu.cn/showproblem.php? pid=1372 Problem Description A friend of you is doing research on t

HDU 1372 Knight Moves【BFS】

题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次[email protected][email protected]) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue&

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

杭电 1372 Knight Moves(广搜模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6439    Accepted Submission(s): 3886 Problem Description A friend of you is doing res