简单有趣#155:吃豆人【难度:3级】:
答案1:
namespace myjinxin
{
using System.Linq;
public class Kata
{
public int PacMan(int size, int[] pacman, int[][] enemies)
{
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] && pair[0] < rbx) rbx = pair[0];
if (pair[0] < PM[0] && pair[0] > lbx) lbx = pair[0];
if (pair[1] > PM[1