Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int INF = (int)1e9;
- struct Point
- {
- int i, j;
- };
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int n, m, startI = -1, startJ = -1;
- cin >> n >> m;
- vector<vector<int>> p(n + 2, vector<int> (m + 2, 1)), dp(n + 2, vector<int> (m + 2, INF));
- vector<pair<int, int>> c{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
- for(int i = 1; i <= n; i++)
- {
- string s;
- cin >> s;
- for(int j = 1; j <= m; j++)
- {
- if(s[j - 1] == '*')
- {
- startI = i;
- startJ = j;
- }
- else
- p[i][j] = s[j - 1] - '0';
- }
- }
- int k;
- cin >> k;
- vector<pair<int, int>> ans(k);
- for(auto& i : ans)
- cin >> i.first >> i.second;
- dp[startI][startJ] = 0;
- queue<Point> q;
- q.push({startI, startJ});
- while(!q.empty())
- {
- Point cur = q.front();
- q.pop();
- for(auto j : c)
- {
- Point next = {cur.i + j.first, cur.j + j.second};
- if(p[next.i][next.j] == 0 && dp[next.i][next.j] == INF)
- {
- dp[next.i][next.j] = dp[cur.i][cur.j] + 1;
- q.push({next.i, next.j});
- }
- }
- }
- int answer = 0;
- for(int i = 0; i < k; i++)
- if(dp[ans[i].first][ans[i].second] < dp[ans[answer].first][ans[answer].second])
- answer = i;
- if(dp[ans[answer].first][ans[answer].second] == INF)
- cout << -1;
- else
- cout << answer + 1;
- }
Add Comment
Please, Sign In to add comment