hdu 1242 Rescue(广搜+优先队列)
2014-02-07 22:05
337 查看
/*
题意:r点到a点最短距离多少,x代表停留一秒;
*/
题意:r点到a点最短距离多少,x代表停留一秒;
*/
#include<iostream> #include<string> #include<cstring> #include<queue> #define N 210 using namespace std; int n, m, mark , dir[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}}; int s_x, s_y, e_x, e_y; char str ; struct node { int x, y, step; friend bool operator < (node n1, node n2) { return n2.step < n1.step; } }; int judge(int x, int y) { if(x>=0 && x<n && y>=0 && y<m && mark[x][y]!=1 && str[x][y] != '#') return 1; else return 0; } int bfs() { int x, y, i; node cur, next; priority_queue<node>q; cur.x=s_x; cur.y=s_y; cur.step=0; q.push(cur); while(!q.empty()) { cur = q.top(); q.pop(); if(cur.x==e_x && cur.y==e_y) return cur.step; for(i=0; i < 4; i++ ) { next.x = x = cur.x + dir[i][0]; next.y = y = cur.y + dir[i][1]; next.step = cur.step + 1; if(judge(x, y)) { mark[x][y] = 1; if(str[x][y] == 'x') next.step++; q.push(next); } } } return -1; } int main() { int i, j; while(scanf("%d%d", &n, &m) != -1) { for( i=0; i < n; i++ ) scanf("%s", str[i]); for( i=0; i < n; i++ ) { for( j=0; j < m; j++ ) { if(str[i][j] == 'a') e_x=i, e_y=j; if(str[i][j] == 'r') s_x=i, s_y=j; } } memset(mark, 0, sizeof(mark)); int ans = bfs(); if(ans == -1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n", ans); } return 0; }
相关文章推荐
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242——Rescue(优先队列)
- HDU 1242 - Rescue(BFS 优先队列)
- HDU 1242 Rescue(广搜+优先队列)
- hdu 1242 Rescue 优先队列问题
- HDU--杭电--1242--Rescue--广搜--又是一个所谓的优先队列起作用的题,擦,虽然比上一个容易,不过还是拿来摆摆
- hdu 1242 Rescue(优先队列)
- hdu 1242 Rescue (优先队列+bfs)
- hdu-1242Rescue(优先队列+bfs)
- HDU 1242 Rescue(优先队列+bfs)
- [ACM] HDU 1242 Rescue (优先队列)
- hdu-1242Rescue(bfs搜索+优先队列)
- hdu 1242 Rescue 搜索 bfs 优先队列
- hdu 1242:Rescue(BFS广搜 + 优先队列)
- HDU 1242 rescue (优先队列模板题)
- hdu 1242 Rescue 优先队列
- hdu 1242 Rescue(广搜+优先队列)
- HDU-1242 Rescue( 广搜 + 优先队列)
- BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)
- hdu-1242-Rescue-优先队列