본문 바로가기

알고리즘/시뮬레이션

3190번 뱀.

하..... 변수형은 int로 써야하는걸 char로 써서 1시간을 날렸다. 오류 찾느라..... 디버깅 모드 할때 어쩐지 100 'd' 일케 되있더라 뭐지 했는데 결국 자료형의 값을 넘어서서 -127로 변해서 그런거였다. ..... 자료형 잘보자...... 그리고 코드 최적화 하자.

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std;
 
typedef struct {
    int time;
    char dir;
}time_dir_info;
 
typedef struct {
    int apple_po_x;
    int apple_po_y;
}apple_po_info;
 
typedef struct {
    int position_x;
    int position_y;
    int dir;
}snake;
 
enum direction{
    UP = 0,
    DOWN,
    LEFT,
    RIGHT
};
 
typedef struct {
    int pos_x;
    int pos_y;
}snake_length_detail;
 
int N, apple_n;
int map[100][100];
int apple_po_x, apple_po_y;
int snake_change_n, time, total_time;
char dir, cur_dir;
int cur_time;
queue<time_dir_info> q;
vector<apple_po_info> v;
queue<snake_length_detail> q_s_d;
int dirx[4] = {0,0,-1, 1};
int diry[4] = {-1,1,0,0};
snake s;
 
 
void Input() {
    scanf("%d %d", &N, &apple_n);
    for (int i = 0; i < apple_n; i++) {
        scanf("%d %d", &apple_po_y, &apple_po_x);
        v.push_back({ apple_po_x-1, apple_po_y-1 });
    }
    scanf("%d", &snake_change_n);
    for (int i = 0; i < snake_change_n; i++) {
        scanf("%d %c", &time, &dir);
        q.push({ time, dir });
    }
    s.position_x = 0; s.position_y = 0; s.dir = RIGHT;
    map[0][0] = 1;
    q_s_d.push({ s.position_x,s.position_y });
}
 
bool apple_check(int position_x, int position_y) {
    vector<apple_po_info>::iterator iter;
    for (iter = v.begin(); iter != v.end(); ++iter) {
        if ((*iter).apple_po_x == position_x && (*iter).apple_po_y == position_y) {
            v.erase(iter);
            return true;
        }
    }
    return false;
}
 
void modify_position(bool flag) {
    int pos_x = q_s_d.front().pos_x;
    int pos_y = q_s_d.front().pos_y; q_s_d.pop();
    map[pos_y][pos_x] = 0;
}
 
 
bool modify_length(int dir) {
    if (map[s.position_y + diry[dir]][s.position_x + dirx[dir]] == 1) return false;
    if (!apple_check(s.position_x+dirx[dir], s.position_y+diry[dir])) {
        modify_position(true);
    }
    s.position_x += dirx[dir];
    s.position_y += diry[dir];
    map[s.position_y][s.position_x] = 1;
    q_s_d.push({ s.position_x, s.position_y });
    return true;
}
 
void change_dir(int dir) {
    switch (s.dir)
    {
    case UP:
        if (dir == 'D') s.dir = RIGHT;
        else s.dir = LEFT;
        break;
    case DOWN:
        if (dir == 'D') s.dir = LEFT;
        else s.dir = RIGHT;
        break;
    case LEFT:
        if (dir == 'D') s.dir = UP;
        else s.dir = DOWN;
        break;
    case RIGHT:
        if (dir == 'D') s.dir = DOWN;
        else s.dir = UP;
        break;
    default:
        break;
    }
}
 
void time_dir_check(int total_time) {
    if (cur_time == total_time) {
        change_dir(cur_dir);
        if (!q.empty()) {
            cur_time = q.front().time;
            cur_dir = q.front().dir; q.pop();
        }
    }
}
 
 
void Calc() {
    if (q.size() > 0) {
        cur_time = q.front().time;
        cur_dir = q.front().dir; q.pop();
    }
    while (true) {
         
        if (s.position_x < 0 || s.position_y < 0 || s.position_x >= N || s.position_y >= N) return;
 
 
        total_time++;
        if (!modify_length(s.dir)) return;
 
        time_dir_check(total_time);
    }
}
 
 
void Solve() {
    Input();
    Calc();
    printf("%d", total_time);
}
 
int main(void) {
    Solve();
    return 0;
}
</apple_po_info></snake_length_detail></apple_po_info></time_dir_info></iostream></queue></cstdlib></cstdio>