Advertisement
CR7CR7

Snake

Apr 6th, 2023
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. public class Main {
  5.  
  6.   public static void main(String[] args) {
  7.     // Create a scanner object to read the input
  8.     Scanner scanner = new Scanner(System.in);
  9.  
  10.     // Read the snake size and number of jumps from the input
  11.     int size = scanner.nextInt();
  12.     int jumps = scanner.nextInt();
  13.  
  14.     // Read the path of fruits from the input
  15.     ArrayList<Integer> path = new ArrayList<Integer>();
  16.     while (scanner.hasNextInt()) {
  17.       path.add(scanner.nextInt());
  18.     }
  19.  
  20.     // Close the scanner object
  21.     scanner.close();
  22.  
  23.     // Initialize the growth balance to zero
  24.     int balance = 0;
  25.  
  26.     // Loop through the path and update the snake size and growth balance
  27.     for (int i = 0; i < path.size(); i++) {
  28.       // If the fruit is neutral, do nothing
  29.       if (path.get(i) == 0) {
  30.         continue;
  31.       }
  32.       // If the fruit is growing, increase the snake size and decrease the growth balance
  33.       else if (path.get(i) == 1) {
  34.         size++;
  35.         balance--;
  36.       }
  37.       // If the fruit is shrinking, check if there are jumps available
  38.       else if (path.get(i) == -1) {
  39.         // If there are jumps, use one and skip the fruit
  40.         if (jumps > 0) {
  41.           jumps--;
  42.         }
  43.         // If there are no jumps, decrease the snake size and increase the growth balance
  44.         else {
  45.           size--;
  46.           balance++;
  47.         }
  48.       }
  49.       // If the snake size becomes zero or negative, break the loop
  50.       if (size <= 0) {
  51.         break;
  52.       }
  53.     }
  54.  
  55.     // Print the final snake size and growth balance to the output
  56.     System.out.println(size + " " + balance);
  57.   }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement