ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1953. [모의 SW 역량테스트] 탈주범 검거
    카테고리 없음 2019. 4. 25. 23:27

    ..딱봐도 뭔가 분기를 세울게 많아보이는데

     bfs 문제 살짝 응용이라 풀고싶어서 풀었다

    이런 문제는 문제에서 하란대로만 풀면된다.

    근데 다만... 자칫하면 미궁속으로 빠지게된다 ㅠㅠㅠㅠ...

    문제를 이렇게 저렇게 푸느라 이것도 쓸모없는 코드가 꽤 많다.. ㅎ.

     

     

    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.StringTokenizer;
    class Pipe{
    	int x;
    	int y;
    	int number; 
    	int up;
    	int down;
    	int left;
    	int right;
    	Pipe(int y, int x, int number ){
    		this.y = y;
    		this.x = x; 
    		this.number = number;
    	}
    	Pipe(int y, int x, int up ,int down, int left, int right){
    		this.y = y;
    		this.x = x;
    		this.up = up;
    		this.down = down;
    		this.left = left;
    		this.right = right;
    	}
    }
    public class SwExpert_탈주범검거 {
    	static int[] possibleUp = {1,2,5,6};
    	static int[] possibleDown = {1,2,4,7};
    	static int[] possibleLeft = {1,3,4,5};
    	static int[] possibleRight = {1,3,6,7};
    	static int[] dy = {-1,1,0,0};
    	static int[] dx = {0,0,-1,1};
    	static int N,M,R,C,L;
    	static int TESTCASE;
    	static int MAP[][] = new int[101][101];
    	static boolean visited[][] = new boolean[101][101];
    	static Queue q = new LinkedList();
    //5              
    //5 6 2 1 3      
    //0 0 5 3 6 0
    //0 0 2 0 2 0
    //3 3 1 3 7 0
    //0 0 0 0 0 0
    //0 0 0 0 0 0
    	public static void main(String[] args) throws IOException {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
    		TESTCASE = Integer.parseInt(st.nextToken());
    		for(int t = 0 ; t < TESTCASE; 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());
    		
    			for(int i = 0; i < N ; i++) {
    				st = new StringTokenizer(br.readLine(), " ");
    				int idx = 0 ;
    				while(st.hasMoreTokens()) {
    					MAP[i][idx]= Integer.parseInt(st.nextToken());
    					idx++;
    				}
    			}
    			solve(t);
    			MAP = new int[101][101];
    			visited = new boolean[101][101];
    			q.clear();
    		}
    		//printMap();
    	
    		//visitedMap();
    	}
    	public static void printMap() {
    		for(int i = 0; i< N; i++){
    			for(int j = 0; j < M; j++){
    				System.out.print(MAP[i][j] + " ");
    			}
    			System.out.println();
    		}
    	}
    	public static void countMap() {
    		int sum = 0;
    		for(int i = 0; i< N; i++){
    			for(int j = 0; j < M; j++){
    				if(visited[i][j] == true) {
    					sum +=1;
    				}
    			}
    		}
    		System.out.println(sum);
    	}
    	public static void visitedMap() {
    		for(int i = 0; i< N; i++){
    			for(int j = 0; j < M; j++){
    				if(visited[i][j] == true) {
    					System.out.print(1 + " ");
    				}
    				else if(visited[i][j] == false) {
    					System.out.print(0 + " ");
    				}
    			}
    			System.out.println();
    		}
    	}
    	public static void solve(int t) {
    		q.add(new Pipe(R, C, MAP[R][C]));
    		visited[R][C] = true;
    		int count = 1 ;
    		int time = 0;
    		while(!q.isEmpty()) {
    			if( time == L-1) {
    				break;
    			}
    			int qSize = q.size();
    			for(int s = 0; s < qSize; s++) {
    				Pipe p = q.poll();
    				int x = p.x;
    				int y = p.y;
    				int num = p.number;
    				for(int d = 0 ; d <4; d++) { //상하좌우 검색 
    					if(!judgeDir(num , d)) {
    						continue;
    					}
    					
    					int yy = y + dy[d];
    					int xx = x + dx[d];
    					if(yy >= 0 && yy < N && xx >=0 && xx < M) {
    						if( MAP[yy][xx] == 0 ) continue; // 0이면 안대고 
    						
    						if(judgeMove(d, MAP[yy][xx]) &&  !visited[yy][xx]) {
    							visited[yy][xx] = true;
    							q.add(new Pipe(yy, xx, MAP[yy][xx]));
    							count +=1;
    						}
    					}
    				}
    			}
    			time +=1;
    		}
    		System.out.println("#"+(t+1)+" " +count);
    		//countMap();
    	}
    	//해당
    	public static boolean judgeMove(int dir, int nextNumber) {
    		if ( dir == 0) { //상일떄 
    			for(int i = 0 ; i< possibleUp.length; i++) {
    				if( nextNumber == possibleUp[i] ) {
    					return true;
    				}
    			}
    			return false;
    		}
    		if ( dir == 1) {
    			for(int i = 0 ; i< possibleDown.length; i++) {
    				if( nextNumber == possibleDown[i] ) {
    					return true;
    				}
    			}
    			return false;
    		}
    		if ( dir == 2) {
    			for(int i = 0 ; i< possibleLeft.length; i++) {
    				if( nextNumber == possibleLeft[i] ) {
    					return true;
    				}
    			}
    			return false;
    		}
    		if( dir == 3 ){
    			for(int i = 0 ; i< possibleRight.length; i++) {
    				if( nextNumber == possibleRight[i] ) {
    					return true;
    				}
    			}
    			return false;
    		}
    		return false;
    	}
    	//번호에 따라 이동 가능한 방향 판단
    	public static boolean judgeDir(int number, int dir) {
    		if ( number == 1) {
    			if(dir == 0) {
    				return true;
    			}
    			if(dir == 1) {
    				return true;
    			}
    			if(dir == 2) {
    				return true;
    			}
    			if(dir == 3) {
    				return true;
    			}
    			return false;
    		}
    		if ( number == 2) {
    			//상 하 
    			if(dir == 0 || dir == 1)  {
    				return true;
    			}
    			return false;
    		}
    		if ( number == 3) {
    			//좌측 우측
    			if(dir == 2 || dir == 3)  {
    				return true;
    			}
    			return false;	
    		}
    		if ( number == 4) {
    			//위 오른쪽
    			if(dir == 0 || dir == 3)  {
    				return true;
    			}
    			return false;
    		}
    		if ( number == 5) {
    			//하 우
    			if(dir == 1 || dir == 3)  {
    				return true;
    			}
    			return false;
    		}
    		if ( number == 6) {
    			//하 좌
    			if(dir == 1 || dir == 2)  {
    				return true;
    			}
    			return false;
    		}
    		if ( number == 7) {
    			//상 좌
    			if(dir == 0 || dir == 2)  {
    				return true;
    			}
    			return false;
    		}
    		
    		return false;
    		
    	}
    }
    
    

    댓글

Designed by Tistory.