-
[백준] 14503 로봇 청소기ALGORITHM/BOJ 2020. 11. 3. 23:25
2020-11-03
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main14503 { public static int N, M, count, arr[][]; public static Queue<int[]> q; public static int dx[] = {-1, 0, 1, 0}; public static int dy[] = {0, 1, 0, -1}; // 북 동 남 서 public static void solve() { loop:while(!q.isEmpty()) { int tmp[] = q.poll(); int sx = tmp[0]; int sy = tmp[1]; int sd = tmp[2]; int nd = sd; int tmpCnt = 0; for(int i = 0; i < 4; i++) { nd = ((nd-1) == -1) ? 3 : (nd-1); int nx = sx + dx[nd]; int ny = sy + dy[nd]; if (nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 2 || arr[nx][ny] == 1) { tmpCnt++; continue; } if (arr[nx][ny] == 0) { q.add(new int[] {nx, ny, nd}); arr[nx][ny] = 2; count++; break; } } if (tmpCnt == 4) { int back = (sd < 2) ? (sd + 2) : (sd - 2); int nx = sx + dx[back]; int ny = sy + dy[back]; if(nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 1) break loop; else { q.add(new int[] {nx, ny, sd}); } } } } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); count = 0; arr = new int[N][M]; q = new LinkedList<int[]>(); st = new StringTokenizer(bf.readLine()); int x = Integer.parseInt(st.nextToken()); // 시작 위치 int y = Integer.parseInt(st.nextToken()); int d = Integer.parseInt(st.nextToken()); for(int i = 0; i < N; i++) { st = new StringTokenizer(bf.readLine()); for(int j = 0; j < M; j++) { arr[i][j] = Integer.parseInt(st.nextToken()); } } q.add(new int[] {x, y, d}); arr[x][y] = 2; count++; solve(); System.out.println(count); } }
입력 조건 중에 지도의 첫 행, 마지막 행, 첫 열, 마지막 열에 있는 모든 칸은 벽이다. 벽이라는 조건을 제대로 확인을 안 해서 예제 2번하고 답이 안 맞았었다.
if (nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 2 || arr[nx][ny] == 1) { tmpCnt++; continue; }
2019-10-14
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { public static int N, M, arr[][], clean; public static Queue<int[]> q; public static int dx[] = {-1, 0, 1, 0}; public static int dy[] = {0, 1, 0, -1}; public static void solve() { loop:while(!q.isEmpty()) { int size = q.size(); while(size > 0) { int tmp[] = q.poll(); int x = tmp[0]; int y = tmp[1]; int dir = tmp[2]; int d = dir; boolean check = false; for(int i = 0; i < 4; i++) { if(d == 0) d = 3; else d-=1; int nx = x + dx[d]; int ny = y + dy[d]; if(nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 1 || arr[nx][ny] == 2) continue; else if(arr[nx][ny] == 0 /*&& !vtd[nx][ny]*/) { arr[nx][ny] = 2; q.add(new int[] {nx, ny, d}); clean++; check = true; break; } } if(!check) { int change = 0; if(dir == 0) change = 2; else if(dir == 1) change = 3; else if(dir == 2) change = 0; else if(dir == 3) change = 1; int nx = x + dx[change]; int ny = y + dy[change]; if(nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 1) break loop; else { q.add(new int[] {nx, ny, dir}); } } size--; } } } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); arr = new int[N][M]; q = new LinkedList<int[]>(); st = new StringTokenizer(bf.readLine()); int r = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); int d = Integer.parseInt(st.nextToken()); q.add(new int[] {r, c, d}); for(int i = 0; i < N; i++) { st = new StringTokenizer(bf.readLine()); for(int j = 0; j < M; j++) { arr[i][j] = Integer.parseInt(st.nextToken()); } } clean = 0; if(arr[r][c] == 0) { arr[r][c] = 2; clean++; } solve(); System.out.println(clean); } }
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 1149 RGB거리 (0) 2020.11.07 [백준] 2606 바이러스 (0) 2020.11.06 [백준] 13300 방 배정 (0) 2020.11.06 [백준] 9095 1, 2, 3 더하기 (0) 2020.11.02 [백준] 2638 치즈 (0) 2020.10.29