Guest User

Rook

a guest
Sep 23rd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. public class Solution {
  2.    
  3.     static int x_dir[];
  4.     static int y_dir[];
  5.    
  6.     static class Pair
  7.     {
  8.         int x;
  9.         int y;
  10.         int dir;
  11.         int step;
  12.        
  13.         public Pair(int x,int y,int dir,int step)
  14.         {
  15.             this.x=x;
  16.             this.y=y;
  17.             this.dir=dir;
  18.             this.step=step;
  19.         }
  20.     }
  21.    
  22.     public static boolean check(int a[][],int x,int y,int n,int m)
  23.     {
  24.         if(x<0 || y<0 || x>=n  || y>=m) return false;
  25.         if(a[x][y]==1) return false;
  26.         return true;
  27.     }
  28.    
  29.     public int solve(int A, int B, int C, int D, ArrayList<String> E) {
  30.    
  31.         x_dir=new int[]{0,0,1,-1};
  32.         y_dir=new int[]{1,-1,0,0};
  33.        
  34.         A--; B--; C--; D--;
  35.        
  36.         int a[][]=new int[E.size()][E.get(0).length()];
  37.         int dist[][]=new int[E.size()][E.get(0).length()];
  38.        
  39.         for(int i=0;i<E.size();i++)
  40.         {
  41.             for(int j=0;j<E.get(i).length();j++)
  42.             {
  43.               a[i][j]=Character.getNumericValue(E.get(i).charAt(j));
  44.               dist[i][j]=Integer.MAX_VALUE;
  45.             }
  46.         }
  47.        
  48.         LinkedList<Pair> qu=new LinkedList<Pair>();
  49.         qu.add(new Pair(A,B,-1,1));
  50.        
  51.         while(!qu.isEmpty())
  52.         {
  53.             Pair p=qu.pollFirst();
  54.             int cur_x=p.x;
  55.             int cur_y=p.y;
  56.             int cur_dir=p.dir;
  57.             int cur_step=p.step;
  58.            
  59.             if(dist[cur_x][cur_y]<cur_step) continue;
  60.    
  61.             dist[cur_x][cur_y]=cur_step;
  62.            
  63.             for(int k=0;k<4;k++)
  64.             {
  65.                 int new_x=cur_x+x_dir[k];
  66.                 int new_y=cur_y+y_dir[k];
  67.                
  68.                 if(check(a,new_x,new_y,a.length,a[0].length))
  69.                 {
  70.                     if(cur_dir==-1)
  71.                     {
  72.                         qu.add(new Pair(new_x,new_y,k,cur_step));
  73.                     }
  74.                     else
  75.                     {
  76.                         if(k==cur_dir)
  77.                            qu.add(new Pair(new_x,new_y,k,cur_step));
  78.                         else
  79.                            qu.add(new Pair(new_x,new_y,k,cur_step+1));
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.        
  85.         if(dist[C][D]!=Integer.MAX_VALUE) return dist[C][D];
  86.        
  87.         return -1;
  88.        
  89.     }
  90. }
Add Comment
Please, Sign In to add comment