UVA11624 Fire!
来源:互联网 发布:北京赛车pk10辅助软件 编辑:程序博客网 时间:2024/05/16 14:32
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <queue>using namespace std;struct node{ int x,y; int t; bool operator < (const node &a)const{ return t > a.t; }};const int dx[] = {-1,0,1,0},dy[] = {0,-1,0,1};int n,m;char maze[1005][1005];int visj[1005][1005],visf[1005][1005];void bfs1(){ memset(visf,-1,sizeof(visf)); node cur; queue<node> q; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(maze[i][j] == 'F'){ visf[i][j] = 0; q.push(node{i,j,0}); } } } while(!q.empty()){ cur = q.front(); q.pop(); for(int i = 0; i < 4; i++){ int xx = cur.x+dx[i],yy = cur.y+dy[i]; if(xx < 0 || xx >= n || yy < 0 || yy >= m || visf[xx][yy] != -1) continue; if(maze[xx][yy] != '#'){ visf[xx][yy] = visf[cur.x][cur.y]+1; q.push(node{xx,yy,visf[xx][yy]}); } } }}int bfs2(int x,int y){ memset(visj,-1,sizeof(visj)); node cur; visj[x][y] = 0; priority_queue<node> q; q.push(node{x,y,0}); while(!q.empty()){ cur = q.top(); q.pop(); if(cur.x == 0 || cur.x == n-1 || cur.y == 0 || cur.y == m-1){ return cur.t+1; } for(int i = 0; i < 4; i++){ int xx = cur.x+dx[i],yy = cur.y+dy[i]; if(xx < 0 || xx >= n || yy < 0 || yy >= m || visj[xx][yy] != -1) continue; if(visf[xx][yy] != -1 && visj[cur.x][cur.y]+1 >= visf[xx][yy]) continue; if(maze[xx][yy] != '#'){ visj[xx][yy] = visj[cur.x][cur.y]+1; q.push(node{xx,yy,visj[xx][yy]}); } } } return -1;}int main(){ int T; scanf("%d",&T); while(T--){ int jx,jy,fx,fy; scanf("%d%d",&n,&m); for(int i = 0; i < n; i++){ scanf("%s",maze[i]); for(int j = 0; j < m; j++){ if(maze[i][j] == 'J'){ jx = i;jy = j; } } } bfs1();//模拟火蔓延,尼玛这题有毒呀,火的位置不止一个 int ans = bfs2(jx,jy);//模拟人 if(ans < 0) printf("IMPOSSIBLE\n"); else printf("%d\n",ans); } return 0;}
0 0
- Fire! UVA11624
- UVA11624 Fire!
- uva11624 - Fire!
- uva11624 Fire!
- UVA11624 Fire!
- UVa11624 Fire!
- UVA11624 Fire!
- UVA11624----Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- UVA11624 Fire!
- 【UVA11624】Fire!
- UVA11624-Fire!
- UVA11624-Fire!(两次bfs)
- [bfs] UVA11624-Fire!
- 堡垒机 | 跳板机 | 中控机
- java面向对象及其特性初探
- [Python标准库]random——伪随机数生成器
- 网络工具类代码---单例(上传文件等等,发送网络请求)
- 自学QT之图片报错Paint device returned engine == 0, type: 1
- UVA11624 Fire!
- 程序运行时间计算
- 08-cmd定时关机和取消定时关机
- ubantu 安装tomcat
- iOS 学习笔记4-NSDictionary 和 NSMutableDictionary
- Leetcode: Number of Digit One
- POJ 3984 迷宫问题(bfs+打印路径)
- python的开发环境搭建
- Linux格式化硬盘 常用命令小记