diff --git a/sitou/src/B142_Reversi.java b/sitou/src/B142_Reversi.java new file mode 100644 index 0000000000000000000000000000000000000000..f1e5fcb124bdcd2467a07621d84b75b7628d90f0 --- /dev/null +++ b/sitou/src/B142_Reversi.java @@ -0,0 +1,352 @@ +package src; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +final public class B142_Reversi { + final public static void main(String[] args) { + final B142_Reversi b142 = new B142_Reversi(); + b142.execute(); + } + + final void execute() { + final InputData inputData = input(); + final Result result = solveReversi(inputData); + output(result); + } + + final private static class InputData { + final int sideLength; + final List boardStatus; + + public InputData(final int sideLength, List boardStatus) { + this.sideLength = sideLength; + this.boardStatus = boardStatus; + } + } + + final private static InputData input() { + final Scanner scan = new Scanner(System.in); + final int sideLength = scan.nextInt(); + List boardStatus = new ArrayList(); + + for (int i = 0; i < sideLength; i++) { + boardStatus.add(scan.next()); + } + + scan.close(); + return new InputData(sideLength, boardStatus); + } + + + + final static Result solveReversi(InputData inputData) { + Reversi reversi = new Reversi(inputData); + final int result = reversi.countPutStonePlace(); + + return new Result(result); + } + + final private void output(final Result result) { + System.out.println(result.getNumOfPutStonePlace()); + } + + final private static class Result { + final int numOfPutStonePlace; + + public Result(final int numOfPutStonePlace) { + this.numOfPutStonePlace = numOfPutStonePlace; + } + + final int getNumOfPutStonePlace() { + return numOfPutStonePlace; + } + } + + final private static class Reversi { + public static enum stoneStatus{ + BLACK("B"), + WHITE("W"), + EMPTY("."); + + private final String stone; + + private stoneStatus(String stone) { + this.stone = stone; + } + + final public String getStone() { + return this.stone; + } + } + + final int sideLength; + final static List> boardList = new ArrayList>(); + + public Reversi(InputData inputData) { + sideLength = inputData.sideLength; + + // 盤を1マスずつに分割 + for (int i = 0; i < sideLength; i++) { + final List row = new ArrayList<>(); + final String stone = inputData.boardStatus.get(i); + for (int j = 0; j < sideLength; j++) { + row.add(stone.substring(j, j + 1)); + } + + boardList.add(row); + } + } + + final public int countPutStonePlace() { + int result = 0; + for (int height = 0; height < sideLength; height++) { + for (int width = 0; width < sideLength; width++) { + if (canPutStone(height, width)) { + result++; + } + } + } + + return result; + } + + final public boolean canPutStone(final int height, final int width) { + if (stoneStatus.EMPTY.getStone().equals(boardList.get(height).get(width))) { + if (canTurnLeftUp(height, width)) { + return true; + } + if (canTurnUp(height, width)) { + return true; + } + if (canTurnRightUp(height, width)) { + return true; + } + if (canTurnLeft(height, width)) { + return true; + } + if (canTurnRight(height, width)) { + return true; + } + if (canTurnLeftDown(height, width)) { + return true; + } + if (canTurnDown(height, width)) { + return true; + } + if (canTurnRightDown(height, width)) { + return true; + } + } + return false; + } + + final String MYSTONE = stoneStatus.BLACK.getStone(); + final String OPPONENT_STONE = stoneStatus.WHITE.getStone(); + final public boolean canTurnLeftUp(final int height, final int width) { + if (height > 1 && width > 1) { + // 左上隣の駒 + final String next = boardList.get(height - 1).get(width - 1); + + if (OPPONENT_STONE.equals(next)) { + // さらに左上隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + if (height - i < 0 + || width - i < 0 + || stoneStatus.EMPTY.getStone().equals(boardList.get(height - i).get(width - i))) { + // 駒がないとき + return false; + + } else if (MYSTONE.equals(boardList.get(height - i).get(width - i))) { + // 自分の駒のとき + return true; + + } + // 相手の駒のとき、さらに左上隣の駒を確認する + } + } + } + return false; + } + + final public boolean canTurnUp(final int height, final int width) { + if (height > 1) { + // 上隣の駒 + final String next = boardList.get(height - 1).get(width); + + if (OPPONENT_STONE.equals(next)) { + // さらに上隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + + if (height - i < 0 + || stoneStatus.EMPTY.getStone().equals(boardList.get(height - i).get(width))) { + // 駒がないとき + return false; + + } else if (MYSTONE.equals(boardList.get(height - i).get(width))) { + // 自分の駒のとき + return true; + + } + // 相手の駒のとき、さらに上隣の駒を確認する + } + } + } + return false; + } + + final public boolean canTurnRightUp(final int height, final int width) { + if (height > 1 && width < sideLength - 2) { + // 右上隣の駒 + final String next = boardList.get(height - 1).get(width + 1); + + if (OPPONENT_STONE.equals(next)) { + // さらに右上隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + if (height - i < 0 + || width + i > sideLength - 1 + || stoneStatus.EMPTY.getStone().equals(boardList.get(height - i).get(width + i))) { + // 駒がないとき + return false; + + } else if (MYSTONE.equals(boardList.get(height - i).get(width + i))) { + // 自分の駒のとき + return true; + + } + // 相手の駒のとき、さらに右上隣の駒を確認する + } + } + } + return false; + } + + final public boolean canTurnLeft(final int height, final int width) { + if (width > 1) { + // 左隣の駒 + final String next = boardList.get(height).get(width - 1); + + if (OPPONENT_STONE.equals(next)) { + // さらに右上隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + if (width - i < 0 + || stoneStatus.EMPTY.getStone().equals(boardList.get(height).get(width - i))) { + // 駒がないとき + return false; + + } else if (MYSTONE.equals(boardList.get(height).get(width - i))) { + // 自分の駒のとき + return true; + + } + // 相手の駒のとき、さらに右上隣の駒を確認する + } + } + } + return false; + } + + final public boolean canTurnRight(final int height, final int width) { + if (width < sideLength - 2) { + // 右隣の駒 + final String next = boardList.get(height).get(width + 1); + + if (OPPONENT_STONE.equals(next)) { + // さらに右隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + if (width + i > sideLength - 1 + || stoneStatus.EMPTY.getStone().equals(boardList.get(height).get(width + i))) { + // 駒がないとき + return false; + } else if (MYSTONE.equals(boardList.get(height).get(width + i))) { + // 自分の駒のとき + return true; + } + // 相手の駒のとき、さらに右上隣の駒を確認する + } + } + } + return false; + } + + final public boolean canTurnLeftDown(final int height, final int width) { + if (height < sideLength - 2 && width > 1) { + // 左下隣の駒 + final String next = boardList.get(height + 1).get(width - 1); + + if (OPPONENT_STONE.equals(next)) { + // さらに左下隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + if (height + i > sideLength + || width - i < 0 + || stoneStatus.EMPTY.getStone().equals(boardList.get(height + i).get(width - i))) { + // 駒がないとき + return false; + + } else if (MYSTONE.equals(boardList.get(height + i).get(width - i))) { + // 自分の駒のとき + return true; + + } + // 相手の駒のとき、さらに左上隣の駒を確認する + } + } + } + return false; + } + + final public boolean canTurnDown(final int height, final int width) { + if (height < sideLength - 2) { + // 下隣の駒 + final String next = boardList.get(height + 1).get(width); + + if (OPPONENT_STONE.equals(next)) { + // さらに下隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + + if (height + i > sideLength + || stoneStatus.EMPTY.getStone().equals(boardList.get(height + i).get(width))) { + // 駒がないとき + return false; + + } else if (MYSTONE.equals(boardList.get(height + i).get(width))) { + // 自分の駒のとき + return true; + + } + // 相手の駒のとき、さらに上隣の駒を確認する + } + } + } + return false; + } + + final public boolean canTurnRightDown(final int height, final int width) { + if (height < sideLength - 2 && width < sideLength - 2) { + // 右上隣の駒 + final String next = boardList.get(height + 1).get(width + 1); + + if (OPPONENT_STONE.equals(next)) { + // さらに右上隣の駒を確認 + for (int i = 2; i < sideLength; i++) { + if (height + i > sideLength - 1 + || width + i > sideLength - 1 + || stoneStatus.EMPTY.getStone().equals(boardList.get(height + i).get(width + i))) { + // 駒がないとき + return false; + + } else if (MYSTONE.equals(boardList.get(height + i).get(width + i))) { + // 自分の駒のとき + return true; + + } + // 相手の駒のとき、さらに右上隣の駒を確認する + } + } + } + return false; + } + + } +}