HDU 1372 HDU 1372
来源:互联网 发布:淘宝女装店铺起名 编辑:程序博客网 时间:2024/05/18 00:47
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9328 Accepted Submission(s): 5498
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.
Source
University of Ulm Local Contest 1996
思路:简单的BFS ,Knight移动有八个方向
在国际象棋中Knight称“马”或“骑士”,Knight的走法和中国象棋中马相同,同样是走“日”字,
或英文字母大写的“L”形:即先向左(或右)走1格,再向上(或下)走2格;或先向左(或右)
走2格,再向上(或下)走1格。不同的是,囯际象棋的Knight没有“绊马脚”的限制,故Knight可越过其
他棋子。吃子与走法相同。
这是一题入门的BFS,很简单:
做了几题BFS,我觉得BFS都要一定的模板的:
学会了模板就可以用了;
这只是我个人的个人看法;
答案如下:
#include <cstdio>#include <iostream>#include <queue>#include <cstring>using namespace std;int vis[500][500];int way[8][2] = { 1,2,2,1,-1,2,-2,1,1,-2,2,-1,-1,-2,-2,-1 };char c1, c2, c3, c4;int xx1, yy1, xx2, yy2;struct node { int xx; int yy; int step;};int BFS();int main(){ while (cin >> c1 >> c2 >> c3 >> c4) { memset(vis, 0, sizeof(vis)); xx1 = c1-'a'+1; yy1 = c2-'0'; xx2 = c3 - 'a'+1; yy2 = c4 - '0'; int n = BFS(); printf("To get from %c%c to %c%c takes %d knight moves.\n", c1, c2, c3, c4, n); } return 0;}int BFS( ){ queue<node> Q; node a, next; a.xx = xx1; a.yy = yy1; a.step = 0; vis[xx1][yy1] = 1; Q.push(a); while (!Q.empty()) { a = Q.front(); Q.pop(); if (a.xx == xx2&&a.yy == yy2) { return a.step; } for (int i = 0; i < 8; i++) { next = a; next.xx = next.xx + way[i][0]; next.yy = next.yy + way[i][1]; if ((next.xx > 0 && next.xx <9) && (next.yy > 0 && next.yy <9) && (vis[next.xx][next.yy] == 0)) { vis[next.xx][next.yy] = 1; next.step++; Q.push(next); } } } return 0;}
- HDU 1372 HDU 1372
- hdu 1372
- hdu-1372
- hdu 1372
- hdu 1372
- hdu-1372
- hdu 1372
- HDU 1372
- HDU 1372 knight moves
- HDU 1372 Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- hdu 1372 Knight Moves
- Hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU-1372Knight Moves
- 数据的标准化与中心化以及R语言中的scale详解
- [Lintcode]Reverse Words in a String
- JSONServlet 过滤器
- ServerSocketChannel与SocketChannel的使用
- Lua标准库 - 输入输出处理(input and output facilities)
- HDU 1372 HDU 1372
- API(APP会议直播)
- mysql innodb 数据安全性分析
- 记开博,立此为证——不求闻达于诸侯,但求精通程序之精髓
- iOS企业版发布 HTTPS证书以及服务器设置
- 权限管理课堂笔记
- 基于HTTP协议的Web服务器
- 第二周项目3:输出星号(2)
- 【Ural1167】 Bicolored Horses——基础Dp