Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- #include<cstdlib>
- #include<iostream>
- #include<vector>
- #include<algorithm>
- #include<string.h>
- #include<string>
- #include<functional>
- #include<queue>
- #include<stack>
- #include<cstring>
- #include<cmath>
- #include<climits>
- #include<set>
- #include<map>
- #include<deque>
- #include<bitset>
- using namespace std;
- int R,C;
- bool visit[55][55];
- int temp_x,temp_y;
- char mp[55][55];
- int x_move[] = {0,0,-1,1};
- int y_move[] = {-1,1,0,0};
- int cnt[55][55];
- int x,y;
- queue<pair<int,int> > q;
- void bfs(){
- visit[temp_x][temp_y] = true;
- q.push({temp_x,temp_y});
- while(!q.empty()){
- int rx = q.front().first;
- int ry = q.front().second;
- q.pop();
- // caution 3
- if(mp[rx][ry] == '*'){
- for(int i=0;i<4;i++){
- int nx = rx+x_move[i];
- int ny = ry+y_move[i];
- if(nx >0 && nx <=R && ny >0 && ny<=C){
- if(mp[nx][ny]!='D'&&mp[nx][ny]!='X'&&mp[nx][ny]!='S'&&!visit[nx][ny]){
- visit[nx][ny]=true;
- q.push({nx,ny});
- // caution 4
- mp[nx][ny]='*';
- }
- }
- }
- }
- else if(mp[rx][ry]=='.'||mp[rx][ry]=='S'){
- for(int i=0;i<4;i++){
- int nx = rx+x_move[i];
- int ny = ry+y_move[i];
- if(nx >0 && nx <=R && ny >0 && ny<=C){
- if(mp[nx][ny]!='X'&&mp[nx][ny]!='*'&&!visit[nx][ny]){
- q.push({nx,ny});
- visit[nx][ny]=true;
- cnt[nx][ny] = cnt[rx][ry]+1;
- if(mp[nx][ny]=='D'){
- x = nx; y=ny;
- return;
- }
- }
- }
- }
- }
- }
- }
- int main(){
- cin >> R >> C;
- getchar();
- for(int i=1;i<=R;i++){
- for(int j=1;j<=C;j++){
- mp[i][j] = getchar();
- if(mp[i][j] == 'S'){
- temp_x = i; temp_y = j;
- }
- else if(mp[i][j] =='*'){
- q.push({i,j});
- }
- }
- getchar();
- }
- bfs();
- if(!cnt[x][y]) printf("KAKTUS");
- else printf("%d",cnt[x][y]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement