如何在二维数组中搜索单词和替换

Posted

技术标签:

【中文标题】如何在二维数组中搜索单词和替换【英文标题】:How to search word and replace in 2D array 【发布时间】:2020-11-30 08:58:28 【问题描述】:

我是 C# 新手。如何搜索整个单词并将其替换为我想要的单词。例如找到“SUCCESS”并替换为“XXXXXXX”。正如您从 if 循环中看到的那样,我可以用 1 个单词替换它,但我想替换整个循环。

谁能给我提示?

public class CrossWordTable
    
        public char[,] CrossWord
        
            get
            
                var table = new char[,]
                
                    'S','U','C','C','E','S','S' ,
                    'E','U','S','S','E','C','U',
                    'U','S','C','C','C','E','C',
                    'S','S','U','C','E','C','C',
                    'S','E','S','S','E','S','S',
                    'U','C','S','E','U','S','S',
                    'C','C','S','S','E','E','S',
                    'S','U','S','S','S','S','E',
                    'U','S','E','S','S','C','S'

                ;
                return table;
            
        

        public void PrintCrossWord()
        
            PrintCrossWord(CrossWord);
        

        static int[] x =  0, 0, -1, 1, 1, -1, -1, 1 ;
        static int[] y =  -1, 1, 0, 0, 1, -1, 1, -1 ;

        public void PrintCrossWord(char[,] crossWordTable)
        
            var totalRowNumber = crossWordTable.GetLength(0);
            var totalColumnNumber = crossWordTable.GetLength(1);
            String word;
            word = "SUCCESS";
            int len = word.Length;

            Console.WriteLine("*** Crossword Table *****");
            for (var row = 0; row < totalRowNumber; row++)
            
                for (var column = 0; column < totalColumnNumber; column++)
                
                    if (CrossWord[row, column] != word[0])
                    



                        
                        Console.Write(crossWordTable[row, column]);
                    
                    else
                    
                        

                        crossWordTable[row, column] = 'X';
                        Console.Write(crossWordTable[row, column]);
                    
                
                Console.WriteLine();
            
            Console.WriteLine("************************");

        

【问题讨论】:

你想换成任何方向的成功阅读吗?水平、垂直、对角线和向后的所有以前的? @insane_developer 是的。我想全方位更换它。 @JayJ 如果它不是一个大数组,你可以在各个方向进行循环。 @VukUskokovic 是这样的吗? len 是单词的长度 for (int dir = 0; dir = R || rd = C | | cd @JayJ 尝试运行代码并从输出中查看,而不是在评论中发布代码。 【参考方案1】:

这是一种方法。这是一个递归解决方案。我第一次尝试重构代码失败了,我选择不花更多时间尝试。

public class CrossWordTable
    
        private readonly char[,] _crossword;
        private readonly string _word;
        private const char MARKER = 'X';
        private readonly int _rowLength;
        private readonly int _colLength;
        private HashSet<(int, int)> _points;

        public CrossWordTable(char[,] crossword, string word)
        
            _crossword = crossword;
            _word = word.ToUpper();
            _rowLength = _crossword.GetLength(0);
            _colLength = _crossword.GetLength(1);
            _points = new HashSet<(int, int)>();
        

        private enum Direction  Right, Down, Left, Up, DiagonalUpRight, DiagonalUpLeft, DiagonalDownRight, DiagonalDownLeft 

        public void PrintCrossWord()
        
            for (int i = 0; i < _rowLength; i++)
            
                for (int j = 0; j < _colLength; j++)
                
                    Console.Write($"_crossword[i, j]  ");
                
                Console.WriteLine();
            
        

        public void SolveCrossWord()
        
            for (int r = 0; r < _rowLength; r++)
            
                for (int c = 0; c < _colLength; c++)
                
                    Explore(r, c, 0, Direction.Right);
                    Explore(r, c, 0, Direction.Down);
                    Explore(r, c, 0, Direction.Left);
                    Explore(r, c, 0, Direction.Up);
                    Explore(r, c, 0, Direction.DiagonalUpRight);
                    Explore(r, c, 0, Direction.DiagonalUpLeft);
                    Explore(r, c, 0, Direction.DiagonalDownRight);
                    Explore(r, c, 0, Direction.DiagonalDownLeft);
                
            
            foreach (var (row, col) in _points)
            
                _crossword[row, col] = MARKER;
            
            PrintCrossWord();
        

        private bool Explore(int row, int col, int wordIndex, Direction direction)
        
            if (wordIndex == _word.Length)
                return true;
            if (_crossword[row, col] == _word[wordIndex])
            
                if (direction == Direction.Right && col + _word.Length - wordIndex <= _colLength)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row, col + 1, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
                else if (direction == Direction.Down && row + _word.Length - wordIndex <= _rowLength)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row + 1, col, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
                else if (direction == Direction.Left && col - (_word.Length - wordIndex) >= -1)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row, col - 1, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
                else if (direction == Direction.Up && row - (_word.Length - wordIndex) >= -1)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row - 1, col, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
                else if (direction == Direction.DiagonalUpRight && row - (_word.Length - wordIndex) >= -1 && col + _word.Length - wordIndex <= _colLength)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row - 1, col + 1, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
                else if (direction == Direction.DiagonalUpLeft && row - (_word.Length - wordIndex) >= -1 && col - (_word.Length - wordIndex) >= -1)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row - 1, col - 1, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
                else if (direction == Direction.DiagonalDownRight && row + _word.Length - wordIndex <= _rowLength && col + _word.Length - wordIndex <= _colLength)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row + 1, col + 1, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
                else if (direction == Direction.DiagonalDownLeft && row + _word.Length - wordIndex <= _rowLength && col - (_word.Length - wordIndex) >= -1)
                
                    var added = _points.Add((row, col));
                    if (!Explore(row + 1, col - 1, wordIndex + 1, direction))
                    
                        if (added)
                            _points.Remove((row, col));
                        return false;
                    
                    return true;
                
            
            return false;
        
    

该类采用二维数组,调用SolveCrossWord() 解决填字游戏并打印结果。

【讨论】:

以上是关于如何在二维数组中搜索单词和替换的主要内容,如果未能解决你的问题,请参考以下文章

C语言,如何找到二维数组中单词的长度?

如何在PHP的二维数组中进行模糊搜索?

每日一题648. 单词替换

替换二维numpy数组中字符的所有实例[重复]

如何在从左到右和从上到下排序的二维数组中搜索数字?

在二维数组中搜索 tableView objC