https://www.acmicpc.net/problem/2206
2206λ²: λ²½ λΆμκ³ μ΄λνκΈ°
N×Mμ νλ ¬λ‘ ννλλ λ§΅μ΄ μλ€. λ§΅μμ 0μ μ΄λν μ μλ κ³³μ λνλ΄κ³ , 1μ μ΄λν μ μλ λ²½μ΄ μλ κ³³μ λνλΈλ€. λΉμ μ (1, 1)μμ (N, M)μ μμΉκΉμ§ μ΄λνλ € νλλ°, μ΄λ μ΅λ¨ κ²½λ‘
www.acmicpc.net
λ¬Έμ 
- N×Mμ νλ ¬λ‘ ννλλ λ§΅μ΄ μλ€. λ§΅μμ 0μ μ΄λν μ μλ κ³³, 1μ λ²½μ΄ μλ κ³³μ λνλΈλ€.
- (1, 1)μμ (N, M)μ μμΉκΉμ§ μ΄λνλ € νλλ°, μ΅λ¨ κ²½λ‘λ‘ μ΄λνλ € νλ€.
- ν μΉΈμμ μ΄λν μ μλ μΉΈμ μνμ’μ°λ‘ μΈμ ν μΉΈ
 
- ν κ°μ λ²½μ λΆμκ³ μ΄λνλ κ²μ΄ μ’ λ κ²½λ‘κ° μ§§μμ§λ€λ©΄, λ²½μ ν κ° κΉμ§ λΆμκ³ μ΄λνμ¬λ λλ€.
- 첫째 μ€μ μ΅λ¨ 거리λ₯Ό μΆλ ₯νλ€. λΆκ°λ₯ν λλ -1μ μΆλ ₯νλ€.
νμ΄
μνμ’μ° μΈμ ν μΉΈμΌλ‘ ν μΉΈμ© μ΄λνμ λμ μ΅λ¨ κ²½λ‘λ₯Ό ꡬνλ λ¬Έμ λ‘, ν μΉΈμ© μ΄λνλ©° μ¬λ°©νμμ ν΅ν΄ λͺ¨λ κ²½μ°μ μμ λν΄ μ΄νΌκ³ λͺ©ν μ§μ μ λλ¬νμμ νμμ μ’ λ£ν΄μΌ νλ€. λ°λΌμ νλ₯Ό μ΄μ©ν BFS μκ³ λ¦¬μ¦μ μ¬μ©νμ¬ λ¬Έμ λ₯Ό νμλ€.
μ 체μ μΈ νμ΄λ λ€λ₯Έ μ΅λ¨ κ²½λ‘λ₯Ό ꡬνλ λ¬Έμ μ κ°μ§λ§, 'λ²½μ ν κ° κΉμ§ λΆμκ³ μ΄λν μ μλ€'λΌλ μ‘°κ±΄μ΄ λΆμΌλ©΄μ νμ΄κ° λ¬λΌμ§λ€.
νμμ ν λ, λ²½μ λΆμλμ§μ λ°λΌ μ΄λμ΄ λ¬λΌμ§κ² λλ€. λ°λΌμ νμ μ’ν μ 보λ₯Ό λ£μ λ, νλ ¬ μ’νκ°, μ΄λ 거리λΏλ§ μλλΌ μ§κΈκΉμ§ λ²½μ λΆμμ μ΄ μλμ§ μ 보λ₯Ό μ μ₯ν νμκ° μλ€. λ°λΌμ μ’ν μ 보λ₯Ό λ΄λ PointλΌλ κ°μ²΄μ boolean νμ λ³μ(hit)μ μΆκ°ν΄μ€¬λ€.
- λ²½μ΄ μλ
- λ²½μ λΆμμ μ΄ μλ€ -> κ·Έλλ‘ νμ
- λ²½μ λΆμμ μ΄ μλ€ -> κ·Έλλ‘ νμ
 
- λ²½μ΄λ€
- λ²½μ λΆμμ μ΄ μλ€ -> λ²½μ λΆμκ³ νμ
- λ²½μ λΆμμ μ΄ μλ€ -> λμ΄μ νμ X
 
μ΄λλ‘ μ±μ μ λλ Έλλ νλ Έλ€. μμ  μ μΆλ ₯μ ν μ€νΈμΌμ΄μ€λ λͺ¨λ ν΅κ³Όνλλ°, λ°±μ€ λ¬Έμ  λ΄ κ²μνμ μλ λ°λ‘λ₯Ό 보λ νλ¦° μμΈμ μ°Ύμ μ μμλ€. (λ°λ‘ : https://www.acmicpc.net/board/view/119335)
방문체ν¬λ₯Ό 2μ°¨μμ boolean λ°°μ΄λ‘ ν΄λΉ μΉΈμ νλ ¬ μ’νκ°μΌλ‘ 체ν¬λ₯Ό ν΄μ£Όκ³ μμλλ°, μ΄ λΆλΆμμ νλ¦° κ²μ λ°κ²¬ν μ μμλ€. μ΄μ μ λ²½μ λΆμκ³ κ°μ λ, 방문체ν¬κ° λμ΄ λμ€μ λ²½μ λΆμμ§ μκ³ ν΄λΉ μ§μ μ λλ¬νκ² λλλΌλ μ΄λ―Έ 방문체ν¬κ° λμ΄ λμ΄μ νμμ ν μ μκ² λλ€. λ¨Όμ  λ²½μ λΆμκ³ κ°λ κ²½λ‘κ° μ΅λ¨ κ±°λ¦¬μΌ μλ μμ§λ§, νμ λ€λ₯Έ μ§μ μμ λ§νκ² λμ΄ λμ°©νμ§ λͺ»νκ² λ μ μλ€. κ·Έλ¬λ―λ‘ λ²½μ λΆμμ§ μκ³ λ°©λ¬Έν λμ κ°λ₯μ±λ μ΄μ΄μΌ νλ€. λ°λΌμ ν μΉΈμ λν΄ λ²½μ λΆμμ§ μκ³ λ°©λ¬Ένμ λμ λ²½μ λΆμκ³ λ°©λ¬Έν λλ‘ λλ 방문체ν¬λ₯Ό ν΄μ€μΌ νλ€.
-  λ°©λ¬Έμ²΄ν¬ : boolean[N][M][2]
- [x][y][0] : λ²½μ λΆμμ§ μκ³ , ν΄λΉ μ§μ μ λ°©λ¬Ένλκ°
- [x][y][1] : λ²½μ λΆμκ³ , ν΄λΉ μ§μ μ λ°©λ¬Ένλκ°
 
μ½λ
import java.io.*;
import java.util.*;
public class Main {
	static int N, M;
	static int[][] graph;
	static boolean[][][] visited;
	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0, 0, -1, 1};
	
	static class Point {
		int x;
		int y;
		int count;
		boolean hit;
		
		public Point(int x, int y, int count, boolean hit) {
			this.x = x;
			this.y = y;
			this.count = count;
			this.hit = hit;
		}
	}
	
	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());
		
		graph = new int[N + 1][M + 1];
		visited = new boolean[N + 1][M + 1][2]; //0 : λ²½ μλΆμ¨, 1: λ²½ λΆμ¨
		
		for(int i = 1; i <= N; i++) {
			String row = br.readLine();
			
			for(int j = 1; j <= M; j++) {
				graph[i][j] = row.charAt(j - 1) - '0';
			}
		}
		
		int answer = bfs(1, 1);
		
		System.out.println(answer);
	}
	
	public static int bfs(int i, int j) {
		Queue<Point> queue = new LinkedList<>();
		
		queue.add(new Point(i, j, 1, false));
		visited[i][j][0] = true;
		
		while(!queue.isEmpty()) {
			Point point = queue.poll();
			
			if(point.x == N && point.y == M) { // μ’
λ£ μ‘°κ±΄
				return point.count;
			}
			
			for(int d = 0; d < 4; d++) {
				int nx = point.x + dx[d];
				int ny = point.y + dy[d];
				
				if(1 <= nx && nx <= N && 1 <= ny && ny <= M) {
					if(graph[nx][ny] == 0) { //λ²½ μλ
						if(!point.hit && !visited[nx][ny][0]) { //λΆμμ  μμ
							queue.add(new Point(nx, ny, point.count + 1, false));
							visited[nx][ny][0] = true;
						} else if(point.hit && !visited[nx][ny][1]) { //λΆμμ  μμ
							queue.add(new Point(nx, ny, point.count + 1, true));
							visited[nx][ny][1] = true;
						}
					}else if(graph[nx][ny] == 1 && !point.hit) { //λ²½ μμ, λΆμμ  μμ
						queue.add(new Point(nx, ny, point.count + 1, true)); //λ²½ λΆμκΈ°
						visited[nx][ny][1] = true;
					}
				}
			}
		}
		
		return -1;
	}
}
κ²°κ³Ό

λ°°μ΄μ 
- 방문체ν¬λ₯Ό ν λ, λ²½μ λΆμλμ§ μλΆμλμ§ λλ μ ν΄μΌ νλ€.
'Algorithm > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| λ°±μ€ 7785λ² : νμ¬μ μλ μ¬λ (0) | 2023.08.30 | 
|---|---|
| λ°±μ€ 14442λ² : λ²½ λΆμκ³ μ΄λνκΈ°2 (0) | 2023.08.30 | 
| λ°±μ€ 16724λ² : νΌλ¦¬ λΆλ μ¬λμ΄ (1) | 2023.08.29 | 
| λ°±μ€ 13460λ² : κ΅¬μ¬ νμΆ2 (0) | 2023.08.02 | 
| λ°±μ€ 13459λ² : κ΅¬μ¬ νμΆ (0) | 2023.08.02 | 
