ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준][자바][파이프옮기기1]17070
    코딩/알고리즘 2019. 11. 1. 21:51

    예전에 다른 분들 푼거 보고 참조해서 풀었다가

     

    이해가 안되서 오늘 다시 새로 풀어봤다

     

    개인적으로는 직관적이라고 생각한다. 코드량이랑 효율적이지는 못하지만..

     

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    
    public class Main_backjoon_17070_파이프옮기기1 {
    	static int[][] MAP;
    	static int N;
    	static int[] dy = {0,1,1};//우측, 하단 ,대각선
    	static int[] dx = {1,0,1}; 
    	static int result = 0;
    	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());
    		MAP = new int[N][N];
    		for(int i = 0 ; i < N; i++) {
    			 st = new StringTokenizer(br.readLine(), " ");
    			 int index = 0;
    			 while(st.hasMoreTokens()) {
    				 MAP[i][index] = Integer.parseInt(st.nextToken());
    				 index += 1;
    			 }
    		}
    
    		dfsme(0,1,0);
    		System.out.println(result);
    	}
    	public static void dfsme(int y, int x, int dir) {
    		//조건 
    		if(y == N - 1 && x == N - 1) {
    			result ++;
    			return;
    		}
    		if( dir == 0 ) { //가로 일 경우 
    			for(int d = 0 ; d < 3; d++) {
    				if(d == 1) { //세로부분 날려 
    					continue;
    				}
    				int yy = y + dy[d];
    				int xx = x + dx[d];
    				if(!isRange(yy, xx)) {
    					continue;
    				}
    				if(!isCheck(y, x, d)) {
    					continue;
    				}
    				dfsme(yy, xx, d);
    			}
    		}
    		else if( dir == 1) { //세로 일 경우  
    			for(int d = 1 ; d < 3; d++) {
    				int yy = y + dy[d];
    				int xx = x + dx[d];
    				if(!isRange(yy, xx)) {
    					continue;
    				}
    				if(!isCheck(y, x, d)) {
    					continue;
    				}
    				dfsme(yy, xx, d);
    			}
    		}
    		else if( dir == 2) { //대각선일 경우 
    			for(int d = 0 ; d < 3; d++) {
    				int yy = y + dy[d];
    				int xx = x + dx[d];
    				if(!isRange(yy, xx)) {
    					continue;
    				}
    				if(!isCheck(y, x, d)) {
    					continue;
    				}
    				dfsme(yy, xx, d);
    			}
    		}
    		
    
    	}
    	public static boolean isCheck(int yy, int xx, int dir ) {
    		if(dir == 0) { //가로 
    			if(MAP[yy][xx+1] == 0) {
    				return true;
    			}
    		}
    		if(dir == 1) { //세로 
    			if(MAP[yy+1][xx] == 0) {
    				return true;
    			}
    		}
    		if(dir == 2) { // 가로 세로 대각선 이 전부 0 이면 
    			if( MAP[yy][xx+1] == 0 && MAP[yy+1][xx] == 0 && MAP[yy+1][xx+1] == 0) {
    				return true;
    			}
    		}
    		return false;
    	}
    
    	public static boolean isRange(int yy, int xx) {
    		if( yy >= 0 && yy < N && xx >= 0 && xx < N   ) {
    			return true;
    		}else {
    			return false;
    		}
    	}
    
    	
    }
    
    

     

    댓글

Designed by Tistory.