1 solutions
-
1
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long using namespace std;
struct node{ int x,y; };
int dis[4][2]={0,1,1,0,0,-1,-1,0}; int n,m; char c; int s[505][505]; node hunt[300000]; int hl; int sx,sy,ex,ey; bool vis[505][505]; queueq;
bool check(int check_x){ while(!q.empty())q.pop();//因为可能队列还没空就return 1,所以每次检验要清空队列 memset(vis,0,sizeof(vis)); q.push({sx,sy}); vis[sx][sy]=1; while(!q.empty()){ int nx=q.front().x,ny=q.front().y; q.pop(); if(nxex&&nyey)return 1; for(int i=0;i<4;i++){ int tx=nx+dis[i][0],ty=ny+dis[i][1]; if(tx>0&&ty>0&&tx<=n&&ty<=m&&!vis[tx][ty]&&s[tx][ty]>=check_x){ vis[tx][ty]=1; q.push({tx,ty}); } } } return 0; }
int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>c; s[i][j]=1e9; if(c=='+')q.push({i,j}),s[i][j]=0; if(c=='V')sx=i,sy=j; if(c=='J')ex=i,ey=j; } } while(!q.empty()){//预处理 int nx=q.front().x,ny=q.front().y; q.pop(); for(int i=0;i<4;i++){ int tx=nx+dis[i][0],ty=ny+dis[i][1]; if(tx>0&&ty>0&&tx<=n&&ty<=m&&s[tx][ty]>s[nx][ny]+1){ s[tx][ty]=s[nx][ny]+1; q.push({tx,ty}); } } } int left=0,righ=min(s[sx][sy],s[ex][ey]),mid,ans; while(left<=righ){ mid=left+righ>>1; if(check(mid)){ ans=mid; left=mid+1; } else righ=mid-1; } cout<<ans<<'\n'; return 0; }
- 1
Information
- ID
- 7057
- Time
- 1000ms
- Memory
- 32MiB
- Difficulty
- 4
- Tags
- # Submissions
- 77
- Accepted
- 23
- Uploaded By