import java util Scanner public class Main public static void main Str

 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
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] digits = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
digits[i][j] = sc.nextInt();
int len = sc.nextInt();
int[] numbers = new int[len];
for (int i = 0; i < len; i++){
numbers[i] = sc.nextInt();
}
sc.close();
int[][] directions = {{0, 1}, {1, 1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {0, -1}};
for (int numberInd = 0; numberInd < numbers.length; numberInd++){
int number = numbers[numberInd];
int numberLen = 0;
while (number > 0){
numberLen++;
number /= 10;
}
boolean isFind = false;
for (int i = 0; i < n && !isFind; i++) {
for (int j = 0; j < m && !isFind; j++) {
if (digits[i][j] == numbers[numberInd] % 10){
for (int iter = 0; iter < directions.length && !isFind; iter++){
number = numbers[numberInd];
int x = i;
int y = j;
if (x + directions[iter][0] * (numberLen - 1) >= 0 && x + directions[iter][0] * (numberLen - 1)< n
&& y + directions[iter][1] * (numberLen - 1) >= 0 && y + directions[iter][1] * (numberLen - 1) < n){
int[][] ans = new int[numberLen][2];
int ind = 0;
while (number > 0 && digits[x][y] == number % 10) {
number = number / 10;
ans[ind][0] = x;
ans[ind++][1] = y;
x += directions[iter][0];
y += directions[iter][1];
}
if (number == 0){
isFind = true;
System.out.print(numbers[numberInd] + ": ");
for (int k = numberLen - 1; k >= 0; k--)
System.out.print(ans[k][0] + ":" + ans[k][1] + " ");
}
}
}
}
}
}
if (!isFind)
System.out.print(numbers[numberInd] + ": нет");
System.out.println();
}
}
}