|
| 1 | +package sea_battle_two; |
| 2 | + |
| 3 | +import java.io.FileReader; |
| 4 | +import java.io.FileWriter; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Scanner; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class SeaBattleTwo { |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + ArrayList<String> data = new ArrayList<>(); |
| 13 | + FileReader file = new FileReader("input.txt"); |
| 14 | + Scanner sc = new Scanner(file); |
| 15 | + while (sc.hasNextLine()){ |
| 16 | + data.add(sc.nextLine()); |
| 17 | + } |
| 18 | + int row = 0, column = 0; |
| 19 | + StringTokenizer st = new StringTokenizer(data.get(0), " "); |
| 20 | + while (st.hasMoreTokens()){ |
| 21 | + row = Integer.valueOf(st.nextToken()); |
| 22 | + column = Integer.valueOf(st.nextToken()); |
| 23 | + } |
| 24 | + data.remove(0); |
| 25 | + |
| 26 | + String[] arrData = data.toArray(new String[0]); |
| 27 | + String[][] arrayReady = new String[row][column]; |
| 28 | + |
| 29 | + for(int i = 0; i < row; i++){ |
| 30 | + char[] ch = arrData[i].toCharArray(); |
| 31 | + for(int j = 0; j < column; j++){ |
| 32 | + arrayReady[i][j] = String.valueOf(ch[j]); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + int count = 0; |
| 37 | + if(row > 2 || column > 2){ |
| 38 | + for(int i = 0; i < row; i++){ |
| 39 | + for(int j = 0; j < column; j++){ |
| 40 | + if(arrayReady[i][j].equals(".")){ |
| 41 | + if( |
| 42 | + ((i == 0 && arrayReady[i+1][j].equals(".") || |
| 43 | + ((i-1 > -1 && arrayReady[i-1][j].equals(".")) && (i+1 < row && (arrayReady[i+1][j].equals("."))))) || |
| 44 | + (i == row-1 && arrayReady[i-1][j].equals("."))) && |
| 45 | + ((j == 0 && arrayReady[i][j+1].equals(".")) || |
| 46 | + ((j-1 > -1 && arrayReady[i][j-1].equals(".") && (j+1 < column && arrayReady[i][j+1].equals(".")))) || |
| 47 | + (j == column-1 && arrayReady[i][j-1].equals("."))) |
| 48 | + ) |
| 49 | + { |
| 50 | + count++; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + FileWriter out = new FileWriter("output.txt"); |
| 58 | + out.write(String.valueOf(count)); |
| 59 | + out.close(); |
| 60 | + } |
| 61 | +} |
0 commit comments