diff --git "a/khj20006/202503/17 BOJ G2 \355\206\265\353\202\230\353\254\264 \354\230\256\352\270\260\352\270\260.md" "b/khj20006/202503/17 BOJ G2 \355\206\265\353\202\230\353\254\264 \354\230\256\352\270\260\352\270\260.md" new file mode 100644 index 00000000..8a39f687 --- /dev/null +++ "b/khj20006/202503/17 BOJ G2 \355\206\265\353\202\230\353\254\264 \354\230\256\352\270\260\352\270\260.md" @@ -0,0 +1,154 @@ +```java + +import java.util.*; +import java.io.*; + +public class Main { + + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st = new StringTokenizer(""); + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() throws Exception { + if(!st.hasMoreTokens()) nextLine(); + return Integer.parseInt(st.nextToken()); + } + static long nextLong() throws Exception { + if(!st.hasMoreTokens()) nextLine(); + return Long.parseLong(st.nextToken()); + } + static void ioClose() throws Exception {bw.flush();bw.close();br.close();} + + /* + * [solving strategy] + * 기차의 왼쪽 위의 점 (x,y)를 잡고, 기차의 방향(dir)을 추가하면 + * 각 상태 (x,y,dir)의 관점에서 보면 항상 최단 거리로 각 상태에 도달하는 것이 이득입니다. + * 따라서, 적절히 시작점을 찾아 3차원 필드에서 BFS를 수행합니다. + * + * [description] + * - N : 공간의 한 변의 길이 + * - A : 공간의 정보 + * - dx, dy : 4방향 탐색을 위한 x, y의 변홧값 배열 + * + * - ready() : input 처리 + * - solve() : 초기 상태 확인, bfs 호출하여 정답 도출 + * - bfs(sx,sy,sdir) : 초기 상태를 (sx,sy,sdir)로 두고 3차원 필드에서 BFS 수행 + * - canRotate(x,y,dir) : 주어진 상태 (x,y,dir)에서 올바르게 회전시킬 수 있는지 확인, boolean 형으로 반환 + * - isEnd(x,y,dir) : 현재 상태 (x,y,dir)이 목적지의 상태와 동일한지 확인, boolean 형으로 반환 + * - safe(x,y,dir) : 상태 (x,y,dir)이 유효한 상태인지 확인, boolean 형으로 반환 + */ + + static int N; + static char[][] A; + static int[] dx = {1,0,-1,0}; + static int[] dy = {0,1,0,-1}; + + public static void main(String[] args) throws Exception { + //--------------솔루션 코드를 작성하세요.-------------------------------- + + ready(); + solve(); + + ioClose(); + + } + + static void ready() throws Exception { + + N = nextInt(); + A = new char[N][]; + for(int i=0;i Q = new LinkedList<>(); + boolean[][][] vis = new boolean[N][N][2]; + + vis[sx][sy][sdir] = true; + Q.offer(new int[] {sx,sy,sdir,0}); + + while(!Q.isEmpty()) { + int[] now = Q.poll(); + int x = now[0], y = now[1], dir = now[2], t = now[3]; + if(isEnd(x,y,dir)) return t; + + if(canRotate(x,y,dir)) { + if(dir == 0) { + if(!vis[x+1][y-1][1]) { + vis[x+1][y-1][1] = true; + Q.offer(new int[] {x+1,y-1,1,t+1}); + } + } + else { + if(!vis[x-1][y+1][0]) { + vis[x-1][y+1][0] = true; + Q.offer(new int[] {x-1,y+1,0,t+1}); + } + } + } + + for(int i=0;i<4;i++) { + int xx = x+dx[i], yy = y+dy[i]; + if(safe(xx,yy,dir) && !vis[xx][yy][dir]) { + vis[xx][yy][dir] = true; + Q.offer(new int[] {xx,yy,dir,t+1}); + } + } + + } + + return 0; + + } + + static boolean canRotate(int x, int y, int dir) { + if(dir == 0) { + if(0<=y-1 && y+1