[Algorithm - Baekjoon] 16929번 Two Dots

nunu·2023년 7월 19일
0

Algorithm

목록 보기
34/142

문제

Two Dots는 Playdots, Inc.에서 만든 게임이다. 게임의 기초 단계는 크기가 N×M인 게임판 위에서 진행된다.

각각의 칸은 색이 칠해진 공이 하나씩 있다. 이 게임의 핵심은 같은 색으로 이루어진 사이클을 찾는 것이다.

다음은 위의 게임판에서 만들 수 있는 사이클의 예시이다.

점 k개 d1, d2, ..., dk로 이루어진 사이클의 정의는 아래와 같다.

모든 k개의 점은 서로 다르다.
k는 4보다 크거나 같다.
모든 점의 색은 같다.
모든 1 ≤ i ≤ k-1에 대해서, di와 di+1은 인접하다. 또, dk와 d1도 인접해야 한다. 두 점이 인접하다는 것은 각각의 점이 들어있는 칸이 변을 공유한다는 의미이다.
게임판의 상태가 주어졌을 때, 사이클이 존재하는지 아닌지 구해보자.

입력

첫째 줄에 게임판의 크기 N, M이 주어진다. 둘째 줄부터 N개의 줄에 게임판의 상태가 주어진다. 게임판은 모두 점으로 가득차 있고, 게임판의 상태는 점의 색을 의미한다. 점의 색은 알파벳 대문자 한 글자이다.

출력

사이클이 존재하는 경우에는 "Yes", 없는 경우에는 "No"를 출력한다.

제한

  • 2 ≤ N, M ≤ 50

예제1-입력

3 4
AAAA
ABCA
AAAA

예제1 - 출력

Yes

예제2-입력

3 4
AAAA
ABCA
AADA

예제2 - 출력

No

예제3-입력

4 4
YYYR
BYBY
BBBY
BBBY

예제3 - 출력

Yes

예제4-입력

7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB

예제4 - 출력

Yes

예제5-입력

2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ

예제5 - 출력

No

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static char[][] board;
    static boolean[][] visited;
    static int n, m;
    static Pair first;
    static boolean check;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());

        board = new char[n][m];
        for (int i = 0; i < n; i++) {
            board[i] = br.readLine().toCharArray();
        }
        br.close();

        check = false;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                first = new Pair(i, j);
                visited = new boolean[n][m];
                visited[i][j] = true;
                dfs(new Pair(i, j), 1);
                if(check){
                    System.out.println("Yes");
                    return;
                }
            }
        }
        System.out.println("No");
    }

    static void dfs(Pair now, int level) {
        int[] mx = {1, 0, -1, 0};
        int[] my = {0, 1, 0, -1};

        for (int i = 0; i < mx.length; i++) {
            int dx = now.x + mx[i];
            int dy = now.y + my[i];
            if ((dx >= 0 && dx < n) && (dy >= 0 && dy < m) && (board[now.x][now.y] == board[dx][dy])) {
                if (!visited[dx][dy]) {
                    visited[dx][dy] = true;
                    dfs(new Pair(dx, dy), level + 1);
                } else {
                    if (level >= 4 && (first.x == dx) && (first.y == dy)) {
                        check = true;
                        return;
                    }
                }
            }
        }
    }

    static class Pair {
        public int x;
        public int y;

        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}
profile
Hello, I'm nunu

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

정말 잘 읽었습니다, 고맙습니다!

답글 달기