-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathAdjacentCells.java
66 lines (57 loc) · 1.92 KB
/
AdjacentCells.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package adjacent_cells;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class AdjacentCells {
private static ArrayList<Integer> out = new ArrayList<>();
private static int fieldSize = 8;
private static int[][] chessboard = new int[fieldSize][fieldSize];
private static void findNeighbors(int cellNumber){
boolean stop = false;
for(int i = 0; i < fieldSize; i++){
for(int j = 0; j < fieldSize; j++){
if(chessboard[i][j] == cellNumber){
if(i-1 >= 0){
out.add(chessboard[i-1][j]);
}
if(i+1 < fieldSize){
out.add(chessboard[i+1][j]);
}
if(j-1 >= 0){
out.add(chessboard[i][j-1]);
}
if(j+1 < fieldSize){
out.add(chessboard[i][j+1]);
}
stop = true;
break;
}
}
if(stop) break;
}
}
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("input.txt");
Scanner sc = new Scanner(file);
int cellNumber = sc.nextInt();
int count = 1;
for(int i = 0; i < fieldSize; i++){
for(int j = 0; j < fieldSize; j++){
chessboard[i][j] = count;
count++;
}
}
findNeighbors(cellNumber);
Collections.sort(out);
StringBuilder result = new StringBuilder();
for(int number : out){
result.append(number).append(" ");
}
FileWriter fileOut = new FileWriter("output.txt");
fileOut.write(String.valueOf(result).trim());
fileOut.close();
}
}