题目链接:https://vjudge.net/contest/274927#problem/H
题目大意:就是joe在迷宫值班(是真的不知道为啥要在迷宫值班),然后突然就着火了,最重要的是可以有不止一个地方着火!!!!!!!!然后判断joe能不能逃出去。
思路:就是先搜火势,记录下火势蔓延到每个点所需要的时间,然后再搜joe,看能否走出去。思路很好理解,嗯。
2021/3/16 (之前写的可真丑 qwq,新贴一个
#include<iostream>
#include<cstdio>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxN = 1e3 + 10;
int read() {
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = -f; ch = getchar(); }
while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
int n, m;
bool vis[maxN][maxN];
char mp[maxN][maxN];
int dis[maxN][maxN];
const int dir[4][2] = {
0, 1,
0, -1,
-1, 0,
1, 0
};
struct pos{
int x, y;
}J;
queue<pos>F;
struct node{
int x, y, t;
};
bool InMap(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
void bfsF(int x, int y) {
queue<node>q;
q.push(node{x, y, 0});
dis[x][y] = INF;
while(!q.empty()) {
node tmp = q.front(); q.pop();
for(int i = 0; i < 4; ++ i ) {
int xx = tmp.x + dir[i][0], yy = tmp.y + dir[i][1], tt = tmp.t + 1;
if(!InMap(xx, yy) || mp[xx][yy] == '#' || (dis[xx][yy] && dis[xx][yy] <= tt)) continue;
q.push(node{xx, yy, tt});
dis[xx][yy] = tt;
}
}
}
int bfsJ(int x, int y) {
queue<node>q;
q.push(node{x, y, 0});
vis[x][y] = true;
while(!q.empty()) {
node tmp = q.front(); q.pop();
for(int i = 0; i < 4; ++ i ) {
int xx = tmp.x + dir[i][0], yy = tmp.y + dir[i][1], tt = tmp.t + 1;
if(!InMap(xx, yy)) return tt;
if(mp[xx][yy] == '#' || vis[xx][yy] || (dis[xx][yy] && dis[xx][yy] <= tt)) continue;
q.push(node{xx, yy, tt});
vis[xx][yy] = true;
}
}
return -1;
}
int main() {
int TAT = read();
while(TAT -- ) {
n = read(), m = read();
for(int i = 0; i < n; ++ i ) for(int j = 0; j < m; ++ j ) vis[i][j] = false, dis[i][j] = 0;
for(int i = 0; i < n; ++ i ) {
scanf("%s", mp[i]);
for(int j = 0; j < m; ++ j ) {
if(mp[i][j] == 'F') {
mp[i][j] = '#';
F.push(pos{i, j});
} else if(mp[i][j] == 'J') J = pos{i, j}, mp[i][j] = '.';
}
}
while(!F.empty()) {
pos tmp = F.front(); F.pop();
bfsF(tmp.x, tmp.y);
}
int ans = bfsJ(J.x, J.y);
if(ans == -1) { puts("IMPOSSIBLE"); }
else printf("%d\n", ans);
}
return 0;
}
#include <stdio.h>
#include<queue>
#include<string.h>
#include<utility>
#define INF 10000000;
using namespace std;
int r,c;//行和列
int initialx,initialy;//joe的起始位置
char mp[1005][1005];//地图
int mark[1005][1005];//标记数组,用来标记火势蔓延
int time[1005][1005];//用来记录火势到达该点时所需要的时间
struct node
{
int x,y,step;
};
const int fix[4][2]=
{
1,0,
0,1,
-1,0,
0,-1
};
queue<node>Qf;
queue<node>Q;
void bfs_fire()
{
while(!Qf.empty())
{
for(int i=0; i<4; i++)
{
node ftemp;
ftemp.x=Qf.front().x+fix[i][0];
ftemp.y=Qf.front().y+fix[i][1];
if(ftemp.x<0||ftemp.y<0||ftemp.x>=r||ftemp.y>=c||mp[ftemp.x][ftemp.y]=='#'||mp[ftemp.x][ftemp.y]=='F'||mark[ftemp.x][ftemp.y]==1)
continue;
ftemp.step=Qf.front().step+1;
time[ftemp.x][ftemp.y]=ftemp.step;
Qf.push(ftemp);
mark[ftemp.x][ftemp.y]=1;
}
Qf.pop();
}
}
int bfs_joe(int x,int y)
{
node position;
position.x=x;
position.y=y;
position.step=0;
Q.push(position);
int ans=0;
while(!Q.empty())
{
for(int i=0; i<4; i++)
{
node temp;
temp.x=Q.front().x+fix[i][0];
temp.y=Q.front().y+fix[i][1];
temp.step=Q.front().step+1;
if(temp.x<0||temp.y<0||temp.x>=r||temp.y>=c||mp[temp.x][temp.y]=='#'||mp[temp.x][temp.y]=='F'||mp[temp.x][temp.y]=='J'||temp.step>=time[temp.x][temp.y])//再加一个条件,就是如果joe走到这个点的时间大于等于火势蔓延到这点所需要的时间,就不能走。
continue;
ans=temp.step;
Q.push(temp);
mp[temp.x][temp.y]='J';//这里我是用的将地图变为'J'来标记joe走过的点
if(temp.x==r-1||temp.y==c-1||temp.x==0||temp.y==0)//这个因为是广搜,只要是达到边缘,就是最短路径。
return ans+1;
}
Q.pop();
}
return -1;
}
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
memset(mark,false,sizeof(mark));//每个样例标记数组必须要清空
memset(mp,false,sizeof(mp));//然后地图也要清空,因为前后的地图大小不一样,所以会影响,欸好像也不会影响
while(!Qf.empty())
{
Qf.pop();
}
while(!Q.empty())
{
Q.pop();
}
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
time[i][j]=INF;//刚开始将所有的时间都列为无穷大,以防有火势达不到的地方。
}
}
scanf("%d%d",&r,&c);
getchar();
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='J')
{
initialx=i;
initialy=j;
}
if(mp[i][j]=='F')
{
node fire;
fire.x=i;
fire.y=j;
fire.step=0;
Qf.push(fire);
mark[i][j]=1;
time[i][j]=0;
}
}
getchar();
}
if(initialx==0||initialy==0||initialx==r-1||initialy==c-1)//如果joe在地图边缘,就不用再搜了,直接就输出1,如果这种情况要用bfs_fire搜的话,也不是不可以,所以这句也可有可无。
{
printf("1\n");
continue;
}
bfs_fire();
int anser=bfs_joe(initialx,initialy);
if(anser!=(-1))
printf("%d\n",anser);
else
printf("IMPOSSIBLE\n");
}
return 0;
}
下面这个是我审错题敲的,只适用于只有一个joe和一个fire的情况……,英语很垃圾没错了,题目都读不懂……但是我改了好久,还是记录一下…………
#include <stdio.h>
#include<queue>
#include<string.h>
#include<utility>
using namespace std;
int r,c;
int initialx,initialy;
int firex,firey;
int summ;
int ssum;
int fsum;
char mp[1005][1005];
struct node
{
int x,y,step;
};
const int fix[4][2]=
{
1,0,
0,1,
-1,0,
0,-1
};
int bfs(int x,int y)
{
int flag=0;
queue<pair<int,int> >Qf;
Qf.push(make_pair(firex,firey));
node position;
position.x=x;
position.y=y;
position.step=0;
queue<node>Q;
Q.push(position);
int ans=0;
summ=0;
fsum=0;
while(!Q.empty())
{
if(fsum==0)
{
for(int i=0; i<4; i++)
{
int fx=Qf.front().first+fix[i][0];
int fy=Qf.front().second+fix[i][1];
if(fx<0||fy<0||fx>=r||fy>=c||mp[fx][fy]=='#'||mp[fx][fy]=='F')
continue;
mp[fx][fy]='F';
Qf.push(make_pair(fx,fy));
summ++;
fsum=1;
}
Qf.pop();
}
else
{
for(int u=0; u<ssum; u++)
{
for(int i=0; i<4; i++)
{
int fx=Qf.front().first+fix[i][0];
int fy=Qf.front().second+fix[i][1];
if(fx<0||fy<0||fx>=r||fy>=c||mp[fx][fy]=='#'||mp[fx][fy]=='F')
continue;
mp[fx][fy]='F';
Qf.push(make_pair(fx,fy));
summ++;
}
Qf.pop();
}
}
ssum=summ;
summ=0;
for(int i=0; i<4; i++)
{
node temp;
temp.x=Q.front().x+fix[i][0];
temp.y=Q.front().y+fix[i][1];
if(temp.x<0||temp.y<0||temp.x>=r||temp.y>=c||mp[temp.x][temp.y]=='#'||mp[temp.x][temp.y]=='F'||mp[temp.x][temp.y]=='J')
continue;
temp.step=Q.front().step+1;
ans=temp.step;
Q.push(temp);
mp[temp.x][temp.y]='J';
if(temp.x==r-1||temp.y==c-1||temp.x==0||temp.y==0)
return ans+1;
}
Q.pop();
}
return -1;
}
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
memset(mp,false,sizeof(mp));
scanf("%d%d",&r,&c);
getchar();
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='J')
{
initialx=i;
initialy=j;
}
if(mp[i][j]=='F')
{
firex=i;
firey=j;
}
}
getchar();
}
if(initialx==0||initialy==0||initialx==r-1||initialy==c-1)
{
printf("1\n");
continue;
}
int anser=bfs(initialx,initialy);
if(anser!=(-1))
printf("%d\n",anser);
else
printf("IMPOSSIBLE\n");
}
return 0;
}