Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- char board[305][305];
- int X[8] = {-1,0,1,1,1,0,-1,-1};
- int Y[8] = {-1,-1,-1,0,1,1,1,0};
- int odl[305][305][8];
- int main(){
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int a,b;
- cin >> a >> b;
- for(int i=1;i<=a;i++){
- for(int j=1;j<=b;j++)cin >> board[i][j];
- }
- int startx,starty;
- for(int i=1;i<=a;i++){
- for(int j=1;j<=b;j++){
- if(board[i][j]=='K'){
- startx=i;
- starty=j;
- }
- }
- }
- queue<tuple<int,int,int> >q;
- for(int i=0;i<8;i++){
- tuple<int,int,int> t(startx,starty,i);
- odl[startx][starty][i]=1;
- q.push(t);}
- while(!q.empty()){
- int a,b,c;
- //tie(a,b,c) = q.front();
- a = get<0>(q.front());
- b = get<1>(q.front());
- c = get<2>(q.front());
- q.pop();
- for(int i=-1;i<=1;i++){
- int x = X[(c+i+8)%8]+a;
- int y = Y[(c+i+8)%8]+b;
- if((board[x][y]=='.'||board[x][y]=='S')&& odl[x][y][(c+i+8)%8]==0){
- odl[x][y][(c+8+i)%8]=odl[a][b][c]+1;
- auto nowy = make_tuple(x,y,((c+8+i)%8));
- q.push(nowy);}
- }
- }
- int wynik = 99999999;
- for(int i=1;i<=a;i++){
- for(int j=1;j<=b;j++){
- if (board[i][j]=='S'){
- for(int g = 0;g<8;g++){if(odl[i][j][g])wynik = min(wynik,odl[i][j][g]);}
- break;
- }
- }
- }
- if(wynik == 99999999)cout <<"NIE";
- else cout << wynik-1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement