hdu 1372 || poj 2243 Knight Moves(搜索:BFS+优先队列水题)
2014-07-29 16:05
323 查看
给出两个坐标,问你按象棋马走日的方式最少需要跳几次
因为涉及到最少所以我就想到了优先队列
这个题很水,但是读入很奇葩!
在杭电上我用cin string和scanf char[]就会超时
但是用cin char[]就不超时了...
刚刚想了下这道题好像没有必要使用优先队列,因为每次遍历的都是当前步数最少的状态
没有别的条件限制,所以直接用队列即可,把优先队列换成队列快了600ms...
代码如下:
因为涉及到最少所以我就想到了优先队列
这个题很水,但是读入很奇葩!
在杭电上我用cin string和scanf char[]就会超时
但是用cin char[]就不超时了...
刚刚想了下这道题好像没有必要使用优先队列,因为每次遍历的都是当前步数最少的状态
没有别的条件限制,所以直接用队列即可,把优先队列换成队列快了600ms...
代码如下:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAXN 1010 #define LL long long using namespace std; bool vis[10][10][MAXN]; int dir[8][2] = {{-2,-1}, {-2,1}, {-1,-2}, {-1,2}, {1,-2}, {1,2}, {2,-1}, {2,1}}; struct Node { int x, y, times; bool operator < (const Node &rhs) const { return times > rhs.times; } }start, end; queue<Node> q; void judgequeue(int x, int y, int times) { if(x>=1 && x<=8 && y>=1 && y<=8 && !vis[x][y][times]) { Node tmp; tmp.x = x; tmp.y = y; tmp.times = times; q.push(tmp); } } int BFS() { memset(vis, 0, sizeof(vis)); while(!q.empty()) q.pop(); start.times = 0; q.push(start); while(!q.empty()) { Node tmp = q.front(); vis[tmp.x][tmp.y][tmp.times] = true; q.pop(); if(tmp.x==end.x && tmp.y==end.y) return tmp.times; for(int i=0; i<8; ++i) judgequeue(tmp.x+dir[i][0], tmp.y+dir[i][1], tmp.times+1); } return 0; } int main(void) { int ans; char str1[5], str2[5]; while(cin >> str1 >> str2) { memset(vis, 0, sizeof(vis)); start.x = str1[0]-'a'; start.y = str1[1]-'0'; start.x++; end.x = str2[0]-'a'; end.y = str2[1]-'0'; end.x++; ans = BFS(); printf("To get from %s to %s takes %d knight moves.\n", str1, str2, ans); } return 0; }
相关文章推荐
- POJ 2243 || HDU 1372:Knight Moves(BFS)
- POJ 2243 || HDU 1372:Knight Moves(BFS)
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)
- poj2243 && hdu1372 Knight Moves(BFS)
- hdu 1372 Knight Moves bfs搜索 解题报告
- ACM 搜索 hdu 1372 Knight Moves
- poj 1915&&poj 2243 Knight Moves (bfs搜索)
- hdu1372-Knight Moves--搜索
- HDU 1372 (搜索方向稍有改变) Knight Moves
- hdu1372 Knight Moves BFS 搜索
- HDU 1372 Knight Moves (搜索 使用 dfs bfs两种实现)
- hdu 1372 Knight Moves(搜索)
- poj - 2243 Knight Moves
- ZZULI 1616(POJ 2243||HDU 1372)…
- HDU 1372 Knight Moves
- poj 2243 bfs Knight Moves
- HDU 1372 Knight Moves
- POJ 2243 Knight Moves
- poj 2243 Knight Moves
- POJ 2243 ZOJ 1091 UVaOJ 439 Knight moves(BFS广度优先搜索)