forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.cpp
More file actions
72 lines (64 loc) ยท 1.88 KB
/
4.cpp
File metadata and controls
72 lines (64 loc) ยท 1.88 KB
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
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, direction;
// ๋ฐฉ๋ฌธํ ์์น๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋งต์ ์์ฑํ์ฌ 0์ผ๋ก ์ด๊ธฐํ
int d[50][50];
// ์ ์ฒด ๋งต ์ ๋ณด
int arr[50][50];
// ๋ถ, ๋, ๋จ, ์ ๋ฐฉํฅ ์ ์
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
// ์ผ์ชฝ์ผ๋ก ํ์
void turn_left() {
direction -= 1;
if (direction == -1) direction = 3;
}
int main(void) {
// N, M์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅ๋ฐ๊ธฐ
cin >> n >> m;
// ํ์ฌ ์บ๋ฆญํฐ์ X ์ขํ, Y ์ขํ, ๋ฐฉํฅ์ ์
๋ ฅ๋ฐ๊ธฐ
cin >> x >> y >> direction;
d[x][y] = 1; // ํ์ฌ ์ขํ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
// ์ ์ฒด ๋งต ์ ๋ณด๋ฅผ ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x;
cin >> x;
arr[i][j] = x;
}
}
// ์๋ฎฌ๋ ์ด์
์์
int cnt = 1;
int turn_time = 0;
while (true) {
// ์ผ์ชฝ์ผ๋ก ํ์
turn_left();
int nx = x + dx[direction];
int ny = y + dy[direction];
// ํ์ ํ ์ดํ ์ ๋ฉด์ ๊ฐ๋ณด์ง ์์ ์นธ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ์ด๋
if (d[nx][ny] == 0 && arr[nx][ny] == 0) {
d[nx][ny] = 1;
x = nx;
y = ny;
cnt += 1;
turn_time = 0;
continue;
}
// ํ์ ํ ์ดํ ์ ๋ฉด์ ๊ฐ๋ณด์ง ์์ ์นธ์ด ์๊ฑฐ๋ ๋ฐ๋ค์ธ ๊ฒฝ์ฐ
else turn_time += 1;
// ๋ค ๋ฐฉํฅ ๋ชจ๋ ๊ฐ ์ ์๋ ๊ฒฝ์ฐ
if (turn_time == 4) {
nx = x - dx[direction];
ny = y - dy[direction];
// ๋ค๋ก ๊ฐ ์ ์๋ค๋ฉด ์ด๋ํ๊ธฐ
if (arr[nx][ny] == 0) {
x = nx;
y = ny;
}
// ๋ค๊ฐ ๋ฐ๋ค๋ก ๋งํ์๋ ๊ฒฝ์ฐ
else break;
turn_time = 0;
}
}
cout << cnt << '\n';
}