Advertisement
MirzaMdAzwad

Abesh.cpp

Aug 11th, 2021
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<iomanip>
  5. using namespace std;
  6.  
  7. #define fastio  ios_base::sync_with_stdio(0);cin.tie(NULL);
  8. int main() {///main function starts here
  9.     fastio;
  10.     int n,x;
  11.     while(!(cin>>n).eof()){
  12.         int a,b;
  13.         int steps,min_steps;
  14.         bool found=false,bfound=false;
  15.         for(int i=0;i<n;i++){
  16.             for(int j=0;j<n;j++){
  17.                 scanf("%1d",&x);
  18.                 if(x==1 && !found){
  19.                     a=i;
  20.                     b=j;
  21.                     found=true;
  22.                 }
  23.                 else if(x==3 && !bfound){
  24.                     bfound=true;
  25.                     min_steps=abs(a-i)+abs(b-j);
  26.                     break;
  27.                 }
  28.                 else if(x==3 && bfound){
  29.                     steps=abs(a-i)+abs(b-j);
  30.                     if(steps<min_steps){
  31.                         min_steps=steps;
  32.                         break;
  33.                     }
  34.                 }
  35.                 if(j>=min_steps && bfound)break;
  36.             }
  37.             if(i>=min_steps)break;
  38.         }
  39.         cout<<min_steps<<endl;
  40.     }
  41.     return 0;
  42. }///main function ends here
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement