Advertisement
vladimirVenkov

Greeedydwarf

Jun 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class TheGreedyDwarf {
  6.  
  7.     static int[] nextCellCoords(int[][] matrix, int r, int c) {
  8.         int[] newCoords = new int[2];
  9.         int up = 0;
  10.         int left = 0;
  11.         int right = 0;
  12.         int down = 0;
  13.         if(r - 1 >= 0) up = matrix[r -1][c];
  14.         if(c - 1 >= 0) left = matrix[r][c -1];
  15.         if(c + 1 < matrix[r].length) right = matrix[r][c + 1];
  16.         if(r + 1 < matrix.length) down = matrix[r +1][c];
  17.  
  18.         if (up == 0 && left == 0 && right == 0 && down == 0) {
  19.             newCoords[0] = -1;
  20.             newCoords[1] = -1;
  21.             return newCoords;
  22.         }
  23.  
  24.         if (left >= up && left >= right && left >= down) {
  25.             newCoords[0] = r;
  26.             newCoords[1] = c - 1;
  27.             return newCoords;
  28.         }
  29.         if (right > left && right >= up && right >= down) {
  30.             newCoords[0] = r;
  31.             newCoords[1] = c + 1;
  32.             return newCoords;
  33.         }
  34.         if (up > left && up > right && up >= down) {
  35.             newCoords[0] = r - 1;
  36.             newCoords[1] = c;
  37.             return newCoords;
  38.         }
  39.         if (down > left && down > right && down > up) {
  40.             newCoords[0] = r + 1;
  41.             newCoords[1] = c;
  42.             return newCoords;
  43.         }
  44.  
  45.         return newCoords;
  46.     }
  47.  
  48.     public static void main(String[] args) throws IOException {
  49.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  50.         String[] inp = br.readLine().split(" ");
  51.         int n = Integer.parseInt(inp[0]);
  52.         int m = Integer.parseInt(inp[1]);
  53.         int[][] matrix = new int [n][m];
  54.         int[] start = new int[2];
  55.  
  56.         for (int i = 0; i < n; i++) {
  57.             String[] next = br.readLine().split(" ");
  58.             for (int j = 0; j < m; j++) {
  59.                 matrix[i][j] = Integer.parseInt(next[j]);
  60.                 if (matrix[i][j] == 0) {
  61.                     start[0] = i;
  62.                     start[1] = j;
  63.                 }
  64.             }
  65.         }
  66.       //  printMatrix(matrix);
  67.         goldCount(matrix, start[0], start[1]);
  68.         System.out.println(gold);
  69.  
  70.     }
  71.     static int gold = 0;
  72.  
  73.     static void goldCount(int[][] matrix, int r, int c) {
  74.         if (r == -1) {
  75.             return;
  76.         }
  77.         int[] next = nextCellCoords(matrix, r, c);
  78.         if(next[0] == -1 )return;
  79.         gold++;
  80.         matrix[next[0]][next[1]] --;
  81.         goldCount(matrix, next[0], next[1]);
  82.     }
  83.  
  84.     static void printMatrix(int[][] matr) {
  85.         for (int i = 0; i < matr.length; i++) {
  86.             for (int j = 0; j < matr[0].length; j++) {
  87.                 System.out.print(matr[i][j]);
  88.             }
  89.             System.out.println();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement