-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP-3.py
More file actions
78 lines (63 loc) · 2.16 KB
/
P-3.py
File metadata and controls
78 lines (63 loc) · 2.16 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
73
74
75
76
77
78
class ArrayStack:
def __init__(self,capacity):
self.capacity = capacity
self.array = [None]*self.capacity
self.top = -1
def isEmpty(self):
return self.top == -1
def isFull( self ):
return self.top == self.capacity-1
def push(self,e):
if not self.isFull():
self.top += 1
self.array[self.top] = e
else: pass
def pop (self):
if not self.isEmpty():
self.top -= 1
return self.array[self.top+1]
else: pass
def peek(self):
if not self.isEmpty():
return self.array[self.top]
else:pass
def isValidPos(x,y,map):
MAZE_SIZE = len(map)
if 0<= x < MAZE_SIZE and 0<= y < MAZE_SIZE:
if map[y][x] == '0' or map[y][x] == 'x':
return True
return False
def DFS(map):
print('DFS: ')
stack= ArrayStack(100)
stack.push((0,1))
move=0
while not stack.isEmpty():
here = stack.pop()
print(here,end='')
(x,y)=here
if(map[y][x]=='x'):
print(f'이동 거리={move}')
return True
else:
map[y][x] ='.'
move +=1
if isValidPos(x,y-1,map): stack.push((x,y-1))
if isValidPos(x,y+1,map): stack.push((x,y+1))
if isValidPos(x-1,y,map): stack.push((x-1,y))
if isValidPos(x+1,y,map): stack.push((x+1,y))
print(f'현재 스택:{stack}')
return False
map = [
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
['e', '0', '0', '0', '0', '0', '1', '0', '0', '1'],
['1', '0', '1', '1', '1', '0', '1', '1', '0', '1'],
['1', '0', '0', '0', '1', '0', '0', '0', '0', '1'],
['1', '0', '1', '0', '1', '1', '1', '1', '0', '1'],
['1', '0', '1', '0', '0', '0', '0', '1', '0', 'x'],
['1', '0', '1', '1', '1', '1', '0', '1', '1', '1'],
['1', '0', '0', '0', '0', '1', '0', '0', '0', '1'],
['1', '1', '1', '1', '0', '1', '1', '1', '0', '1'],
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']
]
DFS(map)