ekostadinov

Greedy Midgets

Feb 22nd, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.07 KB | None | 0 0
  1.                                            
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8.  
  9. namespace GreedyMidgets
  10. {
  11.     class GreedIsGood
  12.     {
  13.         static int coins;
  14.  
  15.         static void Main()
  16.         {
  17.             string valley = Console.ReadLine();
  18.            
  19.             string[] valleyMap = valley.Split(',');
  20.             List<int> valleySteps = new List<int>();
  21.  
  22.             foreach (var item in valleyMap)
  23.             {
  24.                 //Console.WriteLine(item);
  25.                 valleySteps.Add(int.Parse(item));
  26.             }
  27.  
  28.             int patterns = int.Parse(Console.ReadLine());
  29.                        
  30.             string patternMap = string.Empty;
  31.            
  32.             for (int i = 0; i < patterns; i++)
  33.             {
  34.                 patternMap += Console.ReadLine();
  35.                 if (i < patterns - 1)
  36.                 {
  37.                     patternMap += '*';  
  38.                 }                
  39.             }
  40.                        
  41.             //Console.WriteLine(pattMap);
  42.                        
  43.             int bestSum = 0;            
  44.             var dwarf = 0;
  45.  
  46.             string[] allPatterns = patternMap.Split('*');
  47.                        
  48.             foreach (var pattern in allPatterns)
  49.             {
  50.                //Console.WriteLine(pattern);
  51.                List<int> states = new List<int>();
  52.  
  53.                string[] currLine = pattern.Split(',');
  54.  
  55.                foreach (var num in currLine)
  56.                {
  57.                    //Console.WriteLine(num);
  58.                    states.Add(int.Parse(num));
  59.                }
  60.  
  61.                GetCoins(valleySteps,states, dwarf); //search valley by current pattern
  62.                
  63.                //find best found sum of collected coins
  64.                if (coins > bestSum)
  65.                {
  66.                    bestSum = coins;
  67.                    coins = 0; //must reset the coins for the next pattern map
  68.                }
  69.  
  70.             }
  71.                        
  72.             Console.WriteLine(bestSum);
  73.         }
  74.  
  75.         private static int GetCoins(List<int> valleySteps, List<int> states, int dwarf)
  76.         {
  77.             List<int> visited = new List<int>();
  78.  
  79.             dwarf = 0; //dwarf at the valley start index [0]
  80.             coins += valleySteps[0];
  81.            
  82.  
  83.             for (int i = 0, p = 0; i < valleySteps.Count && p < states.Count; i++, p++) //search in valley and current pattern
  84.             {
  85.                 //if pattern is empty/used - start from the begging/top
  86.                 if (p == states.Count - 1)
  87.                 {
  88.  
  89.                     //find visited positions
  90.                     visited.Add(dwarf);
  91.  
  92.                     //set dwarf - moves by i/valleySteps
  93.                     dwarf += states[p]; //dwarf at the valley start index [0] + next step p/states
  94.  
  95.                     try
  96.                     {
  97.                         // break when dwarf steps on visited position - must remember previous steps(can use p/states)  
  98.                         foreach (var steps in visited)
  99.                         {
  100.                             if (dwarf == steps)
  101.                             {
  102.                                 return coins;
  103.                             }
  104.                         }
  105.  
  106.                         //collects coins valleySteps[i] using p/states as i (or in other words - dwarf itself)
  107.                         coins += valleySteps[dwarf];
  108.                     }
  109.                     //breake when dwarf out of rangge
  110.                     catch (ArgumentOutOfRangeException)
  111.                     {
  112.                         return coins;
  113.                     }
  114.  
  115.                     p = - 1;   //because must be 0 (states[p] = 0), but p++ in for loop condition change it                
  116.                    
  117.                 }
  118.                 else
  119.                 {
  120.                    
  121.                     //find visited positions
  122.                     visited.Add(dwarf);
  123.  
  124.                     //set dwarf - moves by i/valleySteps
  125.                     dwarf += states[p]; //dwarf at the valley start index [0] + next step p/states
  126.  
  127.                     try
  128.                     {
  129.                         // break when dwarf steps on visited position - must remember previous steps(can use p/states)  
  130.                         foreach (var steps in visited)
  131.                         {
  132.                             if (dwarf == steps)
  133.                             {
  134.                                 return coins;
  135.                             }
  136.                         }
  137.  
  138.                         //collects coins valleySteps[i] using p/states as i (or in other words - dwarf itself)
  139.                         coins += valleySteps[dwarf];
  140.                     }
  141.                     //breake when dwarf out of rangge
  142.                     catch (ArgumentOutOfRangeException)
  143.                     {
  144.                         return coins;
  145.                     }
  146.                 }    
  147.            }
  148.  
  149.             return coins;
  150.            
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment