알고리즘/SWEA

[Java] SWEA 1227번 미로2

컴공맨 2021. 2. 23. 17:14
 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


풀이

(1, 1)이 시작 지점이므로 (1, 1)부터 DFS를 시작해서 (y, x)가 3인 지점이 있다면 도착지점까지 갈 수 있으므로 answer를 true로 바꾸어 1을 출력하게하여 해결했습니다.


코드

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

public class Solution {
	public static char[][] maze = new char[100][100];
	public static boolean answer;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder("");
		
		for (int tc = 1; tc <= 10; ++tc) {
			br.readLine();
			
			answer = false;		
			for (int i = 0; i < 100; ++i) {
				maze[i] = br.readLine().toCharArray();
			}
			
			dfs(1, 1);
			
			sb.append("#").append(tc).append(" ").append(answer ? 1 : 0).append("\n");
		}
		
		System.out.println(sb);
	}
	
	public static int[] dx = {0, 0, -1, 1};
	public static int[] dy = {-1, 1, 0, 0};
	public static void dfs(int y, int x) {
		maze[y][x] = '1';
		
		for (int i = 0; i < 4; ++i) {
			int ny = y + dy[i];
			int nx = x + dx[i];
			
			if (ny < 0 || nx < 0 || ny >= 100 || nx >= 100) {
				continue;
			}
			
			if (maze[ny][nx] == '3') {
				answer = true;
				return;
			}
		
			if (maze[ny][nx] == '0') {
				dfs(ny, nx);
			}
		}
	}
}

 

pyo7410/Algorithm

1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.

github.com