Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ff first
- #define ss second
- #define mp make_pair
- using namespace std;
- typedef long long ll;
- char mat[1005][1005];
- int dist[1005][1005];
- int n,m;
- int row[4] = {0,1,0,-1};
- int col[4] = {1,0,-1,0};
- char bfs(pair<int,int> s) {
- for(int i = 0; i < n; i++)
- for(int j = 0; j < m; j++)
- dist[i][j] = -1;
- queue<pair<int,int>> Q;
- Q.push(s);
- dist[s.ff][s.ss] = 0;
- while(!Q.empty()) {
- auto x = Q.front();
- Q.pop();
- if(mat[x.ff][x.ss] >= 'a' and mat[x.ff][x.ss])
- return dist[x.ff][x.ss];
- for(int k = 0; k < 4; k++) {
- int i = row[k] + x.ff;
- int j = col[k] + x.ss;
- if(i < 0 or i >= n or j < 0 or j >= m or mat[i][j] == '#' or dist[i][j] != -1)
- continue;
- dist[i][j] = dist[x.ff][x.ss]+1;
- Q.push(mp(i,j));
- }
- }
- return -1;
- }
- int main(){
- pair<int,int> st;
- scanf("%d%d", &n, &m);
- for(int i = 0; i < n; i++) {
- scanf(" %s", mat[i]);
- for(int j = 0; j < m; j++) {
- if(mat[i][j] == '*')
- st = mp(i,j);
- }
- }
- printf("%d\n", bfs(st));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement