ZOJ 1649 Rescue (BFS)
来源:互联网 发布:springboot配置端口号 编辑:程序博客网 时间:2024/04/29 18:32
题目大意:在一个矩阵里,有五种字符,一种是起点,一种是终点,一种代表墙不能走的,一种代表警卫需要花时间杀警卫的,最后一种代表路,每走一个格要花一个单位时间,杀一个警卫要花一个单位时间。求从起点到终点,花得最短时间!
分析:最短路径,其实很简单的,bfs搜索,然后判断一下是不是墙,是不是警卫和路,分别处理;在用一个数组d来记录每个点到起点的最短路径,同时也是用来标记已经遍历过的状态。
代码:
#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;const int N = 210;const int M = 210;const int INF = 1000000;int n, m, xa, ya, xr, yr, d[N][M], x[4] = { 0, 0, 1, -1 }, y[4] = { 1, -1, 0, 0 };bool vis[N][N];char g[N][M];struct P { int x, y; };bool bfs() { queue<P> Q; P e; e.x = xr, e.y = yr; Q.push( e ); for ( int i = 1; i <= n; ++i ) for ( int j = 1; j <= m; ++j ) d[i][j] = INF; d[xr][yr] = 0; while ( !Q.empty() ) { P u = Q.front(); Q.pop(); int ux = u.x , uy = u.y; //printf("%d %d\n", ux, uy); for ( int i = 0; i < 4; ++i ){ int x1 = u.x + x[i], y1 = u.y + y[i]; if ( g[x1][y1] == '#' ) continue; if ( g[x1][y1] == 'x' ) { if ( d[x1][y1] > d[ux][uy] + 2 ) { d[x1][y1] = d[ux][uy] +2 ; e.x = x1, e.y = y1; Q.push(e); } } else if ( g[x1][y1] == '.' || g[x1][y1] == 'a' ) { if ( d[x1][y1] > d[ux][uy] + 1 ) { d[x1][y1] = d[ux][uy] + 1; e.x = x1; e.y = y1; Q.push(e); } } } } if ( d[xa][ya] == INF ) return false; else return true;}int main() { while ( scanf("%d%d", &n, &m) != EOF ) { getchar(); memset(vis, 0, sizeof(vis)); for ( int i = 0; i <= m+1; ++i ) g[i][0] = g[i][m+1] = '#'; for ( int i = 0; i <= n+1; ++i ) g[0][i] = g[n+1][i] = '#'; for ( int i = 1; i <= n; ++i, getchar() ) for ( int j = 1; j <= m; ++j ) { scanf("%c", &g[i][j]); if ( g[i][j] == 'a' ) xa = i, ya = j; if ( g[i][j] == 'r' ) xr = i, yr = j; } //for ( int i = 0; i <= m + 1; ++i ) printf("%s\n", g[i]); if ( bfs() ) printf("%d\n", d[xa][ya]); else printf("Poor ANGEL has to stay in the prison all his life.\n"); }}
- 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 (搜索)
- ZOJ 1649 Rescue BFS水题
- zoj 1649 Rescue (bfs+队列)
- 简单BFS ZOJ 1649Rescue
- ZOJ 1649 && HDU 1242 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)
- DotA-II,让我们关注游戏行业里消逝的东西
- 哪位大神知道,jquery中UI的dialog的buttons中的属性,比如文字和背景图片等了,憋了2天啦
- 嵌入式交叉编译环境搭建
- 红糖什么牌子好
- 网络台球游戏的网络编程(vc++)
- ZOJ 1649 Rescue (BFS)
- windows编程一日一练(3)
- CentOS 6.4 (x86_64) 安装 Oracle 10g2 备记
- iOS生成一个8位的随机密码
- 完美解决ScrollView嵌套ListView滑动冲突(有图有真相)
- mx53 camera HAL preview(接口层)
- python 基础篇之二
- 多么痛的领悟-代码优化导致的BUG
- 哈佛大学图书馆的名言