Queen's Attack II

https://www.hackerrank.com/challenges/queens-attack-2/problem

题目要求计算棋子Queen可以在棋盘上下落的位置个数


只需要遍历所有的障碍位置将位于皇后路径上最近的障碍找到即可时间复杂度为O(k)

class Solution {

    // Complete the queensAttack function below.
    static int queensAttack(int n, int k, int r_q, int c_q, int[][] obstacles) {
               int maxCountPerDic;

        int leftTop = Math.Min(n - r_q,c_q - 1);
        int top = n - r_q;
        int rightTop = Math.Min(n - r_q,n - c_q);
        int right = n-c_q;
        int rightDown = Math.Min(r_q - 1,n-c_q);
        int down = r_q - 1;
        int leftDown = Math.Min(r_q - 1,c_q - 1);
        int left = c_q - 1;

        int length = 0;
        foreach (var item in obstacles)
        {
            if (item[0] > r_q)
            {
                if (item[0] - r_q == c_q - item[1])//左上
                {
                    length = item[0] - r_q;
                    if (leftTop > length)
                    {
                        leftTop = length - 1;
                    }
                }
                if (item[1] == c_q)//上
                {
                    length = item[0] - r_q;
                    if (top > length)
                    {
                        top = length - 1;
                    }
                }
                if (item[0] - r_q == item[1] - c_q)//右上
                {
                    length = item[0] - r_q;
                    if (rightTop > length)
                    {
                        rightTop = length - 1;
                    }
                }
            }
           
            if(item[0] == r_q && item[1] > c_q)//右
            {
                length = item[1] - c_q;
                if(right > length)
                {
                    right = length - 1;
                }
            }

            if (item[0] < r_q)
            {
                if (r_q - item[0] == item[1] - c_q)//右下
                {
                    length = r_q - item[0];
                    if (rightDown > length)
                    {
                        rightDown = length - 1;
                    }
                }
                if (item[1] == c_q)//下
                {
                    length = r_q - item[0];
                    if (down > length)
                    {
                        down = length - 1;
                    }
                }
                if (r_q - item[0] == c_q - item[1])//左下
                {
                    length = r_q - item[0];
                    if (leftDown > length)
                    {
                        leftDown = length - 1;
                    }
                }
            }

         
            if (item[0] == r_q && item[1] < c_q)//左
            {
                length = c_q - item[1];
                if (left > length)
                {
                    left = length - 1;
                }
            }
        }

        return leftTop + top + rightTop + right + rightDown + down + leftDown + left;

    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        string[] nk = Console.ReadLine().Split(' ');

        int n = Convert.ToInt32(nk[0]);

        int k = Convert.ToInt32(nk[1]);

        string[] r_qC_q = Console.ReadLine().Split(' ');

        int r_q = Convert.ToInt32(r_qC_q[0]);

        int c_q = Convert.ToInt32(r_qC_q[1]);

        int[][] obstacles = new int[k][];

        for (int i = 0; i < k; i++) {
            obstacles[i] = Array.ConvertAll(Console.ReadLine().Split(' '), obstaclesTemp => Convert.ToInt32(obstaclesTemp));
        }

        int result = queensAttack(n, k, r_q, c_q, obstacles);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}


猜你喜欢

转载自blog.csdn.net/zh453030035/article/details/80522706
今日推荐