Guest User

Untitled

a guest
Dec 9th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Scanner;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.StringTokenizer;
  7.  
  8. public class LA_MAGRID {
  9. public static void main(String[] args) throws IOException {
  10. Input read = new Input();
  11. int t = read.nextInt();
  12. while (t-- > 0) {
  13. int n = read.nextInt(), m = read.nextInt();
  14. int[][] map = new int[n][m];
  15. for (int i = 0; i < n; i++) {
  16. for (int j = 0; j < m; j++) {
  17. map[i][j] = read.nextInt();
  18. }
  19. }
  20. LinkedList<int[]> q = new LinkedList<int[]>();
  21. int[] curr = { 0, 0, 1, 1 };
  22. q.addLast(curr);
  23. int[][] visitedSrc = new int[n][m];
  24. int[][] visitedCurr = new int[n][m];
  25. int min = Integer.MAX_VALUE;
  26. while (!q.isEmpty()) {
  27. curr = q.removeFirst();
  28. if (curr[0] == n - 1 && curr[1] == m - 1) {
  29. min = min < curr[2] ? min : curr[2];
  30. }
  31. if (curr[0] < n - 1) {
  32. int[] lo = new int[4];
  33. lo[0] = curr[0] + 1;
  34. lo[1] = curr[1];
  35. if (map[lo[0]][lo[1]] + curr[3] <= 0) {
  36. lo[2] = curr[2] + 1 - (map[lo[0]][lo[1]] + curr[3]);
  37. lo[3] = 1;
  38. } else {
  39. lo[2] = curr[2];
  40. lo[3] = curr[3] + map[lo[0]][lo[1]];
  41. }
  42. if ((visitedCurr[lo[0]][lo[1]] <= lo[3] || visitedSrc[lo[0]][lo[1]] >= lo[2]))
  43. q.addLast(lo);
  44. }
  45. if (curr[1] < m - 1) {
  46. int[] lo = new int[4];
  47. lo[0] = curr[0];
  48. lo[1] = curr[1] + 1;
  49. if (map[lo[0]][lo[1]] + curr[3] <= 0) {
  50. lo[2] = curr[2] + 1 - (map[lo[0]][lo[1]] + curr[3]);
  51. lo[3] = 1;
  52. } else {
  53. lo[2] = curr[2];
  54. lo[3] = curr[3] + map[lo[0]][lo[1]];
  55. }
  56. if ((visitedCurr[lo[0]][lo[1]] <= lo[3] || visitedSrc[lo[0]][lo[1]] >= lo[2]))
  57. q.addLast(lo);
  58. }
  59. }
  60. System.out.println(min);
  61. }
  62. }
  63. }
  64.  
  65. class Input {
  66. BufferedReader bf;
  67. StringTokenizer st;
  68.  
  69. public Input() throws IOException {
  70. bf = new BufferedReader(new InputStreamReader(System.in));
  71. st = new StringTokenizer(bf.readLine());
  72. }
  73.  
  74. public String next() throws IOException {
  75. while (!st.hasMoreTokens() && bf.ready()) {
  76. st = new StringTokenizer(bf.readLine());
  77. }
  78. return st.hasMoreTokens() ? st.nextToken() : null;
  79. }
  80.  
  81. public boolean hasNext() throws IOException {
  82. while (!st.hasMoreTokens() && bf.ready()) {
  83. st = new StringTokenizer(bf.readLine());
  84. }
  85. return st.hasMoreTokens();
  86. }
  87.  
  88. public int nextInt() throws IOException {
  89. while (!st.hasMoreTokens() && bf.ready()) {
  90. st = new StringTokenizer(bf.readLine());
  91. }
  92. return st.hasMoreTokens() ? new Integer(st.nextToken()) : 0;
  93.  
  94. }
  95. }
Add Comment
Please, Sign In to add comment