灌水

问题描述

小蓝有一个01 矩阵。他打算将第一行第一列的0变为 2。变化过程有传染性,每次 2 的上下左右四个相邻的位置中的0都会 变成2。直到最后每个2的周围都是1或2结束 请问,最终矩阵中有多少个 2? 以下是小蓝的矩阵,共30行40 列

0000100010000001101010101001001100000011 0101111001111101110111100000101010011111 1000010000011101010110000000001011010100 0110101010110000000101100100000101001001 0000011010100000111111001101100010101001 0110000110000000110100000000010010100011 0100110010000110000000100010000101110000 0010011010100110001111001101100110100010 1111000111101000001110010001001011101101 0011110100011000000001101001101110100001 0000000101011000010011111001010011011100 0000100000011001000100101000111011101100 0010110000001000001010100011000010100011 0110110000100011011010011010001101011011 0000100100000001010000101100000000000010 0011001000001000000010011001100101000110 1110101000011000000100011001001100111010 0000100100111000001101001000001010010001 0100010010000110100001100000110111110101 1000001001100010011001111101011001110001 0000000010100101000000111100110010101101 0010110101001100000100000010000010110011 0000011101001001000111011000100111010100 0010001100100000011000101011000000010101 1001111010010110011010101110000000101110 0110011101000010100001000101001001100010 1101000000010010011001000100110010000101 1001100010100010000100000101111111111100 1001011010101100001000000011000110110000 0011000100011000010111101000101110110001

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的 内容将无法得分。

Mycode

python

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
def print_li(li):
a = str(li)
for i in range(len(a)):
try:
if a[i - 2] == "]" and a[i - 1] == "," and a[i] == " ":
print("\r")
else:
print(a[i], end="")
except IndexError:
break
print("\n")
def input_li():
result=[]
for i in range(30):#5行
a=input()
li=[]
for j in a:
li.append(int(j))
result.append(li)
return result
def DFS(li,x1,y1):
s_x=x1
s_y=y1
temp_y=y1
try:
while li[s_x+1][s_y]==0:
li[s_x+1][s_y]=2
try:
while li[s_x+1][s_y+1]==0:
li[s_x+1][s_y + 1]=2
s_y+=1
except IndexError:
break
s_y=temp_y
s_x+=1
except IndexError:
return
def output_li(li):
result=0
for i in str(li):
if i=="2":
result+=1
return result
li=input_li()
print_li(li)
for j in range(40):#5列
for i in range(30):#5行
DFS(li,j,i)
if li[i][j]==0:
li[i][j]=2
print_li(li)
for j in range(40):#5列
for i in range(30):#5行
try:
if li[i-1][j]==1 and li[i][j-1]==1 and li[i][j+1]==1:
li[i][j]=8
except IndexError:
break
print_li(li)
# if j == 0:
# if li[i - 1][j] == 1 or li[i - 1][j] == 8:
# li[i][j] = 8
# elif j == 39: # 第5列
# if li[i + 1][j] == 1 or li[i + 1][j] == 8:
# li[i][j] = 8
# el

MyOutput

[[2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 8, 1, 8, 1, 8, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1], [2, 1, 2, 1, 8, 1, 1, 2, 2, 1, 1, 1, 1, 1, 2, 1, 8, 1, 8, 1, 8, 1, 1, 2, 2, 2, 2, 2, 1, 2, 1, 8, 1, 2, 2, 1, 1, 1, 8, 1], [1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 8, 1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 2, 1, 8, 1, 2, 2], [8, 1, 1, 2, 1, 8, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 8, 1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 1, 8, 1, 2, 2, 1, 2, 2, 1], [2, 2, 2, 2, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1, 8, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 2, 1, 8, 1, 2, 1, 2, 2, 1], [2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2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