Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace WordInWord
  10. {
  11.     static class Program
  12.     {
  13.         static HashSet<Point> Copy(this HashSet<Point> set)
  14.         {
  15.             var newSet = new HashSet<Point>();
  16.             foreach (var point in set)
  17.             {
  18.                 newSet.Add(point);
  19.             }
  20.             return newSet;
  21.         }
  22.  
  23.         static Point[] GetNeighbors(Point point)
  24.         {
  25.             return new[]
  26.             {
  27.                 new Point(point.X, point.Y - 1),
  28.                 new Point(point.X, point.Y + 1),
  29.                 new Point(point.X - 1, point.Y),
  30.                 new Point(point.X + 1, point.Y - 1),
  31.                 new Point(point.X + 1, point.Y),
  32.                 new Point(point.X - 1, point.Y - 1),
  33.                 new Point(point.X - 1, point.Y + 1),
  34.                 new Point(point.X + 1, point.Y + 1),
  35.             }
  36.             .Where(p => p.X >= 0 && p.X < 5 && p.Y >= 0 && p.Y < 5).ToArray();
  37.         }
  38.  
  39.  
  40.         private static HashSet<string> dict;
  41.         private static Field field;
  42.         private static HashSet<string> words;
  43.  
  44.         static void A(object e)
  45.         {
  46.             var start = (Point)e;
  47.             var stack = new Queue<Tuple<Point, HashSet<Point>, string>>();
  48.             stack.Enqueue(Tuple.Create(start, new HashSet<Point> { start }, "" + field[start]));
  49.             while (stack.Any())
  50.             {
  51.                 var cur = stack.Dequeue();
  52.                 var word = cur.Item3;
  53.                 if (word.Length >= 5)
  54.                 {
  55.                     if (dict.Contains(word) && !words.Contains(word))
  56.                     {
  57.                         Console.WriteLine(word);
  58.                         words.Add(word);
  59.                     }
  60.                 }
  61.                 foreach (var neighbor in GetNeighbors(cur.Item1))
  62.                 {
  63.                     if (!cur.Item2.Contains(neighbor))
  64.                     {
  65.                         var newSet = cur.Item2.Copy();
  66.                         newSet.Add(cur.Item1);
  67.                         stack.Enqueue(Tuple.Create(neighbor, newSet, cur.Item3 + field[neighbor]));
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.  
  73.         static void Main()
  74.         {
  75.             field = new Field(new[]
  76.             {
  77.                 "ельиа",
  78.                 "итонц",
  79.                 "адпчи",
  80.                 "гпскн",
  81.                 "уриот",
  82.             });
  83.             dict =
  84.                 new HashSet<string>(
  85.                     File.ReadAllLines("zdb-win.txt").Select(line => line.Trim()));
  86.             words = new HashSet<string>();
  87.             var threads = new List<Thread>();
  88.             for (int i = 0; i < 25; i++)
  89.                 threads.Add(new Thread(A));
  90.             threads[0].Start(new Point(0, 0));
  91.             threads[1].Start(new Point(0, 1));
  92.             threads[2].Start(new Point(0, 2));
  93.             threads[3].Start(new Point(0, 3));
  94.             threads[4].Start(new Point(0, 4));
  95.             threads[5].Start(new Point(1, 0));
  96.             threads[6].Start(new Point(1, 1));
  97.             threads[7].Start(new Point(1, 2));
  98.             threads[8].Start(new Point(1, 3));
  99.             threads[9].Start(new Point(1, 4));
  100.             threads[10].Start(new Point(2, 0));
  101.             threads[11].Start(new Point(2, 1));
  102.             threads[12].Start(new Point(2, 2));
  103.             threads[13].Start(new Point(2, 3));
  104.             threads[14].Start(new Point(2, 4));
  105.             threads[15].Start(new Point(3, 0));
  106.             threads[16].Start(new Point(3, 1));
  107.             threads[17].Start(new Point(3, 2));
  108.             threads[18].Start(new Point(3, 3));
  109.             threads[19].Start(new Point(3, 4));
  110.             threads[20].Start(new Point(4, 0));
  111.             threads[21].Start(new Point(4, 1));
  112.             threads[22].Start(new Point(4, 2));
  113.             threads[23].Start(new Point(4, 3));
  114.             threads[24].Start(new Point(4, 4));
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement