#유형 : 시뮬레이션

# 예외처리만 잘 생각하면서 구현하면 난이도는 높지 않은데, 벽에 부딪히면 게임이 끝나는 부분이나 사과가 있을때 없을때 잘 구분하여 처리하면 된다.. 항상 느끼지만 시뮬레이션은 코드를 너무 못나게 짜는거 같다.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package bj;
 
 
public class p3190 {
    static int N,K,L;
    
    static int cnt=0;
    static int dir=1;
    
    static int moveX[] = {0,1,0,-1};
    static int moveY[] = {-1,0,1,0};
    
    static int apple[][];
    static ArrayList<Point> arrList = new ArrayList<>();
    static ArrayList<Info> info = new ArrayList<>();
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        apple = new int[N][N];
        StringTokenizer st;
        K = Integer.parseInt(br.readLine());
        
        for(int k=0; k<K; k++) {
             st = new StringTokenizer(br.readLine());
             int y = Integer.parseInt(st.nextToken());
             int x = Integer.parseInt(st.nextToken());
             apple[y-1][x-1= 1;
        }
        
        L = Integer.parseInt(br.readLine());
        for(int l=0; l<L; l++) {
            st = new StringTokenizer(br.readLine());
            int time = Integer.parseInt(st.nextToken());
            char dir = st.nextToken().charAt(0);
            info.add(new Info(time,dir));
        }
        solve(0,0);
    }
    
    public static void solve(int i, int j) {
        
        while(true) {
            for(int k=0; k<info.size(); k++) {
                int next = info.get(k).time;
                while(cnt<next) {
                    arrList.add(new Point(j,i));
                    int newY = i + moveY[dir];
                    int newX = j + moveX[dir];
                    if(!(0<=newY && newY<&& 0<=newX && newX<N)) {
                        System.out.println(cnt+1);
                        return;
                    }
                    
                    if(check(newY,newX)) {
                        System.out.println(cnt+1);
                        return;
                    }else {
                        if(apple[newY][newX]!=1) {
                            arrList.remove(0);
                        }else if(apple[newY][newX]==1)
                            apple[newY][newX]=0;
                        
                        i = newY;
                        j = newX;
                        cnt++;
                    }
                }
                char d = info.get(k).dir;
                if(d == 'L') {
                    if(dir==0)
                        dir=3;
                    else
                        dir--;
                    dir%=4;
                }else if(d == 'D') {
                    dir++;
                    dir%=4;
                }
                
            }
            ///방향전환끝나고나서 돌아야함
            while(!check(i,j)) {
                int newX = j + moveX[dir];
                int newY = i + moveY[dir];
                if(!(0<=newX && newX<&& 0<=newY && newY<N)) {
                    System.out.println(cnt+1);
                    return;
                }
                if(check(newY,newX)) {
                    System.out.println(cnt+1);
                    return;
                }else {
                    if(apple[newY][newX]!=1 && arrList.size()>0) {
                        arrList.remove(0);
                    }
                    i = newY;
                    j = newX;
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
    }
    
    public static boolean check(int i, int j) {
        for(int idx=0; idx<arrList.size(); idx++
            if(arrList.get(idx).x == j && arrList.get(idx).y == i)
                return true;
        return false;
    }
    
    public static class Info{
        int time;
        char dir;
        public Info(int t, char d) {
            this.time=t;
            this.dir=d;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

+ Recent posts