-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_simulation.cpp
105 lines (90 loc) · 2.43 KB
/
03_simulation.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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
int n,m;
int x,y,dir;
int cnt=1; // 방문한 점의 개수
pair<int,int> coordinates;
vector<pair<int,int>> direction;
int map[50][50]; // 지도
int check[50][50]; // 0인 곳만 갈 수 있음
int chkArray() { // 해당 방향 한 칸 앞으로 갈 수 있는지 확인
if (check[coordinates.first+direction[dir].first][coordinates.second+direction[dir].second] == 0 && map[coordinates.first+direction[dir].first][coordinates.second+direction[dir].second] == 0 ) {
return 1; // 감
} else {
return 0; // 못 감
}
}
int chkReverse() { // 해당 방향 한 칸 뒤로 갈 수 있는지 확인
if (map[coordinates.first-direction[dir].first][coordinates.second-direction[dir].second] == 1) {
return 0; // 못 감
} else {
return 1; // 감
}
}
void move() { // 현재 방향이 dir일 때 앞으로 가는 함수
int turnTime=0;
check[coordinates.first][coordinates.second]=1;
while (true) {
dir--;
if (dir<0) {
dir+=4;
}
if ( chkArray() ) {
coordinates.first += direction[dir].first;
coordinates.second += direction[dir].second;
check[coordinates.first][coordinates.second]=1;
cnt++;
turnTime=0;
} else {
turnTime++;
}
if (turnTime==4) {
if ( chkReverse() ) {
coordinates.first -= direction[dir].first;
coordinates.second -= direction[dir].second;
check[coordinates.first][coordinates.second]=1;
turnTime=0;
} else {
break;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
for (int i=0; i<50; i++) {
for (int j=0; j<50; j++) {
map[i][j] = 1;
check[i][j] = 1;
}
}
cin >> n >> m;
cin >> x >> y >> dir;
coordinates = make_pair(x,y);
direction.push_back(make_pair(1,0));
direction.push_back(make_pair(0,1));
direction.push_back(make_pair(-1,0));
direction.push_back(make_pair(0,-1));
int buff;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin >> buff;
map[i][j] = buff;
check[i][j] = buff;
}
}
move();
printf("%d\n",cnt);
return 0;
}
/*
4 4
1 1 0
1 1 1 1
1 0 0 1
1 1 0 1
1 1 1 1
*/