删除最少数量的国际象棋骑士,这样就不会有剩余的骑士威胁另一个骑士

时间:2018-09-21 20:56:08

标签: c# algorithm chess

我很难为骑士游戏任务提出算法。

任务说明为:

骑士游戏是在具有N x N尺寸和许多国际象棋骑士0 <= K <=N²的棋盘上进行的。

我收到一个棋盘(字符矩阵),骑士用'K'表示,空单元用'0'表示。我的任务是删除最少的骑士,这样就不会剩下可以攻击另一个骑士的骑士。在第一行,我收到了木板的N尺寸。在接下来的N行中,我收到带有Ks和0s的字符串。

这是我到目前为止设计的:

public class SolutionTwo
{
    public static void Main()
    {
        var size = int.Parse(Console.ReadLine());

        var board = new char[size][];
        PrepareBoard(board);

        var knights = new List<Knight>();

        for (int x = 0; x < board.Length; x++)
        {
            for (int y = 0; y < board[x].Length; y++)
            {
                if (board[x][y] == 'K')
                {
                    knights.Add(new Knight()
                    {
                        X = x,
                        Y = y,
                        KnightAttacks = GetKnightAttacks(x, y, board)
                    });
                }
            }
        }

        var count = 0;

        foreach (var knight in knights.OrderByDescending(k => k.KnightAttacks.Count()))
        {
            if (!IsReaminingHits(board))
            {
                break;
            }

            board[knight.X][knight.Y] = '0';
            count++;

            foreach (var subK in knight.KnightAttacks)
            {
                var c = knights.Single(k => k.X == subK.Item1 && k.Y == subK.Item2);

                c.KnightAttacks.Remove(new Tuple<int, int>(knight.X, knight.Y));
            }

            // for test purposes
            //Console.WriteLine($"Kn: [{knight.X} {knight.Y}] - he attacks: {string.Join(" ", knight.KnightAttacks)} {knight.KnightAttacks.Count()}");
        }

        Console.WriteLine(count);
    }

    private static bool IsReaminingHits(char[][] board)
    {
        for (int i = 0; i < board.Length; i++)
        {
            for (int j = 0; j < board[i].Length; j++)
            {
                if (board[i][j] == 'K')
                {
                    if (GetKnightAttacks(i, j, board).Count > 0)
                    {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    private static void PrepareBoard(char[][] board)
    {
        for (int i = 0; i < board.Length; i++)
        {
            board[i] = Console.ReadLine()
                .ToCharArray();
        }
    }

    private static List<Tuple<int, int>> GetKnightAttacks(int x, int y, char[][] board)
    {
        var deltas = new int[8][]
        {
            new int[] {-2, -1},
            new int[] {-2, +1},
            new int[] {+2, -1},
            new int[] {+2, +1},
            new int[] {-1, -2},
            new int[] {-1, +2},
            new int[] {+1, -2},
            new int[] {+1, +2}
        };

        var xCandidate = 0;
        var yCandidate = 0;

        var list = new List<Tuple<int, int>>();

        for (int i = 0; i < 8; i++)
        {
            xCandidate = x + deltas[i][0];
            yCandidate = y + deltas[i][1];

            if (0 <= xCandidate && xCandidate < board.Length
                && 0 <= yCandidate && yCandidate < board[0].Length
                && board[xCandidate][yCandidate] == 'K')
            {
                list.Add(new Tuple<int, int>(xCandidate, yCandidate));
            }
        }

        return list;
    }
}

public class Knight
{
    public int X { get; set; }

    public int Y { get; set; }

    public List<Tuple<int, int>> KnightAttacks { get; set; } = new List<Tuple<int, int>>();
}

示例1:

输入:

5 
0K0K0
K000K
00K00
K000K 
0K0K0

预期结果: 1


示例2:

输入:

8
0K0KKK00
0K00KKKK
00K0000K
KKKKKK0K
K0K0000K
KK00000K
00K0K000
000K00KK

预期结果: 12

1 个答案:

答案 0 :(得分:1)

该算法有缺陷,这在较小的板上更容易看到:

4
000K
0K00
00K0
K000

这里的解决方案应该是2;但该算法返回3。其原因是该算法删除了攻击次数最多的 first 骑士;假定此删除是正确答案的一部分;但是,可能会有多个骑士受到如此多的攻击,第一个不一定是最佳选择。

即使knights.OrderByDescending(k => k.KnightAttacks.Count())也不执行您想要的操作,即使您在循环中添加knight.KnightAttacks.Clear();,因为它必须评估所有值才能枚举它们;但是随着您开始移除骑士,这些数字当然会有所不同。

正确的算法将需要尝试所有具有相同攻击次数的替代方法,以找出最佳算法。我还可以提出方案,在该方案中,进攻最多的骑士不是最佳选择。例如:

7
K00000K
00K0K00
KK000KK
0K0K0K0
0000000
0000000
0000000

因此,对代码开头的var count=0;使用以下替换可以改善算法(例如,对于我的小示例,它的正确答案为2,对于“示例2”的正确答案为12);但这不是完整的解决方案:

var count = 0;
while (knights.Any(k => k.KnightAttacks.Count > 0))
{
    var knight = knights.OrderByDescending(k => k.KnightAttacks.Count).First();

    // for test purposes
    //Console.WriteLine($"Kn: [{knight.X} {knight.Y}] - he attacks: {string.Join(" ", knight.KnightAttacks)} {knight.KnightAttacks.Count()}");

    board[knight.X][knight.Y] = '0';
    count++;

    foreach (var subK in knight.KnightAttacks)
    {
        var c = knights.Single(k => k.X == subK.Item1 && k.Y == subK.Item2);

        c.KnightAttacks.Remove(new Tuple<int, int>(knight.X, knight.Y));
    }
    knight.KnightAttacks.Clear();
}