반응형
https://www.acmicpc.net/problem/1726
BFS문제 실수하기 쉬웠던점은 1 2 3 이라는 go 명령어를 실행할때 앞에 1지점에서 막다른길이 있었을경우 2 3 명령은 실행 할수 없는점.
두번째 실수하기 쉬웠던점은 visit[][]처리문과 함께 벽이랑 묶어버려서 go 1 명령어 실행할때 방문햇으면 2 3을 실행하지 못하게 해놔서 정답이 안나올수 있음.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.LinkedList;import java.util.Queue;import java.util.StringTokenizer;public class B1726 {static int N, M;static int map[][];static int dy[] = { 0, 0, 0, 1, -1 };static int dx[] = { 0, 1, -1, 0, 0 };static class DIR{int left;int right;public DIR(int left, int right) {super();this.left = left;this.right = right;}}static DIR dir[] = new DIR[5];static class ROBOT {int y;int x;int d;public ROBOT(int y, int x, int d) {super();this.y = y;this.x = x;this.d = d;}}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());dir[1] = new DIR(4 ,3);dir[2] = new DIR(3,4);dir[3] = new DIR(1,2);dir[4] = new DIR(2,1);map = new int[N + 1][M + 1];for (int i = 1; i <= N; i++) {st = new StringTokenizer(br.readLine());for (int j = 1; j <= M; j++) {map[i][j] = Integer.parseInt(st.nextToken());}}boolean visit[][][] = new boolean[N + 1][M + 1][5];st = new StringTokenizer(br.readLine());Queue<ROBOT> q = new LinkedList<ROBOT>();int y = Integer.parseInt(st.nextToken());int x = Integer.parseInt(st.nextToken());int d = Integer.parseInt(st.nextToken());visit[y][x][d] = true;q.add(new ROBOT(y, x, d));st = new StringTokenizer(br.readLine());int ey = Integer.parseInt(st.nextToken());int ex = Integer.parseInt(st.nextToken());int ed = Integer.parseInt(st.nextToken());int order = 0;int Answer = -1;while (!q.isEmpty()) {int size = q.size();for (int s = 0; s < size; s++) {ROBOT tmp = q.poll();if(tmp.y ==ey && tmp.x ==ex &&tmp.d ==ed) {Answer = order;break;}//좌y = tmp.y; x = tmp.x; d= dir[tmp.d].left;if(!visit[y][x][d]) {visit[y][x][d]= true;q.add(new ROBOT(y, x, d));}//우y = tmp.y; x = tmp.x; d= dir[tmp.d].right;if(!visit[y][x][d]) {visit[y][x][d]= true;q.add(new ROBOT(y, x, d));}//go1~3y = tmp.y; x = tmp.x; d= tmp.d;for(int i=0;i<3;i++) {y = y+dy[d]; x=x+dx[d];if(y>=1&&y<=N&&x>=1&&x<=M&&!visit[y][x][d]&&map[y][x]==0) {visit[y][x][d]= true;q.add(new ROBOT(y, x, d));}else if(y>=1&&y<=N&&x>=1&&x<=M&&visit[y][x][d]) {continue;}else {break;}}}if(Answer !=-1) break;order++;}System.out.println(Answer);}}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
반응형
'ProgramSoliving' 카테고리의 다른 글
백준 : 1194 (0) | 2020.02.29 |
---|---|
백준 : 1939 (0) | 2020.02.29 |
백준 : 12844 (0) | 2020.02.28 |
백준 : 1395 (JAVA) (0) | 2020.02.28 |
백준 : 109999 (JAVA) (0) | 2020.02.27 |