Guest User

Untitled

a guest
Nov 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.ListIterator;
  7. /*
  8. * B.java
  9. *
  10. * Created on 2005/06/10, 2:05
  11. *
  12. * To change this template, choose Tools | Options and locate the template under
  13. * the Source Creation and Management node. Right-click the template and choose
  14. * Open. You can then make changes to the template in the Source Editor.
  15. */
  16.  
  17. /**
  18. *
  19. * @author kunishi
  20. */
  21. public class B {
  22.  
  23. public class Tile {
  24. public static final int RED = 0;
  25. public static final int BLACK = 1;
  26. public static final int OPEN = 1;
  27. public static final int CLOSE = 0;
  28. public int color;
  29. public int status;
  30. public int x, y;
  31.  
  32. public Tile(int x, int y) {
  33. color = BLACK;
  34. status = CLOSE;
  35. this.x = x;
  36. this.y = y;
  37. }
  38. public Tile(int c, int x, int y) {
  39. color = c;
  40. status = CLOSE;
  41. this.x = x;
  42. this.y = y;
  43. }
  44. public int calc(Tile[][] tiles) {
  45. if (color == Tile.RED) {
  46. return 0;
  47. } else if (status == Tile.OPEN) {
  48. return 0;
  49. } else {
  50. int sum = 1;
  51. status = Tile.OPEN;
  52. ArrayList al = new ArrayList();
  53. al.add(tiles[x-1][y]);
  54. al.add(tiles[x][y+1]);
  55. al.add(tiles[x+1][y]);
  56. al.add(tiles[x][y-1]);
  57. ListIterator li = al.listIterator();
  58. while (li.hasNext()) {
  59. sum += ((Tile)li.next()).calc(tiles);
  60. }
  61. return sum;
  62. }
  63. }
  64. }
  65. /** Creates a new instance of B */
  66. public B() {
  67. }
  68.  
  69. public static void main(String[] args) {
  70. B b = new B();
  71. b.doCalculation(args[0]);
  72. }
  73.  
  74. public void doCalculation(String file) {
  75. int w, h;
  76. Tile[][] tiles = null;
  77. Tile mypos = null;
  78. try {
  79. BufferedReader br = new BufferedReader(new FileReader(file));
  80. String s = br.readLine();
  81. while (!s.equals("0 0")) {
  82. w = Integer.parseInt(s.substring(0, s.indexOf(' ')));
  83. h = Integer.parseInt(s.substring(s.indexOf(' ')+1, s.length()));
  84. tiles = new Tile[w+2][h+2];
  85. for (int i = 0; i < h+2; i ++) {
  86. tiles[0][i] = new Tile(Tile.RED, 0, i);
  87. tiles[w+1][i] = new Tile(Tile.RED, w+1, i);
  88. }
  89. for (int i = 0; i < w+2; i ++) {
  90. tiles[i][0] = new Tile(Tile.RED, i, 0);
  91. tiles[i][h+1] = new Tile(Tile.RED, i, h+1);
  92. }
  93. for (int i = 0; i < h; i ++) {
  94. s = br.readLine();
  95. for (int j = 0; j < s.length(); j ++) {
  96. switch (s.charAt(j)) {
  97. case '.':
  98. tiles[j+1][i+1] = new Tile(Tile.BLACK, j+1, i+1);
  99. break;
  100. case '#':
  101. tiles[j+1][i+1] = new Tile(Tile.RED, j+1, i+1);
  102. break;
  103. case '@':
  104. tiles[j+1][i+1] = new Tile(Tile.BLACK, j+1, i+1);
  105. mypos = tiles[j+1][i+1];
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. }
  112. System.out.println(mypos.calc(tiles));
  113. s = br.readLine();
  114. }
  115. } catch (FileNotFoundException e) {
  116. System.out.println(e);
  117. } catch (IOException e) {
  118. System.out.println(e);
  119. }
  120. }
  121. }
Add Comment
Please, Sign In to add comment