-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_1952번.CPP
85 lines (65 loc) · 1.2 KB
/
BOJ_1952번.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
//달팽이 2 1952번
#include<iostream>
using namespace std;
#define MAX_SIZE 100
int map[MAX_SIZE][MAX_SIZE];
int visit[MAX_SIZE][MAX_SIZE];
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
int m, n, check, direction, ret;
int main(void) {
cin >> m >> n;
check = 1;
direction = 0;// 0 -> 오른쪽, 1 -> 아래, 2 -> 왼쪽, 3 -> 위
int x, y;
ret = x = y = 0;
visit[x][y] = 1;
while (check != m*n) {
if (direction == 0) { // 오른쪽
if (n <= y + 1 || visit[x][y+1] == 1) {
ret++;
direction = (direction + 1) % 4;
}
else {
y++;
visit[x][y] = 1;
check++;
}
}
else if (direction == 1) { //아래
if (m <= x + 1 || visit[x+1][y] == 1) {
direction = (direction + 1) % 4;
ret++;
}
else {
x++;
visit[x][y] = 1;
check++;
}
}
else if (direction == 2) { //왼쪽
if (y - 1 < 0 || visit[x][y-1] == 1) {
direction = (direction + 1) % 4;
ret++;
}
else {
y--;
visit[x][y] = 1;
check++;
}
}
else { //위
if (x - 1 < 0 || visit[x-1][y] == 1) {
direction = (direction + 1) % 4;
ret++;
}
else {
x--;
visit[x][y] = 1;
check++;
}
}
}
cout << ret;
return 0;
}