uva101 java

import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        solution(System.in, new Printer() {
            @Override
            public void p(Object... o) {
                System.out.print(format(o));
            }
        });
    }

    public static void solution(InputStream in, Printer p) {
        Scanner scanner = new Scanner(in);

        int size = scanner.nextInt();
        ArrayList<ListNode> bs = new ArrayList<>(size);
        for (int i = 0; i < size; i ++) {
            bs.add(new ListNode(i));
        }
        scanner.nextLine();

        while (scanner.hasNextLine()) {
            String raw = scanner.nextLine();
            Command command = Command.parseCommand(raw);
            if (command.op == Command.OP_QUIT) {
                printBlocks(bs, p);
                break;
            } else {
                if (!isValidCommand(bs, command)) {
                    continue;
                }
                ListNode source = searchFor(bs, command.source);
                assert source != null;
                if (command.op == Command.OP_MOVE) {
                    clearTail(bs, source);
                }
                ListNode dest = searchFor(bs, command.dest);
                assert dest != null;
                if (command.pos == Command.POS_ONTO){
                    clearTail(bs, dest);
                } else {
                    while (dest.next != null) dest = dest.next;
                }
                moveBehind(bs, source, dest);
            }
        }
    }

    private static boolean isValidCommand(ArrayList<ListNode> bs, Command c) {
        return getStackOfBlock(bs, c.source) != getStackOfBlock(bs, c.dest);
    }

    private static int getStackOfBlock(ArrayList<ListNode> bs, int nodeValue) {
        int size = bs.size();
        for (int i = 0; i < size; i++) {
            ListNode ln = bs.get(i);
            while (ln != null) {
                if (ln.value == nodeValue) {
                    return i;
                }
                ln = ln.next;
            }
        }
        return -1;
    }

    private static void printBlocks(ArrayList<ListNode> bs, Printer p) {
        int size = bs.size();
        for (int i = 0; i < size; i++) {
            p.p("%d:", i);
            ListNode ln = bs.get(i);
            while (ln != null) {
                p.p(" %d", ln.value);
                ln = ln.next;
            }
            p.p("\n");
        }
    }

    private static void moveBehind(ArrayList<ListNode> bs, ListNode movable, ListNode dest) {
        if (movable.prev == null) {
            bs.set(movable.value, null);
        } else {
            ListNode prev = movable.prev;
            prev.next = null;
            movable.prev = null;
        }
        dest.next = movable;
        movable.prev = dest;
    }

    private static ListNode searchFor(ArrayList<ListNode> bs, int value) {
        for (ListNode cl : bs) {
            while (cl != null) {
                if (cl.value == value) {
                    return cl;
                }
                cl = cl.next;
            }
        }
        return null;
    }

    private static void clearTail(ArrayList<ListNode> bs, ListNode ln) {
        while (ln.next != null) {
            ListNode next = ln.next;
            ln.next = null;
            next.prev = null;
            ln = next;
            bs.set(ln.value, ln);
        }
    }
}

class ListNode {

    ListNode next, prev;
    int value;

    ListNode(int value) {
        this.value = value;
    }
}


class Command {

    public static final int OP_MOVE = 0, OP_PILE = 1, OP_QUIT = 2;
    public static final int POS_ONTO = 2, POS_OVER = 3;

    int op, pos;
    int source, dest;

    static Command parseCommand(String raw) {
        Command command = new Command();
        if (raw.equals("quit")) {
            command.op = OP_QUIT;
            return command;
        }
        // move 9 onto 1
        command.op = raw.startsWith("move") ? OP_MOVE : OP_PILE;
        command.pos = raw.contains("onto") ? POS_ONTO : POS_OVER;
        Pattern pattern =Pattern.compile("(move|pile) (?<f>\\d{1,2}) (onto|over) (?<s>\\d{1,2})");
        Matcher matcher = pattern.matcher(raw);
        while (matcher.find()) {
            command.source = Integer.parseInt(matcher.group("f"));
            command.dest = Integer.parseInt(matcher.group("s"));
        }
        return command;
    }
}
interface Printer {
    void p(Object... o);

    default String format(Object... o) {
        String format;
        if (o.length == 1) {
            format = o[0].toString();
        } else {
            Object[] dest = new Object[o.length - 1];
            System.arraycopy(o, 1, dest, 0, dest.length);
            format = String.format(o[0].toString(), dest);
        }
        return format;
    }
}

// list node
// move onto, search n, clear n, move 1
// move over, search, clear, move
// pile onto, search, clear, move
// pile over, search, move

// array
// move onto, search n, clear n, move 1
// move over, search, clear, move
// pile onto, search, clear, move
// pile over, search, move

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值