POJ 1915 Knight Moves bfs || 双向bfs
来源:互联网 发布:sql row_number() over 编辑:程序博客网 时间:2024/06/05 03:51
题目:
http://poj.org/problem?id=1915
题意:
给定一个
思路:
水题一道,直接
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef pair<int, int> pii;const int N = 500 + 10, INF = 0x3f3f3f3f;int n;int dis[N][N];int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};int bfs(int sx, int sy, int tx, int ty){ queue<pii> que; memset(dis, 0x3f, sizeof dis); que.push(pii(sx, sy)), dis[sx][sy] = 0; while(! que.empty()) { pii p = que.front(); que.pop(); if(p.first == tx && p.second == ty) return dis[tx][ty]; for(int i = 0; i < 8; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if(nx >= 0 && nx < n && ny >= 0 && ny < n && dis[nx][ny] > dis[p.first][p.second] + 1) { dis[nx][ny] = dis[p.first][p.second] + 1; que.push(pii(nx, ny)); } } } return 0;//这句没用}int main(){ int t, sx, sy, tx, ty; scanf("%d", &t); while(t--) { scanf("%d%d%d%d%d", &n, &sx, &sy, &tx, &ty); printf("%d\n", bfs(sx, sy, tx, ty)); } return 0;}
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef pair<int, int> pii;const int N = 500 + 10, INF = 0x3f3f3f3f;int n;int dis[N][N], vis[N][N];int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};int bfs(int sx, int sy, int tx, int ty){ queue<pii> que; memset(dis, 0, sizeof dis); memset(vis, 0, sizeof vis); if(sx == tx && sy == ty) return 0;//特判起点终点相同的情况 que.push(pii(sx, sy)), dis[sx][sy] = 0, vis[sx][sy] = 1;//1标记是从起点正向搜索访问到的点 que.push(pii(tx, ty)), dis[tx][ty] = 0 ,vis[tx][ty] = 2;//2标记是从终点反向搜索访问到的点 while(! que.empty()) { pii p = que.front(); que.pop(); for(int i = 0; i < 8; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if(nx >= 0 && nx < n && ny >= 0 && ny < n) { if(! vis[nx][ny])//没访问过,正常访问 { vis[nx][ny] = vis[p.first][p.second];//标记此点是正向还是反向搜索到的点 dis[nx][ny] = dis[p.first][p.second] + 1; que.push(pii(nx, ny)); } else if(vis[p.first][p.second] != vis[nx][ny])//相邻两点都被访问且一个正向一个反向,说明一条从起点到终点的最短路径已经出现 return dis[p.first][p.second] + dis[nx][ny] + 1; } } } return 0;}int main(){ int t, sx, sy, tx, ty; scanf("%d", &t); while(t--) { scanf("%d%d%d%d%d", &n, &sx, &sy, &tx, &ty); printf("%d\n", bfs(sx, sy, tx, ty)); } return 0;}
阅读全文
0 0
- POJ 1915 Knight Moves bfs || 双向bfs
- poj 1915 knight moves (双向bfs)
- POJ 1915 Knight Moves(双向BFS)
- POJ 1915 Knight Moves 双向BFS 入门
- poj 1915 Knight Moves 【双向bfs】
- POJ 2243 Knight Moves bfs || 双向bfs
- POJ 1915-Knight Moves (单向BFS && 双向BFS 比较)
- (搜索,双向BFS)POJ.1915.Knight Moves
- POJ 2243:Knight Moves(双向BFS)
- POJ 1915 Knight Moves bfs
- POJ 1915Knight Moves(BFS)
- poj 1915 Knight Moves(bfs)
- poj 1915 Knight Moves(bfs)
- POJ 1915 BFS-Knight Moves
- [BFS]poj 1915 Knight Moves
- Poj 1915 Knight Moves ( BFS
- POJ 1915 Knight Moves BFS
- 【BFS】poj 1915 Knight Moves
- LVM中对基于xfs的文件系统进行扩容
- HDU-6140 Killer Names
- [Azure]ARM模式下删除虚拟机挂盘修复后恢复的脚本(托管磁盘)
- C++ rand()
- 计算机网络面试小结
- POJ 1915 Knight Moves bfs || 双向bfs
- ubuntu14.04搜狗输入法中文无法切换英文
- Android编程权威指南(第2版)—第6章挑战练习
- zookeeper 集群搭建,及zoo.cfg配置详解
- hdu6138 hash+二分
- LoadRunner12_脚本中运行JavaScrip
- 误差函数erf
- Redis分布式锁
- HDU 1233 还是畅通工程