asp.net 如何计算给定.txt文件中的字母“u”

n53p2ov0  于 2023-02-17  发布在  .NET
关注(0)|答案(1)|浏览(77)

我有一个网格,你可以在图中看到。首先,我需要找出字母“u”有多少不同的连接。我使用了一个youtube解释视频,他们在视频中谈到了LeetCode中的岛屿数量。使用+-相同的策略,我发现字母“u”有2个不同的连接。第一个连接包含22个字母“u”,第二个连接包含14个字母“u”.我的输出应该看起来像这样2(不同的连接,我已经找到了),22(没有这个),14(没有这个
输入1:
Input1 data
6和15是尺寸。
输出1:
2
22
14
输入2:
Input2 data
输出2:
1
5
现在,我需要编写一个算法来计算每个连接有多少个字母“u”。例如,第一个连接(在Input 1照片中,它是红色的)有22个字母“u”相互连接。第二个连接(在Input 1照片中,它是粉红色的)有14个字母“u”相互连接。
还有,我不能用linq。
在这段代码中,你可以看到我已经发现有两个不同的连接。
我还写了一个代码来查找每个连接包含多少个字母“u”,方法名为:公共静态int CountLetterUInConnection(char[][]网格),但它不起作用。
也许有人能发现我的错误并给我指出正确的道路,或者也许我做错了,我需要重做?

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string path = "App_Data/Map.txt";
            string[] lines = File.ReadAllLines(HttpContext.Current.Server.MapPath(path));
            int Width = int.Parse(lines[0]);
            int Height = int.Parse(lines[1]);
            Label4.Text = "Width: " + Width.ToString();
            Label5.Text = "Height: " + Height.ToString();
            for (int i = 2; i < lines.Length; i++)
            {
                var newRow = new TableRow();
                var newCell = new TableCell();
                newCell.Text = lines[i];
                newRow.Cells.Add(newCell);
                Table1.Rows.Add(newRow);
            }
        }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = "App_Data/Map.txt";
        string map = File.ReadAllText(HttpContext.Current.Server.MapPath(path));
        char[][] grid = GetGridGFromMap(map);
        int NumberOfMoles = TaskUtils.LettersIsU(grid);
        int Count = TaskUtils.CountLetterUInConnection(grid);
       // Label1.Text = "Cave count: " + NumberOfMoles.ToString();
        Label1.Text = Count.ToString();

        
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
    }
    private char[][] GetGridGFromMap(string map)
    {
        string[] lines = map.Split('\n');
        char[][] grid = new char[lines.Length][];
        for(int i =0; i < lines.Length; i++)
        {
            grid[i] = lines[i].ToCharArray();
        }
        return grid;
    }
}
class TaskUtils
{
     
    public static int LettersIsU(char[][] grid)
    {
        int count = 0;
        for (int i = 0; i < grid.Length; i++)
        {
            for (int j = 0; j < grid[i].Length; j++)
            {
                if (grid[i][j] == 'u')
                {
                    count++;
                    SetCountedLetters(grid, i, j);
                }
            }
        }
        return count;
    }
    private static void SetCountedLetters(char[][] grid, int i, int j)
    {
        if (i < 0 || i >= grid.Length || j < 0 || j >= grid[i].Length || grid[i][j] == 'z')
            return;
        grid[i][j] = 'z';
        SetCountedLetters(grid, i + 1, j);
        SetCountedLetters(grid, i - 1, j);
        SetCountedLetters(grid, i, j + 1);
        SetCountedLetters(grid, i, j - 1);
    }
    public static int CountLetterUInConnection(char[][] grid)
    {
        int count = 0;
        for (int i = 0; i < grid.Length; i++)
        {
            for (int j = 0; j < grid[i].Length; j++)
            {
                if (grid[i][j] == 'u')
                {
                    count++;
                    if ((i - 1 >= 0 && grid[i - 1][j] == 'u') || (i + 1 < grid.Length && grid[i + 1][j] == 'u')
                        || (j - 1 >= 0 && grid[i][j - 1] == 'u') || (j + 1 < grid[i].Length && grid[i][j + 1] == 'u'))
                    {
                        count++;
                    }
                }
            }
        }
        return count;
    }
}`
t1rydlwq

t1rydlwq1#

如果是我,我会将int LettersIsU(char[][] grid)重构为int[] LettersIsU(char[][] grid),其中数组的长度是找到的连续组的数量,数组中的每个值是每个组中的计数。
我会这样做:

public static int[] LettersIsU(char[][] grid)
{
    int[][] found = new int[grid.Length][];
    Dictionary<int, int> count = new();
    void SetCountedLetters(char[][] grid, int i, int j)
    {
        if (i < 0 || i >= grid.Length || j < 0 || j >= grid[i].Length || found[i][j] != 0 || grid[i][j] != 'u')
            return;
        found[i][j] = count.Count;
        count[count.Count] += 1;
        SetCountedLetters(grid, i + 1, j);
        SetCountedLetters(grid, i - 1, j);
        SetCountedLetters(grid, i, j + 1);
        SetCountedLetters(grid, i, j - 1);
    }
    for (int i = 0; i < grid.Length; i++)
        found[i] = new int[grid[i].Length];
    for (int i = 0; i < grid.Length; i++)
        for (int j = 0; j < grid[i].Length; j++)
            if (grid[i][j] == 'u' && found[i][j] == 0)
            {
                count[count.Count + 1] = 0;
                SetCountedLetters(grid, i, j);
            }
    int[] output = new int[count.Count];
    for (int k = 0; k < count.Count; k++)
        output[k] = count[k + 1];
    return output;
}

相关问题