fork download
  1. class Ideone {
  2. public static void main(String[] args){
  3. System.out.println("2x2:\t" + solve(1));
  4. System.out.println("4x4:\t" + solve(2));
  5. System.out.println("6xt:\t" + solve(3));
  6. System.out.println("8x8:\t" + solve(4));
  7. System.out.println("10x10:\t" + solve(5));
  8. }
  9. static int solve(int n){
  10. int m =2*n;
  11. int[][] b = new int[m][m];
  12. for(int i = 0; i < m; i++){
  13. for(int j = 0; j < m; j++){
  14. b[i][j]=0;
  15. }
  16. }
  17. return count(m,m*m,m*m/2,m*m/2,0,b);
  18. }
  19. static int count(int n,int sqLeft, int bLeft, int wLeft, int count, int[][] b){
  20. if(sqLeft == 0){
  21. /*for(int i = 0; i < n; i++){
  22. for(int j = 0; j < n; j++){
  23. System.out.print(b[i][j]);
  24. }
  25. System.out.println();
  26. }
  27. System.out.println();*/
  28. return count+1;
  29. }
  30. int x=(sqLeft-1)%n;
  31. int y=(sqLeft-1)/n;
  32. if(wLeft==0){
  33. if(y!=0){
  34. if ((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!= 1)) {
  35. b[x][y] = 2;
  36. return count(n, sqLeft-1, bLeft-1, wLeft, count, b);
  37. } else {
  38. return 0;
  39. }
  40. } else {
  41. b[x][y]=2;
  42. return count(n,sqLeft-1,bLeft-1,wLeft,count,b);
  43. }
  44. } else if(bLeft==0){
  45. if(y!=n-1){
  46. if((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2)){
  47. b[x][y]=1;
  48. return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
  49. } else {
  50. return 0;
  51. }
  52. } else {
  53. b[x][y]=1;
  54. return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
  55. }
  56. } else{
  57. if(y==0){
  58. if((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2)){
  59. int[][] c=new int[n][n];
  60. for(int i = 0; i < n; i++){
  61. System.arraycopy(b[i], 0, c[i], 0, n);
  62. }
  63. b[x][y]=2;
  64. c[x][y]=1;
  65. return count(n,sqLeft-1,bLeft,wLeft-1,count,c)+count(n,sqLeft-1,bLeft-1,wLeft,count,b);
  66. } else {
  67. b[x][y]=2;
  68. return count(n,sqLeft-1,bLeft-1,wLeft,count,b);
  69. }
  70. }else if(y==n-1){
  71. if((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!=1)){
  72. int[][] c=new int[n][n];
  73. for(int i = 0; i < n; i++){
  74. System.arraycopy(b[i], 0, c[i], 0, n);
  75. }
  76. b[x][y]=2;
  77. c[x][y]=1;
  78. return count(n,sqLeft-1,bLeft,wLeft-1,count,c)+count(n,sqLeft-1,bLeft-1,wLeft,count,b);
  79. } else {
  80. b[x][y]=1;
  81. return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
  82. }
  83. }else{
  84. if(((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!=1))&&((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2))){
  85. int[][] c=new int[n][n];
  86. for(int i = 0; i < n; i++){
  87. System.arraycopy(b[i], 0, c[i], 0, n);
  88. }
  89. b[x][y]=2;
  90. c[x][y]=1;
  91. return count(n,sqLeft-1,bLeft,wLeft-1,count,c)+count(n,sqLeft-1,bLeft-1,wLeft,count,b);
  92. } else if ((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!=1)){
  93. b[x][y]=2;
  94. return count(n,sqLeft-1,bLeft-1,wLeft,count,b);
  95. } else if ((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2)){
  96. b[x][y]=1;
  97. return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
  98. } else {
  99. return 0;
  100. }
  101. }
  102. }
  103. }
  104. }
  105.  
Runtime error #stdin #stdout #stderr 4.98s 380480KB
stdin
Standard input is empty
stdout
2x2:	3
4x4:	30
6xt:	410
8x8:	6148
stderr
/spoj/java_run: line 9: 30944 CPU time limit exceeded /opt/$VER/bin/java -client -Xbatch -Dfile.encoding=UTF-8 -jar tested.zip