Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const unsigned long long MOD=(long long)1e9+7;
- const long long INF=(2*(long long)1e9);
- long long cycle_start,cycle_end;
- vector<pair<int,int> > cycle;
- long long p[(long long)1e5+1]={};
- int ans=0;
- int n,m;
- char g[101][101];
- bool used[101][101]={};
- pair<int,int> parent[101][101];
- int d[101][101];
- pair<int,int> parentt(pair<int,int> pp)
- {
- return parent[pp.first][pp.second];
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- //freopen("knight1.in","r",stdin);
- //freopen("knight1.out","w",stdout);
- cin>>n>>m;
- int sx,sy;
- int fx,fy;
- int x,y,len;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- cin>>g[i][j];
- if(g[i][j]=='*')
- {
- fx=i;
- fy=j;
- }
- parent[i][j]=make_pair(i,j);
- d[i][j]=(int)1e9;
- }
- }
- int qu,qq;
- cin>>qu;
- qq=qu;
- int mn=(int)1e9,minid=-1;
- while(qu--)
- {
- cin>>sx>>sy;
- sx--;
- sy--;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- d[i][j]=(int)1e9;
- used[i][j]=0;
- }
- }
- d[sx][sy]=0;
- queue<pair<int,pair<int,int> > > q;
- q.push(make_pair(0,make_pair(sx,sy)));
- while(!q.empty())
- {
- x=q.front().second.first;
- y=q.front().second.second;
- len=q.front().first;
- q.pop();
- used[x][y]=1;
- if(x-1>=0)
- {
- if(g[x-1][y]!='1' && d[x-1][y]>len+1)
- {
- d[x-1][y]=len+1;
- parent[x-1][y]=make_pair(x,y);
- q.push(make_pair(len+1,make_pair(x-1,y)));
- }
- }
- if(x+1<n)
- {
- if(g[x+1][y]!='1' && d[x+1][y]>len+1)
- {
- d[x+1][y]=len+1;
- parent[x+1][y]=make_pair(x,y);
- q.push(make_pair(len+1,make_pair(x+1,y)));
- }
- }
- if(y-1>=0)
- {
- if(g[x][y-1]!='1' && d[x][y-1]>len+1)
- {
- d[x][y-1]=len+1;
- parent[x][y-1]=make_pair(x,y);
- q.push(make_pair(len+1,make_pair(x,y-1)));
- }
- }
- if(y+1<n)
- {
- if(g[x][y+1]!='1' && d[x][y+1]>len+1)
- {
- d[x][y+1]=len+1;
- parent[x][y+1]=make_pair(x,y);
- q.push(make_pair(len+1,make_pair(x,y+1)));
- }
- }
- }
- if(d[fx][fy]<mn && d[fx][fy]!=(int)1e9)
- {
- mn=d[fx][fy];
- minid=qq-qu;
- }
- }
- cout<<minid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement