Rezultati

Up. imeNalogaJezikRezultatČas oddaje
ekipa5-2017 Jungle Speed Java 100/100OK 11. maj '17 @ 18:26

Test Točke Porabljen spomin Porabljen čas Status
#1 9/9 37,953 MiB 0,035 s OK
#2 9/9 36,926 MiB 0,073 s OK
#3 9/9 33,492 MiB 0,052 s OK
#4 9/9 39,637 MiB 0,033 s OK
#5 9/9 40,145 MiB 0,037 s OK
#6 9/9 40,863 MiB 0,029 s OK
#7 9/9 39,664 MiB 0,045 s OK
#8 9/9 39,699 MiB 0,050 s OK
#9 9/9 39,207 MiB 0,043 s OK
#10 9/9 43,648 MiB 0,036 s OK
#11 10/10 34,641 MiB 0,079 s OK

Ocenjevani program (Jungle.java):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by ekipa5-2017 on 5/11/17.
 */
public class Jungle {

    static Player[] players;
    static int numberOfPlayers;

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String inputLine = null;

        inputLine = br.readLine();
        numberOfPlayers = Integer.parseInt(inputLine);

        inputLine = br.readLine();
        List<Integer> cards = Arrays.stream(inputLine.split(" ")).map(Integer::parseInt).collect(Collectors.toList());

        players = new Player[numberOfPlayers];

        for (int i = 0; i < numberOfPlayers; i++)
            players[i] = new Player();

        for (int cardIndex = 0; cardIndex < cards.size(); cardIndex++) {
            players[cardIndex % numberOfPlayers].dealCard(cards.get(cardIndex));
        }

        int currentPlayer = 0;
        while (true) {
            players[currentPlayer].openCard();

            int sameCardPlayer = hasSame(currentPlayer);

            if (sameCardPlayer > -1) {
                players[currentPlayer].collects(players[sameCardPlayer].getOpenDeck());
                players[sameCardPlayer].cleanOpenDeck();

                if (players[sameCardPlayer].hasWon()) {
                    System.out.println(sameCardPlayer + 1);
                    break;
                }

            } else {
                currentPlayer = (currentPlayer + 1) % numberOfPlayers;
            }
        }


    }

    private static int hasSame(int currentPlayer) {

        int currentsPlayerCard = players[currentPlayer].getOpenCard();

        for (int neighbour = 0; neighbour < numberOfPlayers; neighbour++) {
            if (neighbour == currentPlayer) continue;

            if (currentsPlayerCard == players[neighbour].getOpenCard())
                return neighbour;
        }

        return -1;
    }

    static class Player{

        LinkedList<Integer> closedDeck = new LinkedList<>();
        LinkedList<Integer> openDeck = new LinkedList<>();

        public void dealCard(Integer card) {
            closedDeck.add(card);
        }


        public void printDeck() {
            for(Integer card: closedDeck){
                System.out.print(card + " ");
            }
        }

        public void openCard() {
            Integer last = closedDeck.pollLast();
            if(last == null) {
                Collections.reverse(openDeck);
                closedDeck = openDeck;
                openDeck = new LinkedList<>();
                last = closedDeck.pollLast();
            }
            openDeck.add(last);
        }

        public int getOpenCard() {
            if(openDeck == null || openDeck.size() == 0)
                return -1;
            return openDeck.peekLast();
        }

        public LinkedList<Integer> getOpenDeck() {
            return openDeck;
        }

        public void cleanOpenDeck() {
            openDeck = new LinkedList<>();
        }

        public void collects(LinkedList<Integer> neighboursDeck) {

            neighboursDeck.addAll(openDeck);

            Collections.reverse(neighboursDeck);
            neighboursDeck.addAll(closedDeck);
            closedDeck = neighboursDeck;
            openDeck = new LinkedList<>();
        }

        public boolean hasWon() {
            return openDeck.size() == 0 && closedDeck.size() == 0;
        }
    }
}