Skip to content

Commit 79dd250

Browse files
authored
Merge pull request ashutosh97#39 from Roho000/master
Added competition problem
2 parents 4179122 + 9e359ac commit 79dd250

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Floodfill/Problem_statement.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
For those who don�t like regular images, ASCII Maps Inc. has created maps that are fully printable ASCII characters. Each map is a rectangular grid of lowercase English letters, where each letter stands for various locations. In particular, �w� stands for water and the other 25 letters represent various different land locations. For this problem, we are interested in counting the number of bodies of water on a given ASCII map. A body of water is a maximal set of contiguous grid squares on the ASCII map where each square in the body of water shares a boundary with at least one other square in the body of water. Thus, for two grid squares to be part of the same body of water, one must be above, below, to the left, or to the right of the other grid square.
2+
Input
3+
The first line of input consists of two space separated integers, r (1=r=50) and c (1=c=50), the number of rows and columns, respectively for the input map. The next r lines will each contain c lowercase English letters, representing the corresponding row of the input map.
4+
5+
Output
6+
On a line by itself, output the number of bodies of water in the input map.
7+
8+
Sample test case 1:
9+
10+
Input:
11+
5 6
12+
waaaww
13+
wawawc
14+
bbbbwc
15+
wwwwww
16+
dddddd
17+
18+
Output:
19+
3
20+
21+
Sample test case 2:
22+
Input:
23+
2 8
24+
wxwxwxwx
25+
xwxwxwxw
26+
27+
Output:
28+
8

Floodfill/solution.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class water {
5+
public static void main(String[] args){
6+
Scanner kb=new Scanner(System.in);
7+
System.out.println("Enter the dimensions of the map:");
8+
int n=kb.nextInt();
9+
int m=kb.nextInt();
10+
System.out.println("Enter the map:");
11+
char[][] x=new char[n][m];
12+
for(int i=0;i<n;i++)
13+
x[i]=kb.next().toCharArray();
14+
String str="";
15+
int count=0;
16+
for(int i=0;i<n;i++){
17+
for(int k=0;k<m;k++){
18+
if(x[i][k]!='#'&&x[i][k]=='w'){
19+
fillGrid(x, i, k);
20+
display(x);
21+
count++;
22+
System.out.println(count);
23+
}
24+
}
25+
}
26+
if(count==1)
27+
System.out.println("\nThere is "+count+" body of water.");
28+
else
29+
System.out.println("\nThere are "+count+" bodies of water.");
30+
}
31+
public static void fillGrid(char[][] arr, int r, int c) {
32+
if (inBounds(arr,r,c)&&arr[r][c] == 'w'){
33+
arr[r][c] = '#';
34+
fillGrid(arr,r + 1, c);
35+
fillGrid(arr,r - 1, c);
36+
fillGrid(arr,r, c + 1);
37+
fillGrid(arr,r, c - 1);
38+
}
39+
}
40+
41+
public static boolean inBounds(char[][] arr, int r, int c) {
42+
return r>=0&&r<arr.length&&c>=0&&c<arr[0].length;
43+
}
44+
45+
public static void display(char[][] arr){
46+
System.out.println("\nGrid: ");
47+
for (int i = 0; i < arr.length; i++){
48+
for (int j = 0; j < arr[i].length; j++)
49+
System.out.print(arr[i][j]);
50+
System.out.println();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)