Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n,h,w,wynik=1000000000,a,b;
- bool tab[1005][1005];
- int t[1005][1005];
- int wysokosc[8];
- int szerokosc[8];
- bool q=0;
- pair <int,int> meta;
- pair <int,int> start;
- string s[1005];
- queue <pair< int,int> > v;
- bool prawda(int wiersz,int kolumna)
- {
- return wiersz>=0 and wiersz<h and kolumna>=0 and kolumna<w;
- }
- void bfs(int x,int y)
- {
- tab[x][y]=1;
- v.push({x,y});
- t[x][y]=0;
- while(!v.empty())
- {
- int x=v.front().first;
- int y=v.front().second;
- v.pop();
- for(int i=0;8>i;i++)
- {
- //cout<<x+wysokosc[i]<<" "<<y+szerokosc[i]<<endl;
- if(prawda(x+wysokosc[i],y+szerokosc[i])==1)
- {
- if(s[x+wysokosc[i]][y+szerokosc[i]]=='K')
- {
- wynik=t[x][y]+1;
- return;
- }
- if(s[x+wysokosc[i]][y+szerokosc[i]]=='.')
- {
- if(tab[x+wysokosc[i]][y+szerokosc[i]]==0)
- {
- tab[x+wysokosc[i]][y+szerokosc[i]]=1;
- t[x+wysokosc[i]][y+szerokosc[i]]=t[x][y]+1;
- v.push({x+wysokosc[i],y+szerokosc[i]});
- }
- }
- }
- }
- }
- q=1;
- return;
- }
- int main() {
- cin>>h;
- cin>>w;
- cin>>a;
- cin>>b;
- wysokosc[0]=a;
- szerokosc[0]=b;
- wysokosc[1]=-a;
- szerokosc[1]=b;
- wysokosc[2]=a;
- szerokosc[2]=-b;
- wysokosc[3]=-a;
- szerokosc[3]=-b;
- wysokosc[4]=b;
- szerokosc[4]=a;
- wysokosc[5]=-b;
- szerokosc[5]=a;
- wysokosc[6]=b;
- szerokosc[6]=-a;
- wysokosc[7]=-b;
- szerokosc[7]=-a;
- for(int i=0;h!=i;i++)
- {
- cin>>s[i];
- for(int j=0;w>j;j++)
- {if(s[i][j]=='S')
- {
- start.first=i;
- start.second=j;
- }
- }
- }
- bfs(start.first,start.second);
- if(q==0)
- cout<<wynik;
- else
- cout<<"NIE"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement