POJ 2312<优先队列><最短路>
2014-12-27 14:48
357 查看
点此看题
#include <stdio.h> #include <queue> #include <cstring> using namespace std; char a[305][305]; int n,m; int sx,sy,ex,ey; int v[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; bool f[305][305]; struct node { int x,y,w; bool operator < (const node& q) const { return w > q.w; } }; int fun() { priority_queue<node> que; node g; g.x = sx; g.y = sy; g.w = 0; que.push(g); f[sx][sy] = true; while(!que.empty()) { node tm = que.top(); que.pop(); for(int i = 0; i < 4;i++) { node q; q.x = tm.x + v[i][0]; q.y = tm.y + v[i][1]; q.w = tm.w; if(!f[q.x][q.y]) switch(a[q.x][q.y]) { case 'S': case 'R': { break; } case 'B': { q.w += 2; que.push(q); f[q.x][q.y] = true; break; } case 'E': { q.w += 1; que.push(q); f[q.x][q.y] = true; break; } case 'T': { return q.w+1; } } } } return -1; } int main() { while(scanf("%d%d",&n,&m), n||m) { for(int i = 0; i <= m+1;i++) { a[0][i] = 'S'; a[n+1][i] = 'S'; } for(int i = 0 ; i<= n+1;i++) { a[i][0] = 'S'; a[i][m+1] = 'S'; } getchar(); memset(f,0,sizeof(f)); for(int i = 1; i <= n;i++ ) { for(int j = 1; j <= m;j++) { a[i][j] = getchar(); if(a[i][j] == 'Y') { sx = i; sy = j; } } getchar(); } printf("%d\n",fun()); } return 0; }
相关文章推荐
- poj 1130 Alien Security (dfs+bfs<反向最短路>)
- DP <裸> POJ 1157
- <poj 1046>Color Me Less
- POJ 3162 Walking Race 树的直径+单调队列(其实暴力也可以>_<)
- hdu3790 最短路径问题<最短路>
- nyoj 懒省事的小明<优先队列>
- HDU 2544 最短路 <裸地迪杰斯特拉算法>
- poj 1852 Ants <规律题>
- 【最短路建模】【d[u] <= d[v] + g[v][u] v--->u】【差分系统】
- HDU 2544 最短路 <SPFA算法>
- wata 哥 的 vector<MAX> G 第一次不能用 POJ 3321 树状数组
- 按位与&和短路与&&区别<转载>
- POJ1014:Dividing<动归,背包问题>
- DP<01 背包> POJ 1837
- <poj,sicily>Anti-prime Sequences (DFS)
- <poj 1056>IMMEDIATE DECODABILITY
- 刘汝佳<算法艺术与信息学竞赛>推荐题目——Poj
- poj1753,Flip Game,ArrayDeque<Node>
- poj 1423 Big Number<<求N!位数>>
- poj 2931 Building a Space Station <克鲁斯卡尔>