Georgiy1108

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

Nov 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF = (int)1e9;
  6.  
  7. struct Point
  8. {
  9.     int i, j;
  10. };
  11.  
  12. int main()
  13. {
  14.     ios::sync_with_stdio(0);
  15.     cin.tie(0);
  16.     int n, m, startI = -1, startJ = -1;
  17.     cin >> n >> m;
  18.     vector<vector<int>> p(n + 2, vector<int> (m + 2, 1)), dp(n + 2, vector<int> (m + 2, INF));
  19.     vector<pair<int, int>> c{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  20.     for(int i = 1; i <= n; i++)
  21.     {
  22.         string s;
  23.         cin >> s;
  24.         for(int j = 1; j <= m; j++)
  25.         {
  26.             if(s[j - 1] == '*')
  27.             {
  28.                 startI = i;
  29.                 startJ = j;
  30.             }
  31.             else
  32.                 p[i][j] = s[j - 1] - '0';
  33.         }
  34.     }
  35.     int k;
  36.     cin >> k;
  37.     vector<pair<int, int>> ans(k);
  38.     for(auto& i : ans)
  39.         cin >> i.first >> i.second;
  40.     dp[startI][startJ] = 0;
  41.     queue<Point> q;
  42.     q.push({startI, startJ});
  43.     while(!q.empty())
  44.     {
  45.         Point cur = q.front();
  46.         q.pop();
  47.         for(auto j : c)
  48.         {
  49.             Point next = {cur.i + j.first, cur.j + j.second};
  50.             if(p[next.i][next.j] == 0 && dp[next.i][next.j] == INF)
  51.             {
  52.                 dp[next.i][next.j] = dp[cur.i][cur.j] + 1;
  53.                 q.push({next.i, next.j});
  54.             }
  55.         }
  56.     }
  57.     int answer = 0;
  58.     for(int i = 0; i < k; i++)
  59.         if(dp[ans[i].first][ans[i].second] < dp[ans[answer].first][ans[answer].second])
  60.             answer = i;
  61.     if(dp[ans[answer].first][ans[answer].second] == INF)
  62.         cout << -1;
  63.     else
  64.         cout << answer + 1;
  65. }
Add Comment
Please, Sign In to add comment