fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. class Path
  7. {
  8. int x; int y; int z;
  9. //z is the minimum distance required to reach grid element at index(x,y)
  10. Path(int x, int y,int z)
  11. {
  12. this.x=x;
  13. this.y=y;
  14. this.z=z;
  15. }
  16. }
  17. class Solution {
  18. public int minPathSum(int[][] grid) {
  19. int row=grid.length;
  20. int col=grid[0].length;
  21. boolean[][] visited=new boolean[row][col];
  22. int []dx={1,0};
  23. int []dy={0,1};
  24. PriorityQueue <Path> queue=new PriorityQueue<Path>((e,f)->(e.z-f.z));
  25. queue.offer(new Path(0,0,grid[0][0]));
  26. while(!queue.isEmpty())
  27. {
  28. Path temp=queue.poll();
  29.  
  30. if(temp.x==row-1&&temp.y==col-1)
  31. return temp.z;
  32. for(int i=0;i<2;i++)
  33. {
  34. int a=temp.x+dx[i];
  35. int b=temp.y+dy[i];
  36. if(a<0||a>=row||b<0||b>=col||visited[a][b]==true)
  37. continue;
  38. visited[a][b]=true;
  39. queue.offer(new Path(a,b,temp.z+grid[a][b]));
  40. }
  41. }
  42. return -1;
  43. }
  44. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
spoj: The program compiled successfully, but main class was not found.
      Main class should contain method: public static void main (String[] args).
stdout
Standard output is empty