Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace ConsoleApplication1
- {
- class Program
- {
- static int randoms = 0;
- static void Main(string[] args)
- {
- for (int zap = 0; zap < 1000; zap++)
- {
- main_();
- }
- sr.Close();
- Console.ReadLine();
- }
- static void main_()
- {
- string textFromFile;
- using (FileStream fstream = File.OpenRead(@"C:\Users\Toliak\Documents\W.O.R.K\Школа\10 класс\Московские олимпиады\Информатика\Входные\c2.txt"))
- {
- byte[] array = new byte[fstream.Length];
- fstream.Read(array, 0, array.Length);
- textFromFile = System.Text.Encoding.Default.GetString(array);
- }
- string[] textArray = textFromFile.Split('\n');
- MatchCollection matches = Regex.Matches(textArray[0], @"([0-9]+) ([0-9]+)");
- int col_ = Convert.ToInt32(matches[0].Groups[1].Value), numb = Convert.ToInt32(matches[0].Groups[2].Value);
- string patt="";
- for (int i = 0; i < numb; i++)
- {
- patt += @"([0-9]) ";
- }
- patt = patt.Remove(patt.Length - 1); //creating new patt
- matches = Regex.Matches(textArray[1], patt);
- int[] numbers = new int[numb]; //result numbers array
- for (int i = 1; i <= numb; i++)
- {
- numbers[i - 1] = Convert.ToInt32(matches[0].Groups[i].Value); //string to number array
- //Только не говори, что через Split можно сделать тоже самое
- }
- int[] col = new int[col_]; //Col array with current col number
- for (int i = 0; i < col_; i++) { col[i] = 0; }
- int cPos = 0; //current col position belong to [0; col_]
- string allPatt = ""; //result
- for (int i = 0; i < numb; i++)
- {
- int useActions = int.MaxValue;
- string usePatt = "";
- int useCPos=cPos;
- int k;
- for (k = 0; k < col_; k++)
- {
- int newActions = 0;
- int[] move = { ((k >= cPos) ? (k - cPos) : (col_ - cPos + k)), ((k > cPos) ? (cPos + col_ - k) : (cPos - k)) };
- 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])) };
- string newPatt = ""; //another patt
- if (move[0] < move[1])
- {
- newActions += move[0];
- newPatt += fillStringWithChar(@">",move[0]);
- }
- else
- {
- newActions += move[1];
- newPatt += fillStringWithChar(@"<", move[1]);
- }
- if (rotate[0] < rotate[1])
- {
- newActions += rotate[0];
- newPatt += fillStringWithChar(@"+", rotate[0]);
- }
- else
- {
- newActions += rotate[1];
- newPatt += fillStringWithChar(@"-", rotate[1]);
- }
- if (newActions < useActions)
- {
- useActions = newActions;
- usePatt = newPatt;
- useCPos = k;
- }
- else if (newActions == useActions)
- {
- Random rnd = new Random();
- randoms++;
- if (rnd.Next(0, 2) == 1)
- {
- useActions = newActions;
- usePatt = newPatt;
- useCPos = k;
- }
- }
- //Console.WriteLine(newPatt);
- }
- allPatt += usePatt + "P";
- cPos = useCPos;
- col[useCPos] = numbers[i];
- }
- //Console.WriteLine("\n\n{0}",allPatt);
- Console.WriteLine(randoms);
- drop_(allPatt);
- //Console.ReadLine();
- }
- static string fillStringWithChar(string char_, int numb)
- {
- string ret = "";
- for (int i = 0; i < numb; i++)
- {
- ret += char_;
- }
- return ret;
- }
- static StreamWriter sr = new StreamWriter(@"C:\Users\Toliak\Documents\W.O.R.K\Школа\10 класс\Московские олимпиады\Информатика\Выходные\_c2.txt", true);
- static void drop_(string what)
- {
- sr.Write(what + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment