C#练习题答案: 优化寻路算法【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

优化寻路算法【难度:3级】:

答案1:

using System;

public class PathValidator
{
    public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
    {
        return Go(rows - 1, startRow, startColumn, grid);
    }

    private static int Go(int LastRow, int currR, int currC, bool[][] grid)
    {
        int res = 0;
        if (currR >= grid.Length)
            return 0;
        if (currC >= grid[0].Length || currC < 0)
            return 0;
        if (grid[currR][currC] == false)
            return 0;
        if (currR == LastRow)
            res++;
        grid[currR][currC] = false;
        res += Go(LastRow, currR + 1, currC, grid) + Go(LastRow, currR, currC + 1, grid) + Go(LastRow, currR, currC - 1, grid);
        return res;
    }
}

答案2:

using System;

public class PathValidator
{
    public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
    {
        return Go(rows - 1, startRow, startColumn, grid);
    }

    private static int Go(int LastRow, int currR, int currC, bool[][] grid)
    {
        int res = 0;
        if (currR == LastRow)
            res++;
        if (currR >= grid.Length)
            return 0;
        if (currC >= grid[0].Length || currC < 0)
            return 0;
        if (grid[currR][currC] == false)
            return 0;

        grid[currR][currC] = false;

        res += Go(LastRow, currR + 1, currC, grid);

        res += Go(LastRow, currR, currC + 1, grid);

        res += Go(LastRow, currR, currC - 1, grid);

        return res;
    }

    private static void Main()
    {
        var inputGrid = new[] { new[] { true, true, true },
                                new[] { false, false, true },
                                new[] { false, true, false } };

        int res = PathValidator.GetNumberOfReachableFields(inputGrid, 3, 3, 0, 0);
    }
}

答案3:

using System.Linq;


public class PathValidator
{
        public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
        {
            int step = 1;
            int[][] arr = new int[grid.Length][];
            for (int i = 0; i < grid.Length; i++)
            {
                arr[i] = new int[grid[i].Length];
                for (int j = 0; j < grid[i].Length; j++)
                    if (grid[i][j] == false)
                        arr[i][j] = -1;
            }
            arr[startRow][startColumn] = step;
            bool isPoint = true;
            while (isPoint)
            {
                isPoint = false;
                for (int j = 0; j < arr.Length; j++)
                {
                    for (int i = 0; i < arr[j].Length; i++)
                    {
                        if (arr[j][i] == step)
                        {
                            if (arr[j].Length > i + 1 &amp;&amp; arr[j][i + 1] == 0)
                            {
                                arr[j][i + 1] = step + 1;
                                isPoint = true;
                            }
                            if (arr.Length > j + 1 &amp;&amp; arr[j + 1].Length - 1 >= i &amp;&amp; arr[j + 1][i] == 0)
                            {
                                arr[j + 1][i] = step + 1;
                                isPoint = true;
                            }
                            if (0 <= i - 1 &amp;&amp; arr[j][i - 1] == 0)
                            {
                                arr[j][i - 1] = step + 1;
                                isPoint = true;
                            }
                        }
                    }
                }
                step++;
            }
            arr[arr.Length - 1].Count(x => x > 0);
            return (arr[arr.Length - 1].Count(x => x > 0));
        }
}

答案4:

public class PathValidator
{
    public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)=>
        Path(rows - 1, startRow, startColumn, grid);    

    private static int Path(int end,int row,int column,bool[][] grid)
    {
        int pos = 0;
        if (row<grid.Length &amp;&amp; column<grid[0].Length &amp;&amp; column>=0 &amp;&amp; grid[row][column] != false)
        {
            if (row == end)  pos++;
            grid[row][column] = false;
            pos += Path(end,row+1,column,grid) + Path(end,row,column+1,grid) + Path(end,row,column-1,grid);
            return pos;
        }
        return 0;
    }
}

答案5:

using System.Collections.Generic;

public class PathValidator {
  public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn) {
    var reachable = 0;
    var q = new Queue<(int, int)>();
    q.Enqueue((startRow, startColumn));
    while (q.Count > 0) {
      var (row, col) = q.Dequeue();
      if (!grid[row][col]) continue;
      if (row == rows - 1 &amp;&amp; (row, col) != (startRow, startColumn)) reachable++;
      if (row > 0 &amp;&amp; grid[row-1][col]) q.Enqueue((row-1, col));
      if (row < rows - 1 &amp;&amp; grid[row+1][col]) q.Enqueue((row+1, col));
      if (col > 0 &amp;&amp; grid[row][col-1]) q.Enqueue((row, col-1));
      if (col < columns - 1 &amp;&amp; grid[row][col+1]) q.Enqueue((row, col+1));
      grid[row][col] = false;
    }
    return reachable;
  }
}

答案6:

using System;
using System.Linq;

public class PathValidator
{
    public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
    {

        //Console.WriteLine("rows " + rows + ", columns " + columns + ", startRow " + startRow + ", startColumn " + startColumn);
        //Console.WriteLine(string.Join("\n", grid.Select(p=> string.Join("", p.Select(c => c == true ? "1".ToString() : "0".ToString())))));

        bool[][] path = grid.Select(p=>p.Select(v=>false).ToArray()).ToArray();
        path[startRow][startColumn] = true;

        for(int r = startRow; r < rows; r++)
        {
            for(int c = 0; c < columns; c++)
            {
                if(r-1 > -1 &amp;&amp; path[r-1][c] &amp;&amp; grid[r][c])
                {
                    path[r][c] = true;
                }
                if(c-1 > -1 &amp;&amp; path[r][c-1] &amp;&amp; grid[r][c])
                {
                    path[r][c] = true;
                }
            }
            for(int c = columns-1; c > -1; c--)
            {
                if(r-1 > -1 &amp;&amp; path[r-1][c] &amp;&amp; grid[r][c])
                {
                    path[r][c] = true;
                }
                if(c+1 < columns &amp;&amp; path[r][c+1] &amp;&amp; grid[r][c])
                {
                    path[r][c] = true;
                }
            }
        }

        Console.WriteLine(rows + " " + columns + " - " + path.Select(p=>p.Where(c => c).Count()).ToArray().Sum());
        Console.WriteLine(path[path.Length-1].Where(c => c).Count());

        return path[path.Length-1].Where(c => c).Count();
    }
}

答案7:

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

public class PathValidator
{
    public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
    {
        Dictionary<int, List<int>> possiblePos = new Dictionary<int, List<int>>
        {
            { startRow-1,new List<int>{ startColumn} }
        };
        for (int row = startRow; row < rows; row++)
        {
            List<int> currRow = new List<int>();
            possiblePos.Add(row, currRow);
            List<int> lastRow;
            if (possiblePos.TryGetValue(row - 1, out lastRow))
            {
                foreach (int c in lastRow)
                    if (!currRow.Contains(c) &amp;&amp; grid[row][c])
                    {
                        currRow.Add(c);
                        for (int cBefore = c - 1; cBefore > -1; cBefore--)
                            if (!currRow.Contains(cBefore) &amp;&amp; grid[row][cBefore])
                                currRow.Add(cBefore);
                            else
                                break;
                        for (int cAfter = c + 1; cAfter < columns; cAfter++)
                            if (!currRow.Contains(cAfter) &amp;&amp; grid[row][cAfter])
                                currRow.Add(cAfter);
                            else
                                break;
                    }
            }
            else
                break;
        }
        if (possiblePos.ContainsKey(rows - 1))
            return possiblePos[rows - 1].Count();
        else
            return 0;
    }
}

答案8:

using System;
using System.Linq;

public class PathValidator
{
  public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
  {
      var reachable = new bool[grid[0].Length];
      reachable[startColumn] = true;      
      SpreadSideways(reachable, grid[startRow]);
                    
      for (var row = startRow + 1; row < grid.Length; row++)
      {
        MoveDown(reachable, grid[row]);      
        SpreadSideways(reachable, grid[row]); 
      }
      
      return reachable.Where(r=>r).Count();
  }
    
  private static void SpreadSideways(bool[] reachable, bool[] traversable)
  {
    for (int i = 1; i < reachable.Length; i++)
    {
      reachable[i] |= reachable[i-1] &amp; traversable[i];
    }
    
    for (int i = reachable.Length - 2;  i >= 0; i--)
    {
      reachable[i] |= reachable[i+1] &amp; traversable[i];
    }
  }
  
  private static void MoveDown(bool[] reachable, bool[] traversable)
  {
    for (int i = 0; i < reachable.Length; i++)
    {
      reachable[i] &amp;= traversable[i];
    }
  }
}

答案9:

using System;

public class PathValidator
{
        public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
        {
            var map = new int[rows][];
            for (int i = 0; i < rows; i++)
            {
                var m = new int[columns];
                for (int j = 0; j < columns; j++)
                    m[j] = grid[i][j] ? 0 : -1;
                map[i] = m;
            }
            Find(map, startRow, startColumn, rows, columns);
            var count = 0;
            for (int i = 0; i < columns; i++)
                if (map[rows - 1][i] == 1) count++;
            return count;
        }

        public static void Find(int[][] map, int row, int col, int rows, int columns)
        {
            map[row][col] = 1;
            if (row + 1 < rows &amp;&amp; map[row + 1][col] == 0)
                Find(map, row + 1, col, rows, columns);
            if (col + 1 < columns &amp;&amp; map[row][col + 1] == 0)
                Find(map, row, col + 1, rows, columns);
            if (col - 1 >= 0 &amp;&amp; map[row][col - 1] == 0)
                Find(map, row, col - 1, rows, columns);
        }
}

答案10:

using System;

public class PathValidator
{
    private static int[,] FindPlaces(int[,] array, int row, int column)
    {
        if (row >= array.GetLength(0) || array[row, column] != 0) return array;
        for (int i = column; i < array.GetLength(1) &amp;&amp; array[row, i] != 2; i++)
        {
            if(array[row, i] == 0) array[row, i] = 1;
            array = FindPlaces(array, row + 1, i);
        }
        for (int i = column; i >= 0 &amp;&amp; array[row, i] != 2; i--)
        {
            if(array[row, i] == 0) array[row, i] = 1;
            array = FindPlaces(array, row + 1, i);
        }
        return array;
    }

    private static int[,] IniatializeArray(bool[][] grid, int rows, int columns, int startRow)
    {
        var arr = new int[rows, columns];
        for (int i = startRow; i < rows; i++)
            for (int j = 0; j < columns; j++)
                if (!grid[i][j]) arr[i, j] = 2;
        return arr;
    }

    public static int GetNumberOfReachableFields(bool[][] grid, int rows, int columns, int startRow, int startColumn)
    {
        var arr = IniatializeArray(grid, rows, columns, startRow);
        arr = FindPlaces(arr, startRow, startColumn);
        int count = 0;
        for (int i = 0; i < columns; i++)
            if (arr[rows - 1, i] == 1) count++;
        return count;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值