题目链接:https://vjudge.net/problem/UVA-11624

题解:

坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯一。

火和人使用同一种结构体,用id来区别类型。BFS求解:首先将所有火苗入队,然后人再入队(肯定要火苗先入队,因为人要根据火当前烧到哪里来进行移动)。

对于一个尝试点:如果当前的id是人,且走出界,则逃生成功。如果没有走出界,则:

写法一(模拟过程): 如果没有走出界:如果id是火,且此地方是通道,则不管有没有vis过,都把它烧了,即把地图改为‘F’;如果id是人,且此地方是通道且没有被vis过,则进去。

写法二:其实火苗的本质作用是什么?就是禁止人vis那个地方,相当于会扩散的墙。有一点需要注意:对于一个通道,如果人比火先访问,那么其他相邻的通道,都是人先访问的(BFS的特点),所以火在扩散的时候,不用把周围也改为‘F’,直接把它标为vis,对于vis过的点,火和人都 不能再访问。那么我们就不用把火和人分开来处理了:对于一个合法的尝试点,如果此点是通道,则把它标为已经vis,之后入队即可。

写法一:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m;
char g[MAXN][MAXN];
int vis[MAXN][MAXN], dir[][] = {,,,,-,,,-}; struct node
{
int x, y, id, step; //id为是人是火的标签
}; queue<node>que;
int bfs()
{
ms(vis, );
while(!que.empty()) que.pop(); node now, tmp;
for(int i = ; i<=n; i++) //先将所有的火一次性放进队列中
for(int j = ; j<=m; j++)
{
if(g[i][j]=='F')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} for(int i = ; i<=n; i++) //将人放进队列中
for(int j = ; j<=m; j++)
{
if(g[i][j]=='J')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} while(!que.empty())
{
now = que.front();
que.pop(); for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.step = now.step + ;
tmp.id = now.id;
if(tmp.id== && (tmp.x< || tmp.x>n || tmp.y< || tmp.y>m) ) //如果是人,且走出界,则逃生成功
return tmp.step; if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m) //位置合法
{
//如果是火,并且可以燃烧,不过他有没有vis过,都把它给烧了
if(tmp.id== && g[tmp.x][tmp.y]=='.' || g[tmp.x][tmp.y]=='J' )
{
g[tmp.x][tmp.y] = 'F'; //这个地方变成火了
que.push(tmp);
}
//如果是人,并且这个地方是通道可没有vis过,则可行
if(tmp.id== && g[tmp.x][tmp.y]=='.' && !vis[tmp.x][tmp.y])
{
vis[tmp.x][tmp.y] = ;
que.push(tmp);
}
}
}
}
return -;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++)
scanf("%s", g[i]+); int ans = bfs();
if(ans==-)
puts("IMPOSSIBLE");
else
printf("%d\n", ans);
}
}

写法二:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m;
char g[MAXN][MAXN];
int vis[MAXN][MAXN], dir[][] = {,,,,-,,,-}; struct node
{
int x, y, id, step;
}; queue<node>que; int bfs()
{
ms(vis, );
while(!que.empty()) que.pop(); node now, tmp;
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(g[i][j]=='F')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(g[i][j]=='J')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} while(!que.empty())
{
now = que.front();
que.pop(); for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.step = now.step + ;
tmp.id = now.id;
if(tmp.id== && (tmp.x< || tmp.x>n || tmp.y< || tmp.y>m) ) //逃生成功
return tmp.step; if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m
&& !vis[tmp.x][tmp.y] && g[tmp.x][tmp.y]=='.' ) //如果此地方是通道且没有被vis,则把它标为vis
{
vis[tmp.x][tmp.y] = ;
que.push(tmp);
}
}
}
return -;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++)
scanf("%s", g[i]+); int ans = bfs();
if(ans==-)
puts("IMPOSSIBLE");
else
printf("%d\n", ans);
}
}

最新文章

  1. HikariCP
  2. Java重载遇到泛型
  3. EL和JSTL专题
  4. OC-copy
  5. python操作TexturePacker批量打包资源plist png
  6. Hadoop入门进阶课程11--Sqoop介绍、安装与操作
  7. Difference between _, __ and __xx__ in Python
  8. php里session的用法
  9. Windows任务计划
  10. MyEclipse中文注释乱码解决
  11. 在c++中使用Outlook Object Model发送邮件
  12. WinDBG 技巧:如何生成Dump 文件(.dump 命令)
  13. 解决Android编译时出现aapt.exe finished with non-zero exit value 1
  14. 贪心算法----区间选点问题(POJ1201)
  15. 06-JavaScript的流控制语句
  16. webservice访问的几种方式
  17. restframwork之序列化
  18. android sdk更新代理设置
  19. 每日英语:How the College Bubble Will Pop
  20. wcf的DataContractAttribute与DataMenmberAttribute

热门文章

  1. ReSharper7.1.25.234 注册机
  2. Bootstrap开启模态框后对数据处理(标记模态框的开启与关闭状态)
  3. 【Android】状态栏通知Notification、NotificationManager详解(转)
  4. 关于maven下载慢的问题
  5. Linux 系统的常用命令之 rm ,rm -rf , rm -f 以及rm 命令的其他参数命令
  6. DBCP,C3P0与Tomcat jdbc pool 连接池的比较
  7. DDCTF2019逆向分析前俩题WriteUP
  8. MongoDB学习day10--数据库导入导出
  9. 【PowerShell 学习系列】-- 删除Win10自带应用
  10. [转] Python 常用第三方模块 及PIL介绍