Closest EnemyII

版权声明:本文为博主原创文章,采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。欢迎转载,但请注明作者姓名和文章出处。 https://blog.csdn.net/njit_77/article/details/79748956

Challenge

Using the C# language, have the function  ClosestEnemyII(strArr) read the matrix of numbers stored in  strArr which will be a 2D matrix that contains only the integers 1, 0, or 2. Then from the position in the matrix where a 1 is, return the number of spaces either left, right, down, or up you must move to reach an enemy which is represented by a 2. You are able to wrap around one side of the matrix to the other as well. For example: if  strArr is ["0000", "1000", "0002", "0002"] then this looks like the following: 

0 0 0 0
1 0 0 0
0 0 0 2
0 0 0 2
 

For this input your program should return  2 because the closest enemy (2) is 2 spaces away from the 1 by moving left to wrap to the other side and then moving down once. The array will contain any number of 0's and 2's, but only a single 1. It may not contain any 2's at all as well, where in that case your program should return a 0. 
Sample Test Cases

Input:"000", "100", "200"

Output:1


Input:"0000", "2010", "0000", "2002"

Output:2


Closest EnemyII算法

1、输入一个矩阵数字,包含1,0或2;

2、可以上、下、左、右移动;最上面可以再次向上移动到最下面

3、从1的位置移动到2的位置,返回需要的步数,如果没有2,返回0

        public static int ClosestEnemyII(string[] strArr)
        {
            int Row = strArr.Length;
            int Col = strArr[0].Length;
            List<int[]> Onelist = new List<int[]>();
            List<int[]> Twolist = new List<int[]>();
            for (int i = 0; i < Row; i++)
            {
                for (int j = 0; j < Col; j++)
                {
                    if (strArr[i][j] == '1')
                    {
                        Onelist.Add(new int[] { i, j });
                    }
                    else if (strArr[i][j] == '2')
                    {
                        Twolist.Add(new int[] { i, j });
                    }
                }
            }

            if (Twolist.Count > 0)
            {
                int[] Steps = new int[Twolist.Count];
                int rowStep1 = 0, colStep1 = 0;
                int rowStep2 = 0, colStep2 = 0;
                for (int i = 0; i < Twolist.Count; i++)
                {
                    rowStep1 = Onelist[0][0] - Twolist[i][0];
                    if (rowStep1 < 0)
                    {
                        rowStep1 += Row;
                    }
                    rowStep2 = Twolist[i][0] - Onelist[0][0];
                    if (rowStep2 < 0)
                    {
                        rowStep2 += Row;
                    }
                    colStep1 = Onelist[0][1] - Twolist[i][1];
                    if (colStep1 < 0)
                    {
                        colStep1 += Col;
                    }
                    colStep2 = Twolist[i][1] - Onelist[0][1];
                    if (colStep2 < 0)
                    {
                        colStep2 += Col;
                    }
                    Steps[i] = Math.Min(rowStep1, rowStep2) + Math.Min(colStep1, colStep2);
                }
                return Steps.Min();
            }
            return 0;
        }

猜你喜欢

转载自blog.csdn.net/njit_77/article/details/79748956