Advertisement
Guest User

das

a guest
Apr 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,h,w,wynik=1000000000,a,b;
  4. bool tab[1005][1005];
  5. int t[1005][1005];
  6. int wysokosc[8];
  7. int szerokosc[8];
  8. bool q=0;
  9. pair <int,int> meta;
  10. pair <int,int> start;
  11. string s[1005];
  12. queue <pair< int,int> > v;
  13. bool prawda(int wiersz,int kolumna)
  14. {
  15.     return wiersz>=0 and wiersz<h and kolumna>=0 and kolumna<w;
  16. }
  17. void bfs(int x,int y)
  18. {
  19. tab[x][y]=1;
  20. v.push({x,y});
  21. t[x][y]=0;
  22. while(!v.empty())
  23. {
  24. int x=v.front().first;
  25. int y=v.front().second;
  26. v.pop();
  27. for(int i=0;8>i;i++)
  28. {
  29. //cout<<x+wysokosc[i]<<" "<<y+szerokosc[i]<<endl;
  30.     if(prawda(x+wysokosc[i],y+szerokosc[i])==1)
  31.     {
  32.  
  33. if(s[x+wysokosc[i]][y+szerokosc[i]]=='K')
  34. {
  35. wynik=t[x][y]+1;
  36. return;
  37. }
  38.  
  39.  
  40.     if(s[x+wysokosc[i]][y+szerokosc[i]]=='.')
  41.     {
  42.         if(tab[x+wysokosc[i]][y+szerokosc[i]]==0)
  43.         {
  44.  
  45.             tab[x+wysokosc[i]][y+szerokosc[i]]=1;
  46.             t[x+wysokosc[i]][y+szerokosc[i]]=t[x][y]+1;
  47.             v.push({x+wysokosc[i],y+szerokosc[i]});
  48.         }
  49.     }
  50.     }
  51. }
  52. }
  53. q=1;
  54. return;
  55. }
  56.     int main() {
  57. cin>>h;
  58. cin>>w;
  59. cin>>a;
  60. cin>>b;
  61. wysokosc[0]=a;
  62. szerokosc[0]=b;
  63. wysokosc[1]=-a;
  64. szerokosc[1]=b;
  65. wysokosc[2]=a;
  66. szerokosc[2]=-b;
  67. wysokosc[3]=-a;
  68. szerokosc[3]=-b;
  69. wysokosc[4]=b;
  70. szerokosc[4]=a;
  71. wysokosc[5]=-b;
  72. szerokosc[5]=a;
  73. wysokosc[6]=b;
  74. szerokosc[6]=-a;
  75. wysokosc[7]=-b;
  76. szerokosc[7]=-a;
  77. for(int i=0;h!=i;i++)
  78. {
  79.     cin>>s[i];
  80.     for(int j=0;w>j;j++)
  81.     {if(s[i][j]=='S')
  82.     {
  83.        start.first=i;
  84.        start.second=j;
  85.     }
  86.     }
  87. }
  88. bfs(start.first,start.second);
  89. if(q==0)
  90.     cout<<wynik;
  91. else
  92.     cout<<"NIE"<<endl;
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement