Advertisement
Guest User

Untitled

a guest
May 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. char board[305][305];
  4. int X[8] = {-1,0,1,1,1,0,-1,-1};
  5. int Y[8] = {-1,-1,-1,0,1,1,1,0};
  6. int odl[305][305][8];
  7. int main(){
  8.     ios_base::sync_with_stdio(0);
  9.     cin.tie(0);
  10.     int a,b;
  11.     cin >> a >> b;
  12.  
  13.     for(int i=1;i<=a;i++){
  14.         for(int j=1;j<=b;j++)cin >> board[i][j];
  15.     }
  16.     int startx,starty;
  17.     for(int i=1;i<=a;i++){
  18.         for(int j=1;j<=b;j++){
  19.             if(board[i][j]=='K'){
  20.                 startx=i;
  21.                 starty=j;
  22.             }
  23.         }
  24.     }
  25.     queue<tuple<int,int,int> >q;
  26.     for(int i=0;i<8;i++){
  27.         tuple<int,int,int> t(startx,starty,i);
  28.         odl[startx][starty][i]=1;
  29.         q.push(t);}
  30.  
  31.  
  32.     while(!q.empty()){
  33.         int a,b,c;
  34.         //tie(a,b,c) = q.front();
  35.         a = get<0>(q.front());
  36.         b = get<1>(q.front());
  37.         c = get<2>(q.front());
  38.         q.pop();
  39.         for(int i=-1;i<=1;i++){
  40.             int x = X[(c+i+8)%8]+a;
  41.             int y = Y[(c+i+8)%8]+b;
  42.             if((board[x][y]=='.'||board[x][y]=='S')&& odl[x][y][(c+i+8)%8]==0){
  43.                 odl[x][y][(c+8+i)%8]=odl[a][b][c]+1;
  44.                 auto nowy = make_tuple(x,y,((c+8+i)%8));
  45.                 q.push(nowy);}
  46.         }
  47.    
  48.         }
  49.  
  50.     int wynik = 99999999;
  51.     for(int i=1;i<=a;i++){
  52.         for(int j=1;j<=b;j++){
  53.             if (board[i][j]=='S'){
  54.                 for(int g = 0;g<8;g++){if(odl[i][j][g])wynik = min(wynik,odl[i][j][g]);}
  55.                 break;
  56.                 }
  57.         }
  58.     }
  59.     if(wynik == 99999999)cout <<"NIE";
  60.     else cout << wynik-1;
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement