4.逃跑
原题:https://www.acwing.com/problem/content/submission/code_detail/24498789/
注意可以原地不走,因此要用visited数组判重,队列中也不能仅仅只存坐标。
#pragma GCC optimize (2)
#include<iostream>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int N=110;
struct Node{
char op;
int t,v,x,y;
}S[N];
struct Point{
int dt,x,y;
};
int m,n,k,d;
bool st[1010][N][N];
bool at[N][N];
bool visited[1010][N][N];
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int bfs()
{
queue<Point> q;
q.push({0,0,0});
memset(visited,0,sizeof visited);
int dx[5]={0,-1,0,1,0},dy[5]={0,0,1,0,-1};
while(q.size())
{
auto t=q.front();
q.pop();
if(t.dt>d){
return -1;
}
if(t.x==m&&t.y==n){
return t.dt;
}
for(int i=0;i<5;i++)
{
int x=t.x+dx[i],y=t.y+dy[i],dt=t.dt+1;
if(x<0||x>m||y<0||y>n||at[x][y]||visited[dt][x][y]||st[dt][x][y]) continue;
visited[dt][x][y]=true;
q.push({dt,x,y});
}
}
return -1;
}
int main()
{
map<char,int> ma;
ma['N']=0,ma['E']=1,ma['S']=2,ma['W']=3;
while(cin>>m>>n>>k>>d)
{
memset(st,0,sizeof st);
memset(at,0,sizeof at);
char op;
int t,v,x,y;
for(int i=0;i<k;i++)
{
cin>>op>>t>>v>>x>>y;
S[i]={op,t,v,x,y};
at[x][y]=true;
}
for(int u=0;u<k;u++)
{
op=S[u].op,t=S[u].t,v=S[u].v,x=S[u].x,y=S[u].y;
for(int i=0;i<=d;i+=t)
{
for(int j=1;j<=d-i;j++)
{
int a=dx[ma[op]]*v*j+x,b=dy[ma[op]]*v*j+y;
if(a<0||a>m||b<0||b>n||at[a][b]) break;
st[i+j][a][b]=true;
}
}
}
int tx=bfs();
if(tx==-1) puts("Bad luck!");
else cout<<tx<<endl;
}
}
浙公网安备 33010602011771号