C#练习题答案: 洪水填充【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客聚焦于C#编程中的洪水填充算法,提供了10种不同的解答方案,覆盖从基础到进阶的难度级别,适合C#编程初学者和进阶者提升算法和逻辑能力。
摘要由CSDN通过智能技术生成

洪水填充【难度:3级】:

答案1:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
   
  struct Coord
        {
   
            public int x;
            public int y;

            public Coord(int x, int y)
            {
   
                this.x = x;
                this.y = y;
            }
        }


        public static int[,] FloodFill(int[,] array, int y, int x, int newValue)
        {
   
            if ((y < 0 || x < 0) || array[y, x] == newValue) return array;

            Stack<Coord> stack = new Stack<Coord>();
            stack.Push(new Coord(x, y));

            int valueToFill = array[y, x];

            Coord current;

            do
            {
   
                current = stack.Peek();

                array[current.y, current.x] = newValue;

                if (current.x - 1 >= 0 &amp;&amp; array[current.y, current.x - 1] == valueToFill)
                {
   
                    stack.Push(new Coord(current.x - 1, current.y));
                } else if (current.x + 1 < array.GetLength(1) &amp;&amp; array[current.y, current.x + 1] == valueToFill)
                {
   
                    stack.Push(new Coord(current.x + 1, current.y));
                } else if (current.y - 1 >= 0 &amp;&amp; array[current.y - 1, current.x] == valueToFill)
                {
   
                    stack.Push(new Coord(current.x, current.y - 1));
                }else if (current.y + 1 < array.GetLength(0) &amp;&amp; array[current.y + 1, current.x] == valueToFill)
                {
   
                    stack.Push(new Coord(current.x, current.y + 1));
                }else 
                {
   
                    stack.Pop();
                }
            } while (stack.Count > 0);

            return array;
        }
}

答案2:

using System;
using System.Collections.Generic;

public class Kata
{
   
    public static int[,] FloodFill(int[,] array, int x, int y, int newValue)
    {
   
        var areaValue = array[x,y];
        var s = new Stack<Tuple<int, int>>();
        s.Push(new Tuple<int, int>(x, y));

        while (s.Count > 0)
        {
   
            var point = s.Pop();
            var p_x = point.Item1;
            var p_y = point.Item2;
            if (array[p_x,p_y] != areaValue || array[p_x,p_y] == newValue)
                continue;

            array[p_x,p_y] = newValue;
            if (p_x > 0)
                s.Push(new Tuple<int, int>(p_x - 1, p_y));
            if (p_x < array.GetUpperBound(0))
                s.Push(new Tuple<int, int>(p_x + 1, p_y));
            if (p_y > 0)
                s.Push(new Tuple<int, int>(p_x, p_y - 1));
            if (p_y < array.GetUpperBound(1))
                s.Push(new Tuple<int, int>(p_x, p_y + 1));
        }
        
        return array;
    }
}

答案3:

using System;
using System.Linq;
using System.Collections.Generic;
public class Kata
{
   
  public static int[,] FloodFill(int[,] a, int x, int y, int n)
  {
   
    int lx=a.GetLength(0),ly=a.GetLength(1),m=a[x,y],ct=-1;
    var rs1=new List<int[]>();  // seed list
    a[x,y]=-1;
    rs1.Add(new int[]{
   x,y});  //insert a seed
    while(ct!=rs1.Count){
   
      ct=rs1.Count;  //Current seed count
      for (int i=0;i<rs1.Count;i++){
      
        int cx=rs1[i][0] , cy=rs1[i][1];  //Current coordinates
        int xup=(cx==0 ? 0 : cx-1) , xdn=(cx==lx-1 ? cx : cx+1) , yup=(cy==0 ? 0 : cy-1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值