forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.py
More file actions
59 lines (53 loc) ยท 1.53 KB
/
4.py
File metadata and controls
59 lines (53 loc) ยท 1.53 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
# N, M์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅ๋ฐ๊ธฐ
n, m = map(int, input().split())
# ๋ฐฉ๋ฌธํ ์์น๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋งต์ ์์ฑํ์ฌ 0์ผ๋ก ์ด๊ธฐํ
d = [[0] * m for _ in range(n)]
# ํ์ฌ ์บ๋ฆญํฐ์ X ์ขํ, Y ์ขํ, ๋ฐฉํฅ์ ์
๋ ฅ๋ฐ๊ธฐ
x, y, direction = map(int, input().split())
d[x][y] = 1 # ํ์ฌ ์ขํ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
# ์ ์ฒด ๋งต ์ ๋ณด๋ฅผ ์
๋ ฅ๋ฐ๊ธฐ
array = []
for i in range(n):
array.append(list(map(int, input().split())))
# ๋ถ, ๋, ๋จ, ์ ๋ฐฉํฅ ์ ์
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
# ์ผ์ชฝ์ผ๋ก ํ์
def turn_left():
global direction
direction -= 1
if direction == -1:
direction = 3
# ์๋ฎฌ๋ ์ด์
์์
count = 1
turn_time = 0
while True:
# ์ผ์ชฝ์ผ๋ก ํ์
turn_left()
nx = x + dx[direction]
ny = y + dy[direction]
# ํ์ ํ ์ดํ ์ ๋ฉด์ ๊ฐ๋ณด์ง ์์ ์นธ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ์ด๋
if d[nx][ny] == 0 and array[nx][ny] == 0:
d[nx][ny] = 1
x = nx
y = ny
count += 1
turn_time = 0
continue
# ํ์ ํ ์ดํ ์ ๋ฉด์ ๊ฐ๋ณด์ง ์์ ์นธ์ด ์๊ฑฐ๋ ๋ฐ๋ค์ธ ๊ฒฝ์ฐ
else:
turn_time += 1
# ๋ค ๋ฐฉํฅ ๋ชจ๋ ๊ฐ ์ ์๋ ๊ฒฝ์ฐ
if turn_time == 4:
nx = x - dx[direction]
ny = y - dy[direction]
# ๋ค๋ก ๊ฐ ์ ์๋ค๋ฉด ์ด๋ํ๊ธฐ
if array[nx][ny] == 0:
x = nx
y = ny
# ๋ค๊ฐ ๋ฐ๋ค๋ก ๋งํ์๋ ๊ฒฝ์ฐ
else:
break
turn_time = 0
# ์ ๋ต ์ถ๋ ฅ
print(count)