Skip to content

Commit abc6ae8

Browse files
committed
10
1 parent f2e3dc2 commit abc6ae8

34 files changed

+436
-0
lines changed

1/1.1.py 01/1.1.py

File renamed without changes.

1/1.2.py 01/1.2.py

File renamed without changes.

1/test.txt 01/test.txt

File renamed without changes.

2/2.1.py 02/2.1.py

File renamed without changes.

2/2.2.py 02/2.2.py

File renamed without changes.

2/test.txt 02/test.txt

File renamed without changes.

3/3.1.py 03/3.1.py

File renamed without changes.

3/3.2.py 03/3.2.py

File renamed without changes.

3/test.txt 03/test.txt

File renamed without changes.

4/4.1.py 04/4.1.py

File renamed without changes.

4/4.2.py 04/4.2.py

File renamed without changes.

4/test.txt 04/test.txt

File renamed without changes.

5/5.1.py 05/5.1.py

File renamed without changes.

5/5.2.py 05/5.2.py

File renamed without changes.

5/test.txt 05/test.txt

File renamed without changes.

6/6.1.py 06/6.1.py

File renamed without changes.

6/6.2.py 06/6.2.py

File renamed without changes.

7/7.1.py 07/7.1.py

File renamed without changes.

7/7.2.py 07/7.2.py

File renamed without changes.

7/data.txt 07/data.txt

File renamed without changes.

7/test.txt 07/test.txt

File renamed without changes.

8/8.1.py 08/8.1.py

File renamed without changes.

8/8.2.py 08/8.2.py

File renamed without changes.

8/data.txt 08/data.txt

File renamed without changes.

8/test.txt 08/test.txt

File renamed without changes.

9/9.1.py 09/9.1.py

File renamed without changes.

9/9.2.py 09/9.2.py

File renamed without changes.

9/data.txt 09/data.txt

File renamed without changes.

9/test.txt 09/test.txt

File renamed without changes.

10/10.1.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from queue import Queue
2+
3+
file_path = "data.txt"
4+
5+
arr = []
6+
x = 0
7+
y = 0
8+
9+
startx = 0
10+
starty = 0
11+
12+
with open(file_path, 'r') as file:
13+
for line in file:
14+
line = line.replace('\n', '')
15+
str_values = list(line)
16+
x = len(line)
17+
for i in range(x):
18+
if str_values[i] == 'S':
19+
startx = i
20+
starty = y
21+
arr.append(str_values)
22+
y+=1
23+
24+
print(startx, starty)
25+
print(x, y)
26+
27+
arr[starty][startx] = 'L'
28+
print(arr)
29+
30+
dist = [[100000] * x for _ in range(y)]
31+
visited = [[False] * x for _ in range(y)]
32+
33+
q = Queue()
34+
35+
q.put([starty, startx, 0])
36+
dist[starty][startx] = 0
37+
38+
m = 0
39+
40+
while not q.empty():
41+
cur = q.get()
42+
cury, curx, curdist = cur
43+
print(cury, curx, curdist)
44+
45+
# Check if the current position is out of bounds or already visited
46+
if curx >= x or curx < 0 or cury >= y or cury < 0 or visited[cury][curx]:
47+
continue
48+
49+
# Process the current cell (optional, modify this part as needed)
50+
print(dist[cury][curx], curdist)
51+
dist[cury][curx] = min(curdist, dist[cury][curx])
52+
visited[cury][curx] = True
53+
m = curdist
54+
55+
# Explore neighbors based on the content of the cell
56+
if arr[cury][curx] == '|':
57+
q.put([cury-1, curx, curdist + 1])
58+
q.put([cury+1, curx, curdist + 1])
59+
elif arr[cury][curx] == '-':
60+
q.put([cury, curx+1, curdist + 1])
61+
q.put([cury, curx-1, curdist + 1])
62+
elif arr[cury][curx] == 'F':
63+
q.put([cury, curx+1, curdist + 1])
64+
q.put([cury+1, curx, curdist + 1])
65+
elif arr[cury][curx] == 'J':
66+
q.put([cury-1, curx, curdist + 1])
67+
q.put([cury, curx-1, curdist + 1])
68+
elif arr[cury][curx] == 'L':
69+
q.put([cury-1, curx, curdist + 1])
70+
q.put([cury, curx+1, curdist + 1])
71+
elif arr[cury][curx] == '7':
72+
q.put([cury+1, curx, curdist + 1])
73+
q.put([cury, curx-1, curdist + 1])
74+
print(q)
75+
76+
77+
print(dist)
78+
print(m)

10/10.2.py

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
from queue import Queue
2+
import sys
3+
sys.setrecursionlimit(50000)
4+
5+
file_path = "test.txt"
6+
7+
arr = []
8+
x = 0
9+
y = 0
10+
11+
startx = 0
12+
starty = 0
13+
14+
with open(file_path, 'r') as file:
15+
for line in file:
16+
line = line.replace('\n', '')
17+
str_values = list(line)
18+
x = len(line)
19+
for i in range(x):
20+
if str_values[i] == 'S':
21+
startx = i
22+
starty = y
23+
arr.append(str_values)
24+
y+=1
25+
26+
27+
arr[starty][startx] = 'F'
28+
29+
visited = [[0] * x for _ in range(y)]
30+
31+
32+
33+
q = Queue()
34+
35+
q.put([starty, startx])
36+
37+
38+
while not q.empty():
39+
cur = q.get()
40+
cury, curx = cur
41+
42+
# Check if the current position is out of bounds or already visited
43+
if curx >= x or curx < 0 or cury >= y or cury < 0 or visited[cury][curx] != 0:
44+
continue
45+
46+
visited[cury][curx] = 1
47+
48+
# Explore neighbors based on the content of the cell
49+
if arr[cury][curx] == '|':
50+
q.put([cury-1, curx])
51+
q.put([cury+1, curx])
52+
elif arr[cury][curx] == '-':
53+
q.put([cury, curx+1])
54+
q.put([cury, curx-1])
55+
elif arr[cury][curx] == 'F':
56+
q.put([cury, curx+1])
57+
q.put([cury+1, curx])
58+
elif arr[cury][curx] == 'J':
59+
q.put([cury-1, curx])
60+
q.put([cury, curx-1])
61+
elif arr[cury][curx] == 'L':
62+
q.put([cury-1, curx])
63+
q.put([cury, curx+1])
64+
elif arr[cury][curx] == '7':
65+
q.put([cury+1, curx])
66+
q.put([cury, curx-1])
67+
68+
69+
for i in range(len(visited)):
70+
for j in range(len(visited[i])):
71+
if arr[i][j] == '.':
72+
visited[i][j] = 0
73+
74+
def flood_fill(i, j, new_value):
75+
if i < 0 or i >= len(visited) or j < 0 or j >= len(visited[0]) or visited[i][j] != 0:
76+
return
77+
78+
visited[i][j] = new_value
79+
80+
81+
# Recursively fill neighboring cells
82+
flood_fill(visited, i - 1, j, new_value)
83+
flood_fill(visited, i + 1, j, new_value)
84+
flood_fill(visited, i, j - 1, new_value)
85+
flood_fill(visited, i, j + 1, new_value)
86+
87+
88+
89+
for j in range(len(visited[0])):
90+
if visited[0][j] == 0:
91+
flood_fill(0, j, 2)
92+
93+
# Bottom edge
94+
for j in range(len(visited[0])):
95+
if visited[-1][j] == 0:
96+
flood_fill(len(visited) - 1, j, 2)
97+
98+
# Left edge
99+
for i in range(len(visited)):
100+
if visited[i][0] == 0:
101+
flood_fill(i, 0, 2)
102+
103+
# Right edge
104+
for i in range(len(visited)):
105+
if visited[i][-1] == 0:
106+
flood_fill(i, len(visited[0]) - 1, 2)
107+
108+
def flood_fill(visited, i, j, new_value):
109+
if i < 0 or i >= len(visited) or j < 0 or j >= len(visited[0]) or visited[i][j] != 0:
110+
return
111+
112+
visited[i][j] = new_value
113+
114+
# Recursively fill neighboring cells
115+
flood_fill(visited, i - 1, j, new_value)
116+
flood_fill(visited, i + 1, j, new_value)
117+
flood_fill(visited, i, j - 1, new_value)
118+
flood_fill(visited, i, j + 1, new_value)
119+
120+
def flood_fill_edges(visited, new_value):
121+
rows, cols = len(visited), len(visited[0])
122+
123+
# Top edge
124+
for j in range(cols):
125+
if visited[0][j] == 0:
126+
flood_fill(visited, 0, j, new_value)
127+
128+
# Bottom edge
129+
for j in range(cols):
130+
if visited[rows - 1][j] == 0:
131+
flood_fill(visited, rows - 1, j, new_value)
132+
133+
# Left edge
134+
for i in range(rows):
135+
if visited[i][0] == 0:
136+
flood_fill(visited, i, 0, new_value)
137+
138+
# Right edge
139+
for i in range(rows):
140+
if visited[i][cols - 1] == 0:
141+
flood_fill(visited, i, cols - 1, new_value)
142+
143+
flood_fill_edges(visited, 1)
144+
145+
def count_zeros(array):
146+
count = 0
147+
for row in array:
148+
for element in row:
149+
if element == 0:
150+
count += 1
151+
return count
152+
153+
print(visited)
154+
print(count_zeros(visited))

0 commit comments

Comments
 (0)