J - Fire! UVA - 11624

题意:火每次能烧到上下左右,人碰到非墙的边界则逃火成功,求最短的逃离时间。

由于bfs每个位置最多入队出队一次,所以复杂度为 1e6
一发bfs直接TLE,妙啊 -_-

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
const int maxn = 1e3+10;
using namespace std;
int n, m;
bool vis[maxn][maxn];
char s[maxn][maxn]; // 地图
int Jx, Jy;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};bool checkF(int x, int y) {if(x < 1 || x > n || y < 1 || y > m) return 0;return 1;
} bool checkJ(int x, int y) {if(s[x][y] == 'F' || s[x][y] == '#' || vis[x][y]) return 0;return 1;
} struct node{int x, y;int step;
};queue<node> FQ;int bfs() {node sta;sta.x = Jx; sta.y = Jy; sta.step = 0;queue<node> Q; Q.push(sta); while(!Q.empty()) {node now = Q.front(); Q.pop();if(now.x == 0 || now.x == n+1 || now.y == 0 || now.y == m+1) return now.step;while(!FQ.empty()) {node Fnow = FQ.front(); FQ.pop();for(int i = 0; i < 4; i++) { // 更新火烧到的地方 int nx = Fnow.x + dx[i], ny = Fnow.y + dy[i];if(checkF(nx,ny)) s[nx][ny] = '#';}}for(int i = 0; i < 4; i++) {int nx = now.x + dx[i], ny = now.y + dy[i];if(now.x == 0 || now.x == n+1 || now.y == 0 || now.y == m+1) return now.step+1;if(checkJ(nx,ny)){node tmp;tmp.x = nx, tmp.y = ny, tmp.step = now.step + 1;Q.push(tmp);}}}return 0;
}int main() {//  freopen("test.in", "r", stdin);int T; cin >> T;while(T--) {ios::sync_with_stdio(false);memset(vis, 0, sizeof(vis));while(!FQ.empty()) FQ.pop();int ans = 0;cin >> n >> m;for(int i = 1; i <= n; i++) {cin >> s[i]+1;for(int j = 1; j <= m; j++) {if(s[i][j] == 'J') Jx = i, Jy = j;if(s[i][j] == 'F') {node fire;fire.x = i; fire.y = j;FQ.push(fire);}}}ans = bfs();if(!ans) cout << "IMPOSSIBLE" << endl;else cout << ans << endl;}return 0;
}

注意:最开始可能不止一处起火

发现TLE是因为我在更新火蔓延的位置的时候出问题了,每次都更新,就出不来了,已经有火的地方就没必要再入队了,墙也不需要入队,修改一下就AC了

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
const int maxn = 1e3+10;
using namespace std;
int n, m;
bool vis[maxn][maxn];
char s[maxn][maxn]; // 地图
int Jx, Jy;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};bool checkF(int x, int y) {if(x < 1 || x > n || y < 1 || y > m) return 0;if(s[x][y] == '#') return 0;return 1;
} bool checkJ(int x, int y) {if(s[x][y] == 'F' || s[x][y] == '#' || vis[x][y]) return 0;return 1;
} struct node{int x, y;int step;int id; // 1是人   0是火
};queue<node> Q;int bfs() {node sta;sta.x = Jx; sta.y = Jy; sta.step = 0; sta.id = 1;vis[Jx][Jy] = 1;Q.push(sta); while(!Q.empty()) {int flag = 0;node now = Q.front(); Q.pop();if(now.id == 1 && (now.x == 0 || now.x == n+1 || now.y == 0 || now.y == m+1)) return now.step;for(int i = 0; i < 4; i++) {node tmp; tmp.step = now.step + 1;int nx = now.x + dx[i], ny = now.y + dy[i];if(now.id == 0) {if(checkF(nx,ny)) {s[nx][ny] = '#';tmp.x = nx; tmp.y = ny; tmp.id = 0;Q.push(tmp);}}else if(now.id == 1) {if(nx == 0 || nx == n+1 || ny == 0 || ny == m+1) return tmp.step;if(checkJ(nx,ny)){flag = 1; // 人还有路可走 vis[nx][ny] = 1;tmp.x = nx; tmp.y = ny; tmp.id = 1;Q.push(tmp);}}}}return 0;
}int main() {//  freopen("test.in", "r", stdin);ios::sync_with_stdio(false);int T; cin >> T;while(T--) {memset(vis, 0, sizeof(vis));while(!Q.empty()) Q.pop();int ans = 0;cin >> n >> m;for(int i = 1; i <= n; i++) {cin >> s[i]+1;for(int j = 1; j <= m; j++) {if(s[i][j] == 'J') Jx = i, Jy = j;if(s[i][j] == 'F') {node fire;fire.x = i; fire.y = j; fire.id = 0;Q.push(fire);}}}ans = bfs();if(!ans) cout << "IMPOSSIBLE" << endl;else cout << ans << endl;}return 0;
}

J - Fire! UVA - 11624相关推荐

  1. [kuangbin带你飞]专题1 简单搜索 J - Fire! UVA - 11624

    题目: Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of t ...

  2. Fire! UVA - 11624

    题目链接:Fire! UVA - 11624 =================================================== Fire Time Limit: 1000MS D ...

  3. Fire uva 11624

    题目连接:http://acm.hust.edu.cn/vjudge/problem/28833 /* 首先对整个图bfs一次得到火焰燃烧的时刻表 之后在bfs搜路径时加一个火烧表的判断 坑点在于:如 ...

  4. 【POJ3126 Prime Path】【POJ 3087 Shuffle'm Up】【UVA 11624 Fire!】【POJ 3984 迷宫问题】

    POJ3126Prime Path 给定两个四位素数a  b,要求把a变换到b 变换的过程要 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数  与  前一步得到的素数  只能有一个位 ...

  5. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 1 /* 2 BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. 3 */ 4 /************************ ...

  6. UVA - 11624  Fire! 两次BFS

    UVA - 11624  Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and ...

  7. UVA - 11624 - Fire!

    题目描述: 小明最后也没能进入游戏大厂,也没能娶到心爱的女孩,现在小明在一家迷宫里工作. 不幸的是,迷宫里因为线路老化而发生了火灾.小明现在需要一个逃跑路线,请你帮助倒霉的小明从迷宫中逃离出去吧 小明 ...

  8. UVA - 11624 Fire!

    题意:求Joe走出迷宫的最短时间,有障碍,还有向四周蔓延的火 思路:将火的位置也加进BFS里面 #include <iostream> #include <cstdio> #i ...

  9. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

最新文章

  1. 2021-10-14 yolov5踩坑!!!经验大赏
  2. 《数据结构与抽象:Java语言描述(原书第4版)》一JI2.3 抛出异常
  3. 数据结构C语言实现课后第1-2章答案
  4. 滴滴产品总监:如何合理设计弹窗以保证流畅的用户体验?
  5. 对象的初始化列表const变量的初始化
  6. Spring Boot自定义缓存注解
  7. 如何处理Dubbo调用超时
  8. mybatis 配置文件报错:Referenced file contains errors(file:/D:/config/ mybatis-3-mapper.dtd).
  9. Java8 Optional类
  10. NSArray与NSSet的区别
  11. AIdl server端监听client是否掉线
  12. libcareplus支持的补丁类型
  13. Just For Fun:在windows下模拟一个windows病毒软件(windows.h)
  14. HTML结构:自我简介网页
  15. html5游戏需求文档,游戏PRD丨狼人杀APP产品需求文档
  16. C/C++使用ODBC连接MSSQL数据库
  17. SpringCloud-7-配置中心
  18. 2015广州强网杯 致敬经典
  19. license.lic
  20. 《什么是HTML5》

热门文章

  1. 【OpenCV 4开发详解】直方图应用
  2. JSX设置CSS样式详解
  3. Linux(Windows)下如何改变网卡的LinkSpeed工作模式
  4. SQL语句性能优化--LECCO SQL Expert
  5. 深入分析Parquet列式存储格式
  6. 极速开发之Spring Boot五种热部署方式
  7. 带你玩玩转 MySQL 查询
  8. 用Python批量实现多Excel多Sheet合并的4种方法
  9. 借助tkinter设计人脸检测的界面(摄像头检测,视频检测,视频检测并保存)
  10. P1486 [NOI2004] 郁闷的出纳员 FHQ-Treap