|
|
back to boardMy programm accepted... Posted by Jica 7 Aug 2012 10:00 import java.util.Scanner; public class Hanoi_Tower { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int d[] = new int [n+1]; for (int i = 1; i <= n; i++) { d[i] = sc.nextInt(); } int x =n%2; int a[][] ={{3, 1, 2, 3}, {2, 1, 3, 2}}; double ans = 0; for (int i = n; i > 0; i--) { int y = (i+x)%2; double rep = (ans/Math.pow(2, i) + 1)%3; if (d[i] != a[y][(int)rep]){ if (d[i] != a[y][(int)(rep+1)]){ System.out.println("-1"); return; } ans += Math.pow(2, (i-1)); } } System.out.printf("%.0f",ans); } } |
|
|