C# 单词数组中单词的结束位置不正确

C# 单词数组中单词的结束位置不正确,c#,C#,我正在尝试在字母网格和单词列表中实现单词搜索。该程序应该能够找到网格中的每个单词,并显示开始和结束坐标。这些词可以向八个方向传播。我能够得到正确的开始坐标,但是一些单词的结束坐标不匹配 enter code here using System; 使用系统数据 名称空间词搜索 { 班级计划 { 静态int R,C // For searching in all 8 direction static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 }

我正在尝试在字母网格和单词列表中实现单词搜索。该程序应该能够找到网格中的每个单词,并显示开始和结束坐标。这些词可以向八个方向传播。我能够得到正确的开始坐标,但是一些单词的结束坐标不匹配

enter code here 

using System;
使用系统数据

名称空间词搜索 { 班级计划 { 静态int R,C

    // For searching in all 8 direction
    static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 };
    static int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 };
    static char[,] Grid = new char[,] {
        {'C', 'P', 'K', 'X', 'O', 'I', 'G', 'H', 'S', 'F', 'C', 'H'},
        {'Y', 'G', 'W', 'R', 'I', 'A', 'H', 'C', 'Q', 'R', 'X', 'K'},
        {'M', 'A', 'X', 'I', 'M', 'I', 'Z', 'A', 'T', 'I', 'O', 'N'},
        {'E', 'T', 'W', 'Z', 'N', 'L', 'W', 'G', 'E', 'D', 'Y', 'W'},
        {'M', 'C', 'L', 'E', 'L', 'D', 'N', 'V', 'L', 'G', 'P', 'T'},
        {'O', 'J', 'A', 'A', 'V', 'I', 'O', 'T', 'E', 'E', 'P', 'X'},
        {'C', 'D', 'B', 'P', 'H', 'I', 'A', 'W', 'V', 'X', 'U', 'I'},
        {'L', 'G', 'O', 'S', 'S', 'B', 'R', 'Q', 'I', 'A', 'P', 'K'},
        {'E', 'O', 'I', 'G', 'L', 'P', 'S', 'D', 'S', 'F', 'W', 'P'},
        {'W', 'F', 'K', 'E', 'G', 'O', 'L', 'F', 'I', 'F', 'R', 'S'},
        {'O', 'T', 'R', 'U', 'O', 'C', 'D', 'O', 'O', 'F', 'T', 'P'},
        {'C', 'A', 'R', 'P', 'E', 'T', 'R', 'W', 'N', 'G', 'V', 'Z'}
    };

    static string[] Words = new string[] 
    {
        "CARPET",
        "CHAIR",
        "DOG",
        "BALL",
        "DRIVEWAY",
        "FISHING",
        "FOODCOURT",
        "FRIDGE",
        "GOLF",
        "MAXIMIZATION",
        "PUPPY",
        "SPACE",
        "TABLE",
        "TELEVISION",
        "WELCOME",
        "WINDOW"
    };

    // This function searches in all 8-direction
    // from point (row, col) in grid[, ]
    static bool search2D(char[,] Grid, int row,
                         int col, String word)
    {
        // If first character of word doesn't match
        // with given starting point in grid.
        if (Grid[row, col] != word[0])
        {
            return false;
        }

        int len = word.Length;

        // Search word in all 8 directions
        // starting from (row, col)
        for (int dir = 0; dir < 8; dir++)
        {
            // Initialize starting point
            // for current direction
            int k, rd = row + x[dir], cd = col + y[dir];

            // First character is already checked,
            // match remaining characters
            for (k = 1; k < len; k++)
            {
                // If out of bound break
                if (rd >= R || rd < 0 || cd >= C || cd < 0)
                {
                    break;
                }

                // If not matched, break
                if (Grid[rd, cd] != word[k])
                {
                    break;
                }

                // Moving in particular direction
                rd += x[dir];
                cd += y[dir];
            }

            // If all character matched, then value of k
            // must be equal to length of word
            if (k == len)
            {
                return true;
            }
        }
        return false;
    }

    // Searches given word in a given
    // matrix in all 8 directions
    static void patternSearch(char[,] Grid,
                              String word)
    {
        // Consider every point as starting
        // point and search given word
        for (int row = 0; row < R; row++)
        {
            for (int col = 0; col < C; col++)
            {
                if (search2D(Grid, row, col, word))
                {
                    int endposTest = word.IndexOf(word[word.Length - 1]) -1;
                    Console.WriteLine(word + "" + " found at (" + col + "," + row + ") to (" + col + "," + endposTest + ")");
                }
            }
        }
    }

    static void Main(string[] args)
    {
        R = 12;
        C = 12;
        Console.WriteLine("Word Search");

        for (int y = 0; y < 12; y++)
        {
            for (int x = 0; x < 12; x++)
            {
                Console.Write(Grid[y, x]);
                Console.Write(' ');
            }
            Console.WriteLine("");

        }

        Console.WriteLine("");
        Console.WriteLine("Found Words");
        Console.WriteLine("------------------------------");

        //foreach (string word in Words)
        //{
        //    patternSearch(Grid, word);
        //    Console.WriteLine();
        //}

        FindWords();

        Console.WriteLine("------------------------------");
        Console.WriteLine("");
        Console.WriteLine("Press any key to end");
        Console.ReadKey();
    }

    private static void FindWords()
    {
        foreach (string word in Words)
        {
            patternSearch(Grid, word);
            Console.WriteLine("");
        }
        
    }
}
//用于全方位搜索
静态int[]x={-1,-1,0,0,1,1,1,1};
静态int[]y={-1,0,1,-1,1,0,1};
静态字符[,]网格=新字符[,]{
{'C','P','K','X','O','I','G','H','S','F','C','H'},
{'Y','G','W','R','I','A','H','C','Q','R','X','K'},
{M',A',X',I',M',I',Z',A',T',I',O',N'},
{'E','T','W','Z','N','L','W','G','E','D','Y','W'},
{M',C',L',E',L',D',N',V',L',G',P',T'},
{'O','J','A','A','V','I','O','T','E','E','P','X'},
{'C','D','B','P','H','I','A','W','V','X','U','I'},
{'L','G','O','S','S','B','R','Q','I','A','P','K'},
{'E','O','I','G','L','P','S','D','S','F','W','P'},
{'W','F','K','E','G','O','L','F','I','F','R','S'},
{'O','T','R','U','O','C','D','O','O','F','T','P'},
{'C','A','R','P','E','T','R','W','N','G','V','Z'}
};
静态字符串[]单词=新字符串[]
{
“地毯”,
“主席”,
“狗”,
“球”,
“车道”,
“钓鱼”,
“FOODCOURT”,
“冰箱”,
“高尔夫”,
“最大化”,
“小狗”,
“空间”,
“表”,
“电视”,
“欢迎”,
“窗口”
};
//此函数在所有8个方向上搜索
//从栅格[,]中的点(行、列)
静态布尔搜索2D(字符[,]网格,整数行,
整数列(字符串字)
{
//如果单词的第一个字符不匹配
//在网格中具有给定的起点。
if(网格[行,列]!=字[0])
{
返回false;
}
int len=字长;
//从8个方向搜索单词
//从(行、列)开始
对于(int dir=0;dir<8;dir++)
{
//初始化起始点
//用于当前方向
int k,rd=row+x[dir],cd=col+y[dir];
//第一个字符已被选中,
//匹配剩余字符
对于(k=1;k=R | | rd<0 | | cd>=C | | cd<0)
{
打破
}
//如果不匹配,则断开
if(Grid[rd,cd]!=word[k])
{
打破
}
//向特定方向移动
rd+=x[dir];
cd+=y[dir];
}
//如果所有字符都匹配,则k的值
//必须等于单词的长度
if(k==len)
{
返回true;
}
}
返回false;
}
//在给定的单词中搜索给定的单词
//八个方向的矩阵
静态无效模式搜索(字符[,]网格,
(字符串)
{
把每一点当作出发点。
//指向并搜索给定的单词
for(int行=0;行
}


我认为唯一的问题是在显示结果时没有考虑结果的方向。只需应用以下更改:

static bool search2D(char[,]Grid,int row,int col,String word,out int wordDir)//添加out参数wordDir
{
wordDir=0;//初始化wordDir
//[...]
/[…]
if(k==len)
{
wordDir=dir;//找到解决方案时为wordDir赋值
返回true;
}
//[...]
/[…]
int dir;
if(搜索2D(网格、行、列、字、输出方向))
{
int lastRowIndex=row+x[dir]*(word.Length-1);//计算实际的最终坐标
int lastColIndex=col+y[dir]*(word.Length-1);
Trace.WriteLine(word++,位于(“+col+”,“+row+”)到(“+lastColIndex+”,“+lastRowIndex+”));
}