fork download
  1. import java.util.Scanner;
  2. import java.io.FileInputStream;
  3.  
  4. /*
  5. As the name of the class should be Solution, using Solution.java as the filename is recommended.
  6. In any case, you can execute your program by running 'java Solution' command.
  7. */
  8. class Solution
  9. {
  10. static int N = 100;
  11. static int n, k, AnswerN;
  12. static int[][] map = new int[N][N];
  13.  
  14. public static void main(String args[]) throws Exception
  15. {
  16. int T;
  17. /*
  18. The method below means that the program will read from input.txt, instead of standard(keyboard) input.
  19. To test your program, you may save input data in input.txt file,
  20. and call below method to read from the file when using nextInt() method.
  21. You may remove the comment symbols(//) in the below statement and use it.
  22. But before submission, you must remove the freopen function or rewrite comment symbols(//).
  23. */
  24. //System.setIn(new FileInputStream("sample_input.txt"));
  25.  
  26. /*
  27. Make new scanner from standard input System.in, and read data.
  28. */
  29. Scanner sc = new Scanner(System.in);
  30.  
  31. T=sc.nextInt();
  32.  
  33. for(int test_case = 1; test_case <= T; test_case++)
  34. {
  35. /*
  36. Read each test case from standard input.
  37. */
  38. n=sc.nextInt();
  39. k=sc.nextInt();
  40. for(int i=0;i<n;i++){
  41. for(int j=0;j<n;j++){
  42. map[i][j]=sc.nextInt();
  43. }
  44. }
  45.  
  46.  
  47. /////////////////////////////////////////////////////////////////////////////////////////////
  48. /*
  49. Please, implement your algorithm from this section.
  50. */
  51. /////////////////////////////////////////////////////////////////////////////////////////////
  52. AnswerN = -1;
  53. boolean hasAnswer = false;
  54.  
  55. for(int i=0; i<n; i++) {
  56. for(int j=0; j<n; j++) {
  57. if(map[i][j] == 1) {
  58. int count = 0;
  59. for(int l=j; l<j+k; l++) {
  60. if(l > n-1) {
  61. break;
  62. }else if(l <= n-1 && map[i][l] == 1) {
  63. count++;
  64. }
  65. }
  66.  
  67. int tempAnswer = 0;
  68.  
  69. if(count == k) {
  70. tempAnswer = (int) Math.pow(i-(n/2)+1,2) * k;
  71. for(int l=j; l<j+k; l++){
  72. tempAnswer += (int) Math.pow(j-(n/2)+1,2);
  73.  
  74. }
  75. System.out.println(tempAnswer);
  76.  
  77. if(hasAnswer == false || tempAnswer < AnswerN){
  78. hasAnswer = true;
  79. AnswerN = tempAnswer;
  80. }
  81. }
  82.  
  83.  
  84. }
  85. }
  86. }
  87.  
  88. if(hasAnswer == false) {
  89. AnswerN = -1;
  90. }
  91.  
  92.  
  93. // Print the answer to standard output(screen).
  94. System.out.println("#" + test_case+" "+AnswerN);
  95. }
  96. }
  97. }
Success #stdin #stdout 0.07s 4386816KB
stdin
3
11 5
0 0 0 0 0 0 1 0 0 1 1
1 1 1 1 1 1 1 0 0 0 0
1 1 0 0 0 0 1 1 1 1 1
1 0 1 0 0 1 1 1 0 1 1
0 0 0 0 0 0 0 0 1 1 0
0 0 1 0 0 0 1 0 0 1 0
0 0 1 1 1 0 1 1 1 1 1
0 0 0 0 0 1 1 1 0 0 0
0 0 1 1 1 1 1 1 0 1 1
1 1 1 0 1 1 0 0 0 1 1
0 0 0 0 0 1 1 1 1 1 0
9 7
1 1 1 1 0 0 0 0 0
0 0 0 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0
1 1 1 0 0 0 0 0 0
1 1 1 1 0 1 1 1 1
0 0 1 1 1 0 0 1 1
0 0 1 1 0 0 1 1 1
1 0 1 0 0 1 0 1 0
7 3
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
stdout
125
90
65
40
40
100
85
185
#1 40
#2 -1
24
15
12
15
24
15
6
3
6
15
12
3
0
3
12
15
6
3
6
15
24
15
12
15
24
39
30
27
30
39
60
51
48
51
60
#3 0