Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package hash_code_test;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileReader;
  10. import java.io.IOError;
  11. import java.io.IOException;
  12.  
  13. /**
  14. *
  15. * @author MiguelFreitas(114127
  16. */
  17. public class Main {
  18.  
  19. public static char[][] pizza;
  20. public static String example_file;
  21. public static BufferedReader br = null;
  22. public static FileReader fr = null;
  23. public static int cols = 0;
  24. public static int rows = 0;
  25. //minimo de cada um dos ingredientes
  26. public static int min_each_ing_slice = 0;
  27. //maximo de cells por fatia
  28. public static int max_cells_slice = 0;
  29.  
  30. public static void main(String[] args) {
  31.  
  32. pizza = null;
  33.  
  34. example_file = "example.in";
  35.  
  36. String current_file_line = "";
  37.  
  38. try {
  39.  
  40. fr = new FileReader(example_file);
  41. br = new BufferedReader(fr);
  42.  
  43. current_file_line = br.readLine();
  44.  
  45. String[] size_data = current_file_line.split(" ");
  46.  
  47. rows = Integer.parseInt(size_data[0]);
  48. cols = Integer.parseInt(size_data[1]);
  49. min_each_ing_slice = Integer.parseInt(size_data[2]);
  50. max_cells_slice = Integer.parseInt(size_data[3]);
  51.  
  52. pizza = new char[rows][cols];
  53.  
  54. int current_column = 0;
  55.  
  56. while ((current_file_line = br.readLine()) != null) {
  57.  
  58. for (int i = 0; i < cols; i++) {
  59. pizza[current_column][i] = current_file_line.charAt(i);
  60. }
  61. current_column++;
  62. }
  63.  
  64. printMatrix();
  65.  
  66. System.out.println("\n\nTestando fatia teste: 0,0,2,1");
  67.  
  68. System.out.println(evaluate_slice(new Slice(0,0,2,1)));
  69.  
  70. } catch (IOException e) {
  71. System.out.println("File not found");
  72. }
  73. }
  74.  
  75. public static void printMatrix() {
  76. for (int i = 0; i < pizza.length; i++) {
  77. for (int j = 0; j < pizza[0].length; j++) {
  78. System.out.print(pizza[i][j] + " ");
  79. }
  80. System.out.println("");
  81. }
  82. }
  83.  
  84. public static boolean evaluate_slice(Slice slice) {
  85.  
  86. int num_m = 0;
  87. int num_t = 0;
  88.  
  89.  
  90. if (slice.get_num_cells() > max_cells_slice) {
  91. return false;
  92. }
  93.  
  94. for (int i = slice.row_a; i < slice.row_b; i++) {
  95. for (int j = slice.col_a; j < slice.col_b; j++) {
  96. if (pizza[i][j] == 'M') {
  97. num_m++;
  98. } else {
  99. if (pizza[i][j] == 'T') {
  100. num_t++;
  101. }
  102. }
  103. }
  104.  
  105. }
  106.  
  107. System.out.println("num_m: "+num_m);
  108. System.out.println("num_t: "+num_t);
  109.  
  110. return num_m > min_each_ing_slice && num_t > min_each_ing_slice;
  111. }
  112.  
  113. public static class Slice {
  114.  
  115. public int row_a;
  116. public int row_b;
  117. public int col_a;
  118. public int col_b;
  119.  
  120. public Slice() {
  121. }
  122.  
  123. public Slice(int row_a, int row_b, int col_a, int col_b) {
  124. this.row_a = row_a;
  125. this.row_b = row_b;
  126. this.col_a = col_a;
  127. this.col_b = col_b;
  128. }
  129.  
  130. public int get_num_cells(){
  131. return Math.abs(row_b-row_a) * Math.abs(col_b - col_a);
  132. }
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement