Advertisement
welleyth

Кладоискатель

Aug 10th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const unsigned long long MOD=(long long)1e9+7;
  6. const long long INF=(2*(long long)1e9);
  7. long long cycle_start,cycle_end;
  8.  
  9. vector<pair<int,int> > cycle;
  10.  
  11. long long p[(long long)1e5+1]={};
  12.  
  13. int ans=0;
  14. int n,m;
  15.  
  16. char g[101][101];
  17. bool used[101][101]={};
  18. pair<int,int> parent[101][101];
  19. int d[101][101];
  20.  
  21. pair<int,int> parentt(pair<int,int> pp)
  22. {
  23.     return parent[pp.first][pp.second];
  24. }
  25.  
  26. int main()
  27. {
  28.     ios::sync_with_stdio(false);
  29.     cin.tie(0);
  30.     cout.tie(0);
  31.     //freopen("knight1.in","r",stdin);
  32.     //freopen("knight1.out","w",stdout);
  33.     cin>>n>>m;
  34.     int sx,sy;
  35.     int fx,fy;
  36.     int x,y,len;
  37.     for(int i=0;i<n;i++)
  38.     {
  39.         for(int j=0;j<m;j++)
  40.         {
  41.             cin>>g[i][j];
  42.             if(g[i][j]=='*')
  43.             {
  44.                 fx=i;
  45.                 fy=j;
  46.             }
  47.             parent[i][j]=make_pair(i,j);
  48.             d[i][j]=(int)1e9;
  49.         }
  50.     }
  51.     int qu,qq;
  52.     cin>>qu;
  53.     qq=qu;
  54.     int mn=(int)1e9,minid=-1;
  55.     while(qu--)
  56.     {
  57.         cin>>sx>>sy;
  58.         sx--;
  59.         sy--;
  60.         for(int i=0;i<n;i++)
  61.         {
  62.             for(int j=0;j<m;j++)
  63.             {
  64.                 d[i][j]=(int)1e9;
  65.                 used[i][j]=0;
  66.             }
  67.         }
  68.         d[sx][sy]=0;
  69.         queue<pair<int,pair<int,int> > > q;
  70.         q.push(make_pair(0,make_pair(sx,sy)));
  71.         while(!q.empty())
  72.         {
  73.             x=q.front().second.first;
  74.             y=q.front().second.second;
  75.             len=q.front().first;
  76.             q.pop();
  77.             used[x][y]=1;
  78.             if(x-1>=0)
  79.             {
  80.                 if(g[x-1][y]!='1' && d[x-1][y]>len+1)
  81.                 {
  82.                     d[x-1][y]=len+1;
  83.                     parent[x-1][y]=make_pair(x,y);
  84.                     q.push(make_pair(len+1,make_pair(x-1,y)));
  85.                 }
  86.             }
  87.             if(x+1<n)
  88.             {
  89.                 if(g[x+1][y]!='1' && d[x+1][y]>len+1)
  90.                 {
  91.                     d[x+1][y]=len+1;
  92.                     parent[x+1][y]=make_pair(x,y);
  93.                     q.push(make_pair(len+1,make_pair(x+1,y)));
  94.                 }
  95.             }
  96.             if(y-1>=0)
  97.             {
  98.                 if(g[x][y-1]!='1' && d[x][y-1]>len+1)
  99.                 {
  100.                     d[x][y-1]=len+1;
  101.                     parent[x][y-1]=make_pair(x,y);
  102.                     q.push(make_pair(len+1,make_pair(x,y-1)));
  103.                 }
  104.             }
  105.             if(y+1<n)
  106.             {
  107.                 if(g[x][y+1]!='1' && d[x][y+1]>len+1)
  108.                 {
  109.                     d[x][y+1]=len+1;
  110.                     parent[x][y+1]=make_pair(x,y);
  111.                     q.push(make_pair(len+1,make_pair(x,y+1)));
  112.                 }
  113.             }
  114.         }
  115.         if(d[fx][fy]<mn && d[fx][fy]!=(int)1e9)
  116.         {
  117.             mn=d[fx][fy];
  118.             minid=qq-qu;
  119.         }
  120.     }
  121.     cout<<minid;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement