HDU 1180 诡异的楼梯
2015-04-09 16:22
357 查看
题意:
(中文)
思路:
由于每过1分钟,这个楼梯的通向就会变一次,要走楼梯时,判断一下已经走过的步数是否是偶数,如果是偶数,那么楼梯则没有变,否则,变换楼梯方向。判断一下当前是否可以走这个楼梯,可以走的话,步数就是当前步数+1,否则就是+2(等一分钟再走)。
但是,这里面如果用广搜的话,就会出现这样一个问题:第一个到达的那个点其实并不是最优的步数。例如:
3 4
T…
*-*S
*.|.
如果是这样的话,答案就会是5,但是正确答案却是4。
因为在优先搜索步数的时候,(0,1)这个点已经被标记成不可经过的了(因为下面走楼梯的那条路搜索的时候先到达了(0,1)这个点)。
所以需要有这样一个规则,如果此个地方走过了,只要我到达此个地方的步数少,那么我就还可以走这个地方。
Code:
(中文)
思路:
由于每过1分钟,这个楼梯的通向就会变一次,要走楼梯时,判断一下已经走过的步数是否是偶数,如果是偶数,那么楼梯则没有变,否则,变换楼梯方向。判断一下当前是否可以走这个楼梯,可以走的话,步数就是当前步数+1,否则就是+2(等一分钟再走)。
但是,这里面如果用广搜的话,就会出现这样一个问题:第一个到达的那个点其实并不是最优的步数。例如:
3 4
T…
*-*S
*.|.
如果是这样的话,答案就会是5,但是正确答案却是4。
因为在优先搜索步数的时候,(0,1)这个点已经被标记成不可经过的了(因为下面走楼梯的那条路搜索的时候先到达了(0,1)这个点)。
所以需要有这样一个规则,如果此个地方走过了,只要我到达此个地方的步数少,那么我就还可以走这个地方。
Code:
#include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<bitset> #include<queue> #include<stack> #include<list> #include<map> #include<set> #define TEST #define LL long long #define Mt(f, x) memset(f, x, sizeof(f)); #define rep(i, s, e) for(int i = (s); i <= (e); ++i) #ifdef TEST #define See(a) cout << #a << " = " << a << endl; #define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl; #define debug(a, s, e) rep(_i, s, e) {cout << a[_i] << ' ';} cout << endl; #define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee)} #else #define See(a) #define See2(a, b) #define debug(a, s, e) #define debug2(a, s, e, ss, ee) #endif // TEST const int MAX = 2e9; const int MIN = -2e9; const double eps = 1e-8; const double PI = acos(-1.0); using namespace std; const int N = 25; int mv[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; int n, m; char ch ; bool v ; int f ; int sx, sy, ex, ey; struct Node { int x; int y; int tep; friend bool operator < (Node a, Node b) { return a.tep > b.tep; } Node(){} Node(int x, int y, int tep) : x(x), y(y), tep(tep) {} }st, to; char getC(char c, int tep)//变换楼梯 { if(c == '|' || c == '-') { if(tep & 1) { c = (c == '|' ? '-' : '|'); } } return c; } bool isOK(int x, int y, int tep)//判断下一个是否可以走,如果走过,那就是步数少于以前步数的情况,可以再次通过。 { if(x < 0 || y < 0 || x >= n || y >= m) { return false; } if(v[x][y]) { if(f[x][y] > tep) { return true; } return false; } return true; } int getTep(char c, int i)//判断下一步应该多出几分钟(1分种还是等一会2分种) { if(i & 1) { if(c == '-') { return 1; } else { return 2; } } else { if(c == '|') { return 1; } else { return 2; } } } int bfs() { st = Node(sx, sy, 0); v[sx][sy] = true; f[sx][sy] = 0; priority_queue<Node> Q; Q.push(st); while(!Q.empty()) { st = Q.top(); Q.pop(); // printf("st.xt = %d st.y = %d st.tep = %d\n", st.x, st.y, st.tep); if(ch[st.x][st.y] == 'T') { return st.tep; } for(int i = 0; i < 4; ++i) { int x = st.x + mv[i][0]; int y = st.y + mv[i][1]; if(isOK(x, y, st.tep + 1) && ch[x][y] != '*') { if(ch[x][y] != '|' && ch[x][y] != '-') { v[x][y] = true; f[x][y] = st.tep + 1; Q.push(Node(x, y, st.tep + 1)); } else { char c = getC(ch[x][y], st.tep); int tx = x, ty = y; if(i & 1) { ty += mv[i][1]; } else { tx += mv[i][0]; } int tep = getTep(c, i); if(isOK(tx, ty, st.tep + tep) && ch[tx][ty] != '*') { v[tx][ty] = true; f[tx][ty] = st.tep + tep; Q.push(Node(tx, ty, f[tx][ty])); } } } } } return 0; } int main() { while(~scanf("%d%d", &n, &m)) { Mt(v, false); Mt(f, -1); for(int i = 0; i < n; ++i) { scanf("%s", ch[i]); for(int k = 0; ch[i][k]; ++k) { if(ch[i][k] == 'S') { sx = i; sy = k; } } } printf("%d\n", bfs()); } return 0; }
相关文章推荐
- HDU 1180 诡异的楼梯(bfs+优先队列)
- hdu 1180 诡异的楼梯
- 【搜索之BFS + 优先队列】杭电 hdu 1180 诡异的楼梯
- HDU 1180 诡异楼梯
- HDU 1180 题目诡异的楼梯
- HDU 1180 诡异的楼梯 -- 优先队列
- hdu 1180 诡异的楼梯 (BFS)
- hdu 1180 诡异的楼梯(bfs+最短路+dp)
- hdu1180 诡异的楼梯--BFS
- 诡异的楼梯 HDU - 1180
- HDU 1180 诡异的楼梯【BFS广搜+优先队列】
- hdu1180诡异的楼梯(广搜)
- HDU 1180 诡异的楼梯
- HDU 1180-诡异的楼梯
- hdu 1180 诡异的楼梯
- HDU1180 诡异的楼梯
- HDU 1180 诡异楼梯 bfs
- HDU1180-诡异的楼梯(bfs)
- hdu 1180 诡异的楼梯【BFS+优先队列】
- HDU 1180 诡异的楼梯(BFS+优先队列)