hdu 1180(诡异的楼梯)
2011-08-03 11:57
337 查看
这题还真是诡异,明明思路很清晰,BFS也很熟练 的敲出来了,可就是改了半天,原来问题在这里,“Harry从来不在楼梯上停留”,这里提示的很明显了,他除了不再楼梯上停留,其他地方是可以停留的,当然,也就只有在遇到楼梯,又暂时过不去时,才有必要在原地停留,郁闷呀,疏忽了这里,wa了好几次……
hdu1180 代码
hdu1180 代码
#include <iostream> #include <queue> using namespace std; struct Node { int x, y, time; Node(int _x=0,int _y=0,int _time=0):x(_x),y(_y),time(_time){}; }; Node first; int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; int n, m, a, b; char map[21][21]; bool vis[21][21]; queue <Node> Q; int bfs() { int fx, fy; first.x = a; first.y = b; first.time = 0; Q.push(first); while (!Q.empty()) { first = Q.front(); Q.pop(); if (map[first.x][first.y] == 'T') { return first.time; } int i; for (i = 0; i < 4; ++i) { fx = first.x + dir[i][0]; fy = first.y + dir[i][1]; if (fx >= 0 && fx < n && fy >= 0 && fy < m && map[fx][fy] != '*' && !vis[fx][fy]) { if (map[fx][fy] == '.' || map[fx][fy] == 'T') { vis[fx][fy] = 1; Q.push(Node(fx,fy,first.time+1)); } else if (map[fx][fy] == '-') { if (first.time % 2 == 0) { if (fx == first.x) { fy = 2*fy - first.y; if (fy >= 0 && fy < m && map[fx][fy] != '*' && !vis[fx][fy]) { vis[fx][fy] = 1; Q.push(Node(fx,fy,first.time+1)); } } else Q.push(Node(first.x,first.y,first.time+1));//遇到楼梯,但方向不对,原地停留 } else { if (fy == first.y) { fx = 2*fx - first.x; if (fx >= 0 && fx < n && map[fx][fy] != '*' && !vis[fx][fy]) { vis[fx][fy] = 1; Q.push(Node(fx,fy,first.time+1)); } } else Q.push(Node(first.x,first.y,first.time+1));//同样,原地停留 } } else if (map[fx][fy] == '|') { if (first.time % 2 == 0) { if (fy == first.y) { fx = 2*fx - first.x; if (fx >= 0 && fx < n && map[fx][fy] != '*' && !vis[fx][fy]) { vis[fx][fy] = 1; Q.push(Node(fx,fy,first.time+1)); } } else Q.push(Node(first.x,first.y,first.time+1)); } else { if (fx == first.x) { fy = 2*fy - first.y; if (fy >= 0 && fy < m && map[fx][fy] != '*' && !vis[fx][fy]) { vis[fx][fy] = 1; Q.push(Node(fx,fy,first.time+1)); } } else Q.push(Node(first.x,first.y,first.time+1)); } } } } } return -1; } int main() { while (scanf("%d %d", &n, &m) != EOF) { int i, j; for (i = 0; i < n; ++i) { getchar(); for (j = 0; j < m; ++j) { scanf("%c", &map[i][j]); if (map[i][j] == 'S') { a = i; b = j; } vis[i][j]=0; } } vis[a][b] = 1; while (!Q.empty()) { Q.pop(); } printf("%d\n",bfs()); } return 0; }
相关文章推荐
- HDU 1180 诡异的楼梯
- hdu1180诡异的楼梯(bfs)
- HDU 1180 诡异的楼梯
- HDU 1180 诡异的楼梯【广搜】
- 【Bfs】HDU 1180 诡异的楼梯
- HDU 1180 诡异的楼梯 BFS
- HDU 1180 诡异的楼梯(广搜)
- hdu1180诡异的楼梯(广搜)
- hdu 1180 诡异的楼梯
- hdu1180——诡异的楼梯(优先队列+bfs)
- HDU 1180 诡异的楼梯
- HDU 1180 诡异的楼梯 bfs
- HDU 1180 诡异的楼梯
- hdu 1180 诡异的楼梯(bfs+最短路+dp)
- hdu 1180 诡异的楼梯
- HDU 1180 诡异的楼梯
- 诡异的楼梯 BFS hdu 1180
- hdu 1180 诡异的楼梯
- hdu 1180 诡异的楼梯 (bfs)
- hdu 1180 诡异的楼梯