HDU 1242 Rescue (BFS+优先队列)
2015-10-25 09:50
423 查看
从天使的位置开始,直到找到r,优先队列每次选择最短时间的路
#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<sstream> #include<queue> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const int M = 1010; struct node { int x, y, step; bool operator <(const node &b)const { return step > b.step; } }; int n, m; priority_queue<node>q; char mp[210][210]; int sx, sy; bool vis[210][210]; int dir[][2] = { 1,0,-1,0,0,1,0,-1 }; int bfs() { node st; st.x = sx; st.y = sy; st.step = 0; vis[sx][sy] = 1; q.push(st); while (!q.empty()) { node nw = q.top(); q.pop(); if (mp[nw.x][nw.y] == 'r') { return nw.step; } for (int i = 0; i < 4; ++i) { int xx = nw.x + dir[i][0]; int yy = nw.y + dir[i][1]; if(xx<0 ||xx>=n ||yy<0||yy>=m ||vis[xx][yy]) continue; if(mp[xx][yy]=='#') continue; if (mp[xx][yy] == 'x') { vis[xx][yy] = 1; node nx; nx.x = xx; nx.y = yy; nx.step = nw.step + 2; q.push(nx); } else { vis[xx][yy] = 1; node nx; nx.x = xx; nx.y = yy; nx.step = nw.step + 1; q.push(nx); } } } return -1; } int main() { while (~scanf("%d%d", &n, &m)) { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> mp[i][j]; if (mp[i][j] == 'a') { sx = i; sy = j; } } } memset(vis, 0, sizeof(vis)); int flag=bfs(); if (flag == -1) { puts("Poor ANGEL has to stay in the prison all his life."); } else { printf("%d\n", flag); } } return 0; }
相关文章推荐
- Gradle Plugin Samples 之 Gradle Build Variants(六)
- Gradle Build Configs
- BZOJ 4302 Buildings 解题报告
- BZOJ 4302 Buildings 解题报告
- Hadoop应用-------Hadoop计算能力调度器(Capacity Scheduler)应用和配置{hadoop mradmin -refreshQueues动态更新队列和容量}其他调度器比较
- Learn from Synchronized and BlockingQueue
- Leetcode: Unique Paths
- UE4蓝图案例:制作透明黑色玻璃
- Queue
- POJ 1458 Common Subsequence
- POJ 1458 Common Subsequence
- Android:带你玩转Servie,子线程,与UI线程通信[导入导出]
- cf 11a Increasing Sequence
- Positional parameter does not exist: 1 in query: from Books where isbn=?
- 转载:为什么首次进入界面是SwipeRefreshLayout.setRefreshing(true)没有动画效果
- 【APUE】Chapter12
- 《西安交大电路》(Principles of Electrical Circuits) 学习笔记
- [AndroidUI]自定义view(五):实现动态柱状图
- 实例解析JS布尔对象的toString()方法和valueOf()方法
- uibutton 的setImage和setBackgroundImage的区别