Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int main()
- {
- int n;
- int m;
- cin>>n>>m;
- int n1;
- int m1;
- int k;
- int k1;
- queue<int>q;
- char matrica[n][m];
- for(int x=0; x<n; x++){
- for(int x1=0; x1<m; x1++){
- cin>>matrica[x][x1];
- }
- }
- for(int y=0; y<n; y++){
- for(int y1=0; y1<m; y1++){
- if(matrica[y][y1]=='P'){
- n1=y;
- m1=y1;
- }
- if(matrica[y][y1]=='K'){
- k=y;
- k1=y1;
- }
- }
- }
- bool matrica1[n][m];
- bool matrica2[n][m];
- bool matrica3[n][m];
- for(int l=0; l<n; l++){
- for(int l1=0; l1<m; l1++){
- matrica1[l][l1]=false;
- matrica2[l][l1]=false;
- matrica3[l][l1]=false;
- }
- }
- q.push(n1);
- q.push(m1);
- q.push(0);
- q.push(1);
- int di1[]={-1,1,0,0,};
- int dj1[]={0,0,-1,1};
- int di2[]={-2,2,0,0};
- int dj2[]={0,0,-2,2};
- int di3[]={-3,3,0,0};
- int dj3[]={0,0,-3,3};
- int ti;
- int tj;
- while(!q.empty()){
- int ci=q.front();
- q.pop();
- int cj=q.front();
- q.pop();
- int cekori=q.front();
- q.pop();
- int brcekori=q.front();
- q.pop();
- if((ci==k)and(cj==k1)){
- cout<<cekori;
- break;
- }
- if(brcekori==1){
- for(int i=0; i<4; i++){
- ti=ci+di1[i];
- tj=cj+dj1[i];
- if((ti>=0)and(ti<n)){
- if((tj>=0)and(tj<m)){
- if(matrica[ti][tj]!='#'){
- if(matrica1[ti][tj]==false){
- q.push(ti);
- q.push(tj);
- q.push(cekori+1);
- q.push(brcekori=2);
- matrica1[ti][tj]=true;
- }
- }
- }
- }
- }
- }
- else if(brcekori==2){
- for(int i=0; i<4; i++){
- ti=ci+di1[i];
- tj=cj+dj1[i];
- if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')){
- ti=ci+di2[i];
- tj=cj+dj2[i];
- if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')and(matrica2[ti][tj]==false)){
- q.push(ti);
- q.push(tj);
- q.push(cekori+1);
- q.push(brcekori=3);
- matrica2[ti][tj]=true;
- }
- }
- }
- }
- else if(brcekori==3){
- for(int i=0; i<4; i++){
- ti=ci+di1[i];
- tj=cj+dj1[i];
- if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')){
- ti=ci+di2[i];
- tj=cj+dj2[i];
- if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')){
- ti=ci+di3[i];
- tj=cj+dj3[i];
- if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')and(matrica3[ti][tj]==false)){
- q.push(ti);
- q.push(tj);
- q.push(cekori+1);
- q.push(brcekori=1);
- matrica3[ti][tj]=true;
- }
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement