public BoardChar SetNeighbors(char[,] board, BoardChar bc) { int xmin = 0; int ymin = 0; int xmax = rows - 1; int ymax = cols - 1; bc.North = new BoardChar(); bc.South = new BoardChar(); bc.West = new BoardChar(); bc.East = new BoardChar(); if (bc.Pos.x > xmin) { // North bc.North.Pos.x = bc.Pos.x - 1; bc.North.Pos.y = bc.Pos.y; } else { bc.North.Pos = null; } if (bc.Pos.x < xmax) { // South bc.South.Pos.x = bc.Pos.x + 1; bc.South.Pos.y = bc.Pos.y; } else { bc.South.Pos = null; } if (bc.Pos.y > ymin) { // West bc.West.Pos.x = bc.Pos.x; bc.West.Pos.y = bc.Pos.y - 1; } else { bc.West.Pos = null; } if (bc.Pos.y < ymax) { // East bc.East.Pos.x = bc.Pos.x; bc.East.Pos.y = bc.Pos.y + 1; } else { bc.East.Pos = null; } return(bc); }
public bool Exist(char[,] board, string word) { char[] arr = word.ToArray(); List <XY> result = GetCharList(arr[0], board); rows = board.GetLength(0); cols = board.GetLength(1); foreach (var xy in result) { BoardChar bc = new BoardChar(); finalResult = true; bc.Pos.x = xy.x; bc.Pos.y = xy.y; bc = SetNeighbors(board, bc); for (int i = 0; i < arr.Length; i++) { bool cur = FindCharInBC(board, bc, arr[i]); bool north = FindCharInBC(board, bc.North, arr[i]); bool south = FindCharInBC(board, bc.South, arr[i]); bool east = FindCharInBC(board, bc.East, arr[i]); bool west = FindCharInBC(board, bc.West, arr[i]); finalResult = finalResult && (cur || north || south || east || west); if (finalResult == false) { break; } if (bc.North.IsMatched) { bc = bc.North; } else if (bc.South.IsMatched) { bc = bc.South; } else if (bc.East.IsMatched) { bc = bc.East; } else if (bc.West.IsMatched) { bc = bc.West; } bc = SetNeighbors(board, bc); } if (finalResult) { return(true); } } return(false); }
public bool FindCharInBC(char[,] board, BoardChar bc, char ch) { if (bc.IsVisited == true) { return(false); } bc.IsVisited = true; if (bc.Pos == null) { return(false); } if (board[bc.Pos.x, bc.Pos.y] == ch) { bc.IsMatched = true; return(true); } return(false); }