[英]How to search word and replace in 2D array
我是 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("************************");
}
這是一種方法。 這是一個遞歸解決方案。 我第一次嘗試重構代碼失敗了,我選擇不花更多時間嘗試。
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;
}
}
class 采用二維數組,調用SolveCrossWord()
求解填字游戲並打印結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.