문제풀이/DFS&BFS

[PS] BOJ16929 Two Dots ( DFS ) with JAVA

IT록흐 2023. 7. 26. 11:23
반응형
 

16929번: Two Dots

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

www.acmicpc.net

 

◎ 문제풀이

 

행렬그래프가 주어지고 싸이클을 찾는 문제이다. 이는 DFS로 접근하면 된다. 

 

◎ 코드

package org.example.dfs;


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

//BOJ16929 Two Dots
public class Dfs1 {

    static int n;
    static int m;
    static int startX;
    static int startY;
    static char[][] board;
    static boolean[][] visited;
    static int[] dx = {1,-1,0,0};
    static int[] dy = {0,0,1,-1};


    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];
        visited = new boolean[n][m];

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

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                startX = j;
                startY = i;
                visited[i][j] = true;
                if(dfs(j,i,1)){
                    System.out.println("Yes");
                    return;
                }
                visited[i][j] = false;
            }
        }

        System.out.println("No");

    }

    public static boolean dfs(int x,int y,int depth){
        for(int i=0;i<4;i++){
            int nx = x + dx[i];
            int ny = y + dy[i];

            if(isValidate(nx,ny)){
                if(startX == nx && startY == ny && depth >= 4 ) return true;
                if(!visited[ny][nx] && board[ny][nx] == board[startX][startY]){
                    visited[ny][nx] = true;
                    if(dfs(nx,ny,depth+1)) {
                        visited[ny][nx] = false;
                        return true;
                    }
                    else visited[ny][nx] = false;
                }
            }
        }
        return false;
    }

    public static boolean isValidate(int x,int y){
        return ( x >=0 && y>=0 && x < m & y < n ) ? true : false;
    }
}
반응형