HDU 1372 (搜索方向稍有改变) Knight Moves

其实手写模拟一个队列也挺简单的,尤其是熟练以后。

尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马

所以搜索就有八个方向

对了注意初始化标记数组的时候,不要把起点标记为已走过。

因为测试数据里面有一组 f6 f6,此时样例输出的是0

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 struct Point
 8 {
 9     int x, y;
10     int steps;
11 }start, end, qu[100];
12
13 int vis[8][8];
14 int dir[8][2] = {{1, 2}, {-1, 2}, {1, -2}, {-1, -2}, {2, 1}, {-2, 1}, {2, -1}, {-2, -1}};
15 char col1, col2;
16 int row1, row2;
17 int head, tail;
18
19 bool islegal(int x, int y)
20 {
21     return (x>=0 && x<8 && y>=0 && y<8 && (!vis[x][y]));
22 }
23
24 void BFS(void)
25 {
26     head = 0, tail = 1;
27     qu[0].x = start.x;
28     qu[0].y = start.y;
29     qu[0].steps = 0;
30     while(head < tail)
31     {
32         if(qu[head].x == end.x && qu[head].y == end.y)
33         {
34             printf("To get from %c%d to %c%d takes %d knight moves.\n", col1, row1, col2, row2, qu[head].steps);
35             return;
36         }
37         for(int i = 0; i < 8; ++i)
38         {
39             int xx = qu[head].x + dir[i][0];
40             int yy = qu[head].y + dir[i][1];
41             if(islegal(xx, yy))
42             {
43                 qu[tail].x = xx;
44                 qu[tail].y = yy;
45                 qu[tail++].steps = qu[head].steps + 1;
46                 vis[xx][yy] = 1;
47             }
48         }
49         ++head;
50     }
51 }
52
53 int main(void)
54 {
55     #ifdef LOCAL
56         freopen("1372in.txt", "r", stdin);
57     #endif
58
59     while(cin >> col1 >> row1 >> col2 >> row2)
60     {
61         start.x = row1 - 1, start.y = col1 - ‘a‘;
62         end.x = row2 - 1, end.y = col2 - ‘a‘;
63         memset(vis, 0, sizeof(vis));
64         BFS();
65     }
66     return 0;
67 }

代码君

HDU 1372 (搜索方向稍有改变) Knight Moves

时间: 2024-10-20 23:17:39

HDU 1372 (搜索方向稍有改变) Knight Moves的相关文章

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

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

HDU - 1372 Knight Moves(bfs入门)

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

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 usi

HDU 1372.Knight Moves

Knight Moves Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1372 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour

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&

HDU 1372 Knight Moves 题解

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

HDOJ 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): 7482    Accepted Submission(s): 4481 Problem Description A friend of you is doin

杭电 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