ZOJ 1649 Rescue (BFS)
来源:互联网 发布:蓝格美容院软件 编辑:程序博客网 时间:2024/05/15 17:35
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649
做得蛋疼!!!
要注意以下几点:
- cost[i,j]表示friend到[i,j]的代价
- 每次队列最前元素的周围四个元素,都要计算其代价,即使已经被访问过
- 如果出队列的元素是angle,不需要立即break,因为队列中后续元素可能有更小代价
#include <iostream>#include <vector>#include <algorithm>#include <queue>#include <string.h>#include <stdio.h>using namespace std;#define MAXINT 0x7FFFFFFFint dx[] = {0, 1, 0, -1};int dy[] = {-1, 0, 1, 0};int main(){int N, M;char matrix[200][200];int cost[200][200];while (cin >> N >> M){for (int i = 0; i < N; i++){for (int j = 0; j < M; j++)cin >> matrix[i][j];getchar();}int min = MAXINT;for (int i = 0; i < N; i++)for (int j = 0; j < M; j++)if (matrix[i][j] == 'r') // a friend{// initializefor (int k = 0; k < N; k++)for (int h = 0; h < M; h++)cost[k][h] = MAXINT;// add first onecost[i][j] = 0;queue< pair<int, int> > q;q.push(make_pair<int, int>(i, j));while (!q.empty()){pair<int, int> c = q.front();q.pop();if (matrix[c.first][c.second] == 'a'){if (cost[c.first][c.second] < min)min = cost[c.first][c.second];}else{for (int k = 0; k < 4; k++){int x = c.first + dx[k];int y = c.second + dy[k];if (0 <= x && x < N && 0 <= y && y < M && matrix[x][y] != '#'){if (cost[x][y] == MAXINT ||(matrix[c.first][c.second] == 'x' && cost[x][y] > cost[c.first][c.second] + 2) ||(matrix[c.first][c.second] != 'x' && cost[x][y] > cost[c.first][c.second] + 1)){q.push(make_pair<int, int>(x, y));}if (matrix[c.first][c.second] == 'x' && cost[x][y] > cost[c.first][c.second] + 2){cost[x][y] = cost[c.first][c.second] + 2;}else if (matrix[c.first][c.second] != 'x' && cost[x][y] > cost[c.first][c.second] + 1){cost[x][y] = cost[c.first][c.second] + 1;}}}}}}if (min == MAXINT)cout << "Poor ANGEL has to stay in the prison all his life." << endl;elsecout << min << endl;}}
- ZOJ 1649 Rescue (BFS)
- zoj 1649 rescue (bfs)
- ZOJ 1649 : Rescue - BFS
- zoj 1649 Rescue bfs
- ZOJ 1649 Rescue BFS水题
- ZOJ 1649 Rescue(BFS)
- ZOJ 1649 Rescue (BFS)
- ZOJ 1649:Rescue(BFS)
- zoj 1649 Rescue (bfs+队列)
- 简单BFS ZOJ 1649Rescue
- ZOJ 1649 Rescue(BFS)
- ZOJ 1649 Rescue (BFS) -- from lanshui_Yang
- ZOJ 1649 - Rescue BFS/优先队列
- ZOJ-1649 Rescue BFS (HDU 1242)
- ZOJ 1649 && HDU 1242 Rescue (BFS)
- ZOJ 1649 Rescue(有敌人迷宫BFS)
- ZOJ 1649 Rescue BFS (搜索)
- zoj 1649 Rescue【BFS+优先队列】
- C#委托用法
- 纹理加载时,出现报错是:pixel format with necessary capabilities not found时
- 在ANDROID中使用自定义字体
- 运算符-重载为成员函数 和友元函数的区别.
- 关于ping带源地址和不带源地址有什么区别呢
- ZOJ 1649 Rescue (BFS)
- git、repo、gerrit之间的关系--学习感受第一篇
- Android开发之搜索框应用(一)
- C++重载操作符的简单实例
- 一个企业真有所谓的基因吗?
- 2011-8-1 21:40:28
- CVS使用手册
- java和ibatis事务处理
- 艰难地写文档