# 计算小岛的个数问题

0
[待解决问题]

11110

11010

11000

00000

11000

11000

00100

00011

0

@将客流:

``` class Program
{
static void Main(string[] args)
{
List<List<int>> Array = new List<List<int>>();
//Array.Add(new List<int> { 1, 1, 1, 1, 0 });
//Array.Add(new List<int> { 1, 1, 0, 1, 0 });
//Array.Add(new List<int> { 1, 1, 0, 0, 0 });
//Array.Add(new List<int> { 0, 0, 0, 0, 0 });

}

public static int GetLandCount(List<List<int>> Array)
{
for (int i = 0; i < Array.Count; i++)
{
Array[i].Insert(0, 0);
}
List<int> zeroList = new List<int>();
for (int i = 0; i < Array[0].Count; i++)
{
}
Array.Insert(0, zeroList);
List<Land> landList = new List<Land>();
for (int y = 0; y < Array.Count; y++)
{
for (int x = 0; x < Array[y].Count; x++)
{
try
{
if (Array[y][x] == 1)
{
if (!landList.All(land => land.IsNeighbouredAndSaved(x, y) == true)|| landList.Count == 0)
{
}
}
}
catch
{
continue;
}
}
}
return landList.Count;

}

}

class Land
{
public List<Tuple<int,int>> NeighbouredPairs { get; set; }
public bool IsNeighbouredAndSaved(int x,int y)
{
bool result = NeighbouredPairs.Exists(t=>t.Item1==x||t.Item2==y);
if (result)
{
NeighbouredPairs.Add(new Tuple<int, int> ( x, y ));
NeighbouredPairs= NeighbouredPairs.Distinct().ToList();
}
return result;
}
public Land(int x, int y)
{
NeighbouredPairs = new List<Tuple<int, int>>();
}
}```

@将客流: 同时每个岛的陆地 组成也在这个landList里

0
```\$arr = array(
array(1,0,1,0,0,1,0,1),
array(0,1,1,0,1,1,1,0),
array(1,0,0,1,0,1,0,0),
array(1,0,1,1,0,1,0,1),
);
\$arr1 = array();
\$t = 0;
for (\$i = 0; \$i<4;\$i++){
for (\$j = 0; \$j<8;\$j++){
if (\$arr[\$i][\$j] == 0 && \$arr1[\$i][\$j] == null){
\$t++;
\$arr1[\$i][\$j] = \$t;
if (\$arr[\$i+1][\$j] == 0){ \$arr1[\$i+1][\$j] = \$arr1[\$i][\$j];}
if (\$arr[\$i][\$j+1] == 0){ \$arr1[\$i][\$j+1] = \$arr1[\$i][\$j];}
//                    if (\$arr[\$i-1][\$j] == 0){ \$arr1[\$i-1][\$j] = \$arr1[\$i][\$j];}
//                    if (\$arr[\$i][\$j-1] == 0){ \$arr1[\$i][\$j-1] = \$arr1[\$i][\$j];}
}
}
}
return \$t -1;```

0

DFS 遍历一遍即可

Shendu.CC | 园豆：2138 (老鸟四级) | 2018-04-17 09:55

您需要登录以后才能回答，未注册用户请先注册