UVA 11624 Fire!
来源:互联网 发布:android系统相册源码 编辑:程序博客网 时间:2024/06/05 19:55
题意:在一个森林中有某些地方起火,有一个在森林中要逃出森林,到矩阵边缘即算逃出森林,火蔓延的速度和逃跑速度相同,判断这人是否能够逃出森林
链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28833
思路:对所有起火点进行广搜,记录每个点的最短时间,第二次对人进行广搜,判断到底边界时是否满足条件
注意点:起火点可能有多个
以下为AC代码:
RunIDUserOJProb IDResult Memory
(KB)Time
(ms)Language Length
(Bytes)
luminous11
UVA
11624Accepted 335C++11 4.8.2
30922015-02-28 16:56:42
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <vector>#include <deque>#include <list>#include <cctype>#include <algorithm>#include <climits>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#include <iomanip>#include <cstdlib>#include <ctime>#define ll long long#define ull unsigned long long#define all(x) (x).begin(), (x).end()#define clr(a, v) memset( a , v , sizeof(a) )#define pb push_back#define mp make_pair#define read(f) freopen(f, "r", stdin)#define write(f) freopen(f, "w", stdout)using namespace std;const double pi = acos(-1);int dir[4][2] = { 0,1, 0,-1, 1,0, -1,0 };int adj[1005][1005];char str[1005][1005];bool vis[1005][1005];int m, n;struct node{ int x, y, cnt; node(){} node ( int _x, int _y ) : x(_x), y(_y) {} node ( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}};queue<node> q;void init(){ clr( str, '@' ); for ( int i = 1; i <= m; i ++ ){ scanf ( "%s", &str[i][1] ); str[i][n+1] = '@'; } clr ( adj, 0x3f3f3f3f );}void bfs ( ){ while ( ! q.empty() ){ node tmp = q.front(); q.pop(); for ( int i = 0; i < 4; i ++ ){ int xi = tmp.x + dir[i][0]; int yi = tmp.y + dir[i][1]; if ( xi < 0 || yi < 0 || xi > m + 1 || yi > n + 1 )continue; if ( adj[xi][yi] > adj[tmp.x][tmp.y] + 1 ){ adj[xi][yi] = adj[tmp.x][tmp.y] + 1; if ( str[xi][yi] != '#' && str[xi][yi] != '@' ) q.push ( node ( xi, yi ) ); } } }}int bfs2 ( int x, int y ){ queue<node> q; clr ( vis, 0 ); vis[x][y] = 1; q.push ( node ( x, y, 1 ) ); while ( ! q.empty() ){ node tmp = q.front(); q.pop(); for ( int i = 0; i < 4; i ++ ){ node now = node( tmp.x + dir[i][0], tmp.y +dir[i][1], tmp.cnt + 1 ); if ( vis[now.x][now.y] == 0 && adj[now.x][now.y] > now.cnt && str[now.x][now.y] != '#' ){ if ( str[now.x][now.y] == '@' ){ return tmp.cnt; } vis[now.x][now.y] = 1; q.push ( now ); } } } return -1;}void print(){ for ( int i = 0; i <= 5; i ++ ){ for ( int j = 0; j <= 5; j ++ ){ cout << str[i][j]; } cout << endl; }}void solve(){ int sx, sy; for ( int i = 1; i <= m; i ++ ){ for ( int j = 1; j <= n; j ++ ){ if ( str[i][j] == 'J' ){ sx = i; sy = j; } if ( str[i][j] == 'F' ){ q.push ( node ( i, j ) ); adj[i][j] = 1; } } } int ans = 1 << 29; bfs (); ans = bfs2 ( sx, sy ); if ( ans == -1 ) printf ("IMPOSSIBLE\n"); else printf ( "%d\n", ans );}int main(){ int t; scanf ( "%d", &t ); while ( t -- ){ scanf ( "%d%d", &m, &n ); init(); solve(); } return 0;}
0 0
- UVa 11624 - Fire!
- UVA 11624 - Fire!
- Uva-11624-Fire!
- uva 11624 Fire!
- uva 11624 - Fire!
- UVa 11624 - Fire!
- UVa 11624 Fire!
- UVA 11624 BFS Fire
- UVa Fire! 11624 (BFS)
- UVA - 11624 Fire!
- uva 11624 - Fire! bfs
- UVA 11624 Fire!
- UVa 11624 Fire! / BFS
- UVa:11624 Fire!
- UVA - 11624 Fire!
- UVa 11624 - Fire!
- uva 11624 Fire
- Uva 11624 Fire!
- 任务调度——java.util.Timer
- SP with parameter
- Sharepoint datetime 支持多个
- linux 单独安装oracle客户端
- Transferring Data Without Draining the Battery - Modifying your Download Patterns Based on the Conne
- UVA 11624 Fire!
- SQL SERVER 变量赋值
- Oracle如何限制固定IP的访问
- 使用icomoon制作icon字体
- OC中字符串创建与字符串处理函数
- you gun shang jia
- android listview的用法
- 为什么你应该(从现在开始就)写博客 --via刘未鹏
- FastJson---高性能JSON开发包