hdu1180 诡异的楼梯 --bfs+优先级队列
2016-12-11 14:38
375 查看
遇到楼梯特殊处理
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; struct node { int x, y, t; node() {} node(int xx, int yy, int tt):x(xx), y(yy), t(tt) {} friend bool operator<(const node &a, const node &b) { return a.t > b.t; } }; char mp[50][50]; bool vis[50][50]; int nx[] = {-1, 0, 1, 0}; int ny[] = {0, 1, 0, -1}; int bfs(int m, int n, int sx, int sy) { memset(vis, false, sizeof(vis)); priority_queue<node> q; vis[sx][sy] = true; q.push(node(sx, sy, 0)); while (!q.empty()) { node t = q.top(); q.pop(); if (mp[t.x][t.y] == 'T') return t.t; for (int i = 0; i < 4; i++) { int tx = t.x + nx[i]; int ty = t.y + ny[i]; int tt = t.t + 1; if (tx < 1 || tx > m || ty < 1 || ty > n || vis[tx][ty] || mp[tx][ty] == '*') continue; if(mp[tx][ty] == '|' || mp[tx][ty] == '-') { if (mp[tx][ty] == '|') { if ((i == 0 || i == 2) && t.t%2 || (i == 1 || i == 3) && t.t%2==0) tt++; } else if (mp[tx][ty] == '-') { if ((i == 0 || i == 2) && t.t%2==0 || (i == 1 || i == 3) && t.t%2) tt++; } tx += nx[i]; ty += ny[i]; if (tx < 1 || tx > m || ty < 1 || ty > n || vis[tx][ty] || mp[tx][ty] == '*') continue; } q.push(node(tx, ty, tt)); vis[tx][ty] = true; } } return -1; } int main() { //freopen("in", "r", stdin); int m, n; while(~scanf("%d%d", &m, &n)) { int sx, sy; getchar(); for (int i = 1; i <= m; i++) fgets(mp[i]+1, 50, stdin); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if (mp[i][j] == 'S') { sx = i; sy = j; break; } printf("%d\n", bfs(m, n, sx, sy)); } return 0; }
相关文章推荐
- hdu1180诡异的楼梯……bfs走迷宫……wa了16次,我太渣了
- HDU 1180:诡异的楼梯(BFS)
- HDU 1180 诡异的楼梯(BFS:时间动态图)
- hdu1180 诡异的楼梯--BFS
- hdu 1180 诡异的楼梯 (bfs)
- hdu 1180 诡异的楼梯 (BFS)
- hdu 1180 诡异的楼梯 BFS 这题相当坑爹啊,需要注意几点
- hdu1180——诡异的楼梯(优先队列+bfs)
- HDU-1180 诡异的楼梯(BFS)
- HDU 1180 诡异的楼梯 BFS
- HDU_1180_诡异的楼梯_BFS
- hdu 1180 诡异的楼梯(bfs)
- hdu 1180 诡异的楼梯 (bfs)
- hdu1180 诡异的楼梯(优先队列bfs)
- HDU 1180 诡异的楼梯 BFS
- hdu 1180 诡异的楼梯(BFS)
- HDU1180-诡异的楼梯(bfs)
- HDU 1180 诡异的楼梯(BFS+保存自己的状态)
- 【HDU 1180】诡异的楼梯(BFS)
- hdu1180诡异的楼梯(bfs)