मैं सी # में एक शब्द खोज पहेली लिख रहा हूं और मैं शब्दों के लिए अक्षरों के दो आयामी सरणी को एक सुंदर तरीके से खोजना चाहता हूं।मैं किसी भी दिशा में दो आयामी सरणी कैसे खोजूं
दाएं, ऊपर से नीचे इत्यादि तक की मूल खोजों को लिखना मुश्किल नहीं है, हालांकि सरणी के पार तिरछे रूप से खोज करते समय चीजें थोड़ा वर्बोज़ शुरू हो रही हैं। मुझे यह काम मिल गया है लेकिन मुझे यकीन है कि वहाँ एक बेहतर समाधान है।
यहां एक पहेली का एक उदाहरण है जिसे मैं हल करने की कोशिश कर रहा हूं, किसी भी विचार की सराहना की जाएगी।
BXXD
AXEX
TRXX
FXXX
बैट फ्रेड
संपादित करें: परिणाम: मुझे कम्पास अंक खोज
संपादित करने के विचार देने के लिए स्टीव के लिए कुडोस खोज के लिए ar1, y1 और x2, y2 ar के शब्दों के समन्वय को वापस करने की आवश्यकता है रे।
संपादित करें: सरणी को खोजने के लिए एक अच्छा एल्गोरिदम प्रदान करने के लिए एंटी के लिए धन्यवाद।
यह अंतिम परिणाम मैं आया था। मैंने इसे एंटी के जवाब में एल्गोरिदम पर आधारित किया है, जिसने इसे शुरुआत के लिए सरणी ऑफसेट्स और किसी भी शब्द के अंत में वापस करने के लिए संशोधित किया है। यह एल्गोरिदम एक वर्ड सर्च गेम का इस्तेमाल किया जा रहा है जिसे मैं अपने बच्चों के लिए डब्ल्यूपीएफ में लिख रहा हूं। मुझे मदद करने के लिए हर किसी के लिए धन्यवाद। जब मैं सम्मानजनक हो तो मैं ऐप पर एक लिंक पोस्ट करूंगा।
public class Range
{
public Range(Coordinate start, Coordinate end)
{
Start = start;
End = end;
}
public Coordinate Start { get; set; }
public Coordinate End { get; set; }
}
public class Coordinate
{
public Coordinate(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
}
public class WordSearcher
{
public WordSearcher(char[,] puzzle)
{
Puzzle = puzzle;
}
public char[,] Puzzle { get; set; }
// represents the array offsets for each
// character surrounding the current one
private Coordinate[] directions =
{
new Coordinate(-1, 0), // West
new Coordinate(-1,-1), // North West
new Coordinate(0, -1), // North
new Coordinate(1, -1), // North East
new Coordinate(1, 0), // East
new Coordinate(1, 1), // South East
new Coordinate(0, 1), // South
new Coordinate(-1, 1) // South West
};
public Range Search(string word)
{
// scan the puzzle line by line
for (int y = 0; y < Puzzle.GetLength(0); y++)
{
for (int x = 0; x < Puzzle.GetLength(1); x++)
{
if (Puzzle[y, x] == word[0])
{
// and when we find a character that matches
// the start of the word, scan in each direction
// around it looking for the rest of the word
var start = new Coordinate(x, y);
var end = SearchEachDirection(word, x, y);
if (end != null)
{
return new Range(start, end);
}
}
}
}
return null;
}
private Coordinate SearchEachDirection(string word, int x, int y)
{
char[] chars = word.ToCharArray();
for (int direction = 0; direction < 8; direction++)
{
var reference = SearchDirection(chars, x, y, direction);
if (reference != null)
{
return reference;
}
}
return null;
}
private Coordinate SearchDirection(char[] chars, int x, int y, int direction)
{
// have we ve moved passed the boundary of the puzzle
if (x < 0 || y < 0 || x >= Puzzle.GetLength(1) || y >= Puzzle.GetLength(0))
return null;
if (Puzzle[y, x] != chars[0])
return null;
// when we reach the last character in the word
// the values of x,y represent location in the
// puzzle where the word stops
if (chars.Length == 1)
return new Coordinate(x, y);
// test the next character in the current direction
char[] copy = new char[chars.Length - 1];
Array.Copy(chars, 1, copy, 0, chars.Length - 1);
return SearchDirection(copy, x + directions[direction].X, y + directions[direction].Y, direction);
}
}
आपको बताएंगे कि अपने वर्तमान विधि किसी एक बेहतर तरीका हो सकता है है। जब तक हम नहीं जानते कि आप वर्तमान में इसे कैसे कर रहे हैं, हम वास्तव में इसमें सुधार नहीं कर सकते हैं। – gingerbreadboy
यह लंबी और बदसूरत है और आलोचना करने के लिए समय के लायक नहीं है। मैं उम्मीद कर रहा था कि किसी के पास डेटा संरचनाओं और एल्गोरिदम का बेहतर ज्ञान होगा, क्योंकि मैं मुझे सही दिशा में इंगित करने में सक्षम हूं। –