Correct Path

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

Challenge

Using the C# language, have the function  CorrectPath(str) read the  str parameter being passed, which will represent the movements made in a 5x5 grid of cells starting from the top left position. The characters in the input string will be entirely composed of:  r, l, u, d, ?. Each of the characters stand for the direction to take within the grid, for example: r = right, l = left, u = up, d = down. Your goal is to determine what characters the question marks should be in order for a path to be created to go from the top left of the grid all the way to the bottom right without touching previously travelled on cells in the grid. 

For example: if  str is "r?d?drdd" then your program should output the final correct string that will allow a path to be formed from the top left of a 5x5 grid to the bottom right. For this input, your program should therefore return the string  rrdrdrdd. There will only ever be one correct path and there will always be at least one question mark within the input string. 
Sample Test Cases

Input:"???rrurdr?"

Output:"dddrrurdrd"


Input:"drdr??rrddd?"

Output:"drdruurrdddd"

Correct Path算法要求

1、5 X 5 棋盘格,左上角是起始点(1,1),右下角是结束点(5,5);

2、r->(1,0);l->(-1,0);d->(0,1),u->(0,-1);?->未知

3、不能走到已经走过的位置;

我的思路

1、计算处?的数量和位置;

2、每个位置处?出来起始点和终止点有2个选择,其它位置有4个选择;

3、遍历每个位置的可能性,满足条件就结束;否则继续遍历;

        public static string CorrectPath(string str)
        {
            int Unknown_Count = -1;
            List<int> UnKnownlist = new List<int>();
            char[] chArray = str.ToArray();
            int length = chArray.Length;
            char ch = '\0';
            for (int i = 0; i < length; i++)
            {
                ch = chArray[i];
                if (ch == '?')
                {
                    Unknown_Count++;
                    UnKnownlist.Add(i);
                }
            }

            if (UnKnownlist.Count > 0)
            {
                int count = UnKnownlist.Count;
                int[] NUMlist = new int[count];
                int[] CYCLElist = new int[count];
                Queue<char>[] stacks = new System.Collections.Generic.Queue<char>[count];
                for (int i = 0; i < count; i++)
                {
                    stacks[i] = new Queue<char>();
                    stacks[i].Enqueue('d');
                    stacks[i].Enqueue('r');
                    if (UnKnownlist[i] > 0 && UnKnownlist[i] <= length - 1)
                    {
                        stacks[i].Enqueue('l');
                        stacks[i].Enqueue('u');
                    }
                    NUMlist[i] = stacks[i].Count;
                }

                bool rStatus = true;//字符串是否正确
                int D_Count = 0;
                int R_Count = 0;
                int L_Count = 0;
                int U_Count = 0;

                do
                {
                    for (int i = 0; i < count; i++)
                    {
                        chArray[UnKnownlist[i]] = stacks[i].Peek();
                    }

                    //检测字符串是否正确
                    rStatus = true;
                    D_Count = U_Count = R_Count = L_Count = 0;
                    for (int i = 0; i < length; i++)
                    {
                        ch = chArray[i];
                        //注释部分代码也可以不注释,作用还是避免回到已经走过的位置
                        //if (i > 0 && i < length - 1)
                        //{
                        //    if (ch == 'd' && (chArray[i - 1] == 'u' || chArray[i + 1] == 'u'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'u' && (chArray[i - 1] == 'd' || chArray[i + 1] == 'd'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'r' && (chArray[i - 1] == 'l' || chArray[i + 1] == 'l'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'l' && (chArray[i - 1] == 'r' || chArray[i + 1] == 'r'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //}
                        //else if (i == 0)
                        //{
                        //    if (ch == 'd' && (chArray[i + 1] == 'u'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'u' && (chArray[i + 1] == 'd'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'r' && (chArray[i + 1] == 'l'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'l' && (chArray[i + 1] == 'r'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //}
                        //else if (i == length - 1)
                        //{
                        //    if (ch == 'd' && (chArray[i - 1] == 'u'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'u' && (chArray[i - 1] == 'd'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'r' && (chArray[i - 1] == 'l'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //    else if (ch == 'l' && (chArray[i - 1] == 'r'))
                        //    {
                        //        rStatus = false;
                        //        break;
                        //    }
                        //}
                        if (ch == 'd')
                        {
                            D_Count++;
                        }
                        else if (ch == 'r')
                        {
                            R_Count++;
                        }
                        else if (ch == 'l')
                        {
                            L_Count++;
                        }
                        else if (ch == 'u')
                        {
                            U_Count++;
                        }
                    }

                    if (rStatus)
                    {
                        if (D_Count - U_Count != 4 || R_Count - L_Count != 4)
                        {
                            rStatus = false;
                        }
                        else
                        {
                            int X = 1;
                            int Y = 1;
                            List<int[]> Path = new List<int[]>();
                            Path.Add(new int[] { X, Y });
                            for (int i = 0; i < length; i++)
                            {
                                ch = chArray[i];
                                if (ch == 'd')
                                {
                                    X++;
                                }
                                else if (ch == 'u')
                                {
                                    X--;
                                }
                                else if (ch == 'r')
                                {
                                    Y++;
                                }
                                else if (ch == 'l')
                                {
                                    Y--;
                                }
                                if ((X < 1 || Y < 1))
                                {
                                    rStatus = false;
                                    break;
                                }
                                int[] path = new int[] { X, Y };
                                if (Path.Count > 0)
                                {
                                    for (int j = 0; j < Path.Count; j++)
                                    {
                                        if (Path[j][0] == path[0] && Path[j][1] == path[1])
                                        {
                                            rStatus = false;
                                            break;
                                        }
                                    }
                                    if (rStatus)
                                    {
                                        Path.Add(path);
                                    }
                                }
                            }
                        }
                    }
                    if (!rStatus)
                    {
                        char Ch = stacks[0].Dequeue();
                        stacks[0].Enqueue(Ch);
                        CYCLElist[0]++;

                        for (int i = 0; i < count; i++)
                        {
                            if (CYCLElist[i] == NUMlist[i])//进位
                            {
                                if (i < count - 1)
                                {
                                    Ch = stacks[i + 1].Dequeue();
                                    stacks[i + 1].Enqueue(Ch);
                                    CYCLElist[i + 1]++;
                                }
                                else if (i == count - 1)
                                {
                                    //默认循环一次就可以求出结果的,所以这里不写,或者失败退出了
                                }
                                CYCLElist[i] = 0;
                            }
                        }
                    }

                } while (!rStatus);
            }
            str = new string(chArray);
            return str;
        }

猜你喜欢

转载自blog.csdn.net/njit_77/article/details/79747870
今日推荐