fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. public class Main
  9. {
  10. public static boolean chess[][];
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. chess=new boolean[8][8];
  14. chess[0][0]=true;
  15. for(int i=0;i<8;i++){
  16. for(int j=0;j<8;j++){
  17.  
  18. if(!checkrow(i)&&!checkcolumn(j)&&!checkdiagonals(i,j)&&!checkknight(i,j)){
  19. if(i!=0||j!=0)
  20. chess[i][j]=true;
  21. }
  22. }
  23. }
  24. for(int i = 0; i < 8; i++)
  25. {
  26. for(int j = 0; j < 8; j++)
  27. {
  28. System.out.print(((chess[i][j])?"T":"x")+"|");
  29. }
  30. System.out.println();
  31. }
  32. }
  33. public static boolean checkrow(int a){
  34. for(int i=0;i<8;i++){
  35. if(chess[a][i])return true;
  36. }
  37. return false;
  38. }
  39. public static boolean checkcolumn(int a){
  40. for(int i=0;i<8;i++){
  41. if(chess[i][a])return true;
  42. }
  43. return false;
  44. }
  45. public static boolean checkdiagonals(int pi,int pj){
  46. int i =pi- Math.min(pi,pj);
  47. int j =pj- Math.min(pi,pj);
  48. for(int k=i,l=j;k<8&&l<8;k++,l++){
  49. if(chess[k][l])return true;
  50. }
  51. int i_2 =pi- Math.min(pi,pj);
  52. int j_2 =pj+ Math.min(pi,pj);
  53. for(int k=i_2,l=j_2;k<8&&l>1;k++,l--){
  54. if(chess[k][l])return true;
  55. }
  56. return false;
  57. }
  58. public static boolean checkknight(int pi,int pj){
  59. for(int i=-1;i<=1;i++){
  60. for(int j=-1;j<=1;j++){
  61. System.out.print(i+""+j);
  62. if(0<=pi+2*i&&pi+2*i<8&&0<=pj+j&&pj+j<8)
  63. if(chess[pi+2*i][pj+j])return true;
  64. if(0<=pi+i&&pi+i<8&&0<=pj+2*j&&pj+2*j<8)
  65. if(chess[pi+i][pj+2*i])return true;
  66. }
  67. }
  68. return false;
  69. }
  70. }
Runtime error #stdin #stdout #stderr 0.08s 380160KB
stdin
Standard input is empty
stdout
-1-1-1-1-10-110-100011-11011-1-1-1-1
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
	at Main.checkdiagonals(Main.java:54)
	at Main.main(Main.java:18)