Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace WordInWord
- {
- static class Program
- {
- static HashSet<Point> Copy(this HashSet<Point> set)
- {
- var newSet = new HashSet<Point>();
- foreach (var point in set)
- {
- newSet.Add(point);
- }
- return newSet;
- }
- static Point[] GetNeighbors(Point point)
- {
- return new[]
- {
- new Point(point.X, point.Y - 1),
- new Point(point.X, point.Y + 1),
- new Point(point.X - 1, point.Y),
- new Point(point.X + 1, point.Y - 1),
- new Point(point.X + 1, point.Y),
- new Point(point.X - 1, point.Y - 1),
- new Point(point.X - 1, point.Y + 1),
- new Point(point.X + 1, point.Y + 1),
- }
- .Where(p => p.X >= 0 && p.X < 5 && p.Y >= 0 && p.Y < 5).ToArray();
- }
- private static HashSet<string> dict;
- private static Field field;
- private static HashSet<string> words;
- static void A(object e)
- {
- var start = (Point)e;
- var stack = new Queue<Tuple<Point, HashSet<Point>, string>>();
- stack.Enqueue(Tuple.Create(start, new HashSet<Point> { start }, "" + field[start]));
- while (stack.Any())
- {
- var cur = stack.Dequeue();
- var word = cur.Item3;
- if (word.Length >= 5)
- {
- if (dict.Contains(word) && !words.Contains(word))
- {
- Console.WriteLine(word);
- words.Add(word);
- }
- }
- foreach (var neighbor in GetNeighbors(cur.Item1))
- {
- if (!cur.Item2.Contains(neighbor))
- {
- var newSet = cur.Item2.Copy();
- newSet.Add(cur.Item1);
- stack.Enqueue(Tuple.Create(neighbor, newSet, cur.Item3 + field[neighbor]));
- }
- }
- }
- }
- static void Main()
- {
- field = new Field(new[]
- {
- "ельиа",
- "итонц",
- "адпчи",
- "гпскн",
- "уриот",
- });
- dict =
- new HashSet<string>(
- File.ReadAllLines("zdb-win.txt").Select(line => line.Trim()));
- words = new HashSet<string>();
- var threads = new List<Thread>();
- for (int i = 0; i < 25; i++)
- threads.Add(new Thread(A));
- threads[0].Start(new Point(0, 0));
- threads[1].Start(new Point(0, 1));
- threads[2].Start(new Point(0, 2));
- threads[3].Start(new Point(0, 3));
- threads[4].Start(new Point(0, 4));
- threads[5].Start(new Point(1, 0));
- threads[6].Start(new Point(1, 1));
- threads[7].Start(new Point(1, 2));
- threads[8].Start(new Point(1, 3));
- threads[9].Start(new Point(1, 4));
- threads[10].Start(new Point(2, 0));
- threads[11].Start(new Point(2, 1));
- threads[12].Start(new Point(2, 2));
- threads[13].Start(new Point(2, 3));
- threads[14].Start(new Point(2, 4));
- threads[15].Start(new Point(3, 0));
- threads[16].Start(new Point(3, 1));
- threads[17].Start(new Point(3, 2));
- threads[18].Start(new Point(3, 3));
- threads[19].Start(new Point(3, 4));
- threads[20].Start(new Point(4, 0));
- threads[21].Start(new Point(4, 1));
- threads[22].Start(new Point(4, 2));
- threads[23].Start(new Point(4, 3));
- threads[24].Start(new Point(4, 4));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement