H. Safe Path
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output
You play a new RPG. The world map in it is represented by a grid of n × m cells. Any playing character staying in some cell can move from this cell in four directions — to the cells to the left, right, forward and back, but not leaving the world map.
Monsters live in some cells. If at some moment of time you are in the cell which is reachable by some monster in d steps or less, he immediately runs to you and kills you.
You have to get alive from one cell of game field to another. Determine whether it is possible and if yes, find the minimal number of steps required to do it.
Input
The first line contains three non-negative integers n, m and d (2 ≤ n·m ≤ 200000, 0 ≤ d ≤ 200000) — the size of the map and the maximal distance at which monsters are dangerous.
Each of the next n lines contains m characters. These characters can be equal to «.», «M», «S» and «F», which denote empty cell, cell with monster, start cell and finish cell, correspondingly. Start and finish cells are empty and are presented in the input exactly once.
Output
If it is possible to get alive from start cell to finish cell, output minimal number of steps required to do it. Otherwise, output «-1».
Examples
input
Copy
5 7 1
S.M...M
.......
.......
M...M..
......F
output
12
input
Copy
7 6 2
S.....
...M..
......
.....M
......
M.....
.....F
output
11
input
Copy
7 6 2
S.....
...M..
......
......
.....M
M.....
.....F
output
-1
input
Copy
4 4 2
M...
.S..
....
...F
output
-1
Note
Please note that monsters can run and kill you on start cell and on finish cell as well.
限制 2second
要不是数据那么大是可以用二维数组的,现在就需要把二维图转换成一维的,同时事先处理不能走的地方,然后宽搜就行的
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int ,int >P;
const int N = 200020;
char mp[N];
int dis[N];
int n,m,a,sx,sy,gx,gy;
int nx[4]={-1,0,1,0};
int ny[4]={0,-1,0,1};
int check(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)return 1;
return 0;
}
queue<P>quee;
void bfs()
{
while(quee.size())
{
P d = quee.front();quee.pop();
if(dis[d.first*m+d.second]==0)continue;
for(int i=0;i<4;i++)
{
int dx=d.first+nx[i],dy=d.second+ny[i];
if(check(dx,dy))
{
if(dis[dx*m+dy]<dis[d.first*m+d.second]-1)
{
dis[dx*m+dy]=dis[d.first*m+d.second]-1;
quee.push(P(dx,dy));
}
}
}
}
}
void bfs2(int x,int y)
{
queue<P>que;
que.push(P(x,y));
dis[x*m+y]=0;
while(que.size())
{
P d=que.front();que.pop();
for(int i=0;i<4;i++)
{
int dx=d.first+nx[i],dy=d.second+ny[i];
if(check(dx,dy))
{
if(dis[dx*m+dy]==-1)
{
dis[dx*m+dy]=dis[d.first*m+d.second]+1;
que.push(P(dx,dy));
}
}
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&a))
{
getchar();
memset(dis,-1,sizeof(dis));
for(int i=0;i<n;i++)
{
scanf("%s",&mp[i*m]);
}
sx = -1,sy = -1,gx=-1,gy = -1;
while(quee.size())quee.pop();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i*m+j]=='.')continue;
else if(mp[i*m+j]=='M')
{
quee.push(P(i,j));
dis[i*m+j] = a;
}
else if(mp[i*m+j]=='S')
{
sx=i,sy=j;
}
else if(mp[i*m+j]=='F')
{
gx=i,gy=j;
}
}
}
bfs();
if(sx==-1||sy==-1||gx==-1||gy==-1||dis[sx*m+sy]!=-1||dis[gx*m+gy]!=-1){puts("-1");continue;}
bfs2(sx,sy);
if(dis[gx*m+gy]==-1)puts("-1");
else printf("%d\n",dis[gx*m+gy]);
}
return 0;
}