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>
 6 #define maxn 105
 7 using namespace std;
 8 struct node
 9 {
10     int x,y;
11     int step;
12 } now,next,st,en;
13 queue<node>q;
14 char m,n;
15 int vis[maxn][maxn];
16 int dir[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
17 void bfs(int x,int y)
18 {
19     now.x=x;now.y=y;now.step=0;
20     while(!q.empty()) q.pop();
21     q.push(now);
22     vis[x][y]=1;
23     while(!q.empty())
24     {
25         now=q.front();q.pop();
26         if(now.x==en.x&&now.y==en.y)
27         {
28             printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,now.step);
29             return;
30         };
31         for(int i=0;i<8;i++)
32         {
33             next.x=now.x+dir[i][0];
34             next.y=now.y+dir[i][1];
35             if(next.x<1||next.y>8||next.y<1||next.y>8) continue;
36             if(!vis[next.x][next.y])
37             {
38                 vis[next.x][next.y]=1;
39                 next.step=now.step+1;
40                 q.push(next);
41                 if(next.x==en.x&&next.y==en.y)
42                 {
43                     printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,next.step);
44                     return;
45                 }
46             }
47         }
48     }
49 }
50 int main()
51 {
52     while(cin>>m>>st.y>>n>>en.y)
53     {
54         memset(vis,0,sizeof(vis));
55         st.x=m-‘a‘+1;
56         en.x=n-‘a‘+1;
57         bfs(st.x,st.y);
58     }
59 }

时间: 2024-08-02 11:22:40

HDU 1372 Knight Moves【BFS】的相关文章

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

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 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)

嗯... 题目链接: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

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

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

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

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

HDU 1728 逃离迷宫【BFS】

题意:给出一个起点,一个终点,规定的转弯次数,问能否在规定的转弯次数内到达终点--- 这一题是学(看)习(题)的(解)@[email protected] 主要学了两个地方 一个是剪枝,如果搜到的当前点的转弯次数小于该点turn数组记录下来的转弯次数,才有必要将它加入队列. 另一个是记录转弯方向 在结构体里面定义一个turn来记录转弯的个数,用dir来记录当前所在的方向(因为搜的时候是四个方向dir[4][2]来搜的,直接用i确定当前方向即可) 想想自己第一次做的时候的doubi想法,用dfs做