알고리즘/문제 풀이

[Java/자바 SWEA 1953] 탈주범 검거

광규니 2022. 4. 11. 22:33
반응형

풀이

import java.io.*;
import java.util.*;
// 뱡항을 중심으로 1번은 4방 돌리고
// 2~7 은 들어온 방향으로 돌려야할거같은데
// 상 우 하 좌
public class Solution {
	static int n,m,r,c,l, time;
	static int[][] map;
	static boolean[][] check;
	static Queue<int []> queue= new LinkedList<int[]>();
	
	static int[] dx= {-1,0,1,0};
	static int[] dy= {0,1,0,-1};
	
	//hole에 위치를 기준으로 들어온 방향이 갈 수 있는 곳으로 설정하자.
	// 들어오는 방향마다 가능한거 -1
	// 상: 1,2,5,6
	// 우: 1,3,6,7
	// 하: 1,2,4,7
	// 좌: 1,3,4,5
	
	// 1번을 빼곤 들어온 방향을 따져줫 다음 nx,ny를 따져주자 
	static int[][] dx_hole = {{0,1,0,-1},{-1,0,1,0},{0,0,0,0},{0,0,0,-1}, {0,0,0,1}, {0,1,0,0}, {0,-1,0,0}};
	static int[][] dy_hole = {{1,0,-1,0}, {0,0,0,0}, {0,1,0,-1}, {0,0,1,0}, {1,0,0,0}, {-1,0,0,0},{0,0,-1,0}};
	
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int tc= Integer.parseInt(br.readLine());
		StringTokenizer st;
		label : for(int t=1 ; t<=tc; t++) {
			st= new StringTokenizer(br.readLine()," ");
			n=Integer.parseInt(st.nextToken());
			m=Integer.parseInt(st.nextToken());
			r=Integer.parseInt(st.nextToken());
			c=Integer.parseInt(st.nextToken());
			l=Integer.parseInt(st.nextToken());
			
			map = new int[n][m];
			check = new boolean[n][m];
			check[r][c]=true;
			if(l==1 || map[r][c]==0) {
				if(map[r][c]!=0) System.out.println("#"+t+" "+1);
				else System.out.println("#"+t+" "+0);
				continue label;
			}
			time=2;
			for(int i=0;i<4;i++) {
				if(dx_hole[map[r][c]-1][i]!=0 || dy_hole[map[r][c]-1][i] != 0) {
					int nx= r+dx_hole[map[r][c]-1][i];
					int ny= c+dy_hole[map[r][c]-1][i];
					if(0<=nx && nx<n && 0<=ny && ny<m && map[nx][ny]!=0) {
						if(map[nx][ny]!=0) queue.offer(new int[] {nx,ny,i});
						
					}
				}
			}
			
			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());
				}
			}
			
			bfs();
			int total=0;
			for(int i=0;i<n;i++) {
				for(int j=0; j<m; j++) {
					if(check[i][j]==true) total+=1;
				}
			}
			System.out.println("#"+t+" "+total);
		}
		
		
		
	}

	private static void bfs() {
		
		pass : while(! queue.isEmpty()) {
			
			int cnt=queue.size();
			for(int a=0 ; a<cnt;a++) {
				int[] cur=queue.poll();
				
				int bx=cur[0];
				int by=cur[1];
				//이전 방향 
				int dir=cur[2];
				
				//check 값 들어온 값 유효한지 따져주기
				int cur_num=map[bx][by];
				if(cur_num==1) {
					check[bx][by]=true;
					for(int i=0; i<4;i++) {
						int nx=bx+dx[i];
						int ny=by+dy[i];
						if(0<=nx && nx<n && 0<=ny && ny<m && map[nx][ny]!=0) {
							queue.offer(new int[] {nx,ny,i});
						}
					}
				}
				else {
					//반대 방향이
					int bdx=dx_hole[(dir+2)%4][(dir+2)%4];
					int bdy=dy_hole[(dir+2)%4][(dir+2)%4];
					if(map[bx+bdx][by+bdy]!=0) {
						check[bx][by]=true;
					}
					int nx = bx + dx_hole[cur_num][dir];
					int ny = by + dy_hole[cur_num][dir];
					
					if(0<=nx && nx<n && 0<=ny && ny<m && map[nx][ny]!=0) {
						for(int i=0;i<4;i++) {
							if(i!=dir)
								if(dx_hole[cur_num][i]!=0 || dy_hole[cur_num][i] !=0) queue.offer(new int[] {nx,ny,i});
						}
					}
					
				}
				
			}
			time+=1;
			if(time==l) continue pass;
		}
	}
}
반응형