- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCavityMap.java
More file actions
Latest commit
68 lines (59 loc) · 2.28 KB
/
Copy pathCavityMap.java
File metadata and controls
68 lines (59 loc) · 2.28 KB
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
67
68
/*
You are given a square map of size n×nn×n. Each cell of the map has a value denoting its depth. We will call a cell of the map a cavity if and only if this cell is not on the border of the map and each cell adjacent to it has strictly smaller depth. Two cells are adjacent if they have a common side (edge).
You need to find all the cavities on the map and depict them with the uppercase character X.
Input Format
The first line contains an integer, nn, denoting the size of the map. Each of the following nn lines contains nn positive digits without spaces. Each digit (1-9) denotes the depth of the appropriate area.
Constraints
1≤n≤1001≤n≤100
Output Format
Output nn lines, denoting the resulting map. Each cavity should be replaced with character X.
*/
importjava.io.*;
importjava.util.*;
importjava.text.*;
importjava.math.*;
importjava.util.regex.*;
publicclassSolution {
publicstaticvoidmain(String[] args) {
Scannerin = newScanner(System.in);
intn = in.nextInt();
Stringgrid[] = newString[n];
for(intgrid_i=0; grid_i < n; grid_i++){
grid[grid_i] = in.next();
}
// System.out.println(grid[0].charAt(0));
StringBuildersb = newStringBuilder();
Stringmatrix[][] = newString[n][n];
for(inti=0;i<n;i++)
{
for(intj=0;j<n;j++)
{
intcurrent= Character.getNumericValue(grid[i].charAt(j));
if(i!=0&&j!=0&&i!=n-1&&j!=n-1)
{
intnext= Character.getNumericValue(grid[i].charAt(j+1));
if(current>next)
{
matrix[i][j]="X";
}
else
{
matrix[i][j]=Integer.toString(current);
}
}
else
{
matrix[i][j]=Integer.toString(current);
}
}
}
for(inti=0;i<n;i++)
{
for(intj=0;j<n;j++)
{
System.out.print(matrix[i][j]);
}
System.out.print("\n");
}
}
}