五一期间就只做了这题。
在poj上通过了,在zoj上却老是wa。
- import java.util.LinkedList;
- import java.util.Scanner;
- public class FlipGame1753 {
- static int[] exOr = new int[]{
- Integer.parseInt("1100100000000000", 2),
- Integer.parseInt("1110010000000000", 2),
- Integer.parseInt("0111001000000000", 2),
- Integer.parseInt("0011000100000000", 2),
- Integer.parseInt("1000110010000000", 2),
- Integer.parseInt("0100111001000000", 2),
- Integer.parseInt("0010011100100000", 2),
- Integer.parseInt("0001001100010000", 2),
- Integer.parseInt("0000100011001000", 2),
- Integer.parseInt("0000010011100100", 2),
- Integer.parseInt("0000001001110010", 2),
- Integer.parseInt("0000000100110001", 2),
- Integer.parseInt("0000000010001100", 2),
- Integer.parseInt("0000000001001110", 2),
- Integer.parseInt("0000000000100111", 2),
- Integer.parseInt("0000000000010011", 2)
- };
- static boolean[] stateVisited = new boolean[65536];
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- StringBuilder sb = new StringBuilder();
- for(int i=0; i<4;i++)
- sb.append(sc.nextLine().trim());
- for(int j=0; j<sb.length(); j++){
- if(sb.charAt(j) == 'b')
- sb.setCharAt(j, '1');
- else
- sb.setCharAt(j, '0');
- }
- int id = Integer.parseInt(sb.toString(), 2);
- int result = bfs(id);
- if(result == -1)
- System.out.println("Impossible");
- else
- System.out.println(result);
- }
- private static int bfs(int id) {
- LinkedList<Node> ll = new LinkedList<Node>();
- Node source = new Node(id,0);
- if(source.getId()==0 || source.getId()==0xFFFF)
- return 0;
- ll.addLast(source);
- while(!ll.isEmpty()){
- Node node = ll.removeFirst();
- for(int pos=0; pos<16; pos++){
- //16 childNode
- Node childNode = nextNode(node, pos);
- if(node.getId()==0 || node.getId()==0xFFFF)
- return node.getDepth();
- if(!stateVisited[childNode.getId()]){
- stateVisited[childNode.getId()] = true;
- ll.addLast(childNode);
- }
- }
- }
- return -1;
- }
- private static Node nextNode(Node node, int pos) {
- int id = node.getId();
- id ^= exOr[pos];
- return new Node(id, node.getDepth()+1);
- }
- }
- class Node{
- private int id;
- private int depth;
- public Node(int id, int depth) {
- this.id = id;
- this.depth = depth;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getDepth() {
- return depth;
- }
- public void addDepth() {
- this.depth ++;
- }
- }