mellowdeep

Collect Rsources

Mar 29th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class P_20160228_CollectResources {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.         String resources = scanner.nextLine();
  9.         int rows = scanner.nextInt();
  10.         scanner.nextLine();
  11.         int result = 0;
  12.  
  13.         for (int i = 0; i < rows; i++) {
  14.  
  15.             String[] temp = new String(resources).split(" ");
  16.             String[] steps = scanner.nextLine().split(" ");
  17.             int start = Integer.parseInt(steps[0]);
  18.             int step = Integer.parseInt(steps[1]);
  19.             int tempResult = collectResources(temp, start, step);
  20.  
  21.             if (result < tempResult) {
  22.                 result = tempResult;
  23.             }
  24.         }
  25.         System.out.println(result);
  26.     }
  27.  
  28.     private static int collectResources(String[] resources, int start, int step) {
  29.  
  30.         String[] res = resources[start].split("[^a-zA-Z]+");
  31.         String[] qty = resources[start].split("[^0-9]+");
  32.  
  33.         int sumOfRsources = 0;
  34.         while (res.length != 0) {
  35.             if (start >= resources.length) {
  36.                 start -= resources.length;
  37.                 if (start >= resources.length) {
  38.                     break;
  39.                 }
  40.             }
  41.             res = resources[start].split("[^a-z]+");
  42.             qty = resources[start].split("[^0-9]+");
  43.             if (res.length == 0) {
  44.                 break;
  45.             } else if (res[0].equals("stone") || res[0].equals("gold")
  46.                     || res[0].equals("wood") || res[0].equals("food")) {
  47.  
  48.                 sumOfRsources += qty.length > 0 ? Integer.parseInt(qty[1]) : 1;
  49.                 resources[start] = "*";
  50.             } else {
  51.                 resources[start] = "*";
  52.             }
  53.             start += step;
  54.         }
  55.         return sumOfRsources;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment