HDU 1372(BFS)
来源:互联网 发布:数据分析 ppt 编辑:程序博客网 时间:2024/05/19 19:42
国际象棋里面的骑士和中国象棋里面的马走法是相同的,走日字。所以如果以一个点为中心的话,可以向外扩散八个方向(也就是说可以走出八种方向不同的日字)。所以只用常规BFS+八个方向的搜索就好辣。
代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <ctime>#include <iostream>#include <algorithm>#include <set>#include <vector>#include <sstream>#include <queue>#include <typeinfo>#include <fstream>#include <map>#include <stack>typedef long long LL;using namespace std;int m[10][10];int re[10][10];int dr[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; struct knight{int x, y, step;knight(int x1, int y1, int step1):x(x1), y(y1), step(step1){}};int bfs(int x, int y, int end_x, int end_y){queue<knight> q;knight s(x, y, 0);while (!q.empty())q.pop();q.push(s);while (!q.empty()){knight temp = q.front();q.pop();if (temp.x == end_x && temp.y == end_y){return temp.step;}for (int i=0; i<8; i++){int t1 = temp.x + dr[i][0];int t2 = temp.y + dr[i][1];if (t1>=1 && t1 <=8 && t2 >=1 && t2 <= 8 && !re[t1][t2]){re[t1][t2] = 1;knight test(t1, t2, temp.step+1);q.push(test);}}}}int main(){char a[3], b[3];while (scanf("%s", a) != EOF){getchar();scanf("%s", b);memset(re, 0, sizeof(re));m[a[1]-48][a[0]-96] = 1;m[b[1]-48][b[0]-96] = 1;re[a[1]-48][a[0]-96] = 1;cout << "To get from " << a << " to " << b << " takes " << bfs(a[1]-48, a[0]-96, b[1]-48, b[0]-96) << " knight moves." << endl;}}
0 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)
- BeautifulSoup的高级应用 之.parent .parents .next_sibling.previous_sibling.next_siblings.previous_siblings
- hdu-1874 畅通工程续(SPFA做法)
- Ubuntu14.04下搭建LAMP环境
- HDOJ 3790 最短路径问题(dijkstra算法)
- UrlRewriteFilter 简介
- HDU 1372(BFS)
- Matlab GUI 图像矩形剪切,并存储、发布
- 关于类文件从加载到运行的过程中内存的示意图
- 1.线程与进程概念性总结(初级)
- Swift详解之三----------函数(你想知道的都在这里)
- C语言主函数返回值问题:return type of 'main' is not 'int' [-Wmain]
- 图解classloader加载class的流程及自定义ClassLoader
- Java基础网络编程
- Codeforces Round #316 (Div. 2)