본문으로 바로가기

백준 17070번 : 파이프 옮기기 1 JAVA code

category 알고리즘 2020. 1. 28. 12:53


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
 
public class temp {
    private static class Node{
        int x,y,type;
        
        Node(int x,int y, int type){
            this.x = x;
            this.y = y;
            this.type = type;
        }
    }
    static int size, result;
    static int[][] map;
    static int[] dx = {0,1,1};
    static int[] dy = {1,0,1};
    
    static Queue<Node> q = new LinkedList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        size = Integer.parseInt(st.nextToken());
        map = new int[size][size];
        
        for(int i=0;i<size;i++) {
            st = new StringTokenizer(br.readLine());
            for(int j=0;j<size;j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        
        q.add(new Node(0,1,0));
        bfs();
        
        System.out.println(result);
    }
    
    public static void bfs() {
        
        while(!q.isEmpty()) {
            Node n = q.poll();
            
            if(n.x == (size-1&& n.y == (size-1)) {    // 목적지에 도달 할 경우 
                result++;
                continue;
            }
            
            
            for(int i=0;i<3;i++) {
                if(i==0 && n.type == 1) {    //가로 & 세로
                    continue;
                }else if(i==1 && n.type==0) {//세로 & 가로 
                    continue;
                }
                
                if(isPosible(n.x,n.y,i)) {
                    q.add(new Node(n.x+dx[i],n.y+dy[i],i));
                }
            }
        }
    }
    
    //true, false 
    public static boolean isPosible(int x,int y,int i) {
        int dxx = x + dx[i];
        int dyy = y + dy[i];
        
        if(dxx > size-1 | dyy > size-1) {    //사각형 범위를 벗어 날 경우 
            return false;
        }
        
        if(i==2) {    //대각 일 경우
            if(map[dxx-1][dyy] != 0 || map[dxx][dyy-1!= 0 || map[dxx][dyy] !=0) {
                return false;
            }
        }else {
            if(map[dxx][dyy] == 1) {
                return false;
            }    
        }
        return true;
    }
    
}
 
cs