Home > other >  Breadth-first traversal of the island of related issues, have bosses to come to a train of thought
Breadth-first traversal of the island of related issues, have bosses to come to a train of thought

Time:11-19

Strives for the number of islands and iterate through all the possible problems
Problem: given a '1' (land) and '0' (water) n grid (n is odd number), the initial array of array heart place 1 phalanx, n 3, initial island is like this:
[[0, 0],
[0, 0],
[0, 0]]
Because of reclaiming land from the sea, coastal island radiation around the island, island only require new radiation and neighbouring island before, and so on,
That is to say,
[[0,1,1],
[0, 0],
[0, 0]] this island is in conformity with the rules,
The
[[0, 1],
[0, 0],
[0, 1]] is not in conformity with the rules,

If radiation may be suspended at any time, then ask the reclaiming land from the sea how possibility? O bosses give a train of thought

CodePudding user response:

https://blog.csdn.net/qq_21553301/article/details/101119095

CodePudding user response:

reference 1/f, old coconut response:
https://blog.csdn.net/qq_21553301/article/details/101119095
that is a given number of islands in the simulation, and this also is not too to me

CodePudding user response:

 import numpy as np 
B=[]
The class Solution () :
Def countIslands (self, a) :
The row, col=a.s hape
X, y=int ((row 1)/2), int ((col - 1)/2)
# traversal two-dimensional arrays, encounter to 1 point, infection function called
Self. Infect (a, the row, col, x, y)
Def infect (self, m, the row, col, I, j) :

If (i<0 or i>=row or j<0 or j>=col) :
Return
If m [I] [j].=1:
M [I] [j]=1

B.a ppend (m)
Print (m)

# infect (m, the row, col, I, j - 1)
Self. Infect (m, the row, col, I, j - 1)
Self. Infect (m, the row, col, I - 1, j)
Self. Infect (m, the row, col, I + 1, j)
Self. Infect (m, the row, col, I, j + 1)
N=3
M=int ((n - 1)/2)
A=np. Zeros ((n, n))

A [m] [m]=1
B.a ppend (a)
Print (a.s hape)
S=Solution ()
S.c ountIslands (a)

CodePudding user response:

Is there a big help to look at the code, ran out of the computer has been a dead circulation, whether recursive the required force is too strong? How to solve?
  • Related