HDU 1372 BFS
2016-07-23 21:49
441 查看
HDU 1372 经典的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): 10117 Accepted Submission(s): 5954
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.”.
Sample Input
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.
这题的大致题意就是,国际象棋的骑士,只能走日,八个方向的日都能走,不过你的棋盘的大小是8*8的,你不能走到棋盘之外。 用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): 10117 Accepted Submission(s): 5954
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.”.
Sample Input
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.
这题的大致题意就是,国际象棋的骑士,只能走日,八个方向的日都能走,不过你的棋盘的大小是8*8的,你不能走到棋盘之外。 用BFS找到即可。
//这题跟1915一样的。 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <functional> #include <set> #include <map> #include <queue> #include <stack> #include <vector> using namespace std; int k, sx, sy, ex, ey; int vis[10][10]; int dirt[8][2] = {-1, -2, -2, -1, -2, 1, -1, 2, 1, -2, 2, -1, 2, 1, 1, 2}; struct node{ int x, y, step; }; int judge(int x, int y) { if(x < 0 || x >= 8 || y < 0 || y >= 8) return 1 ; return vis[x][y]; } void bfs() { queue<node>q; node a, b, next; a.x = sx; a.y = sy; a.step = 0; q.push(a); vis[sx][sy] = 1; while(!q.empty()){ b = q.front(); q.pop(); if(b.x == ex && b.y == ey){ k = b.step; return; } for(int i = 0 ; i < 8 ; i++){ next.x = b.x + dirt[i][0]; next.y = b.y + dirt[i][1]; if(judge(next.x, next.y)) continue; next.step = b.step + 1; q.push(next); vis[next.x][next.y] = 1; } } } int main() { char y1, y2, y3, y4; while(~scanf("%c%c %c%c",&y1,&y2,&y3,&y4)){ k = 0 ; memset(vis , 0 , sizeof(vis)); sx = y1 - 'a' , sy = y2 - '1'; ex = y3 - 'a' , ey = y4 - '1'; bfs(); printf("To get from %c%c to %c%c takes %d knight moves.\n", y1,y2,y3,y4,k); getchar(); } return 0; }
相关文章推荐
- js算法: 图的两种表示方法以及广度优先算法
- 【日常练习 搜索】诡异的楼梯(队列+bfs)
- Surrounded Regions
- Word Ladder, Gray Code
- UVA 11624
- HDU1495
- HDU2612 Find a way
- HDU1241 Oil Deposits
- Hdu2444二分图
- http://codeforces.com/gym/100803/attachments A题 Bit String Reordering
- 最少步数BFS
- 转v_JULY_v的 BFS和DFS优先搜索算法
- 2015 寒假搜索专题 I - Meteor Shower(BFS)
- Same Tree
- E - Roads in the North
- DFS&BFS算法总结(1)
- Word Ladder I
- 图的BFS和DFS学习笔记
- [LeetCode] Binary Tree Level Order Traversal
- [LeetCode] Binary Tree Level Order Traversal II