C#练习题答案: 简单有趣#155:吃豆人【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这是一个C#编程练习,包含了吃豆人问题的多个解答,难度为3级,旨在提升C#编程技能和算法逻辑。
摘要由CSDN通过智能技术生成

简单有趣#155:吃豆人【难度:3级】:

答案1:

namespace myjinxin
{
   
    using System.Linq;
    
    public class Kata
    {
   
        public int PacMan(int size, int[] pacman, int[][] enemies)
        {
   
            // Add "border" enemies at (-1,-1) and (size,size)
            var enemyList = enemies.ToList();
            enemyList.AddRange(new int[][] {
    new[] {
    -1, -1 }, new[] {
    size, size } });

            var minHorizontal = enemyList.Select(enemy => enemy[0]).Where(xCoordinate => xCoordinate < pacman[0]).Max();
            var maxHorizontal = enemyList.Select(enemy => enemy[0]).Where(xCoordinate => xCoordinate > pacman[0]).Min();
            var minVertical = enemyList.Select(enemy => enemy[1]).Where(yCoordinate => yCoordinate < pacman[1]).Max();
            var maxVertical = enemyList.Select(enemy => enemy[1]).Where(yCoordinate => yCoordinate > pacman[1]).Min();

            return ((maxHorizontal - minHorizontal - 1) * (maxVertical - minVertical - 1)) - 1;
        }
    }
}

答案2:

using System;
using System.Linq;
namespace myjinxin
{
   
    using System;
    
    public class Kata
    {
   
        public int PacMan(int N, int[] PM, int[][] enemies){
   
           int count = 1;
            for (int i = 0; i < 2; i++)
            {
   
                var rightEnemies = enemies.Where(e => e[i] > PM[i]);
                var rightX = rightEnemies.Any() ? rightEnemies.Min(n => n[i]) : N;
                var leftEnemies = enemies.Where(e => e[i] < PM[i]);
                var leftX = leftEnemies.Any() ? leftEnemies.Max(n => n[i]) : -1;
                count *= rightX - leftX - 1;
            }
            return --count > 0 ? count : 0 ;
        }
    }
}

答案3:

namespace myjinxin
{
   
    using System;
    
    public class Kata
    {
   
        public int PacMan(int N, int[] PM, int[][] enemies){
   
          int lbx = -1;
          int rbx = N;
          int lby = -1;
          int rby = N;

          foreach (int[] pair in enemies) {
   
            if (pair[0] > PM[0] &amp;&amp; pair[0] < rbx) rbx = pair[0];
            if (pair[0] < PM[0] &amp;&amp; pair[0] > lbx) lbx = pair[0];
            if (pair[1] > PM[1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值