import java util class Fill static String str static int char ar new c

 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
import java.util.*;
class Fill {
static String str;
static int r, c;
char ar[][] = new char [1000][1000];
void memset(int a, int b, int r, int c, char x) {
for(int i=a; i<=r; i++) {
for(int j=b; j<=c; j++) {
ar[i][j] = x;
}
}
}
void print(int a, int b, int r, int c) {
for(int i=a; i<=r; i++) {
for(int j=b; j<=c; j++) {
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
}
void dfs(int i, int j, int z) {
if(Character.isLetter(ar[i][j])) return;
if(z == str.length()) return;
ar[i][j] = str.charAt(z);
//Order of recursion is very crucial, as it determines the initial moves.
//dfs(i,j,z) denotes the movement in respective direction after a turn
dfs(i+1, j, z+1);
dfs(i, j+1, z+1);
dfs(i-1, j, z+1);
dfs(i, j-1, z+1);
}
public static void main(String args[]) {
Fill obj = new Fill();
Scanner sc = new Scanner(System.in);
System.out.println("Enter row and column");
str = sc.nextLine();
r = sc.nextInt();
c = sc.nextInt();
obj.memset(0,0,r,c,'x');
obj.memset(1,1,r-1,c-1,' ');
//obj.print(0,0,r,c);
obj.dfs(1,1,0);
obj.print(1,1,r-1,c-1);
}
}