8000 Create B223.CavityMap.cs · xieqilu/Qilu-leetcode@a65743d · GitHub
[go: up one dir, main page]

Skip to content

Commit a65743d

Browse files
committed
Create B223.CavityMap.cs
1 parent 23049db commit a65743d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

B223.CavityMap.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
Problem Statement
3+
4+
You are given a square map of size n×n. Each cell of the map has a value denoting its depth. We will call a cell of the map
5+
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.
6+
Two cells are adjacent if they have a common side.
7+
8+
You need to find all the cavities on the map and depict them with the uppercase character X.
9+
10+
Input Format
11+
The first line contains an integer, n, denoting the size of the map. Each of the following n lines contains n positive digits without spaces.
12+
Each digit (1-9) denotes the depth of the appropriate area.
13+
14+
Constraints
15+
1≤n≤100
16+
17+
Output Format
18+
Output n lines, denoting the resulting map. Each cavity should be replaced with character X.
19+
20+
Sample Input
21+
4
22+
1112
23+
1912
24+
1892
25+
1234
26+
Sample Output
27+
1112
28+
1X12
29+
18X2
30+
1234
31+
32+
33+
Solution:
34+
This is a very simple problem, but there are some points to be noticed. The method is just traverse the whole map, if current
35+
cell is a border cell, output its value. Then compare current cell with all four adjecent cells, if the value of current cell is
36+
larger than all four adjecent cells, output "X". Otherwise output its value.
37+
38+
Note:
39+
1, When parse the input and build a 2D int array (matrix), the line of input doesn't contain any space. So converse the line to
40+
a char array using string.ToCharArray(), then map[i,j] = line[j]-'0'. Don't try to use int.Parse to convert a string array to int.
41+
Think outside of the box.
42+
43+
2, Don't try to modify the value of the matrix then output the whole matrix. This will require an extra traverse which is not
44+
efficient. Besides, we cannot change the value of an integer cell to "X", it's better to output the result when traversing the
45+
matrix. Then we only need to traverse for once.
46+
*/
47+
48+
using System;
49+
using System.Collections.Generic;
50+
using System.IO;
51+
52+
class Solution {
53+
public static void FindCavity(int[,] map, int size){
54+
for(int i=0;i<size;i++){
55+
for(int j=0;j<size;j++){
56+
if(i==0 || j==0 || i==size-1 || j==size-1)
57+
Console.Write(map[i,j]);
58+
else if(map[i,j]>map[i-1,j]&&map[i,j]>map[i+1,j]&&map[i,j]>map[i,j+1]&&map[i,j]>map[i,j-1])
59+
Console.Write("X");
60+
else
61+
Console.Write(map[i,j]);
62+
}
63+
Console.WriteLine(); //start a new line
64+
}
65+
}
66+
67+
static void Main(String[] args) {
68+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
69+
int size = int.Parse(Console.ReadLine());
70+
int[,] map = new int[size,size];
71+
for(int i=0;i<size;i++){
72+
char[] line = Console.ReadLine().ToCharArray();
73+
for(int j=0;j<size;j++)
74+
map[i,j] = line[j]-'0';
75+
}
76+
FindCavity(map,size);
77+
}
78+
}

0 commit comments

Comments
 (0)
0