C#-尝试求解Kattis海岸长度
我正在努力解决卡蒂斯海岸的问题,我在网站上找到了这个问题。挑战的目标是计算海岸线。为此,我们得到了风景地图。其中包括:56011101011011100001010000000000。输出或结果应为20作为整数 其想法是将输入置于锯齿状阵列中,一个用于X轴(N),一个用于Y轴(M)。然后循环这个数组,计算陆地(1)和水(0)之间的差值。这还不奏效,所以我试着用网格线对精确的海岸线进行编码,结果是20 我不知道我在数组和计算差分方面是否走上了正确的道路,所以我在这里寻求帮助。 找到此:。在第3页/幻灯片中,有一个关于海岸长度问题的简短解释。它没有给我任何东西,但也许有人能抓住它 有人能看看我的程序并给我指出解决方案吗C#-尝试求解Kattis海岸长度,c#,arrays,multidimensional-array,difference,C#,Arrays,Multidimensional Array,Difference,我正在努力解决卡蒂斯海岸的问题,我在网站上找到了这个问题。挑战的目标是计算海岸线。为此,我们得到了风景地图。其中包括:56011101011011100001010000000000。输出或结果应为20作为整数 其想法是将输入置于锯齿状阵列中,一个用于X轴(N),一个用于Y轴(M)。然后循环这个数组,计算陆地(1)和水(0)之间的差值。这还不奏效,所以我试着用网格线对精确的海岸线进行编码,结果是20 我不知道我在数组和计算差分方面是否走上了正确的道路,所以我在这里寻求帮助。 找到此:。在第3页/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Task_1 // Coast
{
class Program
{
static void Main(string[] args)
{
int[][] nboxjaggedarray = new int[5][]
{
new int[6] { 0, 1, 1, 1, 1, 0 }, // The N-input. Counted from the X-Axis
new int[6] { 0, 1, 0, 1, 1, 0 },
new int[6] { 1, 1, 1, 0, 0, 0 },
new int[6] { 0, 0, 0, 0, 1, 0 },
new int[6] { 0, 0, 0, 0, 0, 0 }
};
int[][] mboxjaggedarray = new int[6][] // The M-input. Counted from the Y-Axis
{
new int[5] { 0, 0, 1, 0, 0 },
new int[5] { 1, 1, 1, 0, 0 },
new int[5] { 1, 0, 1, 0, 0 },
new int[5] { 1, 1, 0, 0, 0 },
new int[5] { 1, 1, 0, 1, 0 },
new int[5] { 0, 0, 0, 0, 0 }
};
int[][] ngridlinesjaggedarray = new int[6][] //Consists of every N-line in the grid. (N = X-Axis)
{
new int[6] { 0, 1, 1, 1, 1, 0 },
new int[6] { 0, 0, 0, 0, 0, 0 },
new int[6] { 1, 0, 0, 1, 1, 0 },
new int[6] { 1, 1, 1, 0, 1, 0 },
new int[6] { 0, 0, 0, 0, 1, 0 },
new int[6] { 0, 0, 0, 0, 0, 0 }
};
int[][] mgridlinesjaggedarray = new int[7][] // Consists of every M-line in the grid. (M = Y-Axis)
{
new int[5] { 0, 0, 1, 0, 0 },
new int[5] { 1, 1, 0, 0, 0 },
new int[5] { 0, 0, 0, 0, 0 },
new int[5] { 0, 0, 1, 0, 0 },
new int[5] { 0, 0, 0, 1, 0 },
new int[5] { 1, 1, 0, 1, 0 },
new int[5] { 0, 0, 0, 0, 0 }
};
int ntotal = Sum(ngridlinesjaggedarray);
int mtotal = Sum(mgridlinesjaggedarray);
Console.WriteLine(ntotal + mtotal);
Console.ReadKey();
}
public static int Sum(int[][] arr) // Sums up every item in a JaggedArray
{
int total = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] != null)
{
for (int j = 0; j < arr[i].Length; j++)
{
total += arr[i][j];
}
}
}
return total;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
命名空间任务\u 1//Coast
{
班级计划
{
静态void Main(字符串[]参数)
{
int[]nboxjaggedarray=新int[5][]
{
new int[6]{0,1,1,1,1,0},//N输入。从X轴开始计数
新int[6]{0,1,0,1,1,0},
新int[6]{1,1,1,0,0,0},
新int[6]{0,0,0,0,1,0},
新int[6]{0,0,0,0,0,0}
};
int[][]mboxjaggedarray=new int[6][]//M输入。从Y轴开始计数
{
新int[5]{0,0,1,0,0},
新int[5]{1,1,1,0,0},
新int[5]{1,0,1,0,0},
新int[5]{1,1,0,0,0},
新int[5]{1,1,0,1,0},
新int[5]{0,0,0,0,0}
};
int[][]ngridlinesjaggedarray=new int[6][]//由网格中的每一条N线组成。(N=X轴)
{
新int[6]{0,1,1,1,1,0},
新int[6]{0,0,0,0,0,0},
新int[6]{1,0,0,1,1,0},
新int[6]{1,1,1,0,1,0},
新int[6]{0,0,0,0,1,0},
新int[6]{0,0,0,0,0,0}
};
int[][]mgridlinesjaggedarray=new int[7][]//由网格中的每条M线组成。(M=Y轴)
{
新int[5]{0,0,1,0,0},
新int[5]{1,1,0,0,0},
新int[5]{0,0,0,0,0},
新int[5]{0,0,1,0,0},
新int[5]{0,0,0,1,0},
新int[5]{1,1,0,1,0},
新int[5]{0,0,0,0,0}
};
int ntotal=总和(ngridlinesjaggedaray);
int mtotal=总和(mgridlinesjaggedaray);
控制台写入线(ntotal+mtotal);
Console.ReadKey();
}
public static int Sum(int[][]arr)//对JaggedArray中的每个项求和
{
int-total=0;
对于(int i=0;i
问题的难点在于移除位置1,2处的海洋广场,并将其设置为陆地广场,以便在计算海岸时不将其包括在内。所以第一步是分析每个海洋广场,看看它是否是海洋的一部分。一个海洋广场应该是一个连接到其他海洋广场并最终连接到游戏边界的广场,该边界是一个行索引为0或4的广场;或列索引0或6。你可以使用Dijkstra的算法来帮助找到这些正方形。问题的难点是移除位置1,2处的海洋正方形,并将其设置为陆地正方形,这样你就不会将其包括在计算海岸线中。所以第一步是分析每个海洋广场,看看它是否是海洋的一部分。一个海洋广场应该是一个连接到其他海洋广场并最终连接到游戏边界的广场,该边界是一个行索引为0或4的广场;或列索引0或6。您可以使用Dijkstra算法来帮助查找这些正方形。