-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19237.cpp
193 lines (171 loc) · 4.98 KB
/
19237.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 19237. 어른 상어
// 2020.10.17
// 시뮬레이션
#include<iostream>
using namespace std;
int dx[5] = { -1, -1, 1, 0, 0 };
int dy[5] = { -1, 0, 0, -1, 1 };
pair<int, int> smell[21][21]; // 상어번호, 냄새남은시간
struct shark
{
int dir;
int x;
int y;
int op[5][5]; // 각 방향별 우선순위를 저장
bool move = false; // 이동했는지 체크 하는 변수
bool isAlive = true; // 살아있는지 체크 하는 변수
};
shark sharks[401];
int main()
{
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> smell[i][j].first;
if (smell[i][j].first != 0)
{
smell[i][j].second = k;
sharks[smell[i][j].first].x = i;
sharks[smell[i][j].first].y = j;
}
}
}
for (int i = 1; i <= m; i++)
{
cin >> sharks[i].dir;
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= 4; j++)
{
for (int k = 1; k <= 4; k++)
{
cin >> sharks[i].op[j][k];
}
}
}
bool flag = true;
int ans = 0;
while (1)
{
ans++;
if (ans == 1001)
{
ans = -1;
break;
}
// 상어의 이동 유무를 초기화
for (int i = 1; i <= m; i++)
{
sharks[i].move = false;
}
// 모든 상어에 대해 조사
for (int num = 1; num <= m; num++)
{
if (sharks[num].isAlive == false)
{
continue;
}
for (int j = 1; j <= 4; j++)
{
int newDir = sharks[num].op[sharks[num].dir][j];
int xx = sharks[num].x + dx[newDir];
int yy = sharks[num].y + dy[newDir];
// 이동할 수 없는 칸
if (xx < 0 || yy < 0 || xx >= n || yy >= n)
{
continue;
}
// 이동 가능한 칸
if (smell[xx][yy].first == 0)
{
smell[xx][yy].first = num;
sharks[num].x = xx;
sharks[num].y = yy;
sharks[num].move = true;
sharks[num].dir = newDir;
break;
}
// 이동하는곳에 상어가 있으나 냄새가 아직 베이지 않은경우(둘이 같은 곳을 이동하는 경우)
else if (smell[xx][yy].first != 0 && smell[xx][yy].second == 0)
{
smell[xx][yy].first = min(smell[xx][yy].first, num);
sharks[max(smell[xx][yy].first, num)].isAlive = false;
sharks[num].x = xx;
sharks[num].y = yy;
sharks[num].move = true;
sharks[num].dir = newDir;
break;
}
else
{
continue;
}
}
// 자신의 냄새로 다시 이동
if (sharks[num].move == false && sharks[num].isAlive)
{
for (int j = 1; j <= 4; j++)
{
int newDir = sharks[num].op[sharks[num].dir][j];
int xx = sharks[num].x + dx[newDir];
int yy = sharks[num].y + dy[newDir];
// 이동할 수 없는 칸
if (xx < 0 || yy < 0 || xx >= n || yy >= n)
{
continue;
}
if (smell[xx][yy].first == num)
{
sharks[num].x = xx;
sharks[num].y = yy;
sharks[num].dir = newDir;
smell[xx][yy].second = k;
break;
}
}
}
}
// 모든 맵의 냄새를 지워줌
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (smell[i][j].second != 0)
{
smell[i][j].second--;
if (smell[i][j].second == 0)
{
smell[i][j].first = 0;
}
}
}
}
// 모든 상어가 새로 이동한곳에 냄새를 새로 추가함
for (int i = 1; i <= m; i++)
{
if (sharks[i].isAlive)
{
smell[sharks[i].x][sharks[i].y].second = k;
}
}
// 상어 1만 남았는지 체크
bool isEnd = true;
for (int i = 2; i <= m; i++)
{
if (sharks[i].isAlive)
{
isEnd = false;
}
}
if (isEnd)
{
break;
}
}
cout << ans << endl;
return 0;
}