코드굽는 타자기
SWEA[1953] - 탈주범 검거 본문
링크
SWEA[1953]
문제설명
- BFS+노가다 시뮬레이션
문제풀이
- BFS+노가다 시뮬레이션
문제코드
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Solution {
public static int[][] dir = {
{-1,0},
{1,0},
{0,-1},
{0,1}
};
public static int[][] map;
public static boolean[][] visit;
public static int N;
public static int M;
public static int R;
public static int C;
public static int L;
public static void main(String args[]) throws Exception
{
//System.setIn(new FileInputStream("res/swea/1953.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for(int test_case = 1; test_case <= T; test_case++)
{
int ans=0;
st = new StringTokenizer(br.readLine());
N=Integer.parseInt(st.nextToken());
M=Integer.parseInt(st.nextToken());
map = new int[N][M];
visit = new boolean[N][M];
R=Integer.parseInt(st.nextToken());
C=Integer.parseInt(st.nextToken());
L=Integer.parseInt(st.nextToken());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
LinkedList<int[]> q = new LinkedList<>();
int time=1;
q.add(new int[] {R,C});
visit[R][C]=true;
int nr=0;
int nc=0;
int[] now;
int qsize=0;
while(!q.isEmpty()) {
if(time==L)break;
qsize = q.size();
for (int c = 0; c < qsize; c++) {
now = q.poll();
for (int i = 0; i < 4; i++) {
nr = now[0] + dir[i][0];
nc = now[1] + dir[i][1];
if(nr>=0 && nr<N && nc>=0 && nc<M && map[nr][nc]!=0 &&!visit[nr][nc]) { //경계 탐색
switch (map[now[0]][now[1]]) {
case 1:
switch (i) {
case 0: //상
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==5 || map[nr][nc]==6) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 1: //하
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==4 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 2: //좌
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==4 || map[nr][nc]==5) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 3: //우
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==6 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
default:
break;
}
break;
case 2:
switch (i) {
case 0: //상
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==5 || map[nr][nc]==6) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 1: //하
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==4 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
default:
break;
}
break;
case 3:
switch (i) {
case 2: //좌
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==4 || map[nr][nc]==5) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 3: //우
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==6 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
default:
break;
}
break;
case 4:
switch (i) {
case 0: //상
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==5 || map[nr][nc]==6) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 3: //우
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==6 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
default:
break;
}
break;
case 5:
switch (i) {
case 1: //하
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==4 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 3: //우
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==6 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
default:
break;
}
break;
case 6:
switch (i) {
case 1: //하
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==4 || map[nr][nc]==7) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 2: //좌
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==4 || map[nr][nc]==5) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
default:
break;
}
break;
case 7:
switch (i) {
case 0: //상
if(map[nr][nc]==1 || map[nr][nc]==2 || map[nr][nc]==5 || map[nr][nc]==6) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
case 2: //좌
if(map[nr][nc]==1 || map[nr][nc]==3 || map[nr][nc]==4 || map[nr][nc]==5) {
q.add(new int[] {nr,nc});
visit[nr][nc]=true;
}
break;
default:
break;
}
break;
default:
break;
}
}
}//dir for end
}//q cycle for end
time++;
}//q while end
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if(visit[i][j])ans++;
}
}
System.out.println("#"+test_case+" "+ans);
}
}
}
아쉬운점
- 노가다함
잘한점
- Switch 써봤음
- 다행히 한번에 통과함
- BFS공부
'알고리즘 > 깊이&너비 우선 탐색(DFS&BFS)' 카테고리의 다른 글
SWEA[1861] - 정사각형 방[D4] (0) | 2020.02.13 |
---|---|
SWEA[1949] - 등산로 조성 (0) | 2020.02.11 |
Jungol[1462] - 보물섬 (0) | 2020.02.11 |
Jungol[1661] - 미로 탈출 로봇 (0) | 2020.02.10 |
Baekjoon[2667] - 단지번호붙이기 (0) | 2020.02.10 |
Comments