Baekjoon 21736

Tadap·2023년 10월 27일
0

Baekjoon

목록 보기
66/94

문제

Solved.ac Class3++

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int y = Integer.parseInt(split[0]);
		int x = Integer.parseInt(split[1]);
		Position positionNow = null;
		char[][] data = new char[y][x];
		boolean[][] isVisit = new boolean[y][x];
		int count = 0;
		for (int i = 0; i < y; i++) {
			char[] readData = br.readLine().toCharArray();
			for (int j = 0; j < x; j++) {
				if (readData[j] == 'I') {
					positionNow = new Position(j, i);
				}
				data[i][j] = readData[j];
			}
		}
		Queue<Position> queue = new LinkedList<>();
		queue.add(positionNow);

		while (!queue.isEmpty()) {
			Position now = queue.remove();
			int xNow = now.x;
			int yNow = now.y;

			if (xNow - 1 >= 0 && !isVisit[yNow][xNow - 1]) {
				isVisit[yNow][xNow - 1] = true;
				if (data[yNow][xNow - 1] != 'X') {
					queue.add(new Position(xNow - 1, yNow));
					if (data[yNow][xNow - 1] == 'P') {
						count++;
					}
				}
			}

			if (yNow - 1 >= 0 && !isVisit[yNow - 1][xNow]) {
				isVisit[yNow - 1][xNow] = true;
				if (data[yNow - 1][xNow] != 'X') {
					queue.add(new Position(xNow, yNow - 1));
					if (data[yNow - 1][xNow] == 'P') {
						count++;
					}
				}
			}

			if (xNow + 1 < x && !isVisit[yNow][xNow + 1]) {
				isVisit[yNow][xNow + 1] = true;
				if (data[yNow][xNow + 1] != 'X') {
					queue.add(new Position(xNow + 1, yNow));
					if (data[yNow][xNow + 1] == 'P') {
						count++;
					}
				}
			}

			if (yNow + 1 < y && !isVisit[yNow + 1][xNow]) {
				isVisit[yNow + 1][xNow] = true;
				if (data[yNow + 1][xNow] != 'X') {
					queue.add(new Position(xNow, yNow + 1));
					if (data[yNow + 1][xNow] == 'P') {
						count++;
					}
				}
			}
		}

		if (count == 0) {
			System.out.println("TT");
		} else {
			System.out.println(count);
		}
	}

	static class Position {
		int x;
		int y;

		public Position(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}
}

성공

ToKotlin

fun main() {
    val split = readln().split(" ")
    val y = split[0].toInt()
    val x = split[1].toInt()
    var positionNow: Position? = null
    val data = Array(y) { CharArray(x) }
    val isVisit = Array(y) { BooleanArray(x) }
    var count: Int = 0
    for (i in 0..<y) {
        val readData = readln().toCharArray()
        for (j in 0..<x) {
            if (readData[j] == 'I') {
                positionNow = Position(j, i)
            }
            data[i][j] = readData[j]
        }
    }

    val queue: Queue<Position> = LinkedList()
    queue.add(positionNow)
    isVisit[positionNow!!.y][positionNow.x] = true

    while (queue.isNotEmpty()) {
        val now = queue.remove()
        val xNow = now.x
        val yNow = now.y

        if (xNow - 1 >= 0 && !isVisit[yNow][xNow - 1]) {
            isVisit[yNow][xNow - 1] = true;
            if (data[yNow][xNow - 1] != 'X') {
                queue.add(Position(xNow - 1, yNow));
                if (data[yNow][xNow - 1] == 'P') {
                    count++;
                }
            }
        }

        if (yNow - 1 >= 0 && !isVisit[yNow - 1][xNow]) {
            isVisit[yNow - 1][xNow] = true;
            if (data[yNow - 1][xNow] != 'X') {
                queue.add(Position(xNow, yNow - 1));
                if (data[yNow - 1][xNow] == 'P') {
                    count++;
                }
            }
        }

        if (xNow + 1 < x && !isVisit[yNow][xNow + 1]) {
            isVisit[yNow][xNow + 1] = true;
            if (data[yNow][xNow + 1] != 'X') {
                queue.add(Position(xNow + 1, yNow));
                if (data[yNow][xNow + 1] == 'P') {
                    count++;
                }
            }
        }

        if (yNow + 1 < y && !isVisit[yNow + 1][xNow]) {
            isVisit[yNow + 1][xNow] = true;
            if (data[yNow + 1][xNow] != 'X') {
                queue.add(Position(xNow, yNow + 1));
                if (data[yNow + 1][xNow] == 'P') {
                    count++;
                }
            }
        }
    }

    if (count == 0) {
        println("TT")
    } else {
        println(count)
    }

}

class Position(val x: Int, val y: Int)

0개의 댓글