-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.java
More file actions
187 lines (154 loc) · 3.43 KB
/
Copy pathcolor.java
File metadata and controls
187 lines (154 loc) · 3.43 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package graphs;
import java.util.Scanner;
public class color {
public static int valid(int i, int j, int n, int m) {
if (i >= 0 && i < n && j >= 0 && j < m) {
return 1;
}
return 0;
}
public static int dx[] = { 1, -1, 0, 0 };
public static int dy[] = { 0, 0, 1, -1 };
public static int ans = 0;
public static int helper(String board[], int n, int m, int i, int j, int[][] vis, char ch) {
if (valid(i, j, n, m) == 0) {
return 0;
}
if (board[i].charAt(j) != ch) {
return 0;
}
if (vis[i][j] == 1) {
return 1;
}
vis[i][j] = 1;
// int ans=0;
for (int mv = 0; mv < 4; mv++) {
int x = i + dx[mv];
int y = j + dy[mv];
ans = helper(board, n, m, x, y, vis, ch);
}
return ans;
}
public static void initial(int[][] loop, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
loop[i][j] = 0;
}
}
}
public static int colorcycle(String[] board, int n, int m) {
// Write your code here.
int loopvisit[][] = new int[n][m];
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (loopvisit[i][j] == 0) {
ans = helper(board, n, m, i, j, loopvisit, board[i].charAt(j));
initial(loopvisit, n, m);
if (ans == 1) {
return 1;
}
}
}
}
return 0;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N, M, i;
N = scan.nextInt();
M = scan.nextInt();
String[] board = new String[N];
for (i = 0; i < N; i++) {
board[i] = scan.next();
}
System.out.println(colorcycle(board, N, M));
}
}
/*
* import java.util.*; public class solution {
*
*
* public static int valid(int i,int j,int n,int m ){
*
* if(i>=0 && i<n && j>=0 && j<m){ return 1; } return 0;
*
* } public static int loopvisit[][]; public static int dx[]={1,-1,0,0}; public
* static int dy[]={0,0,1,-1}; public static int ans=0;
*
* public static int helper(String board[],int n,int m,int i,int j,int
* [][]vis,char ch,int pi,int pj){
*
* if(valid(i,j,n,m)==0){ return 0; }
*
* if(board[i].charAt(j)!=ch){ return 0; } if(vis[i][j]==1){ return 1; }
*
*
*
*
* vis[i][j]=1; // int ans=0; for(int mv=0;mv<4;mv++){
*
* int x=i+dx[mv]; int y=j+dy[mv];
*
* if(pi==x && pj==y){ continue; } ans=helper(board,n,m,x,y,vis,ch,i,j);
*
* }
*
*
* //vis[i][j]=0;
*
*
* return ans;
*
*
*
*
*
*
*
* }
*
*
*
*
*
*
*
* public static void initial(int [][]loop,int n,int m){
*
* for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ loop[i][j]=0; } }
*
*
* }
*
*
*
*
*
*
* int solve(String[] board , int n, int m) { // Write your code here.
*
* loopvisit=new int[n][m]; for(int k=0;k<n;k++){ Arrays.fill(loopvisit[k],0); }
*
* int res=-1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++){
*
* if(loopvisit[i][j]==0){
*
* res=helper(board,n,m,i,j,loopvisit,board[i].charAt(j),-1,-1);
*
* initial(loopvisit,n,m); if(res==1){ return 1; }
*
* }
*
* } }
*
*
*
*
*
* return 0;
*
* } }
*
*
*/