Toliak

Untitled

Feb 14th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static int randoms = 0;
  13.  
  14. static void Main(string[] args)
  15. {
  16. for (int zap = 0; zap < 1000; zap++)
  17. {
  18. main_();
  19. }
  20. sr.Close();
  21. Console.ReadLine();
  22. }
  23.  
  24. static void main_()
  25. {
  26. string textFromFile;
  27. using (FileStream fstream = File.OpenRead(@"C:\Users\Toliak\Documents\W.O.R.K\Школа\10 класс\Московские олимпиады\Информатика\Входные\c2.txt"))
  28. {
  29. byte[] array = new byte[fstream.Length];
  30. fstream.Read(array, 0, array.Length);
  31. textFromFile = System.Text.Encoding.Default.GetString(array);
  32. }
  33.  
  34. string[] textArray = textFromFile.Split('\n');
  35.  
  36. MatchCollection matches = Regex.Matches(textArray[0], @"([0-9]+) ([0-9]+)");
  37. int col_ = Convert.ToInt32(matches[0].Groups[1].Value), numb = Convert.ToInt32(matches[0].Groups[2].Value);
  38. string patt="";
  39. for (int i = 0; i < numb; i++)
  40. {
  41. patt += @"([0-9]) ";
  42. }
  43. patt = patt.Remove(patt.Length - 1); //creating new patt
  44.  
  45. matches = Regex.Matches(textArray[1], patt);
  46. int[] numbers = new int[numb]; //result numbers array
  47. for (int i = 1; i <= numb; i++)
  48. {
  49. numbers[i - 1] = Convert.ToInt32(matches[0].Groups[i].Value); //string to number array
  50. //Только не говори, что через Split можно сделать тоже самое
  51. }
  52.  
  53. int[] col = new int[col_]; //Col array with current col number
  54.  
  55. for (int i = 0; i < col_; i++) { col[i] = 0; }
  56.  
  57. int cPos = 0; //current col position belong to [0; col_]
  58.  
  59. string allPatt = ""; //result
  60. for (int i = 0; i < numb; i++)
  61. {
  62. int useActions = int.MaxValue;
  63. string usePatt = "";
  64. int useCPos=cPos;
  65. int k;
  66. for (k = 0; k < col_; k++)
  67. {
  68. int newActions = 0;
  69. int[] move = { ((k >= cPos) ? (k - cPos) : (col_ - cPos + k)), ((k > cPos) ? (cPos + col_ - k) : (cPos - k)) };
  70. int[] rotate = { (col[k] > numbers[i] ? (10 - col[k] + numbers[i]) : (numbers[i] - col[k])), ((col[k] >= numbers[i]) ? (col[k] - numbers[i]) : (col[k] + 10 - numbers[i])) };
  71. string newPatt = ""; //another patt
  72.  
  73. if (move[0] < move[1])
  74. {
  75. newActions += move[0];
  76. newPatt += fillStringWithChar(@">",move[0]);
  77. }
  78. else
  79. {
  80. newActions += move[1];
  81. newPatt += fillStringWithChar(@"<", move[1]);
  82. }
  83.  
  84. if (rotate[0] < rotate[1])
  85. {
  86. newActions += rotate[0];
  87. newPatt += fillStringWithChar(@"+", rotate[0]);
  88. }
  89. else
  90. {
  91. newActions += rotate[1];
  92. newPatt += fillStringWithChar(@"-", rotate[1]);
  93. }
  94.  
  95. if (newActions < useActions)
  96. {
  97. useActions = newActions;
  98. usePatt = newPatt;
  99. useCPos = k;
  100. }
  101. else if (newActions == useActions)
  102. {
  103. Random rnd = new Random();
  104. randoms++;
  105. if (rnd.Next(0, 2) == 1)
  106. {
  107. useActions = newActions;
  108. usePatt = newPatt;
  109. useCPos = k;
  110. }
  111. }
  112. //Console.WriteLine(newPatt);
  113.  
  114. }
  115. allPatt += usePatt + "P";
  116. cPos = useCPos;
  117. col[useCPos] = numbers[i];
  118. }
  119.  
  120. //Console.WriteLine("\n\n{0}",allPatt);
  121. Console.WriteLine(randoms);
  122. drop_(allPatt);
  123. //Console.ReadLine();
  124.  
  125. }
  126.  
  127. static string fillStringWithChar(string char_, int numb)
  128. {
  129. string ret = "";
  130. for (int i = 0; i < numb; i++)
  131. {
  132. ret += char_;
  133. }
  134. return ret;
  135. }
  136.  
  137. static StreamWriter sr = new StreamWriter(@"C:\Users\Toliak\Documents\W.O.R.K\Школа\10 класс\Московские олимпиады\Информатика\Выходные\_c2.txt", true);
  138.  
  139. static void drop_(string what)
  140. {
  141.  
  142. sr.Write(what + "\n");
  143.  
  144. }
  145.  
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment