Advertisement
ogv

Untitled

ogv
Sep 12th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace ConsoleApp3
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] a;
  11.  
  12.             var random = new Random();
  13.             int n = 10;
  14.             a = new int[n];
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 a[i] = random.Next(3);
  19.             }
  20.  
  21.             //a = new[] {1,0,2};
  22.             Console.WriteLine(string.Join(",", a));
  23.  
  24.             Stopwatch stopwatch = new Stopwatch();
  25.             stopwatch.Start();
  26.             Console.WriteLine("brute: {0}", Brute(a));
  27.             stopwatch.Stop();
  28.             Console.WriteLine($"brute took {stopwatch.ElapsedMilliseconds} ms");
  29.             Console.ReadKey();
  30.         }
  31.  
  32.         private static int Brute(int[] a)
  33.         {
  34.             int n = a.Length;
  35.             bool[] activated = new bool[n];
  36.  
  37.             int min = n;
  38.  
  39.             int i = 0;
  40.             for(;;)
  41.             {
  42.                 bool coversAll = true;
  43.                 for (int probe = 0; probe < n; probe++)
  44.                 {
  45.                     bool covers = false;
  46.                     for (int k = 0; k < n; k++)
  47.                     {
  48.                         if (activated[k] && probe >= k - a[k] && probe <= k + a[k])
  49.                         {
  50.                             covers = true;
  51.                             break;
  52.                         }
  53.                     }
  54.  
  55.                     if (!covers)
  56.                     {
  57.                         coversAll = false;
  58.                         break;
  59.                     }
  60.                 }
  61.  
  62.                 if (coversAll)
  63.                 {
  64.                     int m = 0;
  65.                     for (int k = 0; k < n; k++)
  66.                     {
  67.                         if (activated[k]) m++;
  68.                     }
  69.  
  70.                     if (m < min) min = m;
  71.                 }
  72.  
  73.                 if (!activated[i])
  74.                 {
  75.                     activated[i] = true;
  76.                 }
  77.                 else
  78.                 {
  79.                     while (i < n && activated[i])
  80.                     {
  81.                         activated[i++] = false;
  82.                     }
  83.                    
  84.                     if (i >= n)
  85.                     {
  86.                         break;
  87.                     }
  88.                     activated[i] = true;
  89.                     i = 0;
  90.                 }
  91.             }
  92.  
  93.             return min;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement