HDU 1372 BFS
来源:互联网 发布:git linux 安装 编辑:程序博客网 时间:2024/05/19 17:04
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找到即可。
//这题跟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;}
- HDU 1372 (BFS)
- HDU 1372 BFS
- HDU 1372 BFS
- hdu 1372 简单bfs
- poj2243 hdu 1372 bfs
- hdu 1372 bfs
- HDU 1372(BFS)
- HDU 1372 BFS
- HDU<1372>/bfs
- hdu 1372 BFS
- HDU 1372 骑士漫游 bfs
- hdu 1372 bfs Knight Moves
- HDU-1372 Knight Moves (BFS)
- HDU-1372 Knight Moves (BFS)
- HDU 1372 Knight Moves【BFS】
- HDU 1372 Knight Moves(BFS)
- HDU 1372--Knight Moves【BFS】
- hdu 1372 Knight Moves(bfs)
- 中缀表达式转换成前缀表达式和后缀表达式的极其简单方法
- Linux系统常用命令(咋个办呢 zgbn)
- grails数据库部署
- 二分查找算法的C++和Python实现
- Understanding HDFS Recovery Processes (Part 2)
- HDU 1372 BFS
- Jam's math problem<hdoj5615>
- C#软件绑定QQ群验证代码
- php7开启强类型模式
- 关键字、结构体总结以及typedef,枚举与#define区别
- 函数和函数库
- Android设置透明、半透明等效果
- Dialog源码分析
- lodash常用函数解释