exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
listlengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED
|
5d7ab82d9d4e3a206a780af89fe8459f
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class P1627A {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int tc = i();
for (int i = 0; i < tc; i++) {
solve();
}
}
private static void solve() {
int n = i(), m = i(), r = i() - 1, c = i() - 1;
String[] rows = new String[n];
for (int i = 0; i < n; i++) {
rows[i] = s();
}
if (bl(rows, r, c)) {
p("0");
return;
}
int best = 3;
all: for (int i = 0; i < n ; i++) {
for (int j = 0; j < m; j++) {
if (bl(rows, i, j)) {
if (i == r || j == c) {
best = 1;
break all;
} else {
best = 2;
}
}
}
}
p(best == 3 ? "-1" : "" + best);
}
private static int i() {
return scan.nextInt();
}
private static String s() {
return scan.next();
}
private static boolean bl(String[] rows, int r, int c) {
return rows[r].charAt(c) == 'B';
}
private static void p(String text) {
System.out.println(text);
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
624361a1c883354084fe0dc4193586d5
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
//code by tishrah_
public class _practise {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
int[] ia(int n)
{
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=nextInt();
return a;
}
int[][] ia(int n , int m)
{
int a[][]=new int[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextInt();
return a;
}
long[][] la(int n , int m)
{
long a[][]=new long[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextLong();
return a;
}
char[][] ca(int n , int m)
{
char a[][]=new char[n][m];
for(int i=0;i<n;i++)
{
String x =next();
for(int j=0;j<m ;j++) a[i][j]=x.charAt(j);
}
return a;
}
long[] la(int n)
{
long a[]=new long[n];
for(int i=0;i<n;i++)a[i]=nextLong();
return a;
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static void sort(long[] a)
{int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {long oi=r.nextInt(n), temp=a[i];a[i]=a[(int)oi];a[(int)oi]=temp;}Arrays.sort(a);}
static void sort(int[] a)
{int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);}
public static long sum(long a[])
{long sum=0; for(long i : a) sum+=i; return(sum);}
public static long count(long a[] , long x)
{long c=0; for(long i : a) if(i==x) c++; return(c);}
public static int sum(int a[])
{ int sum=0; for(int i : a) sum+=i; return(sum);}
public static int count(int a[] ,int x)
{int c=0; for(int i : a) if(i==x) c++; return(c);}
public static int count(String s ,char ch)
{int c=0; char x[] = s.toCharArray(); for(char i : x) if(ch==i) c++; return(c);}
public static boolean prime(int n)
{for(int i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static boolean prime(long n)
{for(long i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static int gcd(int n1, int n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static long gcd(long n1, long n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static int[] freq(int a[], int n)
{ int f[]=new int[n+1]; for(int i:a) f[i]++; return f;}
public static int[] pos(int a[], int n)
{ int f[]=new int[n+1]; for(int i=0; i<n ;i++) f[a[i]]=i; return f;}
public static int[] rev(int a[])
{
for(int i=0 ; i<(a.length+1)/2;i++)
{
int temp=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=temp;
}
return a;
}
public static boolean palin(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
String str=String.valueOf(sb.reverse());
if(s.equals(str))
return true;
else return false;
}
public static String rev(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
return String.valueOf(sb.reverse());
}
public static void main(String args[])
{
FastReader in=new FastReader();
PrintWriter so = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
_practise ob = new _practise();
int T = in.nextInt();
// int T = 1;
tc : while(T-->0)
{
int n = in.nextInt();
int m = in.nextInt();
int x=in.nextInt();
int y=in.nextInt();
char ch[][] = in.ca(n, m);
boolean flag=false;
for(int i=0 ; i<n ; i++)
{
for(char e : ch[i])
if(e=='B')
flag=true;
}
if(!flag) so.println(-1);
else if(ch[x-1][y-1]=='B') so.println(0);
else
{
flag=false;
for(int i=0 ; i<n ; i++)
if(ch[i][y-1]=='B') flag=true;
for(int i=0 ; i<m ; i++)
if(ch[x-1][i]=='B') flag=true;
if(flag) so.println("1");
else so.println(2);
}
}
so.flush();
/*String s = in.next();
* Arrays.stream(f).min().getAsInt()
* BigInteger f = new BigInteger("1"); 1 ke jagah koi bhi value ho skta jo aap
* initial value banan chahte
int a[] = new int[n];
Stack<Integer> stack = new Stack<Integer>();
Deque<Integer> q = new LinkedList<>(); or Deque<Integer> q = new ArrayDeque<Integer>();
PriorityQueue<Long> pq = new PriorityQueue<Long>();
ArrayList<Integer> al = new ArrayList<Integer>();
StringBuilder sb = new StringBuilder();
HashSet<Integer> st = new LinkedHashSet<Integer>();
Set<Integer> s = new HashSet<Integer>();
Map<Long,Integer> hm = new HashMap<Long, Integer>(); //<key,value>
for(Map.Entry<Integer, Integer> i :hm.entrySet())
for(int i : hm.values())
for(int i : hm.keySet())
HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
so.println("HELLO");
Arrays.sort(a,Comparator.comparingDouble(o->o[0]))
Arrays.sort(a, (aa, bb) -> Integer.compare(aa[1], bb[1]));
Set<String> ts = new TreeSet<>();*/
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
54dcdbc7977c7bcdd7e7831bd757f6d1
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
public class Main {
public static void main(String[] args) {
for(int t=nextInt();t-->0;) {
int n=nextInt(),m=nextInt(),r=nextInt()-1,c=nextInt()-1,ans=99;
for(int i=0;i<n;i++) {
char s[]=next().toCharArray();
for(int j=0;j<m;j++)
if(s[j]=='B') {
int x;
if(r==i&&c==j) x=0;
else if(r==i||c==j) x=1;
else x=2;
ans=Math.min(ans,x);
}
}
println(ans>50?-1:ans);
}
close();
}
static int nextInt() {
int x=0,p=1,c=0;do c=read();while(c<=32);if(c=='-'){p=-1;c=read();}
for(;c>='0'&&c<='9';c=read())x=x*10+c-'0'; return x*p;
}
static String next() {
StringBuilder s=new StringBuilder();
int c;do c=read();while(c<=32);for(;c>32;c=read()) s.append((char)c);
return s.toString();
}
static int read(){int z=-1;try{z=System.in.read();}catch(Throwable e){}return z;}
static final java.io.BufferedWriter bw=new java.io.BufferedWriter(new java.io.OutputStreamWriter(System.out));
//static {Runtime.getRuntime().addShutdownHook(new Thread(()->close()));}
static void close(){try{bw.close();}catch(Throwable e){}}
static void print(String s){try{bw.write(s);}catch(Throwable e){}}
static void println(Object s){print(s+"\n");}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
7dec3b3c825380eb9a65e875893a1b9a
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
/**
* A_Not_Shading
*/
public class A_Not_Shading {
public static int solve(String[] s,int r,int c){
int res=-1;
// Scanner sc = new Scanner(System.in) ;
// int n= sc.nextInt();
// int m= sc.nextInt();
// int r= sc.nextInt();
// r--;
// int c= sc.nextInt();
// c--;
// String[] s=new String[n];
// sc.nextLine();
// char[][] grid = new char[n][m];
// for (int i = 0; i < n; i++) {
// s[i]= new String();
// s[i]=sc.nextLine();
// for (int k = 0; k < grid.length; k++) {
// if(s[i].charAt(k)=='B' ){
// res=2;
// }
// if (s[i].charAt(k)=='B' && (r==i || c==k)){
// res=1;
// fina=res;
// }
// }
// }
if (s[r].charAt(c)=='B') {
return 0;
}
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s[0].length(); j++) {
if (s[i].charAt(j)=='B' && (r==i || c==j)) {
return 1;
}
else if(s[i].charAt(j)=='B' ){
res=2;
}
}
}
return res;
}
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int t= sc.nextInt();
while (t>0) {
int fina=-1;
// Scanner sc = new Scanner(System.in) ;
int n= sc.nextInt();
int m= sc.nextInt();
int r= sc.nextInt();
r--;
int c= sc.nextInt();
c--;
String[] s=new String[n];
sc.nextLine();
// char[][] grid = new char[n][m];
for (int i = 0; i < n; i++) {
s[i]= new String();
s[i]=sc.nextLine();
}
fina = solve(s, r, c);
System.out.println(fina);
t--;
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
47afab16a0a544c75720ba00de1fe801
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
//package div766;
import java.util.ArrayList;
import java.util.Scanner;
public class A766 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
boolean F =false;
outer:
for (int i = 0; i < N; i++) {
int r = sc.nextInt();
int l = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
// System.out.println(r);
// System.out.println(l);
// System.out.println(a);
// System.out.println(b);
// int[] row = new int[r];
// int[] lo = new int[l];
ArrayList<Integer> row = new ArrayList<>(r);
ArrayList<Integer> lo = new ArrayList<>(l);
for (int ro = 0; ro < r; ro++) {
row.add(0);
}
for (int low = 0; low < l; low++) {
lo.add(0);
}
// for(int ro:row){
// ro = 0;
// }
// for(int low:lo){
// low = 0;
// }
for(int j = 0; j < r; j++){
String p = sc.next();
// System.out.println(p);
char[] ppp = p.toCharArray();
// for()
// System.out.println(p.charAt(0));
for (int pp = 0; pp < l; pp++) {
// System.out.println(ppp[pp]);
if(ppp[pp] == 'B'){
if(j==a-1&&pp==b-1){
System.out.println(0);
F = true;
}
row.set(j,1);
lo.set(pp,1);
}
}
}
if(F){
F = false;
continue outer;
}
if(!row.contains(1)&&!lo.contains(1)){
System.out.println(-1);
// break outer;
}else{
if(row.get(a-1)==0&&lo.get(b-1)==0){
System.out.println(2);
// break outer;
}else{
System.out.println(1);
// break outer;
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
e9251f97e8a48ba01926ef92edf2b168
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class NotShading {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
NotShading y = new NotShading();
int cases = Integer.parseInt(in.nextLine());
int a,b,c,d;
String tmp;
String[] tmp1;
for (int i = 0; i < cases; i++) {
tmp = in.nextLine();
tmp1 = tmp.split(" ");
a = Integer.parseInt(tmp1[0]);
b = Integer.parseInt(tmp1[1]);
c = Integer.parseInt(tmp1[2]);
d = Integer.parseInt(tmp1[3]);
System.out.println(y.rowsColumns(in,a,b,c-1,d-1));
}
in.close();
}
public int rowsColumns(Scanner in, int n, int m, int r, int c) {
// System.out.println("input: "+n+" "+m+" "+r+" "+c);
boolean oneOrZero = false;
boolean positive = false;
boolean zero = false;
String curRow;
for (int i = 0; i < n; i++) {
curRow = in.nextLine();
boolean column = curRow.charAt(c) == 'B';
if (i == r) {
if (column) {
zero = true;
}
if (curRow.indexOf("B") >= 0) {
oneOrZero = true;
}
} else {
if (column) {
oneOrZero = true;
}
if (curRow.indexOf("B") >= 0) {
positive = true;
}
}
}
if (zero) {
return 0;
} else if (oneOrZero) {
return 1;
} else if (positive) {
return 2;
} else {
return -1;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
e7f980c4b20bfe3e3e91865287471b93
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.lang.*;
import java.io.*;
import java.util.*;
public class Cfone{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int m=sc.nextInt();
int r=sc.nextInt();
int c=sc.nextInt();
sc.nextLine();
r--;
c--;
char[][] arr=new char[n][m];
boolean flag = false;
for(int i=0;i<n;i++){
String row = sc.nextLine();
char[] ch = row.toCharArray();
for(int j=0;j<m;j++){
arr[i][j]=ch[j];
if(arr[i][j] == 'B') flag = true;
}
}
int ans = -1;
int minAns = Integer.MAX_VALUE;
if(flag == true){
outerloop:
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j] == 'B' && i == r && j == c){
ans =0;
if(minAns>ans){
minAns = ans;
}
break outerloop;
}
if(arr[i][j] == 'B'){
if(i == r || j == c){
ans = 1;
if(minAns>ans){
minAns = ans;
}
}else{
ans = 2;
if(minAns>ans){
minAns = ans;
}
}
}
}
}
}else{
ans = -1;
minAns = -1;
}
System.out.println(minAns);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
c3cfba2cba9f2d199afe0a7e4b3d439f
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
//import java.util.HashSet;
import java.util.ArrayList;
// import java.util.Arrays;
// import java.util.Collections;
import java.util.Scanner;
//import java.lang.StringBuilder;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cases = in.nextInt();
for (int t = 1; t <= cases; t++) {
int rows = in.nextInt();
int columns = in.nextInt();
int targetRow = in.nextInt();
int targetColumn = in.nextInt();
ArrayList<ArrayList<Character>> grid = new ArrayList<>();
int solution = 0;
boolean bOccurred = false;
// Filling grid
for (int i = 0; i < rows; i++) {
ArrayList<Character> row = new ArrayList<>();
if (i == 0)
in.nextLine();
String s = in.nextLine();
for (int j = 0; j < columns; j++) {
if (s.charAt(j) == 'B') {
bOccurred = true;
}
row.add(s.charAt(j));
}
grid.add(row);
}
if (!bOccurred) {
System.out.println(-1);
continue;
}
if (grid.get(targetRow - 1).get(targetColumn - 1) == 'B') {
System.out.println(0);
continue;
}
if (bAtRow(targetRow, grid) == 0 && bAtCol(targetColumn, grid) == 0) {
System.out.println(2);
continue;
}
System.out.println(1);
}
in.close();
}
public static int bAtRow(int targetRow, ArrayList<ArrayList<Character>> grid) {
int count = 0;
ArrayList<Character> row = grid.get(targetRow - 1);
for (int i = 0; i < row.size(); i++) {
if (row.get(i) == 'B') {
count++;
}
}
return count;
}
public static int bAtCol(int targetCol, ArrayList<ArrayList<Character>> grid) {
int count = 0;
for (int i = 0; i < grid.size(); i++) {
if (grid.get(i).get(targetCol - 1) == 'B') {
count++;
}
}
return count;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
9de726428094d52e1c43c8e6e650a64d
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class m
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int a=0;a<t;a++)
{
int n=sc.nextInt();
int m=sc.nextInt();
int r=sc.nextInt();
int c=sc.nextInt();
int arr[][]=new int[n][m];
int b=0,w=0,f=0;
sc.nextLine();
for(int i=0;i<n;i++)
{
String s=sc.nextLine();
for(int j=0;j<m;j++)
{
if(s.charAt(j)=='W')
{
arr[i][j]=0;
w++;
}
else if(s.charAt(j)=='B')
{
arr[i][j]=1;
b++;
}
}
}
r=r-1;
c=c-1;
if(b==0)
{
System.out.println("-1");
}
else if(arr[r][c]==1)
{
System.out.println(f);
}
else
{
if(arr[r][c]!=1)
{
for(int q1=0;q1<m;q1++)
{
if(arr[r][q1]==1)
{
f++;
break;
}
}
if(f!=1)
{
for(int q1=0;q1<n;q1++)
{
if(arr[q1][c]==1)
{
f++;
break;
}
}
}
if(f==1)
{
System.out.println(f);
}
else
{
System.out.println(2);
}
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
649a8b975548ff5a5d91f15baeafe7ea
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int tc = Integer.parseInt(line);
while(tc -- > 0){
line = sc.nextLine();
//System.out.println(line);
//System.exit(1);
String [] inputs = line.trim().split(" ");
int m = Integer.parseInt(inputs[0]);
int n = Integer.parseInt(inputs[1]);
int r = Integer.parseInt(inputs[2])-1;
int c = Integer.parseInt(inputs[3])-1;
int result = Integer.MAX_VALUE;
for(int i = 0; i < m; i++){
line = sc.nextLine();
for(int j = 0; j < line.length(); j++) {
if(line.charAt(j) == 'B') {
if(r == i && c == j) {
result = Math.min(result,0);
} else if (r == i || c == j) {
result = Math.min(result,1);
} else {
result = Math.min(result,2);
}
}
}
}
System.out.println(result == Integer.MAX_VALUE ? -1 : result);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
30bd012f6ef4b3c197b064972a84d8e6
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
sc=new MScanner(System.in);
pw=new PrintWriter(System.out);
int t=Integer.valueOf(sc.nextLine()) ;
for (int i=0;i<t;i++){
solver();
}
pw.flush();
}
private static void solver() throws IOException {
String[] s = sc.nextLine().split(" ");
int n=Integer.valueOf(s[0]);
int m=Integer.valueOf(s[1]);
int r=Integer.valueOf(s[2]);
int c=Integer.valueOf(s[3]);
int ans=3;
for (int i=0;i<n;i++){
String s1=sc.nextLine();
for (int j=0;j<m;j++){
if(s1.charAt(j)=='B'){
if(r-1==i && c-1==j) ans=0;
ans=Math.min(ans,(r-1==i || c-1==j)?1:2);
}
}
}
if(ans==3) ans=-1;
pw.write(ans+"\n");
pw.flush();
}
static int curt;
static PrintWriter pw;
static MScanner sc;
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] intArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public long[] longArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public int[] intSortedArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
shuffle(in);
Arrays.sort(in);
return in;
}
public long[] longSortedArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
shuffle(in);
Arrays.sort(in);
return in;
}
public Integer[] IntegerArr(int n) throws IOException {
Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public Long[] LongArr(int n) throws IOException {
Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
static void dbg(int[]in) {
System.out.println(Arrays.toString(in));
}
static void dbg(long[]in) {
System.out.println(Arrays.toString(in));
}
static void sort(int[]in) {
shuffle(in);
Arrays.sort(in);
}
static void sort(long[]in) {
shuffle(in);
Arrays.sort(in);
}
static void shuffle(int[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
int tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
static void shuffle(long[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
long tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
0d88a3629ade4c99b490fb8e8e6eef13
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static FastReader in = new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
static String solve() {
StringBuilder st = new StringBuilder();
String S = "";
int n = in.nextInt();
int m = in.nextInt();
int r = in.nextInt();
int c = in.nextInt();
int flag = 0;
int flagr = 0;
int flagc = 0;
String ar[] = new String[n];
for(int i = 0; i<n; i++){
ar[i]= in.nextLine();
for(int j = 0; j<m; j++){
char ch = ar[i].charAt(j);
if(ch == 'B' && flag == 0){
flag = 1;
}
if((j+1 == c || i+1 == r )&&ch == 'B'){
flagr = flagc = 1;
}
}
}
if(flag == 0){
return "-1";
}
else if(ar[r-1].charAt(c-1) == 'B'){
return "0";
}
else if(flagr ==1 || flagc ==1){
return "1";
}
else{
return "2";
}
}
public static void main(String args[]) throws IOException {
int t=in.nextInt();
StringBuilder res=new StringBuilder();
int cse=1;
loop:
while(t-->0)
{
String S = solve();
res.append(S + "\n");
}
print(res);
System.out.flush();
}
static int max(int a, int b)
{
if(a<b)
return b;
return a;
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static < E > void print(E res)
{
System.out.println(res);
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static int arraygcd(int[] ar) {
int n = ar.length;
int result = ar[0];
for(int i = 1; i<n; i++) {
result = gcd(ar[i], result);
}
return result;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int arraylcm(int[] ar) {
int result = ar[0];
int n = ar.length;
for(int i = 1; i<n; i++) {
result = lcm(result, ar[i]);
}
return result;
}
static int abs(int a)
{
if(a<0)
return -1*a;
return a;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int [] readintarray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] readlongarray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
a7dfbbc2398d7c4b371d72e51a9d58a3
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int noTestCases = scanner.nextInt();
while (noTestCases-- > 0) {
int rowsNumber = scanner.nextInt();
int colsNumber = scanner.nextInt();
int targetRow = scanner.nextInt() - 1;
int targetCol = scanner.nextInt() - 1;
scanner.nextLine();
char[][] rows = new char[rowsNumber][];
for (int idx = 0; idx < rowsNumber; idx++) {
rows[idx] = scanner.nextLine().toCharArray();
}
if (rows[targetRow][targetCol] == 'B') {
System.out.println(0);
} else if (isBlackInRow(rows, targetRow) || isBlackInCol(rows, targetCol)) {
System.out.println(1);
} else if (isBlack(rows)) {
System.out.println(2);
} else {
System.out.println(-1);
}
}
}
}
private static boolean isBlack(char[][] rows) {
for (char[] row : rows) {
for (char c : row) {
if (c == 'B') {
return true;
}
}
}
return false;
}
private static boolean isBlackInCol(char[][] rows, int targetCol) {
for (char[] row : rows) {
if (row[targetCol] == 'B') {
return true;
}
}
return false;
}
private static boolean isBlackInRow(char[][] rows, int targetRow) {
for (char c : rows[targetRow]) {
if (c == 'B') {
return true;
}
}
return false;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
0835bfd399d91a6e0a52de687cdb7e5b
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class file
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner (System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
int r=sc.nextInt();
int c=sc.nextInt();
char arr[][]=new char[n][m];
boolean flag=false,flag2=false;
int ans=0;
for(int i=0;i<n;i++)
{
String s=sc.next();
for(int j=0;j<m;j++)
{
arr[i][j]=s.charAt(j);
if(arr[i][j]=='B')
flag=true;
}
}
if(arr[r-1][c-1]=='B')
ans=0;
else if(flag==false)
ans=-1;
else
{
for(int j=0;j<m;j++)
{if(arr[r-1][j]=='B')
{
flag2=true;
}}
if(flag2==false)
{for(int i=0;i<n;i++)
{
if(arr[i][c-1]=='B')
flag2=true;
}
}
if(flag2==true)
ans=1;
else
ans=2;
}
System.out.println(ans);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
548a8cf1a85247919d0bbdf5435ed301
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class A1627 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt() - 1;
int c = sc.nextInt() - 1;
boolean oneB = false;
int ans = 2;
for (int i = 0; i < n; i++) {
char[] row = sc.next().toCharArray();
for (int j = 0; j < m; j++) {
if (row[j] == 'B') {
oneB = true;
if (i == r && j == c) {
ans = 0;
} else if (i == r || j == c) {
ans = Math.min(ans, 1);
}
}
}
}
pw.println(oneB ? ans : -1);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
3a9593f697405b21508f2cf19a12c225
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class BinaryDecimal {
public static void main(String[] args) {
Scanner fs = new Scanner(System.in);
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
int m = fs.nextInt();
int r = fs.nextInt();
int c = fs.nextInt();
int a[][] = new int[n][m];
int black = 0;
for (int i = 0; i < n; i++) {
String s = fs.next();
for (int j = 0; j < m; j++) {
if (s.charAt(j) == 'B') {
black++;
a[i][j] = 1;
}
}
}
r--;
c--;
if (a[r][c] == 1)
System.out.println(0);
else if (black == 0)
System.out.println(-1);
else {
boolean flag = false;
for (int i = 0; i < m; i++)
if (a[r][i] == 1) {
flag = true;
break;
}
if (!flag) {
for (int i = 0; i < n; i++)
if (a[i][c] == 1) {
flag = true;
break;
}
}
if (flag)
System.out.println(1);
else
System.out.println(2);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
cb2d7261af8571a4915008c37c7b1762
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class A1627 {
static FastScanner sc = new FastScanner();
public static void main(String[] args) {
int T = sc.nextInt();
while (T-- > 0) {
solve();
}
}
public static void solve() {
int n = sc.nextInt(), m = sc.nextInt(), r = sc.nextInt(), c = sc.nextInt();
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = sc.next();
}
if (a[r - 1].charAt(c - 1) == 'B') {
System.out.println(0);
} else {
int d = 0;
for (int i = 0; i < n; i++) {
for (int k = 0; k < m; k++) {
if (a[i].charAt(k) == 'W') {
d++;
}
}
}
int b = 0;
for (int q = 0; q < m; q++) {
if (a[r - 1].charAt(q) == 'B') {
b++;
}
}
int j = 0;
for (int e = 0; e < n; e++) {
if (a[e].charAt(c - 1) == 'B') {
j++;
}
}
if (d == n * m) {
System.out.println(-1);
} else {
if (b == 0 && j == 0) {
System.out.println(2);
} else {
System.out.println(1);
}
}
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
return a;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
18a5d70ca11177cbbe3a2579298f3dd5
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class test302 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int j=0;j<t;j++) {
boolean b=false,c=false,d=false;
int m=in.nextInt();
int n=in.nextInt();
int x=in.nextInt();
int y=in.nextInt();
for(int i=0;i<m;i++) {
char[] a=in.next().toCharArray();
for(int k=0;k<n;k++) {
if(a[k]=='B') {
c=true;
}
if(k==y-1 && i==x-1) {
if(a[k]=='B')
d=true;
}
else if(k==y-1 || i==x-1) {
if(a[k]=='B')
b=true;
}
}
}
if(c) {
if(d) {
System.out.println(0);
}
else if(b) {
System.out.println(1);
}
else {
System.out.println(2);
}
}
else {
System.out.println(-1);
}
}
in.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
35cb37d21bcb201ead45d9be69dc129e
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
public final class CFPS {
static FastReader fr = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static final int gigamod = 1000000007;
static final int mod = 998244353;
static int t = 1;
static boolean[] isPrime;
static int[] smallestFactorOf;
static final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;
static long cmp;
@SuppressWarnings({"unused"})
public static void main(String[] args) throws Exception {
t = fr.nextInt();
OUTER:
for (int tc = 0; tc < t; tc++) {
int n = fr.nextInt(), m = fr.nextInt(), r = fr.nextInt() - 1, c = fr.nextInt() - 1;
char[][] grid = new char[n][];
for (int i = 0; i < n; i++)
grid[i] = fr.next().toCharArray();
int blackCnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (grid[i][j] == 'B')
blackCnt++;
if (blackCnt == 0) {
out.println(-1);
continue OUTER;
}
// it is possible
if (grid[r][c] == 'B') {
out.println(0);
continue OUTER;
}
for (int col = 0; col < m; col++)
if (grid[r][col] == 'B') {
out.println(1);
continue OUTER;
}
for (int row = 0; row < n; row++) {
if (grid[row][c] == 'B') {
out.println(1);
continue OUTER;
}
}
out.println(2);
}
out.close();
}
static class Pair implements Comparable<Pair> {
int first, second;
int idx;
Pair() { first = second = 0; }
Pair (int ff, int ss) {first = ff; second = ss;}
Pair (int ff, int ss, int ii) { first = ff; second = ss; idx = ii; }
public int compareTo(Pair that) {
cmp = first - that.first;
if (cmp == 0)
cmp = second - that.second;
return (int) cmp;
}
}
// (range add - segment min) segTree
static int nn;
static int[] arr;
static int[] tree;
static int[] lazy;
static void build(int node, int leftt, int rightt) {
if (leftt == rightt) {
tree[node] = arr[leftt];
return;
}
int mid = (leftt + rightt) >> 1;
build(node << 1, leftt, mid);
build(node << 1 | 1, mid + 1, rightt);
tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]);
}
static void segAdd(int node, int leftt, int rightt, int segL, int segR, int val) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (leftt != rightt) {
lazy[node << 1] += lazy[node];
lazy[node << 1 | 1] += lazy[node];
}
lazy[node] = 0;
}
if (segL > rightt || segR < leftt) return;
if (segL <= leftt && rightt <= segR) {
tree[node] += val;
if (leftt != rightt) {
lazy[node << 1] += val;
lazy[node << 1 | 1] += val;
}
lazy[node] = 0;
return;
}
int mid = (leftt + rightt) >> 1;
segAdd(node << 1, leftt, mid, segL, segR, val);
segAdd(node << 1 | 1, mid + 1, rightt, segL, segR, val);
tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]);
}
static int minQuery(int node, int leftt, int rightt, int segL, int segR) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (leftt != rightt) {
lazy[node << 1] += lazy[node];
lazy[node << 1 | 1] += lazy[node];
}
lazy[node] = 0;
}
if (segL > rightt || segR < leftt) return Integer.MAX_VALUE / 10;
if (segL <= leftt && rightt <= segR)
return tree[node];
int mid = (leftt + rightt) >> 1;
return Math.min(minQuery(node << 1, leftt, mid, segL, segR),
minQuery(node << 1 | 1, mid + 1, rightt, segL, segR));
}
static class Segment {
int li, ri, wi, id;
Segment(int ll, int rr, int ww, int ii) {
li = ll;
ri = rr;
wi = ww;
id = ii;
}
}
static void compute_automaton(String s, int[][] aut) {
s += '#';
int n = s.length();
int[] pi = prefix_function(s.toCharArray());
for (int i = 0; i < n; i++) {
for (int c = 0; c < 26; c++) {
int j = i;
while (j > 0 && 'A' + c != s.charAt(j))
j = pi[j-1];
if ('A' + c == s.charAt(j))
j++;
aut[i][c] = j;
}
}
}
static void timeDFS(int current, int from, UGraph ug,
int[] time, int[] tIn, int[] tOut) {
tIn[current] = ++time[0];
for (int adj : ug.adj(current))
if (adj != from)
timeDFS(adj, current, ug, time, tIn, tOut);
tOut[current] = ++time[0];
}
static boolean areCollinear(long x1, long y1, long x2, long y2, long x3, long y3) {
// we will check if c3 lies on line through (c1, c2)
long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
return a == 0;
}
static int[] treeDiameter(UGraph ug) {
int n = ug.V();
int farthest = -1;
int[] distTo = new int[n];
diamDFS(0, -1, 0, ug, distTo);
int maxDist = -1;
for (int i = 0; i < n; i++)
if (maxDist < distTo[i]) {
maxDist = distTo[i];
farthest = i;
}
distTo = new int[n + 1];
diamDFS(farthest, -1, 0, ug, distTo);
distTo[n] = farthest;
return distTo;
}
static void diamDFS(int current, int from, int dist, UGraph ug, int[] distTo) {
distTo[current] = dist;
for (int adj : ug.adj(current))
if (adj != from)
diamDFS(adj, current, dist + 1, ug, distTo);
}
static class TreeDistFinder {
UGraph ug;
int n;
int[] depthOf;
LCA lca;
TreeDistFinder(UGraph ug) {
this.ug = ug;
n = ug.V();
depthOf = new int[n];
depthCalc(0, -1, ug, 0, depthOf);
lca = new LCA(ug, 0);
}
TreeDistFinder(UGraph ug, int a) {
this.ug = ug;
n = ug.V();
depthOf = new int[n];
depthCalc(a, -1, ug, 0, depthOf);
lca = new LCA(ug, a);
}
private void depthCalc(int current, int from, UGraph ug, int depth, int[] depthOf) {
depthOf[current] = depth;
for (int adj : ug.adj(current))
if (adj != from)
depthCalc(adj, current, ug, depth + 1, depthOf);
}
public int dist(int a, int b) {
int lc = lca.lca(a, b);
return (depthOf[a] - depthOf[lc] + depthOf[b] - depthOf[lc]);
}
}
public static long[][] GCDSparseTable(long[] a)
{
int n = a.length;
int b = 32-Integer.numberOfLeadingZeros(n);
long[][] ret = new long[b][];
for(int i = 0, l = 1;i < b;i++, l*=2) {
if(i == 0) {
ret[i] = a;
} else {
ret[i] = new long[n-l+1];
for(int j = 0;j < n-l+1;j++) {
ret[i][j] = gcd(ret[i-1][j], ret[i-1][j+l/2]);
}
}
}
return ret;
}
public static long sparseRangeGCDQ(long[][] table, int l, int r)
{
// [a,b)
if(l > r)return 1;
// 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3
int t = 31-Integer.numberOfLeadingZeros(r-l);
return gcd(table[t][l], table[t][r-(1<<t)]);
}
static class Trie {
TrieNode root;
Trie(char[][] strings) {
root = new TrieNode('A', false);
construct(root, strings);
}
public Stack<String> set(TrieNode root) {
Stack<String> set = new Stack<>();
StringBuilder sb = new StringBuilder();
for (TrieNode next : root.next)
collect(sb, next, set);
return set;
}
private void collect(StringBuilder sb, TrieNode node, Stack<String> set) {
if (node == null) return;
sb.append(node.character);
if (node.isTerminal)
set.add(sb.toString());
for (TrieNode next : node.next)
collect(sb, next, set);
if (sb.length() > 0)
sb.setLength(sb.length() - 1);
}
private void construct(TrieNode root, char[][] strings) {
// we have to construct the Trie
for (char[] string : strings) {
if (string.length == 0) continue;
root.next[string[0] - 'a'] = put(root.next[string[0] - 'a'], string, 0);
if (root.next[string[0] - 'a'] != null)
root.isLeaf = false;
}
}
private TrieNode put(TrieNode node, char[] string, int idx) {
boolean isTerminal = (idx == string.length - 1);
if (node == null) node = new TrieNode(string[idx], isTerminal);
node.character = string[idx];
node.isTerminal |= isTerminal;
if (!isTerminal) {
node.isLeaf = false;
node.next[string[idx + 1] - 'a'] = put(node.next[string[idx + 1] - 'a'], string, idx + 1);
}
return node;
}
class TrieNode {
char character;
TrieNode[] next;
boolean isTerminal, isLeaf;
boolean canWin, canLose;
TrieNode(char c, boolean isTerminallll) {
character = c;
isTerminal = isTerminallll;
next = new TrieNode[26];
isLeaf = true;
}
}
}
static class Edge implements Comparable<Edge> {
int from, to;
long weight, ans;
int id;
// int hash;
Edge(int fro, int t, long wt, int i) {
from = fro;
to = t;
id = i;
weight = wt;
// hash = Objects.hash(from, to, weight);
}
/*public int hashCode() {
return hash;
}*/
public int compareTo(Edge that) {
return Long.compare(this.id, that.id);
}
}
public static long[][] minSparseTable(long[] a)
{
int n = a.length;
int b = 32-Integer.numberOfLeadingZeros(n);
long[][] ret = new long[b][];
for(int i = 0, l = 1;i < b;i++, l*=2) {
if(i == 0) {
ret[i] = a;
}else {
ret[i] = new long[n-l+1];
for(int j = 0;j < n-l+1;j++) {
ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]);
}
}
}
return ret;
}
public static long sparseRangeMinQ(long[][] table, int l, int r)
{
// [a,b)
if(l >= r)return Integer.MAX_VALUE;
// 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3
int t = 31-Integer.numberOfLeadingZeros(r-l);
return Math.min(table[t][l], table[t][r-(1<<t)]);
}
public static long[][] maxSparseTable(long[] a)
{
int n = a.length;
int b = 32-Integer.numberOfLeadingZeros(n);
long[][] ret = new long[b][];
for(int i = 0, l = 1;i < b;i++, l*=2) {
if(i == 0) {
ret[i] = a;
}else {
ret[i] = new long[n-l+1];
for(int j = 0;j < n-l+1;j++) {
ret[i][j] = Math.max(ret[i-1][j], ret[i-1][j+l/2]);
}
}
}
return ret;
}
public static long sparseRangeMaxQ(long[][] table, int l, int r)
{
// [a,b)
if(l >= r)return Integer.MIN_VALUE;
// 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3
int t = 31-Integer.numberOfLeadingZeros(r-l);
return Math.max(table[t][l], table[t][r-(1<<t)]);
}
static class LCA {
int[] height, first, segtree;
ArrayList<Integer> euler;
boolean[] visited;
int n;
LCA(UGraph ug, int root) {
n = ug.V();
height = new int[n];
first = new int[n];
euler = new ArrayList<>();
visited = new boolean[n];
dfs(ug, root, 0);
int m = euler.size();
segtree = new int[m * 4];
build(1, 0, m - 1);
}
void dfs(UGraph ug, int node, int h) {
visited[node] = true;
height[node] = h;
first[node] = euler.size();
euler.add(node);
for (int adj : ug.adj(node)) {
if (!visited[adj]) {
dfs(ug, adj, h + 1);
euler.add(node);
}
}
}
void build(int node, int b, int e) {
if (b == e) {
segtree[node] = euler.get(b);
} else {
int mid = (b + e) / 2;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
int l = segtree[node << 1], r = segtree[node << 1 | 1];
segtree[node] = (height[l] < height[r]) ? l : r;
}
}
int query(int node, int b, int e, int L, int R) {
if (b > R || e < L)
return -1;
if (b >= L && e <= R)
return segtree[node];
int mid = (b + e) >> 1;
int left = query(node << 1, b, mid, L, R);
int right = query(node << 1 | 1, mid + 1, e, L, R);
if (left == -1) return right;
if (right == -1) return left;
return height[left] < height[right] ? left : right;
}
int lca(int u, int v) {
int left = first[u], right = first[v];
if (left > right) {
int temp = left;
left = right;
right = temp;
}
return query(1, 0, euler.size() - 1, left, right);
}
}
static class FenwickTree {
long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates
public FenwickTree(int size) {
array = new long[size + 1];
}
public long rsq(int ind) {
assert ind > 0;
long sum = 0;
while (ind > 0) {
sum += array[ind];
//Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number
ind -= ind & (-ind);
}
return sum;
}
public long rsq(int a, int b) {
assert b >= a && a > 0 && b > 0;
return rsq(b) - rsq(a - 1);
}
public void update(int ind, long value) {
assert ind > 0;
while (ind < array.length) {
array[ind] += value;
//Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number
ind += ind & (-ind);
}
}
public int size() {
return array.length - 1;
}
}
static class Point implements Comparable<Point> {
long x;
long y;
long z;
long id;
// private int hashCode;
Point() {
x = z = y = 0;
// this.hashCode = Objects.hash(x, y, cost);
}
Point(Point p) {
this.x = p.x;
this.y = p.y;
this.z = p.z;
this.id = p.id;
// this.hashCode = Objects.hash(x, y, cost);
}
Point(long x, long y, long z, long id) {
this.x = x;
this.y = y;
this.z = z;
this.id = id;
// this.hashCode = Objects.hash(x, y, id);
}
Point(long a, long b) {
this.x = a;
this.y = b;
this.z = 0;
// this.hashCode = Objects.hash(a, b);
}
Point(long x, long y, long id) {
this.x = x;
this.y = y;
this.id = id;
}
@Override
public int compareTo(Point o) {
if (this.x < o.x)
return -1;
if (this.x > o.x)
return 1;
if (this.y < o.y)
return -1;
if (this.y > o.y)
return 1;
if (this.z < o.z)
return -1;
if (this.z > o.z)
return 1;
return 0;
}
@Override
public boolean equals(Object that) {
return this.compareTo((Point) that) == 0;
}
}
static class BinaryLift {
// FUNCTIONS: k-th ancestor and LCA in log(n)
int[] parentOf;
int maxJmpPow;
int[][] binAncestorOf;
int n;
int[] lvlOf;
// How this works?
// a. For every node, we store the b-ancestor for b in {1, 2, 4, 8, .. log(n)}.
// b. When we need k-ancestor, we represent 'k' in binary and for each set bit, we
// lift level in the tree.
public BinaryLift(UGraph tree) {
n = tree.V();
maxJmpPow = logk(n, 2) + 1;
parentOf = new int[n];
binAncestorOf = new int[n][maxJmpPow];
lvlOf = new int[n];
for (int i = 0; i < n; i++)
Arrays.fill(binAncestorOf[i], -1);
parentConstruct(0, -1, tree, 0);
binConstruct();
}
// TODO: Implement lvlOf[] initialization
public BinaryLift(int[] parentOf) {
this.parentOf = parentOf;
n = parentOf.length;
maxJmpPow = logk(n, 2) + 1;
binAncestorOf = new int[n][maxJmpPow];
lvlOf = new int[n];
for (int i = 0; i < n; i++)
Arrays.fill(binAncestorOf[i], -1);
UGraph tree = new UGraph(n);
for (int i = 1; i < n; i++)
tree.addEdge(i, parentOf[i]);
binConstruct();
parentConstruct(0, -1, tree, 0);
}
private void parentConstruct(int current, int from, UGraph tree, int depth) {
parentOf[current] = from;
lvlOf[current] = depth;
for (int adj : tree.adj(current))
if (adj != from)
parentConstruct(adj, current, tree, depth + 1);
}
private void binConstruct() {
for (int node = 0; node < n; node++)
for (int lvl = 0; lvl < maxJmpPow; lvl++)
binConstruct(node, lvl);
}
private int binConstruct(int node, int lvl) {
if (node < 0)
return -1;
if (lvl == 0)
return binAncestorOf[node][lvl] = parentOf[node];
if (node == 0)
return binAncestorOf[node][lvl] = -1;
if (binAncestorOf[node][lvl] != -1)
return binAncestorOf[node][lvl];
return binAncestorOf[node][lvl] = binConstruct(binConstruct(node, lvl - 1), lvl - 1);
}
// return ancestor which is 'k' levels above this one
public int ancestor(int node, int k) {
if (node < 0)
return -1;
if (node == 0)
if (k == 0) return node;
else return -1;
if (k > (1 << maxJmpPow) - 1)
return -1;
if (k == 0)
return node;
int ancestor = node;
int highestBit = Integer.highestOneBit(k);
while (k > 0 && ancestor != -1) {
ancestor = binAncestorOf[ancestor][logk(highestBit, 2)];
k -= highestBit;
highestBit = Integer.highestOneBit(k);
}
return ancestor;
}
public int lca(int u, int v) {
if (u == v)
return u;
// The invariant will be that 'u' is below 'v' initially.
if (lvlOf[u] < lvlOf[v]) {
int temp = u;
u = v;
v = temp;
}
// Equalizing the levels.
u = ancestor(u, lvlOf[u] - lvlOf[v]);
if (u == v)
return u;
// We will now raise level by largest fitting power of two until possible.
for (int power = maxJmpPow - 1; power > -1; power--)
if (binAncestorOf[u][power] != binAncestorOf[v][power]) {
u = binAncestorOf[u][power];
v = binAncestorOf[v][power];
}
return ancestor(u, 1);
}
}
static class DFSTree {
// NOTE: The thing is made keeping in mind that the whole
// input graph is connected.
UGraph tree;
UGraph backUG;
int hasBridge;
int n;
Edge backEdge;
DFSTree(UGraph ug) {
this.n = ug.V();
tree = new UGraph(n);
hasBridge = -1;
backUG = new UGraph(n);
treeCalc(0, -1, new boolean[n], ug);
}
private void treeCalc(int current, int from, boolean[] marked, UGraph ug) {
if (marked[current]) {
// This is a backEdge.
backUG.addEdge(from, current);
backEdge = new Edge(from, current, 1, 0);
return;
}
if (from != -1)
tree.addEdge(from, current);
marked[current] = true;
for (int adj : ug.adj(current))
if (adj != from)
treeCalc(adj, current, marked, ug);
}
public boolean hasBridge() {
if (hasBridge != -1)
return (hasBridge == 1);
// We have to determine the bridge.
bridgeFinder();
return (hasBridge == 1);
}
int[] levelOf;
int[] dp;
private void bridgeFinder() {
// Finding the level of each node.
levelOf = new int[n];
levelDFS(0, -1, 0);
// Applying DP solution.
// dp[i] -> Highest level reachable from subtree of 'i' using
// some backEdge.
dp = new int[n];
Arrays.fill(dp, Integer.MAX_VALUE / 100);
dpDFS(0, -1);
// Now, we will check each edge and determine whether its a
// bridge.
for (int i = 0; i < n; i++)
for (int adj : tree.adj(i)) {
// (i -> adj) is the edge.
if (dp[adj] > levelOf[i])
hasBridge = 1;
}
if (hasBridge != 1)
hasBridge = 0;
}
private void levelDFS(int current, int from, int lvl) {
levelOf[current] = lvl;
for (int adj : tree.adj(current))
if (adj != from)
levelDFS(adj, current, lvl + 1);
}
private int dpDFS(int current, int from) {
dp[current] = levelOf[current];
for (int back : backUG.adj(current))
dp[current] = Math.min(dp[current], levelOf[back]);
for (int adj : tree.adj(current))
if (adj != from)
dp[current] = Math.min(dp[current], dpDFS(adj, current));
return dp[current];
}
}
static class UnionFind {
// Uses weighted quick-union with path compression.
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of sites in tree rooted at i
// Note: not necessarily correct if i is not a root node
private int count; // number of components
public UnionFind(int n) {
count = n;
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
// Number of connected components.
public int count() {
return count;
}
// Find the root of p.
public int find(int p) {
while (p != parent[p])
p = parent[p];
return p;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int numConnectedTo(int node) {
return size[find(node)];
}
// Weighted union.
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make smaller root point to larger one
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
count--;
}
public static int[] connectedComponents(UnionFind uf) {
// We can do this in nlogn.
int n = uf.size.length;
int[] compoColors = new int[n];
for (int i = 0; i < n; i++)
compoColors[i] = uf.find(i);
HashMap<Integer, Integer> oldToNew = new HashMap<>();
int newCtr = 0;
for (int i = 0; i < n; i++) {
int thisOldColor = compoColors[i];
Integer thisNewColor = oldToNew.get(thisOldColor);
if (thisNewColor == null)
thisNewColor = newCtr++;
oldToNew.put(thisOldColor, thisNewColor);
compoColors[i] = thisNewColor;
}
return compoColors;
}
}
static class UGraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public UGraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
adj[to].add(from);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int degree(int v) {
return adj[v].size();
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void dfsMark(int current, boolean[] marked, UGraph g) {
if (marked[current]) return;
marked[current] = true;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, marked, g);
}
public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) {
if (marked[current]) return;
marked[current] = true;
if (from != -1)
distTo[current] = distTo[from] + 1;
HashSet<Integer> adj = g.adj(current);
int alreadyMarkedCtr = 0;
for (int adjc : adj) {
if (marked[adjc]) alreadyMarkedCtr++;
dfsMark(adjc, current, distTo, marked, g, endPoints);
}
if (alreadyMarkedCtr == adj.size())
endPoints.add(current);
}
public static void bfsOrder(int current, UGraph g) {
}
public static void dfsMark(int current, int[] colorIds, int color, UGraph g) {
if (colorIds[current] != -1) return;
colorIds[current] = color;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, colorIds, color, g);
}
public static int[] connectedComponents(UGraph g) {
int n = g.V();
int[] componentId = new int[n];
Arrays.fill(componentId, -1);
int colorCtr = 0;
for (int i = 0; i < n; i++) {
if (componentId[i] != -1) continue;
dfsMark(i, componentId, colorCtr, g);
colorCtr++;
}
return componentId;
}
public static boolean hasCycle(UGraph ug) {
int n = ug.V();
boolean[] marked = new boolean[n];
boolean[] hasCycleFirst = new boolean[1];
for (int i = 0; i < n; i++) {
if (marked[i]) continue;
hcDfsMark(i, ug, marked, hasCycleFirst, -1);
}
return hasCycleFirst[0];
}
// Helper for hasCycle.
private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) {
if (marked[current]) return;
if (hasCycleFirst[0]) return;
marked[current] = true;
HashSet<Integer> adjc = ug.adj(current);
for (int adj : adjc) {
if (marked[adj] && adj != parent && parent != -1) {
hasCycleFirst[0] = true;
return;
}
hcDfsMark(adj, ug, marked, hasCycleFirst, current);
}
}
}
static class Digraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public Digraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public Digraph reversed() {
Digraph dg = new Digraph(V());
for (int i = 0; i < V(); i++)
for (int adjVert : adj(i)) dg.addEdge(adjVert, i);
return dg;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static int[] KosarajuSharirSCC(Digraph dg) {
int[] id = new int[dg.V()];
Digraph reversed = dg.reversed();
// Gotta perform topological sort on this one to get the stack.
Stack<Integer> revStack = Digraph.topologicalSort(reversed);
// Initializing id and idCtr.
id = new int[dg.V()];
int idCtr = -1;
// Creating a 'marked' array.
boolean[] marked = new boolean[dg.V()];
while (!revStack.isEmpty()) {
int vertex = revStack.pop();
if (!marked[vertex])
sccDFS(dg, vertex, marked, ++idCtr, id);
}
return id;
}
private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) {
marked[source] = true;
id[source] = idCtr;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id);
}
public static Stack<Integer> topologicalSort(Digraph dg) {
// dg has to be a directed acyclic graph.
// We'll have to run dfs on the digraph and push the deepest nodes on stack first.
// We'll need a Stack<Integer> and a int[] marked.
Stack<Integer> topologicalStack = new Stack<Integer>();
boolean[] marked = new boolean[dg.V()];
// Calling dfs
for (int i = 0; i < dg.V(); i++)
if (!marked[i])
runDfs(dg, topologicalStack, marked, i);
return topologicalStack;
}
static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) {
marked[source] = true;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex])
runDfs(dg, topologicalStack, marked, adjVertex);
topologicalStack.add(source);
}
}
static class FastReader {
private BufferedReader bfr;
private StringTokenizer st;
public FastReader() {
bfr = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
if (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(bfr.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().toCharArray()[0];
}
String nextString() {
return next();
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
double[] nextDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextDouble();
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
int[][] nextIntGrid(int n, int m) {
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
grid[i][j] = fr.nextInt();
}
return grid;
}
}
@SuppressWarnings("serial")
static class CountMap<T> extends TreeMap<T, Integer>{
CountMap() {
}
CountMap(Comparator<T> cmp) {
}
CountMap(T[] arr) {
this.putCM(arr);
}
public Integer putCM(T key) {
return super.put(key, super.getOrDefault(key, 0) + 1);
}
public Integer removeCM(T key) {
int count = super.getOrDefault(key, -1);
if (count == -1) return -1;
if (count == 1)
return super.remove(key);
else
return super.put(key, count - 1);
}
public Integer getCM(T key) {
return super.getOrDefault(key, 0);
}
public void putCM(T[] arr) {
for (T l : arr)
this.putCM(l);
}
}
static long dioGCD(long a, long b, long[] x0, long[] y0) {
if (b == 0) {
x0[0] = 1;
y0[0] = 0;
return a;
}
long[] x1 = new long[1], y1 = new long[1];
long d = dioGCD(b, a % b, x1, y1);
x0[0] = y1[0];
y0[0] = x1[0] - y1[0] * (a / b);
return d;
}
static boolean diophantine(long a, long b, long c, long[] x0, long[] y0, long[] g) {
g[0] = dioGCD(Math.abs(a), Math.abs(b), x0, y0);
if (c % g[0] > 0) {
return false;
}
x0[0] *= c / g[0];
y0[0] *= c / g[0];
if (a < 0) x0[0] = -x0[0];
if (b < 0) y0[0] = -y0[0];
return true;
}
static long[][] prod(long[][] mat1, long[][] mat2) {
int n = mat1.length;
long[][] prod = new long[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
// determining prod[i][j]
// it will be the dot product of mat1[i][] and mat2[][i]
for (int k = 0; k < n; k++)
prod[i][j] += mat1[i][k] * mat2[k][j];
return prod;
}
static long[][] matExpo(long[][] mat, long power) {
int n = mat.length;
long[][] ans = new long[n][n];
if (power == 0)
return null;
if (power == 1)
return mat;
long[][] half = matExpo(mat, power / 2);
ans = prod(half, half);
if (power % 2 == 1) {
ans = prod(ans, mat);
}
return ans;
}
static int KMPNumOcc(char[] text, char[] pat) {
int n = text.length;
int m = pat.length;
char[] patPlusText = new char[n + m + 1];
for (int i = 0; i < m; i++)
patPlusText[i] = pat[i];
patPlusText[m] = '^'; // Seperator
for (int i = 0; i < n; i++)
patPlusText[m + i] = text[i];
int[] fullPi = piCalcKMP(patPlusText);
int answer = 0;
for (int i = 0; i < n + m + 1; i++)
if (fullPi[i] == m)
answer++;
return answer;
}
static int[] piCalcKMP(char[] s) {
int n = s.length;
int[] pi = new int[n];
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static boolean[] prefMatchesSuff(char[] s) {
int n = s.length;
boolean[] res = new boolean[n + 1];
int[] pi = prefix_function(s);
res[0] = true;
for (int p = n; p != 0; p = pi[p])
res[p] = true;
return res;
}
static int[] prefix_function(char[] s) {
int n = s.length;
int[] pi = new int[n];
for (int i = 1; i < n; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static long hash(long key) {
long h = Long.hashCode(key);
h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4);
return h & (gigamod-1);
}
static void Yes() {out.println("Yes");}static void YES() {out.println("YES");}static void yes() {out.println("Yes");}static void No() {out.println("No");}static void NO() {out.println("NO");}static void no() {out.println("no");}
static int mapTo1D(int row, int col, int n, int m) {
// Maps elements in a 2D matrix serially to elements in
// a 1D array.
return row * m + col;
}
static int[] mapTo2D(int idx, int n, int m) {
// Inverse of what the one above does.
int[] rnc = new int[2];
rnc[0] = idx / m;
rnc[1] = idx % m;
return rnc;
}
static boolean[] primeGenerator(int upto) {
// Sieve of Eratosthenes:
isPrime = new boolean[upto + 1];
smallestFactorOf = new int[upto + 1];
Arrays.fill(smallestFactorOf, 1);
Arrays.fill(isPrime, true);
isPrime[1] = isPrime[0] = false;
for (long i = 2; i < upto + 1; i++)
if (isPrime[(int) i]) {
smallestFactorOf[(int) i] = (int) i;
// Mark all the multiples greater than or equal
// to the square of i to be false.
for (long j = i; j * i < upto + 1; j++) {
if (isPrime[(int) j * (int) i]) {
isPrime[(int) j * (int) i] = false;
smallestFactorOf[(int) j * (int) i] = (int) i;
}
}
}
return isPrime;
}
static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) {
if (smallestFactorOf == null)
primeGenerator(num + 1);
HashMap<Integer, Integer> fnps = new HashMap<>();
while (num != 1) {
fnps.put(smallestFactorOf[num], fnps.getOrDefault(smallestFactorOf[num], 0) + 1);
num /= smallestFactorOf[num];
}
return fnps;
}
static HashMap<Long, Integer> primeFactorization(long num) {
// Returns map of factor and its power in the number.
HashMap<Long, Integer> map = new HashMap<>();
while (num % 2 == 0) {
num /= 2;
Integer pwrCnt = map.get(2L);
map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1);
}
for (long i = 3; i * i <= num; i += 2) {
while (num % i == 0) {
num /= i;
Integer pwrCnt = map.get(i);
map.put(i, pwrCnt != null ? pwrCnt + 1 : 1);
}
}
// If the number is prime, we have to add it to the
// map.
if (num != 1)
map.put(num, 1);
return map;
}
static HashSet<Long> divisors(long num) {
HashSet<Long> divisors = new HashSet<Long>();
divisors.add(1L);
divisors.add(num);
for (long i = 2; i * i <= num; i++) {
if (num % i == 0) {
divisors.add(num/i);
divisors.add(i);
}
}
return divisors;
}
static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) {
if (m > limit) return;
if (m <= limit && n <= limit)
coprimes.add(new Point(m, n));
if (coprimes.size() > numCoprimes) return;
coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes);
coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes);
coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes);
}
static long nCr(long n, long r, long[] fac) { long p = gigamod; if (r == 0) return 1; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; }
static long modInverse(long n, long p) { return power(n, p - 2, p); }
static long modDiv(long a, long b){return mod(a * power(b, mod - 2, mod), mod);}
static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }
static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); }
static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; }
static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); }
static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } }
static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } }
static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } }
static boolean isPrime(long n) {if (n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
static String toString(int[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();}
static String toString(boolean[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();}
static String toString(long[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();}
static String toString(char[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+"");return sb.toString();}
static String toString(int[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();}
static String toString(long[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++) {sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();}
static String toString(double[][] dp){StringBuilder sb=new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();}
static String toString(char[][] dp){StringBuilder sb = new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+"");}sb.append('\n');}return sb.toString();}
static long mod(long a, long m){return(a%m+1000000L*m)%m;}
}
/*
*
* int[] arr = new int[] {1810, 1700, 1710, 2320, 2000, 1785, 1780
, 2130, 2185, 1430, 1460, 1740, 1860, 1100, 1905, 1650};
int n = arr.length;
sort(arr);
int bel1700 = 0, bet1700n1900 = 0, abv1900 = 0;
for (int i = 0; i < n; i++)
if (arr[i] < 1700)
bel1700++;
else if (1700 <= arr[i] && arr[i] < 1900)
bet1700n1900++;
else if (arr[i] >= 1900)
abv1900++;
out.println("COUNT: " + n);
out.println("PERFS: " + toString(arr));
out.println("MEDIAN: " + arr[n / 2]);
out.println("AVERAGE: " + Arrays.stream(arr).average().getAsDouble());
out.println("[0, 1700): " + bel1700 + "/" + n);
out.println("[1700, 1900): " + bet1700n1900 + "/" + n);
out.println("[1900, 2400): " + abv1900 + "/" + n);
*
* */
// NOTES:
// ASCII VALUE OF 'A': 65
// ASCII VALUE OF 'a': 97
// Range of long: 9 * 10^18
// ASCII VALUE OF '0': 48
// Primes upto 'n' can be given by (n / (logn)).
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
92b0fc13bda609b37df0e3827f51845c
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t=scanner.nextInt();
while (t--!=0){
int n=scanner.nextInt();
int m=scanner.nextInt();
int r=scanner.nextInt();
int c=scanner.nextInt();
boolean hasblack=false;
char[][] map=new char[n][m];
boolean rhasblack=false;
boolean chasblack=false;
for (int i=0;i<n;i++){
String s=scanner.next();
for (int j=0;j<s.length();j++){
map[i][j]=s.charAt(j);
if (map[i][j]=='B'){
hasblack=true;
}
if (j==c-1&&map[i][j]=='B'){
chasblack=true;
}
if (i==r-1&&map[i][j]=='B'){
rhasblack=true;
}
}
}
if (!hasblack){
System.out.println(-1);
continue;
}
if (map[r-1][c-1]=='B'){
System.out.println(0);
continue;
}
if (rhasblack||chasblack){
System.out.println(1);
continue;
}
System.out.println(2);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
259662a514bab018060ac3347a3d061f
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.StringTokenizer;
public class Not_Shading
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0)
{
break;
}
else
{
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
{
c = read();
}
boolean neg = (c == '-');
if (neg)
{
c = read();
}
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
{
return -ret;
}
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
{
c = read();
}
boolean neg = (c == '-');
if (neg)
{
c = read();
}
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
{
return -ret;
}
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
{
c = read();
}
boolean neg = (c == '-');
if (neg)
{
c = read();
}
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
{
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
{
buffer[0] = -1;
}
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
{
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
{
return;
}
din.close();
}
}
public static void main(String Args[]) throws java.lang.Exception
{
try
{
Scanner obj = new Scanner (System.in);
int t = obj.nextInt();
while (t > 0)
{
t--;
int n = obj.nextInt();
int m = obj.nextInt();
int r = obj.nextInt();
int c = obj.nextInt();
r--;
c--;
int i, j;
boolean cell = false, black = false, rowcol = false;
for (i=0;i<n;i++)
{
String str = obj.next();
for (j=0;j<m;j++)
{
char ch = str.charAt(j);
if (ch == 'B')
{
black = true;
}
if (i == r && j == c && ch == 'B')
{
cell = true;
}
if ((i == r || j == c) && ch == 'B')
{
rowcol = true;
}
}
}
if (!black)
{
System.out.println ("-1");
}
else if (cell)
{
System.out.println ("0");
}
else if (rowcol)
{
System.out.println ("1");
}
else
{
System.out.println ("2");
}
}
}catch(Exception e){
return;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
a6451601d82a2db2f5e4211ad1311c67
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Solution{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
int MAX = Integer.MAX_VALUE/2;
public static void main(String... args) throws Exception{
FastReader in = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
Solution sol = new Solution();
int t = in.nextInt();
while(t-->0){
sol.solve(in, out);
}
out.flush();
}
// boolean debug = true;
boolean debug = false;
HashMap<String, Long> hm;
private void solve(FastReader in, BufferedWriter out) throws Exception{
int n = in.nextInt();
int m = in.nextInt();
int r = in.nextInt();
int c = in.nextInt();
r--;c--;
boolean[][] black = new boolean[n][m];
for(int i=0;i<n;i++) Arrays.fill(black[i], false);
boolean blackP = false;
for(int i=0;i<n;i++){
char[] input = in.next().toCharArray();
for(int j=0;j<m;j++){
if(input[j] == 'B'){
black[i][j] = true;
blackP = true;
}
}
}
if(!blackP){
out.write("-1\n"); return;
}
boolean vis[][] = new boolean[n][m];
for(int i=0;i<n;i++) Arrays.fill(vis[i], false);
LinkedList<int[]> queue = new LinkedList<>();
queue.add(new int[]{r, c});
vis[r][c] = true;
int ans = 0;
boolean found = false;
while(queue.size()>0){
int size = queue.size();
for(int i=0;i<size;i++){
int u[] = queue.remove();
int x = u[0];
int y = u[1];
if(debug) System.out.println(x+ " " + y);
if(black[x][y]){
found = true;
break;
}
// go to each same row cells
for(int j=0;j<m;j++){
if(!vis[r][j]){
queue.add(new int[]{r, j});
vis[r][j] = true;
}
}
// go to each same col cells
for(int j=0;j<n;j++){
if(!vis[j][c]){
queue.add(new int[]{j, c});
vis[j][c] = true;
}
}
}
if(found) break;
ans++;
}
out.write(ans+"\n");
}
private boolean isPoss(int x, int y, int n, int m){
if(x<0 || y<0 || x>=n || y>=m) return false;
return true;
}
}
/*
------
[2,6,4,6]
[3,7,6,1]
3 6 6 6
2 7 4 1
-------------------------
3
2
3 6
2 7
3 7
2 6
-------------------------
2
3
-------------------------
*/
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
d8706e0ee7cf43d8c838f3ea185d45ad
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.System.currentTimeMillis;
/*
* @author: Hivilsm
* @createTime: 2022-04-27, 23:29:16
* @description: Platform
*/
public class Accepted {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static Random rand = new Random();
static int[][] DIRS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public static void main(String[] args) {
// long start = currentTimeMillis();
// int t = 3;
int t = i();
out.println();
out.println();
out.println();
while (t-- > 0){
out.println(sol());
// int[] ans = sol();
// for (int i : ans){
// out.print(i + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
// long end = currentTimeMillis();
// out.println(end - start);
out.flush();
out.close();
}
static int sol() {
int n = i(), m = i();
int row = i() - 1, col = i() - 1;
char[][] g = new char[n][m];
boolean isBlack = false;
for (int i = 0; i < n; i++){
String s = s();
for (int j = 0; j < m; j++){
g[i][j] = s.charAt(j);
if (g[i][j] == 'B'){
isBlack = true;
}
}
}
if (!isBlack){
return -1;
}
if (g[row][col] == 'B'){
return 0;
}
int sum = 0;
for (int i = 0; i < n; i++){
if (g[i][col] == 'B'){
sum++;
}
}
for (int i = 0; i < m; i++){
if (g[row][i] == 'B'){
sum++;
}
}
return sum == 0 ? 2 : 1;
}
static boolean check(int x, int y, int n, int m){
return x >= 0 && y >= 0 && x < n && y < m;
}
static void swap(int[] nums, int l, int r){
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
static void ranArr() {
int n = 3, len = 10, val = 10;
System.out.println(n);
for (int i = 0; i < n; i++) {
int cnt = rand.nextInt(len) + 1;
System.out.println(cnt);
for (int j = 0; j < cnt; j++) {
System.out.print(rand.nextInt(val) + " ");
}
System.out.println();
}
}
static double fastPower(double x, int n) {
if (x == 0) return 0;
long b = n;
double res = 1.0;
if (b < 0) {
x = 1 / x;
b = -b;
}
while (b > 0) {
if ((b & 1) == 1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static int[] inputI(int n) {
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
return nums;
}
static long[] inputLong(int n) {
long nums[] = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextLong();
}
return nums;
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
a6e0ca03cfe8525baaa78ac5effb6809
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class Problem_1627A {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
int n, m, r, c;
for (int i = 0; i < t; i++) {
n = scanner.nextInt();
m = scanner.nextInt();
r = scanner.nextInt() - 1;
c = scanner.nextInt() - 1;
char[][] grid = new char[n][];
for (int j = 0; j < n; j++) {
grid[j] = scanner.next().toCharArray();
}
boolean allWhite = true;
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
if (grid[j][k] == 'B') {
allWhite = false;
break;
}
}
}
boolean exist = false;
for (int j = 0; j < n; j++) {
if (j != r && grid[j][c] == 'B') {
exist = true;
break;
}
}
for (int j = 0; j < m; j++) {
if (j != c && grid[r][j] == 'B') {
exist = true;
break;
}
}
if (allWhite) {
System.out.println(-1);
} else if (grid[r][c] == 'B') {
System.out.println(0);
} else if (exist) {
System.out.println(1);
} else {
System.out.println(2);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
16be6ba0b8cbeeafa0a757eb3d4ce799
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
/**
* <a href = "https://codeforces.com/problemset/problem/1627/A"> Link </a>.
* @author Bris
* @version 1.0
* @since 6:46:11 PM - Apr 20, 2022
*/
public class A1627 {
/**
* The main method.
* @param args Unused.
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-->0) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int r = scanner.nextInt();
int c = scanner.nextInt();
char[][] grid = new char[n][m];
for (int i = 0; i < n; i++) {
grid[i] = scanner.next().toCharArray();
}
int ans = -1;
if (grid[r-1][c-1] == 'B') {
ans = 0;
System.out.println(ans);
continue;
}
for (int i = 0; i < m; i++) {
if (grid[r-1][i] == 'B') {
ans = 1;
}
}
for (int i = 0; i < n; i++) {
if (grid[i][c-1] == 'B') {
ans = 1;
}
}
if (ans == 1) {
System.out.println(ans);
continue;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'B') {
ans = 2;
}
}
}
System.out.println(ans);
}
scanner.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
2556e8bd759db48ddc4bfa3e373eeb98
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class A1627 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int rows = in.nextInt();
int columns = in.nextInt();
int R = in.nextInt()-1;
int C = in.nextInt()-1;
char[][] G = new char[rows][];
for (int r=0; r<rows; r++) {
G[r] = in.next().toCharArray();
}
int best = -1;
for (int r=0; r<rows; r++) {
for (int c=0; c<columns; c++) {
if (G[r][c] == 'B') {
int current = 0;
if (r == R) current++;
if (c == C) current++;
best = Math.max(best, current);
}
}
}
if (best != -1) {
best = 2 - best;
}
System.out.println(best);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
4bfe1df3fec0aacd2dfdd5a28208c66e
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Rough {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = s.nextInt();
for(int t=1;t<=tc;t++) {
int r=s.nextInt();
int c=s.nextInt();
int x=s.nextInt();
int y=s.nextInt();
char[][] ar=new char[r][c];
s.nextLine();
for(int j=0;j<r;j++) {
ar[j]=s.next().toCharArray();
}
boolean doer=false;
for(int i=0;i<r;i++) {
for(int k=0;k<c;k++) {
if(ar[i][k]=='B') {
doer=true;
break;
}
}
}
boolean doer1=false;
for(int i=0;i<c;i++) {
if(ar[x-1][i]=='B') {
doer1=true;
break;
}
}
boolean doer2=false;
for(int i=0;i<r;i++) {
if(ar[i][y-1]=='B') {
doer2=true;
break;
}
}
if(doer) {
if(ar[x-1][y-1]=='B')pw.println(0);
else if(doer1||doer2)pw.println(1);
else pw.println(2);
}
else pw.println(-1);
}
pw.close();
}
static final Random ran=new Random();
static void sort(int[] ar) {
int n=ar.length;
for(int i=0;i<n;i++) {
int doer=ran.nextInt(n),temp=ar[doer];
ar[doer]=ar[i];ar[i]=temp;
}
Arrays.sort(ar);
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
dcdb35fcc3251e4bd24ef6f64021f827
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Array;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import org.w3c.dom.Node;
public class codechef3 {
static class comp implements Comparator<String>
{
@Override
public int compare(String o1, String o2) {
if(o1.length()>o2.length())
return 1;
else if(o1.length()<o2.length())
return -1;
else return o1.compareTo(o2);
}
}
static class Pair<Integer,Intetger>
{
int k=0;
int v=0;
public Pair(int a,int b)
{
k=a;
v=b;
}
public int getKey()
{
return k;
}
}
static class FastReader
{BufferedReader br;
StringTokenizer st;
public FastReader()
{ br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
//-------------------------------------------------------------------------------------------
public static void main(String[] args) {
FastReader s=new FastReader();
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
int m=s.nextInt();
int r=s.nextInt();
int c=s.nextInt();
String a[]=new String[n];
for(int i=0;i<n;i++)
a[i]=s.next();
// System.out.println(a[r-1].charAt(c-1));
if(a[r-1].charAt(c-1)=='B')
System.out.println("0");
else
{
int flag=0;
int flag1=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i].charAt(j)=='B')
{
if(i==r-1||j==c-1)
flag1=1;
flag=1;
}
}
if(flag1==1)
break;
}
if(flag==0)
System.out.println("-1");
else if(flag1==1)
System.out.println(flag1);
else
System.out.println("2");
}
}
}
//1 1 1 1 1 1 1 1 1 1 1 1
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
98a9a6a0b0fe5742f0908204d605685c
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class fun{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
int a=sc.nextInt();
int b=sc.nextInt();
char ar[][]=new char[n][m];int c=0,k=0;
for(int i=0;i<n;i++)
{
String s=sc.next();
for(int j=0;j<m;j++)
{
ar[i][j]=s.charAt(j);
if(ar[i][j]=='B')
{
c++;
}if(ar[i][j]=='B'&&(i==a-1||j==b-1))
{
k++;
}
}
}
if(c==0)
{
System.out.println("-1");
}else if(ar[a-1][b-1]=='B')
{
System.out.println("0");
}else if(k==0)
{
System.out.println("2");
}else
{
System.out.println("1");
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
daa570c66505e22739e48cb423f732b6
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/*----->Hope Can Set You Free<-----*/
import java.util.*;
public class NotShading {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt(),m=sc.nextInt(),r=sc.nextInt(),c=sc.nextInt(),yo=0,jaigamoto=0,ashepase=0;
for(int i=0;i<n;i++) {
StringBuilder sb=new StringBuilder(sc.next());
for(int k=0;k<sb.length();k++){
if(sb.charAt(k)=='B') {
yo=1;
}
if(i==r-1) {
if(sb.charAt(k)=='B') {
ashepase=1;
}
}else {
if(k==c-1) {
if(sb.charAt(k)=='B') {
ashepase=1;
}
}
}
if(i==r-1 && k==c-1) {
if(sb.charAt(k)=='B') {
jaigamoto=1;
}
}
}
}
if(jaigamoto==1) {
System.out.println("0");
}else if(ashepase==1) {
System.out.println("1");
}else if(yo==1) {
System.out.println("2");
}else {
System.out.println("-1");
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
10e133c6f7c41e7c636665f1b81d7030
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class A {
static PrintWriter pw = new PrintWriter(System.out);
static FastReader fr = new FastReader();
public static void main(String[] args) {
int t = fr.nextInt();
while (t-- > 0) {
solve();
}
pw.flush();
}
static void solve() {
int n = fr.nextInt();
int m = fr.nextInt();
int r = fr.nextInt();
int c = fr.nextInt();
r--;
c--;
int ans = 1337;
char[][] grid = new char[n][m];
for (int i = 0; i < n; i++) {
grid[i] = fr.nextLine().toCharArray();
}
if (grid[r][c] == 'B')
ans = 0;
else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((i == r && grid[i][j] == 'B') || (j == c && grid[i][c] == 'B')) {
ans = min(ans, 1);
break;
}
else if (grid[i][j] == 'B') {
ans = min(ans, 2);
}
}
}
}
pw.println( ans == 1337 ? -1 : ans);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
1e491c2fc1668ca38f9456b52b0a98b7
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class SolveProblem {
public static void main(String[] args) {
notShading();
}
private static void notShading(){
Scanner scanner = new Scanner(System.in);
int caseNum = scanner.nextInt();
while(caseNum>0){
int n,m,r,c;
n = scanner.nextInt();
m = scanner.nextInt();
r = scanner.nextInt() - 1;
c = scanner.nextInt() - 1;
scanner.nextLine();
char[][] grid = new char[n][m];
boolean findBlack = false;
boolean blackInRC = false;
for(int i=0;i<n;i++){
String s = scanner.nextLine();
for(int j=0;j<m;j++){
grid[i][j] = s.charAt(j);
if(grid[i][j] == 'B'){
findBlack = true;
if(i == r || j == c){
blackInRC = true;
}
}
}
}
// 要把r,c变黑
// 如果本身就是黑,0
// 如果全部都是白,-1
// 如果r,c上有黑,1
// 其他,2
if(grid[r][c] == 'B'){
System.out.println(0);
}else if(!findBlack){
System.out.println(-1);
}else if(blackInRC){
System.out.println(1);
}else{
System.out.println(2);
}
caseNum --;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
4b21c3dd283bd5d2b23803c8f703ed87
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt(), m=sc.nextInt();
int x=sc.nextInt(), y=sc.nextInt();
char arr[][]=new char[n][m];
boolean isBlack=false;
for(int i=0;i<n;i++)
{
String st = sc.next();
arr[i] = st.toCharArray();
for(int j=0;j<m;j++)
{
// arr[i][j]=sc.next().charAt(0);
if(arr[i][j]=='B')
{
isBlack=true;
}
}
}
if(isBlack==false)
{
System.out.println(-1);
continue;
}
x--;
y--;
if(arr[x][y]=='B')
{
System.out.println(0);
// System.out.println(x+" "+y);
continue;
}
boolean isOne=false;
for(int i=0;i<m;i++)
{
if(arr[x][i]=='B')
{
isOne=true;
break;
}
}
if(isOne)
{
System.out.println(1);
continue;
}
for(int i=0;i<n;i++)
{
if(arr[i][y]=='B')
{
isOne=true;
break;
}
}
if(isOne)
{
System.out.println(1);
}
else{
System.out.println(2);
}
}
} catch(Exception e) {
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
8413922b3c9d0d9413917df3ccb1ebe9
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
//CP- MASS_2701
import java.util.*;
import java.io.*;
public class A_Not_Shading {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
public static void main(String args[]) throws IOException {
int t=in.nextInt();
int cse=1;
loop:
while(t-->0)
{
StringBuilder res=new StringBuilder();
//res.append("Hello"+"\n");
int m=in.nextInt();
int n=in.nextInt();
int r=in.nextInt();
int c=in.nextInt();
String[] str=new String[m];
int b=0;
int k;
for(int i=0;i<m;i++)
{
str[i]=in.next();
}
if(str[r-1].charAt(c-1)=='B')
{
println(0);
}
else{
int f=0;
for(int i=0;i<n;i++)
{
if(str[r-1].charAt(i)=='B')
{
println(1);
f=1;
break;
}
}
if(f==0)
{
for(int i=0;i<m;i++)
{
if(str[i].charAt(c-1)=='B')
{
println(1);
f=1;
break;
}
}
if(f==0)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(str[i].charAt(j)=='B')
{
println(2);
f=1;
break;
}
}
if(f==1)
{break;}
}
if(f==0)
{
println(-1);
}
}
}
}
}
}
static int max(int a, int b)
{
if(a<b)
return b;
return a;
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static < E > void print(E res)
{
System.out.print(res);
}
static < E > void println(E res)
{
System.out.println(res);
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int abs(int a)
{
if(a<0)
return -1*a;
return a;
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int [] readintarray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] readlongarray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
2e112e2b0f20e1262da5aa54228be92a
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
/**
*
* @author Carlos
*/
public class NotShading {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for(int i=0;i<t;i++){
int n = scan.nextInt();
int m = scan.nextInt();
int r = scan.nextInt();
int c = scan.nextInt();
String[] grid = new String[n];
for(int j=0;j<n;j++){
grid[j] = scan.next();
}
System.out.println(solucion(grid, n, m, r, c));
}
}
public static int solucion(String[] grid, int n, int m, int r, int c){
if(grid[r-1].charAt(c-1) == 'B') return 0;
for(int i=0;i<n;i++){
if(grid[i].charAt(c-1) == 'B') return 1;
}
for(int i=0;i<m;i++){
if(grid[r-1].charAt(i) == 'B') return 1;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i].charAt(j) == 'B') return 2;
}
}
return -1;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
c0d522f5015692e554398e3768111c4b
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class BinaryDecimal {
public static void main(String[] args) {
Scanner fs = new Scanner(System.in);
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
int m = fs.nextInt();
int r = fs.nextInt();
int c = fs.nextInt();
int a[][] = new int[n][m];
int black = 0;
for (int i = 0; i < n; i++) {
String s = fs.next();
for (int j = 0; j < m; j++) {
if (s.charAt(j) == 'B') {
black++;
a[i][j] = 1;
}
}
}
r--;
c--;
if (a[r][c] == 1)
System.out.println(0);
else if (black == 0)
System.out.println(-1);
else {
boolean flag = false;
for (int i = 0; i < m; i++)
if (a[r][i] == 1) {
flag = true;
break;
}
if (!flag) {
for (int i = 0; i < n; i++)
if (a[i][c] == 1) {
flag = true;
break;
}
}
if (flag)
System.out.println(1);
else
System.out.println(2);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
5fec6df4b3eebd25d8d9b74ac412f317
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.Scanner;
public class Solution extends CodeForces {
public static void main(String[] args) {
int t= I();
while (t-->0){
int n = I();
int m = I();
int r = I();
int c = I();
boolean flag = false;
char[][] bw= new char[n][m];
for (int i = 0; i < n; i++) {
String s = S();
for (int j = 0; j < m; j++) {
bw[i][j] = s.charAt(j);
if (bw[i][j] == 'B'){
flag = true;
}
}
}
System.out.println(solution(n,m,r,c,bw,flag));
}
}
private static int solution(int n, int m, int r, int c,char[][] bw,boolean flag) {
if (flag==false){
return -1;
}
if (bw[r-1][c-1]=='B') return 0;
for (int i = 0; i < n; i++) {
if (bw[i][c-1]=='B'){
return 1;
}
}
for (int i = 0; i < m; i++) {
if (bw[r-1][i]=='B'){
return 1;
}
}
return 2;
}
}
class CodeForces {
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=1000000007;
static long mod1=998244353;
static int MAX=Integer.MAX_VALUE;
static int MIN=Integer.MIN_VALUE;
static long MAXL=Long.MAX_VALUE;
static long MINL=Long.MIN_VALUE;
public static class pair
{
int a;
int b;
public pair(int val,int index)
{
a=val;
b=index;
}
}
public static class myComp implements Comparator<pair>
{
//sort in ascending order.
public int compare(CodeForces.pair p1, pair p2)
{
if(p1.a==p2.a)
return 0;
else if(p1.a<p2.a)
return -1;
else
return 1;
}
//sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return 1;
// else
// return -1;
// }
}
public static long fact(long n)
{
long fact=1;
for(long i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long fact(int n)
{
long fact=1;
for(int i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long kadane(long a[],int n)
{
long max_sum=Long.MIN_VALUE,max_end=0;
for(int i=0;i<n;i++){
max_end+=a[i];
if(max_sum<max_end){max_sum=max_end;}
if(max_end<0){max_end=0;}
}
return max_sum;
}
public static void DFS(ArrayList<Integer> arr[],int s,boolean visited[])
{
visited[s]=true;
for(int i:arr[s]){
if(!visited[i]){
DFS(arr,i,visited);
}
}
}
public static int BS(int a[],int x,int ii,int jj)
{
// int n=a.length;
int mid=0;
int i=ii,j=jj,in=0;
while(i<=j)
{
mid=(i+j)/2;
if(a[mid]<x){
in=mid+1;
i=mid+1;
}
else
j=mid-1;
}
return in;
}
public static int lower_bound(int arr[], int N, int X)
{
int mid;
int low = 0;
int high = N;
while(low<high) {
mid=low+(high-low)/2;
if(X<=arr[mid]){
high=mid;
}
else{
low=mid+1;
}
}
if(low<N && arr[low]<X){
low++;
}
out.println(arr[low]);
return low;
}
public static ArrayList<Integer> primeSieve(int n)
{
ArrayList<Integer> arr=new ArrayList<>();
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
public static class FenwickTree
{
long farr[];
int n;
public FenwickTree(int c)
{
n=c+1;
farr=new long[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x)
{
for(;x<=n;x+=x&(-x))
{
farr[x]++;
}
}
public long get(int x)
{
long ans=0;
for(;x>0;x-=x&(-x))
{
ans=ans+farr[x];
}
return ans;
}
}
//Disjoint Set Union
public static class DSU
{
static int par[],rank[];
public DSU(int c)
{
par=new int[c+1];
rank=new int[c+1];
for(int i=0;i<=c;i++)
{
par[i]=i;
rank[i]=0;
}
}
public static int find(int a)
{
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public static void union(int a,int b)
{
int a_rep=find(a),b_rep=find(b);
if(a_rep==b_rep)
return;
if(rank[a_rep]<rank[b_rep])
par[a_rep]=b_rep;
else if(rank[a_rep]>rank[b_rep])
par[b_rep]=a_rep;
else
{
par[b_rep]=a_rep;
rank[a_rep]++;
}
}
}
//SEGMENT TREE CODE
// public static void segmentUpdate(int si,int ss,int se,int qs,int qe,long x)
// {
// if(ss>qe || se<qs)return;
// if(qs<=ss && qe>=se)
// {
// seg[si][0]+=1L;
// seg[si][1]+=x*x;
// seg[si][2]+=2*x;
// return;
// }
// int mid=(ss+se)/2;
// segmentUpdate(2*si+1,ss,mid,qs,qe,x);
// segmentUpdate(2*si+2,mid+1,se,qs,qe,x);
// }
// public static long segmentGet(int si,int ss,int se,int x,long f,long s,long t,long a[])
// {
// if(ss==se && ss==x)
// {
// f+=seg[si][0];
// s+=seg[si][1];
// t+=seg[si][2];
// long ans=a[x]+(f*((long)x+1L)*((long)x+1L))+s+(t*((long)x+1L));
// return ans;
// }
// int mid=(ss+se)/2;
// if(x>mid){
// return segmentGet(2*si+2,mid+1,se,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a);
// }else{
// return segmentGet(2*si+1,ss,mid,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a);
// }
// }
public static class myComp1 implements Comparator<CodeForces.pair1>
{
//sort in ascending order.
public int compare(pair1 p1,pair1 p2)
{
if(p1.a==p2.a)
return 0;
else if(p1.a<p2.a)
return -1;
else
return 1;
}
//sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return 1;
// else
// return -1;
// }
}
public static class pair1
{
long a;
long b;
public pair1(long val,long index)
{
a=val;
b=index;
}
}
public static ArrayList<CodeForces.pair1> mergeIntervals(ArrayList<CodeForces.pair1> arr)
{
//****************use this in main function-Collections.sort(arr,new myComp1());
ArrayList<CodeForces.pair1> a1=new ArrayList<>();
if(arr.size()<=1)
return arr;
a1.add(arr.get(0));
int i=1,j=0;
while(i<arr.size())
{
if(a1.get(j).b<arr.get(i).a)
{
a1.add(arr.get(i));
i++;
j++;
}
else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b)
{
i++;
}
else if(a1.get(j).b>=arr.get(i).a)
{
long a=a1.get(j).a;
long b=arr.get(i).b;
a1.remove(j);
a1.add(new CodeForces.pair1(a,b));
i++;
}
}
return a1;
}
public static boolean palindrome(String s,int n)
{
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static void printArray(long a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(char a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(boolean a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(char a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(ArrayList<Long> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapInt(HashMap<Integer,Integer> hm){
for(Map.Entry<Integer,Integer> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMapLong(HashMap<Long,Long> hm){
for(Map.Entry<Long,Long> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static long pwr(long m,long n)
{
long res=1;
m=m%mod;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m)%mod;
}
n=n>>1;
m=(m*m)%mod;
}
return res;
}
public static void sort(int[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(long[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static int I(){return sc.I();}
public static long L(){return sc.L();}
public static String S(){return sc.S();}
public static double D(){return sc.D();}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int I(){
return Integer.parseInt(next());
}
long L(){
return Long.parseLong(next());
}
double D(){
return Double.parseDouble(next());
}
String S(){
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
9d377f3840af14ac9fee256b44a5a07b
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.HashMap;
import java.util.Scanner;
public class NotShading {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt(), m = scanner.nextInt(), r = scanner.nextInt(), c = scanner.nextInt();
HashMap<Integer, HashMap<Integer, Integer>> map = new HashMap<>();
String outPut = "";
boolean isContainsBlack = false;
for (int j = 0; j < n; j++) {
String str = scanner.next();
map.put(j + 1, getMap(str));
}
if (map.get(r).containsKey(c)) {
System.out.println("0");
continue;
}
if (map.get(r).size() >= 1) {
System.out.println("1");
continue;
}
for (Integer name : map.keySet()) {
if(map.get(name).size() >= 1){
isContainsBlack = true;
}
if (map.get(name).containsKey(c)) {
outPut = "1";
break;
}
}
if (!outPut.equals("")) {
System.out.println(outPut);
continue;
}
System.out.println(isContainsBlack ? "2" : "-1");
}
}
static HashMap<Integer, Integer> getMap(String str) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int k = 0; k < str.length(); k++) {
if (str.charAt(k) == 'B') map.put(k + 1, 1);
}
return map;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
7f195293f989c77115d33774333a44c7
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/*
Challenge 1: Newbie to CM in 1year (Dec 2021 - Nov 2022) 🔥 5* Codechef
Challenge 2: CM to IM in 1 year (Dec 2022 - Nov 2023) 🔥🔥 6* Codechef
Challenge 3: IM to GM in 1 year (Dec 2023 - Nov 2024) 🔥🔥🔥 7* Codechef
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Coder {
static StringBuffer str=new StringBuffer();
static int n, m, r, c;
static char mat[][];
static int solve(){
if(mat[r][c]=='B') return 0;
int ans=3;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]=='B'){
if(i==r || j==c) ans=1;
else ans=Math.min(ans, 2);
}
}
}
return ans==3?-1:ans;
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
int t = Integer.parseInt(bf.readLine().trim());
while (t-- > 0) {
String st[]=bf.readLine().trim().split("\\s+");
n=Integer.parseInt(st[0]);
m=Integer.parseInt(st[1]);
r=Integer.parseInt(st[2]);
c=Integer.parseInt(st[3]);
r--;
c--;
mat=new char[n][m];
for(int i=0;i<n;i++){
mat[i]=bf.readLine().trim().toCharArray();
}
str.append(solve()).append("\n");
}
pw.println(str);
pw.flush();
// System.outin.print(str);
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
a73f428e24bd896309ee51462621f5fe
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static class Pair implements Comparable < Pair > {
int d;
int i;
Pair(int d, int i) {
this.d = d;
this.i = i;
}
public int compareTo(Pair o) {
return this.d - o.d;
}
}
public static class SegmentTree {
long[] st;
long[] lazy;
int n;
SegmentTree(long[] arr, int n) {
this.n = n;
st = new long[4 * n];
lazy = new long[4 * n];
construct(arr, 0, n - 1, 0);
}
public long construct(long[] arr, int si, int ei, int node) {
if (si == ei) {
st[node] = arr[si];
return arr[si];
}
int mid = (si + ei) / 2;
long left = construct(arr, si, mid, 2 * node + 1);
long right = construct(arr, mid + 1, ei, 2 * node + 2);
st[node] = left + right;
return st[node];
}
public long get(int l, int r) {
return get(0, n - 1, l, r, 0);
}
public long get(int si, int ei, int l, int r, int node) {
if (r < si || l > ei)
return 0;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei)
return st[node];
int mid = (si + ei) / 2;
return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2);
}
public void update(int index, int value) {
update(0, n - 1, index, 0, value);
}
public void update(int si, int ei, int index, int node, int val) {
if (si == ei) {
st[node] = val;
return;
}
int mid = (si + ei) / 2;
if (index <= mid) {
update(si, mid, index, 2 * node + 1, val);
} else {
update(mid + 1, ei, index, 2 * node + 2, val);
}
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
public void rangeUpdate(int l, int r, int val) {
rangeUpdate(0, n - 1, l, r, 0, val);
}
public void rangeUpdate(int si, int ei, int l, int r, int node, int val) {
if (r < si || l > ei)
return;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei) {
st[node] += val * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (si + ei) / 2;
rangeUpdate(si, mid, l, r, 2 * node + 1, val);
rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val);
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setIn(new FileInputStream(new File("input.txt")));
System.setOut(new PrintStream(new File("output.txt")));
} catch (Exception e) {
}
}
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int r = sc.nextInt();
int c = sc.nextInt();
int tr = sc.nextInt() - 1;
int tc = sc.nextInt() - 1;
int[] row = new int[r];
int[] col = new int[c];
String[] arr = new String[r];
boolean flag = false;
for (int i = 0; i < r; i++) {
String s = sc.next();
int b = 0;
arr[i] = s;
for (int j = 0; j < c; j++) {
if (s.charAt(j) == 'B') {
b += 1;
col[j] += 1;
flag = true;
}
}
if (b > 0)
flag = true;
row[i] = b;
}
if (arr[tr].charAt(tc) == 'B') {
sb.append("0");
} else if (row[tr] > 0 || col[tc] > 0) {
sb.append("1");
} else {
if (flag)
sb.append("2");
else
sb.append("-1");
}
sb.append("\n");
}
System.out.println(sb);
}
public static String Util(String s) {
for (int i = s.length() - 1; i >= 1; i--) {
int l = s.charAt(i - 1) - '0';
int r = s.charAt(i) - '0';
if (l + r >= 10) {
return s.substring(0, i - 1) + (l + r) + s.substring(i + 1);
}
}
int l = s.charAt(0) - '0';
int r = s.charAt(1) - '0';
return (l + r) + s.substring(2);
}
public static boolean isPos(int idx, long[] arr, long[] diff) {
if (idx == 0) {
for (int i = 0; i <= Math.min(arr[0], arr[1]); i++) {
diff[idx] = i;
arr[0] -= i;
arr[1] -= i;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[0] += i;
arr[1] += i;
}
} else if (idx == 1) {
if (arr[2] - arr[1] >= 0) {
long k = arr[1];
diff[idx] = k;
arr[1] = 0;
arr[2] -= k;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[1] = k;
arr[2] += k;
} else
return false;
} else {
if (arr[2] == arr[0] && arr[1] == 0) {
diff[2] = arr[2];
return true;
} else {
return false;
}
}
return false;
}
public static boolean isPal(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
static int upperBound(ArrayList<Long> arr, long key) {
int mid, N = arr.size();
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr.get(mid)) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
return low;
}
static int lowerBound(ArrayList<Long> array, long key) {
// Initialize starting index and
// ending index
int low = 0, high = array.size();
int mid;
// Till high does not crosses low
while (low < high) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is less than or equal
// to array[mid], then find in
// left subarray
if (key <= array.get(mid)) {
high = mid;
}
// If key is greater than array[mid],
// then find in right subarray
else {
low = mid + 1;
}
}
// If key is greater than last element which is
// array[n-1] then lower bound
// does not exists in the array
if (low < array.size() && array.get(low) < key) {
low++;
}
// Returning the lower_bound index
return low;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
69cf445a59ecc783d00cee4b8cdffa5e
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main{
static void testCase(FastScanner sc) {
int n = sc.nextInt(), m = sc.nextInt(), r = sc.nextInt() - 1, c = sc.nextInt() - 1;
boolean black = false, one = false;
char grid[][] = new char[n][m];
for(int i=0; i<n; i++) {
grid[i] = sc.next().toCharArray();
for(int j=0; j<m; j++) {
if(grid[i][j] == 'B' && !black) black = true;
if(i==r){
if(grid[i][j]=='B' && !one) one = true;
}
if(j==c){
if(grid[i][j]=='B' && !one) one = true;
}
}
}
System.out.println(!black ? -1 : grid[r][c] == 'B' ? 0 : one ? 1 : 2);
}
public static void main(String[] args){
FastScanner sc = new FastScanner();
int t = sc.nextInt();
for(int tt=0; tt<t; tt++) {
testCase(sc);
}
}
static class FastScanner{
InputStreamReader is;
BufferedReader br;
StringTokenizer st;
public FastScanner(){
try {
br = new BufferedReader(new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
}
catch (Exception e) {
is = new InputStreamReader(System.in);
br = new BufferedReader(is);
}
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
int[] readIntArray(int n){
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=nextInt();
}
return arr;
}
void printIntArray(int[] arr) {
int n = arr.length;
for(int i=0; i<n; i++) System.out.print(arr[i] + " ");
System.out.println();
}
int[][] readIntArrayTwo(int n,int m){
int arr[][]=new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
arr[i][j]=nextInt();
}
}
return arr;
}
int[][] readIntArrayTwo(int n){
int arr[][]=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j]=nextInt();
}
}
return arr;
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
void close() throws Exception{
is.close();
br.close();
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
f85646403b6ebe46f4ee838f7ece02c5
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class index
{
public static void main (String[] args)
{
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int T = sc.nextInt();
for(int t = 0; t<T; t++){
int N = sc.nextInt(), M = sc.nextInt(), R = sc.nextInt(), C = sc.nextInt();
char arr[][] = new char[N][M];
boolean b = false;
for(int i = 0; i<N; i++){
String s = sc.next();
if(s.indexOf('B') != -1)b = true;
arr[i] = s.toCharArray();
}
if(arr[R - 1][C - 1] == 'B')out.println(0);
else{
boolean flag = false;
for(int i = 0; i<N; i++){
if(arr[i][C - 1] == 'B')flag = true;
}
for(int i = 0; i<M; i++){
if(arr[R - 1][i] == 'B')flag = true;
}
if(flag)out.println("1");
else if(!b)out.println(-1);
else out.println(2);
}
}
out.flush();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
//NOTES
// Arrays.sort(arr,(a,b)->a[0]-b[0]);
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
4ba5b3719bd698b4261fc2a3ba47c314
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class NotShading
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int round = in.nextInt();
for (int z = 0; z < round; z++)
{
int n = in.nextInt(), m = in.nextInt();
int r = in.nextInt(), c = in.nextInt();
int ans = 99;
for (int i = 1; i <= n; i++)
{
char[] str = ("-" + in.next()).toCharArray();
for (int j = 1; j <= m; j++)
{
if (ans > 2 && str[j] == 'B')
ans = 2;
if (ans > 1 && (i == r || j == c) && str[j] == 'B')
ans = 1;
if (ans > 0 && (i == r && j == c) && str[j] == 'B')
{
ans = 0;
break;
}
}
}
System.out.println(ans != 99 ? ans : -1);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
0994d404244291dd4b7273563afe0deb
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.lang.*;
public class Solution {
public static void main(String args[]) throws IOException {
InputStream is = null;
BufferedReader br;
PrintStream out;
if (System.getProperty("ONLINE_JUDGE") == null) {
is = new FileInputStream(new File("input.txt"));
out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
} else {
is = System.in;
}
br = new BufferedReader(new InputStreamReader(is));
int testcases = Integer.parseInt(br.readLine().trim());
while (testcases-- > 0) {
String line[] = br.readLine().split(" ");
int row = Integer.parseInt(line[0]);
int col = Integer.parseInt(line[1]);
int r = Integer.parseInt(line[2]) - 1;
int c = Integer.parseInt(line[3]) - 1;
char matrix[][] = new char[row][col];
boolean haveBlack = false;
boolean ok = false;
for (int i = 0; i < row; i++) {
String str = br.readLine();
for (int j = 0; j < col; j++) {
matrix[i][j] = str.charAt(j);
if (matrix[i][j] == 'B') {
haveBlack = true;
if (i == r || j == c) {
ok = true;
}
}
}
}
if (!haveBlack) {
System.out.println(-1);
} else if (matrix[r][c] == 'B') {
System.out.println("0");
} else if (ok) {
System.out.println("1");
} else {
System.out.println(2);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
bc005c8fff7f0fa457143dd77a6cad07
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class A
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int round = in.nextInt();
for (int z = 0; z < round; z++)
{
int n = in.nextInt(), m = in.nextInt();
int r = in.nextInt(), c = in.nextInt();
int ans = 99;
for (int i = 1; i <= n; i++)
{
char[] str = ("-" + in.next()).toCharArray();
for (int j = 1; j <= m; j++)
{
if (ans > 2 && str[j] == 'B')
ans = 2;
if (ans > 1 && (i == r || j == c) && str[j] == 'B')
ans = 1;
if (ans > 0 && (i == r && j == c) && str[j] == 'B')
{
ans = 0;
break;
}
}
}
System.out.println(ans != 99 ? ans : -1);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
8a757abf899b1e5323cff0d6625df6e0
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class NotShading {
public static int getAns(String[] strArr,int row, int col, int r, int c) {
if(strArr[r-1].charAt(c-1) == 'B') {
return 0;
}
else {
for(int i1 = 0; i1 < col; i1++) {
if(strArr[r-1].charAt(i1) == 'B') {
return 1;
}
}
for(int i1 = 0; i1 < row; i1++) {
if(strArr[i1].charAt(c - 1) == 'B') {
return 1;
}
}
boolean bIsPresent = false;
for(int i1 = 0; i1 < row ; i1++) {
for(int i2 = 0; i2 < col; i2++) {
if(strArr[i1].charAt(i2) == 'B') {
bIsPresent = true;
break;
}
}
}
if(!bIsPresent) {
return -1;
} else {
return 2;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for(int i = 0; i < testCases; i++) {
int row = sc.nextInt();
int col = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
String[] strArr = new String[row];
for(int j = 0; j < row; j++) {
strArr[j] = sc.next();
}
int ans = getAns(strArr,row,col,r,c);
System.out.println(ans);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
11ae929d8731870736e709d118264bf5
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class X {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t=s.nextInt();
while(t>0)
{
int n=s.nextInt();
int m=s.nextInt();
int r=s.nextInt();
int c=s.nextInt();
char a[][]=new char[n][m];
int b=0;
for(int i=0;i<n;i++){
String sq=s.next();
for(int j=0;j<m;j++){
a[i][j]= sq.charAt(j);
if(a[i][j]=='B')
b++;
}
}
int pr=0, pc=0;
if(a[r-1][c-1]=='B')
System.out.println(0);
else
if(b!=0)
{
for(int i=0;i<m;i++)
{
if(a[r-1][i]=='B')
{
pr=1;
break;
}
}
for(int i=0;i<n;i++)
{
if(a[i][c-1]=='B')
{
pc=1;
break;
}
}
if(pr==1 || pc==1)
System.out.println(1);
else
System.out.println(2);
}
else
System.out.println(-1);
t--;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
530c11b49aef50285a78fbc8129cc21d
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static String ab,b;
static class Node
{
int val;
Node left;
Node right;
public Node(int x) {
// TODO Auto-generated constructor stub
this.val=x;
this.left=null;
this.right=null;
}
}
static class Pair<U, V> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) x).compareTo(o.x);
if (value != 0) return value;
return ((Comparable<V>) y).compareTo(o.y);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return x.equals(pair.x) && y.equals(pair.y);
}
public int hashCode() {
return Objects.hash(x, y);
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int[] nextArray(int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=nextInt();
return arr;
}
}
static String string;
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
return gcd(b, a % b);
}
static long gcd(long a, long b)
{
// Everything divides 0
for(long i=2;i<=b;i++)
{
if(a%i==0&&b%i==0)
return i;
}
return 1;
}
static int fac(int n)
{
int c=1;
for(int i=2;i<n;i++)
if(n%i==0)
c=i;
return c;
}
static int lcm(int a,int b)
{
for(int i=Math.min(a, b);i<=a*b;i++)
if(i%a==0&&i%b==0)
return i;
return 0;
}
static int maxHeight(char[][] ch,int i,int j,String[] arr)
{
int h=1;
if(i==ch.length-1||j==0||j==ch[0].length-1)
return 1;
while(i+h<ch.length&&j-h>=0&&j+h<ch[0].length&&ch[i+h][j-h]=='*'&&ch[i+h][j+h]=='*')
{
String whole=arr[i+h];
//System.out.println(whole.substring(j-h,j+h+1));
if(whole.substring(j-h,j+h+1).replace("*","").length()>0)
return h;
h++;
}
return h;
}
static boolean all(BigInteger n)
{
BigInteger c=n;
HashSet<Character> hs=new HashSet<>();
while((c+"").compareTo("0")>0)
{
String d=""+c;
char ch=d.charAt(d.length()-1);
if(d.length()==1)
{
c=new BigInteger("0");
}
else
c=new BigInteger(d.substring(0,d.length()-1));
if(hs.contains(ch))
continue;
if(d.charAt(d.length()-1)=='0')
continue;
if(!(n.mod(new BigInteger(""+ch)).equals(new BigInteger("0"))))
return false;
hs.add(ch);
}
return true;
}
static int cal(long n,long k)
{
System.out.println(n+","+k);
if(n==k)
return 2;
if(n<k)
return 1;
if(k==1)
return 1+cal(n, k+1);
if(k>=32)
return 1+cal(n/k, k);
return 1+Math.min(cal(n/k, k),cal(n, k+1));
}
static Node buildTree(int i,int j,int[] arr)
{
if(i==j)
{
//System.out.print(arr[i]);
return new Node(arr[i]);
}
int max=i;
for(int k=i+1;k<=j;k++)
{
if(arr[max]<arr[k])
max=k;
}
Node root=new Node(arr[max]);
//System.out.print(arr[max]);
if(max>i)
root.left=buildTree(i, max-1, arr);
else {
root.left=null;
}
if(max<j)
root.right=buildTree(max+1, j, arr);
else {
root.right=null;
}
return root;
}
static int height(Node root,int val)
{
if(root==null)
return Integer.MAX_VALUE-32;
if(root.val==val)
return 0;
if((root.left==null&&root.right==null))
return Integer.MAX_VALUE-32;
return Math.min(height(root.left, val), height(root.right, val))+1;
}
static void shuffle(int a[], int n)
{
for (int i = 0; i < n; i++) {
// getting the random index
int t = (int)Math.random() * a.length;
// and swapping values a random index
// with the current index
int x = a[t];
a[t] = a[i];
a[i] = x;
}
}
static void sort(int[] arr )
{
shuffle(arr, arr.length);
Arrays.sort(arr);
}
static boolean arraySortedInc(int arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i - 1] > arr[i])
return false;
// No unsorted pair found
return true;
}
static boolean arraySortedDec(int arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i - 1] > arr[i])
return false;
// No unsorted pair found
return true;
}
static int largestPower(int n, int p) {
// Initialize result
int x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
x += n;
}
return x;
}
// Utility function to do modular exponentiation.
// It returns (x^y) % p
static int power(int x, int y, int p) {
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y % 2 == 1) {
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n! % p
static int modFact(int n, int p) {
if (n >= p) {
return 0;
}
int res = 1;
// Use Sieve of Eratosthenes to find all primes
// smaller than n
boolean isPrime[] = new boolean[n + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
// Consider all primes found by Sieve
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
// Find the largest power of prime 'i' that divides n
int k = largestPower(n, i);
// Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p;
}
}
return res;
}
static boolean[] seiveOfErathnos(int n2)
{
boolean isPrime[] = new boolean[n2 + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n2; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n2; j += i) {
isPrime[j] = false;
}
}
}
return isPrime;
}
static boolean[] seiveOfErathnos2(int n2,int[] ans)
{
boolean isPrime[] = new boolean[n2 + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n2; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n2; j += i) {
if(isPrime[j])
ans[i]++;
isPrime[j] = false;
}
}
}
return isPrime;
}
static long helper(int[] arr,int i,int used,long[][] dp)
{
if(i==arr.length)
return 3-used;
if(dp[i][used]!=-1)
return dp[i][used];
long sum=0,ans=Integer.MAX_VALUE;
for(int j=i;j<arr.length;j++)
{
sum+=arr[j];
long n=(long) Math.ceil(Math.log10(sum)/Math.log10(2));
// System.out.println(sum+","+(1L<<n));
if(used<2||j==arr.length-1)
ans=Math.min(ans,(1L<<n)-sum+helper(arr, j+1, used+1, dp));
}
return dp[i][used]=ans;
}
static boolean canDefeat(long hc,long dc,long hm,long dm)
{
// System.out.println(hc+","+dc);
long steps1=(long) Math.ceil(Double.valueOf(hm)/dc);
long steps2=(long) Math.ceil(Double.valueOf(hc)/dm);
return steps1<=steps2;
}
public static void main(String[] args)throws IOException
{
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
FastReader fs=new FastReader();
// int[] ans=new int[1000001];
int T=fs.nextInt();
// seiveOfErathnos2(1000000, ans);
StringBuilder sb=new StringBuilder();
while(T-->0)
{
int n=fs.nextInt(),m=fs.nextInt(),r=fs.nextInt()-1,c=fs.nextInt()-1;
char[][] arr=new char[n][m];
boolean containsRow=false,contains=false;
for(int i=0;i<n;i++)
{
arr[i]=fs.nextLine().toCharArray();
for(int j=0;j<m;j++)
{
if(arr[i][j]=='B')
{
contains=true;
if(i==r||j==c)
containsRow=true;
}
}
}
if(arr[r][c]=='B')
sb.append(0);
else if(containsRow)
sb.append(1);
else if(contains)
sb.append(2);
else
sb.append(-1);
// sb.append(ans?"YES":"NO");
sb.append("\n");
// System.out.println(ans);
}
System.out.println(sb);
}
private static void qprint(int sx) {
// TODO Auto-generated method stub
System.out.println(sx);
}
private static void qprint(String sx) {
// TODO Auto-generated method stub
System.out.println(sx);
}
private static void qprint(int sx, int ex) {
// TODO Auto-generated method stub
System.out.println(sx+","+ex);
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
f718c6d86ed2403f0218909b05c57296
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class temp {
// Let's Go!! ------------->
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t =sc.nextInt();
while(t-- > 0 ) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
char arr[][] = new char[n][m];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextLine().toCharArray();
}
int whitecnt =0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(arr[i][j] == 'W') whitecnt++;
}
}
if(arr[r-1][c-1] == 'B') {
out.println(0);
continue;
}
int row = 0;
int col = 0;
for(int i = 0; i < m; i++) {
if(arr[r-1][i] == 'B') row++;
}
for(int i = 0; i < n; i++) {
if(arr[i][c-1] == 'B') col++;
}
if(row != 0 || col != 0) {
out.println(1);
continue;
}else if(whitecnt != n*m){
out.println(2);
}else {
out.println(-1);
}
}
// -------END-------
out.close();
}
// <-----------------------Template --------------------->
// Pair Class && Sort by First Value(Asc)----------->
// static class pair<T> implements Comparable<pair>{
// T first, second;
// pair(T first, T second) {
// this.first = first;
// this.second = second;
// }
// @Override
// public int compareTo(pair o) {
// return (Integer) this.first - (Integer) o.first;
// }
// }
static class pair{
int first, second;
pair(int first, int second) {
this.first = first;
this.second = second;
}
}
// Ruffle Sort
static void ruffleSort(int[] a) {
//Shuffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Fast I/O
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() {return Double.parseDouble(next());}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
33f3ba35fe265cce6eb76063fc8070b2
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/* package codechef; // don't place package name! */
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codeforces
{
public static void main (String[] args) throws java.lang.Exception
{
try {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int m=sc.nextInt();
int r=sc.nextInt();
int c=sc.nextInt();
char a[][]=new char[n][m];
int b=0;
for(int i=0;i<n;i++){
String s=sc.next();
for(int j=0;j<m;j++){
a[i][j]= s.charAt(j);
if(a[i][j]=='B')
b++;
}
}
int pr=0, pc=0;
if(a[r-1][c-1]=='B')
System.out.println(0);
else
if(b!=0)
{
for(int i=0;i<m;i++)
{
if(a[r-1][i]=='B')
{
pr=1;
break;
}
}
for(int i=0;i<n;i++)
{
if(a[i][c-1]=='B')
{
pc=1;
break;
}
}
if(pr==1 || pc==1)
System.out.println(1);
else
System.out.println(2);
}
else
System.out.println(-1);
}
} catch(Exception e){
return;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
4842814d3c30b4f0e505bf86743c97e5
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class _1627_A {
public static void main(String[] args) {
Scanner test = new Scanner(System.in);
int t = test.nextInt();
while (t > 0) {
int n = test.nextInt();
int m = test.nextInt();
int r = test.nextInt();
int c = test.nextInt();
char[][] mat=new char[n][m];
for(int i=0;i<n;i++){
String s=test.next();
for(int j=0;j<m;j++) {
mat[i][j] = s.charAt(j);
}
}
if(mat[r-1][c-1]=='B'){
System.out.println(0);
}
else{
if(checkRC(mat,n,m,r,c)){
System.out.println(1);
}
else if(checkWhole(mat,n,m)){
System.out.println(2);
}
else{
System.out.println(-1);
}
}
t--;
}
}
static boolean checkWhole(char[][] mat,int n,int m) {
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]=='B'){
return true;
}
}
}
return false;
}
static boolean checkRC(char[][] mat,int n,int m,int r,int c){
for(int i=0;i<n;i++){
if(mat[i][c-1]=='B'){
return true;
}
}
for(int i=0;i<m;i++){
if(mat[r-1][i]=='B'){
return true;
}
}
return false;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
9ef73e8f171e432f19259a98a2929018
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.awt.Container;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Main
{
public static boolean check(char a[][])
{
for (int i = 0; i <a.length; i++) {
for (int j = 0; j<a[i].length; j++) {
if(a[i][j]=='B')
{
return false;
}
}
}
return true;
}
public static boolean sameroworcol(char a[][],int x,int y)
{
for (int i = 0; i <a.length; i++) {
for (int j = 0; j<a[i].length; j++) {
if(a[i][j]=='B'&&i==x)
{
return true;
}
else if(a[i][j]=='B'&&j==y)
{
return true;
}
}
}
return false;
}
public static void main(String[] args)
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work:
while (tc-- > 0) {
int n = input.nextInt();
int m = input.nextInt();
int needx = input.nextInt()-1;
int needy = input.nextInt()-1;
char a[][] = new char[n][m];
for (int i = 0; i <n; i++) {
a[i] = input.next().toCharArray();
}
if(a[needx][needy]=='B')
{
System.out.println("0");
}
else if(check(a))
{
System.out.println("-1");
}
else if(sameroworcol(a, needx, needy))
{
System.out.println("1");
}
else
{
System.out.println("2");
}
}
}
static class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next()
{
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
b38e4aa6a16b30d8da20eba506708395
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
//package com.company;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;
import java.lang.String;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
for(int hey=0; hey<t; hey++)
{
int n,m,r,c,bugi=0,chigi=0,flag=0;
n = sc.nextInt();
m = sc.nextInt();
r = sc.nextInt();
c = sc.nextInt();
int [][] a = new int[n][m];
for(int i=0;i<n;i++)
{
String s = sc.next();
for(int j=0;j<m;j++)
{
//char s = sc.next().charAt(0);
//char x = s.charAt(0);
if(s.charAt(j)=='B')flag++;
if(s.charAt(j)=='W')a[i][j]=1;
else a[i][j]=0;
}
}
if(flag==0)
{
System.out.println(-1);
}
else if(a[r-1][c-1]==0)
{
System.out.println(0);
}
else
{
flag=0;
for(int i=0;i<n;i++)
{
if(a[i][c-1]==0)
{
flag++;
break;
}
}
for(int i=0;i<m;i++)
{
if(a[r-1][i]==0)
{
flag++;
break;
}
}
if(flag>0)
System.out.println(1);
else System.out.println(2);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
1b6c4721ccfdb79b2fb9787be7ae35e7
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[52]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args)
throws IOException
{
Reader s = new Reader();
int t=s.nextInt();
while(t-->0){
int r=s.nextInt();
int c=s.nextInt();
int tr=s.nextInt();
int tc=s.nextInt();
boolean ba[][]=new boolean [r][c];
boolean tb=false;
for(int i=0;i<r;i++){
String st=s.readLine();
for(int j=0;j<c;j++){
if( st.charAt(j)=='W' )
ba[i][j]=false;
else{
ba[i][j]=true;
tb=true;
}
}
}
if(ba[tr-1][tc-1] )System.out.println("0");
else{
boolean rb=false;
boolean cb=false;
if(tb){
for(int i=0;i<r;i++){
if(ba[i][tc-1] )
rb=true;
}
for(int i=0;i<c;i++){
if(ba[tr-1][i] )
cb=true;
}
if(rb||cb) System.out.println(1);
else System.out.println(2);
}
else System.out.println(-1);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
ab3636116f80074b45fa5b06a58ccd9e
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class cf1627a {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while (t --> 0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken()) - 1;
int c = Integer.parseInt(st.nextToken()) - 1;
boolean[][] g = new boolean[n][m];
boolean none = true;
for (int i = 0; i < n; i++) {
String s = br.readLine();
for (int j = 0; j < m; j++) {
g[i][j] = s.charAt(j) == 'B';
if (g[i][j]) {
none = false;
}
}
}
if (g[r][c]) {
pw.println("0");
} else if (none) {
pw.println("-1");
} else {
boolean good = false;
for (int i = 0; i < n; i++) {
if (g[i][c]) good = true;
}
for (int i = 0; i < m; i++) {
if (g[r][i]) good = true;
}
if (good) pw.println("1");
else pw.println("2");
}
}
pw.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
d0daf8b103eacfaddb631ad23edf3446
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int k = 0; k < t; k++) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++) {
String temp = sc.next();
for (int j = 0; j < m; j++) {
mat[i][j] = temp.charAt(j);
}
}
System.out.println(fun(mat, r - 1, c - 1));
}
}
private static int fun ( char[][] mat,int r, int c){
if (mat[r][c] == 'B') return 0;
else {
for (int i = 0; i < mat[0].length; i++) {
if (mat[r][i] == 'B') return 1;
}
for (int i = 0; i < mat.length; i++) {
if (mat[i][c] == 'B') return 1;
}
}
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
if(mat[i][j]=='B') return 2;
}
}
return -1;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
efb031c1bc9076308c47821d5abdf9fc
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class NotShading {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t --> 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
boolean allWhite = true;
sc.nextLine();
String[] rows = new String[n];
for (int i=0;i<n;++i) {
rows[i] = sc.nextLine();
}
if (rows[r-1].charAt(c-1) == 'B') {
System.out.println(0);
}
else {
if (rows[r-1].contains("B")) {
System.out.println(1);
}
else {
for (int i=0;i<n;++i) {
if (rows[i].contains("B")) {
allWhite = false;
}
if (rows[i].charAt(c-1) == 'B') {
System.out.println(1);
break;
}
else if (i == n-1 && allWhite) {
System.out.println(-1);
}
else if (i == n-1) {
System.out.println(2);
}
}
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
fe45176af93d54651ea385cdb27c4095
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
//1627A
//package com.ALevelProgram;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class NotShading {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st==null||!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String temp ="";
try {
temp = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;
}
}
public int maxSteps(int m, int n, int x, int y, char[][] arr) {
for(int i = 0 ;i< x ;i++) {
//System.out.println("row - "+arr[i][n-1]);
if(arr[i][n-1] == 'B') {
return 1;
}
}
for(int i = 0 ;i< y ;i++) {
//System.out.println("column - "+arr[m-1][i]);
if(arr[m-1][i] == 'B') {
return 1;
}
}
return 2;
}
public static void main(String[] args) {
NotShading notShading = new NotShading();
FastReader fr = new FastReader();
int t = fr.nextInt();
ArrayList<Integer> result= new ArrayList<Integer>();
for(int i = 0 ; i<t; i++) {
int r = fr.nextInt();
int c = fr.nextInt();
char[][] arr = new char[r][c];
int rs = fr.nextInt();
int cs = fr.nextInt();
int allWhite = 0;
String temp;
for(int j = 0; j<r ;j++) {
temp = fr.nextLine();
for(int k = 0 ; k<temp.length() ;k++) {
arr[j][k] = temp.charAt(k);
if(arr[j][k] == 'W') allWhite++;
}
}
// for(int j =0;j<r;j++) {
// System.out.println("");
// for(int k = 0;k<c;k++) {
// System.out.print(arr[j][k]+" ");
// }
// }
// System.out.print(allWhite);
if(allWhite == (r*c)) {
result.add(-1);
}
else if(arr[rs-1][cs-1] == 'B') {
result.add(0);
}
else {
result.add(notShading.maxSteps(rs ,cs ,r ,c ,arr));
}
}
for(int i = 0 ; i<t ; i++) {
System.out.println(result.get(i));
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
5ccbc418ed42ff679392ed1773549a4a
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
StringBuilder sb = new StringBuilder();
while(t-- > 0){
int n = scn.nextInt();
int m = scn.nextInt();
int r = scn.nextInt() - 1;
int c = scn.nextInt() - 1;
scn.nextLine();
char[][] arr = new char[n][m];
boolean flag = false;
for(int i = 0; i < n; i++){
String s = scn.nextLine();
for(int j = 0; j < m; j++){
arr[i][j] = s.charAt(j);
if(arr[i][j] == 'B')flag = true;
}
}
if(flag == false){
sb.append("-1\n");
continue;
}
if(arr[r][c] == 'B'){
sb.append("0\n");
continue;
}
flag = false;
for(int i = 0; i < n; i++){
if(arr[i][c] == 'B'){
flag = true;
break;
}
}
for(int i = 0; i < m; i++){
if(arr[r][i] == 'B'){
flag = true;
break;
}
}
if(flag){
sb.append("1\n");
}else{
sb.append("2\n");
}
}
System.out.println(sb);
scn.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
2b8432b91f734c7d755aff07173373b8
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class A {
public static void process() throws IOException {
int n = sc.nextInt(), m = sc.nextInt();
int r = sc.nextInt(), c = sc.nextInt();
char[][] arr = new char[n][m];
for(int i = 0; i<n; i++)arr[i] = sc.next().toCharArray();
boolean flag = false;
for(int i = 0; i<n; i++) {
for(char e : arr[i]) {
if(e == 'B')flag = true;
}
}
if(!flag) {
System.out.println(-1);
return;
}
if(arr[r-1][c-1] == 'B') {
System.out.println(0);
return;
}
for(int i = 0; i<n; i++) {
if(arr[i][c-1] == 'B') {
System.out.println(1);
return;
}
}
for(int i = 0; i<m; i++) {
if(arr[r-1][i] == 'B') {
System.out.println(1);
return;
}
}
System.out.println(2);
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readArrayDouble(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
02adda09e3a5bcaa17d4dc6ff61e99d3
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import javax.management.Query;
import java.io.*;
public class Contest1 {
public static void main(String[] args) throws Exception {
//Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int m=sc.nextInt();
int r=sc.nextInt()-1;
int c=sc.nextInt()-1;
boolean imp=true;
boolean iszero=false;
boolean isone=false;
String s="";
for (int i = 0; i < n; i++) {
s=sc.next();
if(s.contains("B"))
imp=false;
if(i==r&&s.charAt(c)=='B')
iszero=true;
if((i==r&&s.contains("B"))||s.charAt(c)=='B')
isone=true;
}
if(imp)
pw.println(-1);
else if(iszero)
pw.println(0);
else if(isone)
pw.println(1);
else
pw.println(2);
}
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
da80366e8d3d6ccb563a27f59bacdc06
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.StringTokenizer;
public class A_NotShading_800 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt()-1;
int c = sc.nextInt()-1;
char[][] C = new char[n][m];
for(int i = 0; i < n; i++) {
C[i] = sc.next().toCharArray();
}
int ans = Integer.MAX_VALUE;
boolean flag = true;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(C[i][j] == 'B') {
flag = false;
ans = Math.min(ans, 2);
if(i == r && j == c) {
ans = Math.min(ans, 0);
} else if(i == r || j == c) {
ans = Math.min(ans, 1);
}
}
}
}
if(flag) ans = -1;
System.out.println(ans);
}
out.close();
}
public static PrintWriter out;
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
public int x,y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
552d4eb3a73802c17e7014d156a576eb
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
/**
* @author by guiwei
* @Classname Main
* @Description
* @Date 2022/1/23 17:49
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
int[] v = new int[t];
out: for (int i = 0; i < t; i++) {
int count = 0;
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int d = scanner.nextInt();
char[][] e = new char[a][b];
for (int j = 0; j < a; j++) {
String s = scanner.next();
e[j] = s.toCharArray();
}
if (e[c-1][d-1] == 'B'){
v[i] = 0;
continue out;
}
for (int l = 0; l < b; l++) {
if (e[c-1][l] == 'B'){
v[i] = 1;
continue out;
}
}
for (int l = 0; l < a; l++) {
if (e[l][d-1] == 'B'){
v[i] = 1;
continue out;
}
}
for (int n = 0; n < a; n++) {
for (int l = 0; l < b; l++) {
if (e[n][l] == 'W'){
count++;
}
if (count == a*b){
v[i] = -1;
continue out;
}
}
}
v[i] = 2;
}
for (int i = 0; i < t; i++) {
System.out.println(v[i]);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
b74277d18f6106709d08febb317eab2c
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.PrintWriter;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.nextInt();
while(tc-->0){
int n = sc.nextInt(), m = sc.nextInt(), r = sc.nextInt(), c = sc.nextInt();
r--; c--;
char[][] arr = new char[n][m];
for(int i = 0; i<n; i++)arr[i] = sc.next().toCharArray();
if(arr[r][c] == 'B')pw.println(0);
else{
boolean flag = false;
for(char x: arr[r]){
if(x == 'B'){
flag = true;
break;
}
}
if(flag){
pw.println(1);
continue;
}
for(int i = 0; i<n; i++){
if(arr[i][c] == 'B'){
flag = true;
break;
}
}
if(flag){
pw.println(1);
continue;
}
here:
for(char[] x: arr){
for(char z: x){
if(z == 'B'){
flag = true;
break here;
}
}
}
if(flag)pw.println(2);
else pw.println(-1);
}
}
pw.flush();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
157f9b0bc36a894a770524097abe280f
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A {
public static void solution(BufferedReader reader, PrintWriter writer)
throws IOException {
In in = new In(reader);
Out out = new Out(writer);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int n = in.nextInt(), m = in.nextInt();
int r = in.nextInt() - 1, c = in.nextInt() - 1;
char[][] grid = new char[n][m];
for(int i = 0; i < n; i++)
grid[i] = in.next().toCharArray();
boolean[] b = new boolean[3];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(grid[i][j] == 'B') {
int cnt = (i == r ? 1 : 0) + (j == c ? 1 : 0);
b[cnt] = true;
}
int rst = -1;
for(int i = 2; i >= 0; i--)
if(b[i]) {
rst = 2 - i;
break;
}
out.println(rst);
}
}
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
solution(reader, writer);
writer.close();
}
protected static class In {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public In(BufferedReader reader) {
this.reader = reader;
}
public String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArray1(int n) throws IOException {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArraySorted(int n) throws IOException {
int[] a = nextIntArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArray1(int n) throws IOException {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArraySorted(int n) throws IOException {
long[] a = nextLongArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
}
protected static class Out {
private PrintWriter writer;
private static boolean local = System
.getProperty("ONLINE_JUDGE") == null;
public Out(PrintWriter writer) {
this.writer = writer;
}
public void print(char c) {
writer.print(c);
}
public void print(int a) {
writer.print(a);
}
public void printb(int a) {
writer.print(a);
writer.print(' ');
}
public void println(Object a) {
writer.println(a);
}
public void println(Object[] os) {
for (int i = 0; i < os.length; i++) {
writer.print(os[i]);
writer.print(' ');
}
writer.println();
}
public void println(int[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public void println(long[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public void flush() {
writer.flush();
}
public static void db(Object... objects) {
if (local)
System.out.println(Arrays.deepToString(objects));
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
c934940898767865d600532e760267dd
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
import java.math.*;
public class NotShading {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
// Loop through n test cases
for(int i=0; i < n; i++) {
int rows,columns,r,c;
Boolean isAlreadyBlack = false;
Boolean oneRequired = false;
Boolean blackSeen = false;
st = new StringTokenizer(br.readLine());
rows = Integer.parseInt(st.nextToken());
columns = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
for(int j=0; j < rows; j++) {
st = new StringTokenizer(br.readLine());
String current = st.nextToken();
for(int k=0; k<columns; k++) {
if(current.charAt(k) == 'B') {
if(j+1==r && k+1==c) {
isAlreadyBlack = true;
break;
}
if(j+1 == r || k+1 == c) {
oneRequired = true;
continue;
}
blackSeen = true;
}
}
}
if(isAlreadyBlack) {
System.out.println("0");
}
else if(oneRequired) {
System.out.println("1");
}
else if(blackSeen) {
System.out.println("2");
}
else {
System.out.println("-1");
}
}
}
/////////////////////////////////////////////////////////////
// Template Functions
/////////////////////////////////////////////////////////////
// Gets primes from 1..n
public static ArrayList<Integer> getPrimes(int n) {
ArrayList<Integer> primes = new ArrayList<Integer>();
primes.add(2);
for(int i=3; i<n; i++) {
boolean prime = true;
for(int j : primes) {
if(i%j == 0) {
prime = false;
break;
}
}
if(prime) {
primes.add(i);
}
}
return primes;
}
public static int gcd(int n1, int n2) {
if(n2 == 0) {
return n1;
}
return gcd(n2, n1 % n2);
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 17
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
8c44c9bb5c44f79f52fd727826362e2b
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
//https://codeforces.com/problemset/problem/1627/A
import java.util.Scanner;
public class Not_Shading {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
while (testcase-- != 0) {
int hang = sc.nextInt(), cot = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt();
String str[] = new String[hang];
char s[][] = new char[hang][cot];
boolean has_black = false;
for (int i = 0; i < hang; i++) {
str[i] = sc.next();
if (str[i].contains("B"))
has_black = true;
s[i] = str[i].toCharArray();
}
if (has_black) {
has_black = false;
if (s[x - 1][y - 1] == 'B')
System.out.print(0);
else {
for (int i = 0; i < hang; i++) {
if (i != x - 1 && s[i][y - 1] == 'B') {
has_black = true;
break;
}
}
if (!has_black) {
for (int i = 0; i < cot; i++) {
if (i != y - 1 && s[x - 1][i] == 'B') {
has_black = true;
break;
}
}
}
if (has_black)
System.out.print(1);
else
System.out.print(2);
}
} else
System.out.print(-1);
if (testcase > 0)
System.out.println();
}
sc.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 17
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
41cd34e8b12b0dcd19e268bab7c866f3
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class notShading {
public static void p(Object o) {
System.out.println(o);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
while (testCases --> 0) {
int row = sc.nextInt(), column = sc.nextInt();
int rowTarget = sc.nextInt() - 1, columnTarget = sc.nextInt() - 1;
int closestRow = Integer.MAX_VALUE, closestColumn = Integer.MAX_VALUE;
boolean allWhite = true , gotYou = false;
for (int i = 0; i < row; i++) {
String temp = sc.next();
if (i == rowTarget && temp.charAt(columnTarget) == 'B') gotYou = true;
if (temp.contains("B")) {
allWhite = false;
closestRow = Math.min(closestRow, Math.abs(i - rowTarget));
}
for (int j = 0; j < column; j++) {
if (temp.charAt(j) == 'B') closestColumn = Math.min(closestColumn, Math.abs(j - columnTarget));
}
}
if (gotYou) {
p(0);
} else if (allWhite) {
p(-1);
} else if (closestColumn == 0 || closestRow == 0) {
p(1);
} else {
p(2);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 17
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
cfc49b2d32c356d3ca1b540109c19ef7
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
private static void run() throws IOException {
int n, m, r, c;
n = in.nextInt();
m = in.nextInt();
r = in.nextInt() - 1;
c = in.nextInt() - 1;
char[][] s = new char[n][];
for (int i = 0; i < n; i++) {
s[i] = in.next().toCharArray();
}
if (s[r][c] == 'B') {
out.println(0);
return;
}
for (int i = 0; i < n; i++) {
if (s[i][c] == 'B') {
out.println(1);
return;
}
}
for (int i = 0; i < m; i++) {
if (s[r][i] == 'B') {
out.println(1);
return;
}
}
boolean flag = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == 'B') {
flag = true;
}
}
}
if (flag) {
out.println(2);
} else {
out.println(-1);
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long add_mod(long... longs) {
long ans = 0;
for (long now : longs) {
ans = (ans + now) % mod;
if (ans < 0) ans += mod;
}
return ans;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 17
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
6be57a1b3c78c80698d460449b61802d
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class CF1627A {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int r = scanner.nextInt();
int c = scanner.nextInt();
char[][] gird = new char[n][m];
for (int j = 0; j < n; j++) {
gird[j] = scanner.next().toCharArray();
}
System.out.println(solve(n, m, r, c, gird));
}
}
private static String solve(int n, int m, int r, int c, char[][] grid) {
// BFS
if (grid[r - 1][c - 1] == 'B') {
return "0";
}
for (int i = 0; i < n; i++) {
if (grid[i][c - 1] == 'B') {
return "1";
}
}
for (int j = 0; j < m; j++) {
if (grid[r - 1][j] == 'B') {
return "1";
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'B') {
return "2";
}
}
}
return "-1";
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 17
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
fa8d56910a8de4332e39d27cf874549f
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round766A {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Round766A sol = new Round766A();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n) or matrix(n, 2) for graph edges, pairs, ...
int n, m, r, c;
String[] a;
void getInput() {
n = in.nextInt();
m = in.nextInt();
r = in.nextInt()-1;
c = in.nextInt()-1;
a = in.nextStringArray(n);
}
void printOutput() {
out.printlnAns(ans);
}
int ans;
void solve(){
int cnt = 0;
boolean existsSame = false;
if(a[r].charAt(c) == 'B') {
ans = 0;
return;
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(a[i].charAt(j) == 'B') {
cnt++;
if(i == r || j == c)
existsSame = true;
}
}
}
if(cnt == 0) {
ans = -1;
}
else {
if(existsSame)
ans = 1;
else
ans = 2;
}
}
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 17
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
41bc963d29504e8a61c6d959c0d17bf6
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
import static java.lang.Math.min;
public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int testcase=scan.nextInt();
if(testcase>=1 && testcase<=100){
for(int t=1;t<=testcase;t++){
int row=scan.nextInt();
int col=scan.nextInt();
int brow=scan.nextInt();
int bcol=scan.nextInt();
brow--;
bcol--;
int mx=10;
char[][] matrix = new char[row][col];
int counter=0;
for (int i=0;i<row;i++){
String ten=scan.next();
for (int j=0;j<col;j++){
matrix[i][j]=ten.charAt(j);
}
}
for(int i=0;i<row;i++){
for (int j=0;j<col;j++){
if(matrix[i][j]=='B'){
if(i==brow && j==bcol){
// System.out.println(0+" "+i+""+j+""+matrix[i][j]+" ");
// System.out.println(0+" "+brow+" "+bcol);
mx=min(0,mx);
}
else if(i==brow || j==bcol){
mx=min(1,mx);
}else{
mx=min(2,mx);
}
}
}
}
if(mx==10){
System.out.println(-1);
}else {
System.out.println(mx);
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
57edafca97ed9e687aa24a224ff790e6
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import javax.management.MBeanRegistration;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
//TODO: create char array input
static final int MOD = 1000000007;
static class Input {
static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
Input() throws IOException {
}
static int getInt() throws IOException {
return Integer.parseInt(bufferedReader.readLine());
}
static int[] getArray(int n) throws IOException {
StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine());
int[] ar = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = Integer.parseInt(stringTokenizer.nextToken());
}
return ar;
}
static long[] getLongArray(int n) throws IOException {
StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine());
long[] ar = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = Long.parseLong(stringTokenizer.nextToken());
}
return ar;
}
static String getString() throws IOException {
return bufferedReader.readLine();
}
static int[] getArrayDontKnowSize() throws IOException {
StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine());
List<Integer> array = new ArrayList<>();
while (stringTokenizer.hasMoreTokens()) {
array.add(Integer.parseInt(stringTokenizer.nextToken()));
}
return array.stream().mapToInt(value -> value).toArray();
}
}
static long modPower(long x, int y) {
long ans = 1;
x = x % MOD;
while (y > 0) {
if (y % 2 == 1)
ans = (ans * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return ans;
}
static int upperBound(int[] ar, int x) {
int mid, n = ar.length;
int start = 0;
int end = n;
if (x < ar[0])
return 0;
if (x > ar[n - 1])
return n;
while (start < end) {
mid = start + (end - start) / 2;
if (x >= ar[mid]) {
start = mid + 1;
} else {
end = mid;
}
}
return start;
}
public static void main(String[] args) throws IOException {
int t = Input.getInt();
while (t-- > 0) {
int[] rnmrc = Input.getArray(4);
int n = rnmrc[0];
int m = rnmrc[1];
int r = rnmrc[2];
int c = rnmrc[3];
String[] input = new String[n];
Set<Integer> br = new HashSet<>();
Set<Integer> bc = new HashSet<>();
for(int i = 0 ; i < n ; i++){
input[i] = Input.getString();
for(int j = 0; j < m ;j++){
char cell = input[i].charAt(j);
if(cell == 'B'){
br.add(i+1);
bc.add(j+1);
}
}
}
// System.out.println(br);
// System.out.println(bc);
if(br.size() == 0){
System.out.println(-1);
continue;
}
if(br.contains(r) || bc.contains(c)){
if(input[r-1].charAt(c-1) == 'B')
System.out.println(0);
else
System.out.println(1);
}else
System.out.println(2);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
39cab429291394c17617e944172075f5
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Solution{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args){
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0){
int n = in.nextInt();
int m = in.nextInt();
int r = in.nextInt();
int c = in.nextInt();
String[] str = new String[n];
for(int i = 0;i<n;i++){
str[i] = in.next();
}
int f=-1;
for(int i=1;i<=n;i++){
if(f==0) continue;
boolean cont= str[i-1].contains("B");
if(cont){
if(str[i-1].charAt(c-1)=='B' && r==i ) f = 0;
else if(f!=0 && str[i-1].charAt(c-1)=='B') f=1;
else if(f!=0 && r==i) f= 1;
else if(f!=0 && f!=1){
f= 2;
}
}
}
System.out.println(f);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
2bb684247ac31b2cb744130850a16e85
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class Prob {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt()-1;
int c = sc.nextInt()-1;
char[][]arr = new char[n][];
boolean bool = false;
boolean ok = false;
for (int j = 0; j < arr.length; j++) {
arr[j] = sc.next().toCharArray();
}
for (int j = 0; j < n; j++) {
if(arr[r][c]=='B') {
System.out.println(0);
bool = true;
break;
}
if(arr[j][c]=='B') {
System.out.println(1);
bool = true;
break;
}
for (int j2 = 0; j2 < m; j2++) {
if(arr[r][j2]=='B') {
System.out.println(1);
bool = true;
break;
}
if(arr[j][j2]=='B')
{
ok=true;
}
}
if(bool) {
break;
}
}
if(!bool)
{
if(ok)
{
System.out.println(2);
}
else
{
System.out.println(-1);
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
55e0b11b50a12aec4ac55fd5e7124992
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
import java.io.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int z = 0; z < t; z++) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
r--;c--;
boolean flag = false;
int blck_cnt = 0;
char arr[][] = new char[n][m];
for(int i =0;i<n;i++)
arr[i] = sc.next().toCharArray();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] == 'B') {
if(i==r||j==c) flag = true;
blck_cnt++;
}
}
}
if(blck_cnt==0) System.out.println(-1);
else if(arr[r][c]=='B') System.out.println(0);
else if(flag==true) System.out.println(1);
else{
System.out.println(2);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
fe510eef9fcb313c59413b663b47cfaa
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;
/*
*/
public class DineshPagal {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int z = 0; z < T; z++) {
int n = fs.nextInt();
int m = fs.nextInt();
int r = fs.nextInt();
int c = fs.nextInt();
r--;c--;
boolean flag = false;
int blck_cnt = 0;
char arr[][] = new char[n][m];
for (int i = 0; i < n; i++) {
arr[i] = fs.next().toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] == 'B') {
if(i==r||j==c) flag = true;
blck_cnt++;
}
}
}
if(blck_cnt==0) System.out.println(-1);
else if(arr[r][c]=='B') System.out.println(0);
else if(flag==true) System.out.println(1);
else{
System.out.println(2);
}
}
// for (int tt=0; tt<T; tt++) {
// int n=fs.nextInt();
// char[] line=fs.next().toCharArray();
// if (n>2) {
// System.out.println("NO");
// }
// else if (n==2 && line[0]==line[1]){
// System.out.println("NO");
// }
// else {
// System.out.println("YES");
// }
// }
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
1356411e5ecf7bb5f43950806e77f3ae
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while ( t-- > 0 ) {
int n = scan.nextInt();
int m = scan.nextInt();
int r = scan.nextInt();
int c = scan.nextInt();
String[] array = new String[n];
for ( int i = 0; i < n; ++i ) {
array[i] = scan.next();
}
char[][] list = new char[n][m];
boolean isTrue = true;
for ( int i = 0; i < n; ++i ) {
for ( int j = 0; j < m; ++j ) {
list[i][j] = array[i].charAt(j);
if ( list[i][j] == 'B' ) {
isTrue = false;
}
}
}
if ( list[r - 1][c - 1] == 'B' ) {
System.out.println(0);
}
else if ( m == 1 && !isTrue ) {
System.out.println(1);
}
else if ( isTrue ) {
System.out.println(-1);
}
else {
if ( array[r - 1].contains("B") ) {
System.out.println(1);
}
else {
boolean isAns = true;
for ( int i = 0; i < n; ++i ) {
if ( list[i][c - 1] == 'B' ) {
isAns = false;
break;
}
}
System.out.println( isAns ? 2 : 1 );
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
77314dea68727275e527d6839a0f71c2
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while ( t-- > 0 ) {
int n = scan.nextInt();
int m = scan.nextInt();
int r = scan.nextInt();
int c = scan.nextInt();
String[] array = new String[n];
for ( int i = 0; i < n; ++i ) {
array[i] = scan.next();
}
char[][] list = new char[n][m];
boolean isTrue = true;
for ( int i = 0; i < n; ++i ) {
for ( int j = 0; j < m; ++j ) {
list[i][j] = array[i].charAt(j);
if ( list[i][j] == 'B' ) {
isTrue = false;
}
}
}
int idx = 0;
if ( list[r - 1][c - 1] == 'B' ) {
System.out.println(0);
}
else if ( m == 1 && !isTrue ) {
System.out.println(1);
}
else if ( isTrue ) {
System.out.println(-1);
}
else {
if ( array[r - 1].contains("B") ) {
System.out.println(1);
}
else {
boolean isAns = true;
for ( int i = 0; i < n; ++i ) {
if ( list[i][c - 1] == 'B' ) {
isAns = false;
break;
}
}
System.out.println( isAns ? 2 : 1 );
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
6f29d7e9dcef312af50a9db1e217cc01
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
if(t>=0&&t<=100){
while(t>0) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int r = scanner.nextInt();
int c = scanner.nextInt();
int sum = 0;
int p = 0;
if(1<=n&&n<=50&&m>=1&&m<=50&&1<=r&&r<=n&&c>=1&&c<=m){
String str[] = new String[n];
for(int i = 1;i<=n;i++) {
str[i-1]= scanner.next();
}
int i = 0;
int k = 3;
while(i<n) {
if(k==1||k==2||k==0) {
break;
}
for(int j = 1;j<=m;j++) {
if(str[i].charAt(j-1)=='B'&&str[r-1].charAt(c-1)=='W') {
if(r-1==i||j-1==c-1) {
k=1;
System.out.println(k);
break;
}
}
if(str[r-1].charAt(c-1)=='B') {
k=0;
System.out.println(k);
break;
}
if(str[i].charAt(j-1)=='W') {
sum++;
}
if(str[i].charAt(c-1)=='W'&&str[r-1].charAt(j-1)=='W'&&str[r-1].charAt(c-1)=='W') {
p++;
}
}
i++;
}
if(sum==n*m) {
System.out.println(-1);
}
else if(p==n*m) {
System.out.println(2);
}
t--;
}
// TODO Auto-generated method stub
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
0fa85d8ceaa126b7113656a780581e0e
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
// https://codeforces.com/contest/1627/problem/A
import java.util.*;
import java.io.*;
public class NotShading {
static FastReader in = new FastReader();
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) throws IOException {
//int t=1;
int t = in.nextInt();
while (t>0) {
solve(t);
t--;
}
System.out.println(sb);
}
/********** code here ******************/
static void solve(int test) {
String str[] = in.nextLine().split(" ");
int n = integer(str[0]);
int m = integer(str[1]);
int r = integer(str[2]);
int c = integer(str[3]);
String ans = "";
String arr[] = new String[n];
boolean temp = false;
for (int i=0; i<n; i++) {
arr[i] = in.next();
if (arr[i].charAt(c-1)=='B') {
temp = true;
}
}
int count = 0;
for (int j=0; j<arr.length; j++) {
if (!arr[j].contains("B")) {
count++;
}
}
if (count==n) {
ans = "-1\n";
} else if (arr[r-1].charAt(c-1)=='B') {
ans = "0\n";
} else if (arr[r-1].contains("B") || temp) {
ans = "1\n";
} else {
ans = "2\n";
}
sb.append(ans);
}
/***************************************/
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readintarray(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
long[] readlongarray(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
}
static <E> void println(E res) {
System.out.println(res);
}
static <E> void print(E res) {
System.out.print(res);
}
static String string(int a) {
return Integer.toString(a);
}
static String string(char a) {
return Character.toString(a);
}
static int integer(String a) {
return Integer.parseInt(a);
}
static int len(String a) {
return a.length();
}
static boolean even(int a) {
return (a & 1) == 0 ? true : false;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
8f086c790d20d57ac13679627e3150df
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class Test {
public static void main(String[] args) {
int T;
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
sc.nextLine();
for(int i = 0;i < T;i++) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
String[] arr = new String[n];
sc.nextLine();
for(int j = 0;j < n;j++) {
arr[j] = sc.nextLine();
}
int result = notShading(n, m, r, c, arr);
System.out.println(result);
}
sc.close();
}
public static int notShading(int rows, int cols, int r, int c, String arr[]) {
int result = 100000;
// Check if there exists a black cell already
if(arr[r - 1].charAt(c - 1) == 'B') {
return 0;
}
// Check if there exists a black cell in the same column
for(int i = 0;i < rows;i++) {
if(arr[i].charAt(c - 1) == 'B') {
return 1;
}
}
// Check if there exists a black cell in the same row
for(int i = 0;i < cols;i++) {
if(arr[r - 1].charAt(i) == 'B') {
return 1;
}
}
for(int i = 0;i < rows;i++) {
for(int j = 0;j < cols;j++) {
if(arr[i].charAt(j) == 'B') return 2;
}
}
return result == 100000 ? -1 : result;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
a4e587a4a76718fd8da2783b6a77c7ed
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int testCases = Integer.parseInt(br.readLine());
while (testCases--!= 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int min = -1;
for (int i = 1; i <= n; i++) {
String chars = br.readLine();
for (int j = 1; j <= m; j++) {
if (chars.substring(j - 1, j).equals("B")) {
if (i == r && j == c) {
min = 0;
break;
} else if (i == r || j == c) {
min = 1;
} else if (min != 1) {
min = 2;
}
}
}
if (min == 0) {
while (i++ < n) {
br.readLine();
}
}
}
pw.println(min);
}
pw.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
a9fbb1ed886328c23dfe8401c48965ac
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int tests=scanner.nextInt();
for(int i=0;i<tests;i++){
int rows=Integer.parseInt(scanner.next());
int cols=Integer.parseInt(scanner.next());
int rowToBlack=Integer.parseInt(scanner.next());
int colToBlack=Integer.parseInt(scanner.next());
char[][] grid= new char[rows][cols];
boolean isBlackPresents=false;
for(int j=0;j<rows;j++){
String rowChars = scanner.next();
isBlackPresents = isBlackPresents || rowChars.contains("B");
grid[j] = rowChars.toCharArray();
}
if(!isBlackPresents){
System.out.println(-1);
continue;
}
rowToBlack--;
colToBlack--;
if(grid[rowToBlack][colToBlack]=='B'){
System.out.println(0);
continue;
}
boolean isRowContainsAnyBlack=false;
for(int k=0;k<cols;k++) {
if(grid[rowToBlack][k]=='B'){
isRowContainsAnyBlack=true;
break;
}
}
if(isRowContainsAnyBlack){
System.out.println(1);
continue;
}
boolean isColsContainsAnyBlack=false;
for(int k=0;k<rows;k++) {
if(grid[k][colToBlack]=='B'){
isColsContainsAnyBlack=true;
break;
}
}
if(isColsContainsAnyBlack){
System.out.println(1);
continue;
}
System.out.println(2);
}
}
// int solve(char[][] grid,boolean[][] visited,int i,int j, int rows, int cols, int rowToBlack, int colToBlack,int steps){
// if(i==rows) return Integer.MAX_VALUE;
// if(j==cols) return Integer.MAX_VALUE;
//
// visited[i][j] = true;
//
// if(grid[i][j]=='B'){
// if(i==rowToBlack || j==colToBlack){
// return steps;
// }
// }
//
// return solve(grid,visited,i+1,j,rows,cols,rowToBlack,colToBlack) ||
// solve(grid,visited,i,j+1,rows,cols,rowToBlack,colToBlack);
//
// }
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
1359085c3165cb93adbac90b9bd0226d
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
// Problem: A. Not Shading
// Contest: Codeforces - Codeforces Round #766 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1627/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- >0)
{
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
sc.nextLine();
int ans=3;
char[][]arr = new char[n][m];
for(int i=0 ; i < n ; i++) {
String temp=sc.next();
for(int j=0;j<m;j++)
{
arr[i][j]=temp.charAt(j);
if(arr[i][j]=='B')
{
if(i+1==r&&j+1==c)
{
ans=0;
break;
}
else if(i+1==r||j+1==c)
{
ans=Math.min(1,ans);
}
else
{
ans=Math.min(ans,2);
}
}
}
}
System.out.println(ans==3?"-1":ans);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
a84262bd2fa4f1418be72274a723439e
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class Basic {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int p=0;p<t;p++) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt()-1;
int c = sc.nextInt()-1;
char arr[][] = new char[n][m];
for (int i=0;i<n;i++) {
String s = sc.next();
for (int j=0;j<m;j++) {
arr[i][j] = s.charAt(j);
}
}
func(arr, n, m, r , c);
}
}
public static void func(char arr[][], int n, int m, int r, int c) {
if (arr[r][c]=='B') {
System.out.println(0);
} else {
for (int i=0;i<n;i++) {
if (arr[i][c]=='B') {
System.out.println(1);
return;
}
}
for (int i=0;i<m;i++) {
if (arr[r][i]=='B') {
System.out.println(1);
return;
}
}
for (int i=0;i<n;i++) {
for (int j=0;j<m;j++) {
if (arr[i][j]=='B') {
System.out.println(2);
return;
}
}
}
System.out.println(-1);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
68c7dac59428b8277033eacd2fb7aafe
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class CF6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc>0){
tc--;
int n=sc.nextInt();
int m=sc.nextInt();
int r=sc.nextInt();
int c=sc.nextInt();
int[][] mat = new int[n][m];
sc.nextLine();
for(int i=0;i<n;i++){
String tem2 = sc.nextLine();
for(int j=0;j<m;j++){
char temp = tem2.charAt(j);
if(temp=='B'){
mat[i][j]=0;
}else{
mat[i][j]=1;
}
}
}
if(mat[r-1][c-1]==0){
System.out.println(0);
}else{
int found =0;
for(int i=0;i<n;i++){
if(mat[i][c-1]==0){
found=1;
}
}
if(found==0){
for(int i=0;i<m;i++){
if(mat[r-1][i]==0){
found=1;
}
}
}
if(found==1){
System.out.println(1);
}
if(found==0){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]==0){
found=1;
}
}
}
if(found==1){
System.out.println(2);
}
if(found==0){
System.out.println(-1);
}
}
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
1199dcc53b11757b1de53aa856cefc57
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.*;
public class NotShading {
static int numRows;
static int numCol;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numTests = sc.nextInt();
for(int i = 0; i < numTests; i++) {
numRows = sc.nextInt();
numCol = sc.nextInt();
char[][] graph = new char[numRows][numCol];
int rowVal = sc.nextInt() - 1;
int colVal = sc.nextInt() - 1;
boolean containsBlack = false;
for(int j = 0; j < numRows; j++) {
String input = sc.next();
if(input.contains("B")) {
containsBlack = true;
}
graph[j] = input.toCharArray();
}
int answer = getNumOperations(graph, rowVal, colVal, containsBlack);
System.out.println(answer);
}
}
public static int getNumOperations(char[][] graph, int rVal, int cVal, boolean containsBlack) {
if(graph[rVal][cVal] == 'B') {
return 0;
}
if(rowOrColContainsBlack(graph, rVal, cVal)) {
return 1;
}
if(containsBlack) {
return 2;
}
return -1;
}
public static boolean rowOrColContainsBlack(char[][] graph, int rVal, int cVal) {
for(int i = 0; i <= numRows - 1; i++) {
if(graph[i][cVal] == 'B') {
return true;
}
}
for(int i = 0; i <= numCol - 1; i++) {
if(graph[rVal][i] == 'B') {
return true;
}
}
return false;
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
c67ba47bc874d764c1d89e5117e86224
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner( System.in );
int no = in.nextInt();
int[] n = new int[no];
int[] m = new int[no];
int[] r = new int[no];
int[] c = new int[no];
int k1 = 0;
int k2 = 0;
for(int i = 0;i<no;i++){
n[i] = in.nextInt();
m[i] = in.nextInt();
r[i] = in.nextInt();
c[i] = in.nextInt();
String[] mat = new String[n[i]];
for(int j = 0;j<n[i];j++){
mat[j] = in.next();
}
if(mat[r[i]-1].charAt(c[i]-1)=='B'){
System.out.println("0");
}
else{
for(int j = 0;j<n[i];j++){
if(mat[j].charAt(c[i]-1)=='B'){
k1++;
}
}
for(int j = 0;j<m[i];j++){
if(mat[r[i]-1].charAt(j)=='B'){
k1++;
}
}
if(k1>=1){
System.out.println("1");
}
else{
for(int j = 0;j<n[i];j++){
for(int k = 0;k<m[i];k++){
if(mat[j].charAt(k)=='B'){
k2++;
}
}
}
if(k2>=1){
System.out.println("2");
}
else{
System.out.println("-1");
}
}
}
k1 = 0;
k2 = 0;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
2c7a402569f3a05d4dac39806afe95ff
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class Not_Shading {
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
int test_cases = Sc.nextInt();
while(test_cases-- != 0)
{
int rows = Sc.nextInt();
int columns = Sc.nextInt();
int target_row = Sc.nextInt();
int target_column = Sc.nextInt();
int count = 0;
boolean found = false;
int white_count = 0;
for(int row = 1; row <= rows; row++)
{
String s = Sc.next();
if(row == target_row)
{
for(int column = 0; column < columns ; column++)
{
if(s.charAt(column) == 'B')
count = 1;
if(s.charAt(column) == 'B' && column == target_column-1)
{
count = 2;
break;
}
}
}
if(count == 2)
{
found = true;
continue;
}
if(count == 1)
continue;
int k = 0;
for(int i = 0; i<s.length(); i++)
{
if(s.charAt(i) == 'B' && i == target_column-1)
count = 1;
if(s.charAt(i) == 'W')
k++;
}
if(k == s.length())
white_count++;
}
if(white_count == rows)
System.out.println("-1");
else if(found)
System.out.println("0");
else if(count == 1)
System.out.println("1");
else
System.out.println("2");
}
Sc.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
3c68296ec8aafb90e2cf9f2f4df946fa
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class Not_Shading {
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
int test_cases = Sc.nextInt();
while(test_cases-- != 0)
{
int rows = Sc.nextInt();
int columns = Sc.nextInt();
int target_row = Sc.nextInt();
int target_column = Sc.nextInt();
int count = 0;
boolean found = false;
int white_count = 0;
for(int row = 1; row <= rows; row++)
{
int k = 0;
String s = Sc.next();
if(row == target_row)
{
for(int column = 0; column < columns ; column++)
{
if(s.charAt(column) == 'B')
count = 1;
if(s.charAt(column) == 'B' && column == target_column-1)
{
count = 2;
break;
}
}
}
if(count == 2)
{
found = true;
continue;
}
for(int i = 0; i<s.length(); i++)
{
if(s.charAt(i) == 'B' && i == target_column-1 && count != 2)
count = 1;
if(s.charAt(i) == 'W')
k++;
}
if(k == s.length())
white_count++;
}
if(white_count == rows)
System.out.println("-1");
else if(found)
System.out.println("0");
else if(count == 1)
System.out.println("1");
else
System.out.println("2");
}
Sc.close();
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
09c6f4b1bec02dc25e07e2bd2d640a03
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
//nmrc
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
for (int q = 1; q <= t; q++) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
sc.nextLine();
List<int[]> b = new ArrayList<int[]>();
Set<Integer> row = new HashSet<Integer>();
Set<Integer> column = new HashSet<Integer>();
for (int i = 1; i <= n; i++) {
String d = sc.next();
for (int j = 1; j <= m; j++) {
char dj = d.charAt(j - 1);
if (dj == ('B')) {
row.add(i);
column.add(j);
int[] vec = new int[2];
vec[0] = i;
vec[1] = j;
b.add(vec);
}
}
sc.nextLine();
}
if (b.isEmpty())
System.out.println(-1);
else {
int flag = 2;
for (int[] i : b) {
if (i[0] == r && i[1] == c) {
flag = 0;
break;
}
if (i[0] == r || i[1] == c)
flag = 1;
}
System.out.println(flag);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
1cd471bf5fb9dcf32260f84fd0d3bd70
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0) {
int n = s.nextInt();
int m = s.nextInt();
int r = s.nextInt();
int c = s.nextInt();
char[][] arr = new char[n+1][m+1];
HashMap<Integer,Integer> mapR = new HashMap<>();
HashMap<Integer,Integer> mapC = new HashMap<>();
for(int i = 1;i<=n;i++) {
String st = s.next();
for(int j = 1;j<=m;j++) {
arr[i][j] = st.charAt(j-1);
if(arr[i][j]=='B') {
mapR.put(i,1);
mapC.put(j,1);
}
}
}
int ans = solve(arr,mapR,mapC,r,c);
System.out.println(ans);
}
}
private static int solve(char[][] arr, HashMap<Integer, Integer> mapR, HashMap<Integer, Integer> mapC, int r,
int c) {
if(mapR.size()==0 && mapC.size()==0) {
return -1;
}
else if(arr[r][c]=='B') {
return 0;
}
else if(mapR.containsKey(r)==true || mapC.containsKey(c)==true) {
return 1;
}
else {
return 2;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
28937d338ccf1085194f65f11d5f1292
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0; i < t; i++) {
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt() - 1;
int c = sc.nextInt() - 1;
sc.nextLine();
boolean impossible = true;
char[][] mat = new char[n][m];
for(int j = 0; j < n; j++) {
String line = sc.nextLine();
for(int k = 0; k < m; k++) {
if(line.charAt(k) == 'B') impossible = false;
mat[j][k] = line.charAt(k);
}
}
if(impossible) {
System.out.println(-1);
continue;
}
if(mat[r][c] == 'B') {
System.out.println(0);
continue;
}
boolean not = false;
for(int j = 0; j < n; j++) {
if(mat[j][c] == 'B') {
System.out.println(1);
not = true;
break;
}
}
if(!not) {
for(int j = 0; j < m; j++) {
if(mat[r][j] == 'B') {
System.out.println(1);
not = true;
break;
}
}
}
if(!not) System.out.println(2);
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
da0bbc33084c8b2d2a3c3221cecc81c2
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class Main{
public static PrintWriter out;
public static FastReader in;
public static void main(String[] args) {
try {
in=new FastReader();
out = new PrintWriter(new BufferedOutputStream(System.out));
int testCases=in.nextInt();
while(testCases-- > 0){
int n = in.nextInt();
int m = in.nextInt();
int r = in.nextInt();
int c = in.nextInt();
char[][] a = new char[n][m];
int x =-1,y=-1,res=0,count=0;
boolean temp=false;
for(int i=0;i<n;i++)
{
String s=in.next();
for(int j=0;j<m;j++)
{
a[i][j] =s.charAt(j);
if(a[i][j]=='B')
count++;
}
}
if(count>0)
{
if(a[r-1][c-1]=='B')
{
System.out.println("0");
}
else
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='B')
{
if(i==(r-1)||j==(c-1))
{
temp=true;
break;
}
}
}
if(temp==true)
break;
}
if(temp==true)
System.out.println("1");
else
System.out.println("2");
}
}
else
{
System.out.println("-1");
}
}
out.close();
} catch (Exception e) {
out.println("Exception");
return;
}
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
1b424b14dfeccb1f1a7f226adbbe1454
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
//package practice;
import java.io.*;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
FastReader fr=new FastReader();
int t=fr.nextInt();
while(t-->0) {
int n=fr.nextInt();
int m=fr.nextInt();
int r=fr.nextInt();
int c=fr.nextInt();
int min=Integer.MAX_VALUE;
boolean f=false;
for(int i=1;i<=n;i++) {
String str=fr.next();
for(int j=1;j<=m;j++) {
if((i==r && j==c) && str.charAt(j-1)=='B') {
min=Math.min(min, 0);
}
else if ((i==r || j==c) && str.charAt(j-1)=='B') {
min=Math.min(min, 1);
}
else if(str.charAt(j-1)=='B')min=Math.min(min, 2);
// System.out.println(s[i][j]);
}
}
if(min!=Integer.MAX_VALUE){
System.out.println(min);
}
else {
System.out.println(-1);
}
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
03aa6ec7796394a55304856795318e71
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*
* To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other
*/
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static boolean alreadyBlack(List<String> cells, int r, int c) {
return cells.get(r).charAt(c) == 'B';
}
public static boolean allWhite(List<String> cells) {
return !cells.stream().anyMatch(s -> s.contains("B"));
}
public static boolean rowOrColHasBlack(List<String> cells, int r, int c) {
for(int i = 0; i < cells.size(); i++) {
if(cells.get(i).charAt(c) == 'B') {
return true;
}
}
return cells.get(r).contains("B");
}
public static void main(String[] args) {
FastReader fr = new FastReader();
int test = fr.nextInt();
while(test > 0) {
int n = fr.nextInt(), m = fr.nextInt(), r = fr.nextInt() - 1, c = fr.nextInt() - 1;
List<String> cells = new ArrayList<>(n);
for(int i = 0; i < n; i++) {
cells.add(fr.nextLine());
}
if(alreadyBlack(cells, r, c)) {
System.out.println(0);
} else if (allWhite(cells)) {
System.out.println(-1);
} else if(rowOrColHasBlack(cells, r, c)) {
System.out.println(1);
} else {
System.out.println(2);
}
test--;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
4f885037f07bd46a4e8f56bcd9fe7ad1
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*
* To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other
*/
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static boolean alreadyBlack(List<String> cells, int r, int c) {
return cells.get(r).charAt(c) == 'B';
}
public static boolean allWhite(List<String> cells) {
return !cells.stream().anyMatch(s -> s.contains("B"));
}
public static boolean rowOrColHasBlack(List<String> cells, int r, int c) {
for(char ch: cells.get(r).toCharArray()) {
if(ch == 'B') {
return true;
}
}
for(int i = 0; i < cells.size(); i++) {
if(cells.get(i).charAt(c) == 'B') {
return true;
}
}
return false;
}
public static void main(String[] args) {
FastReader fr = new FastReader();
int test = fr.nextInt();
while(test > 0) {
int n = fr.nextInt(), m = fr.nextInt(), r = fr.nextInt() - 1, c = fr.nextInt() - 1;
List<String> cells = new ArrayList<>(n);
for(int i = 0; i < n; i++) {
cells.add(fr.nextLine());
}
if(alreadyBlack(cells, r, c)) {
System.out.println(0);
} else if (allWhite(cells)) {
System.out.println(-1);
} else if(rowOrColHasBlack(cells, r, c)) {
System.out.println(1);
} else {
System.out.println(2);
}
test--;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
327e9cf46bca9749ca93a5ab3df5db85
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*
* To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other
*/
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static boolean alreadyBlack(List<String> cells, int r, int c) {
return cells.get(r).charAt(c) == 'B';
}
public static boolean allWhite(List<String> cells) {
for(String row: cells) {
for(char c: row.toCharArray()) {
if(c == 'B') {
return false;
}
}
}
return true;
}
public static boolean rowOrColHasBlack(List<String> cells, int r, int c) {
for(char ch: cells.get(r).toCharArray()) {
if(ch == 'B') {
return true;
}
}
for(int i = 0; i < cells.size(); i++) {
if(cells.get(i).charAt(c) == 'B') {
return true;
}
}
return false;
}
public static void main(String[] args) {
FastReader fr = new FastReader();
int test = fr.nextInt();
while(test > 0) {
int n = fr.nextInt(), m = fr.nextInt(), r = fr.nextInt() - 1, c = fr.nextInt() - 1;
List<String> cells = new ArrayList<>(n);
for(int i = 0; i < n; i++) {
cells.add(fr.nextLine());
}
if(alreadyBlack(cells, r, c)) {
System.out.println(0);
} else if (allWhite(cells)) {
System.out.println(-1);
} else if(rowOrColHasBlack(cells, r, c)) {
System.out.println(1);
} else {
System.out.println(2);
}
test--;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
| |
PASSED
|
7e386469eea0574c18406d260b23443a
|
train_109.jsonl
|
1642257300
|
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
|
256 megabytes
|
import java.util.Scanner;
public class shading {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
try{
int t = scn.nextInt();
while(t-- > 0){
int n = scn.nextInt();
int m = scn.nextInt();
int r = scn.nextInt();
int c = scn.nextInt();
char arr[][] = new char[n][m];
for (int i = 0; i < n; i++)
{
String str = scn.next();
for (int j = 0; j < m; j++)
{
arr[i][j] = str.charAt(j);
}
}
int countB = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(arr[i][j] == 'B'){
countB++;
}
}
}
if(countB == 0){
System.out.println("-1");
}
else if(arr[r-1][c-1] == 'B'){
System.out.println("0");
}
else{
boolean flag = false;
for (int i = 0; i < m; i++) {
if (arr[r - 1][i] == 'B' && i != c - 1) {
System.out.println("1");
flag = true;
break;
}
}
if(flag == false) {
for (int i = 0; i < n; i++) {
if (arr[i][c - 1] == 'B' && i != r-1) {
System.out.println("1");
flag = true;
break;
}
}
}
if(flag == false){
System.out.println("2");
}
}
}
}
catch (Exception e){
return;
}
}
}
|
Java
|
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
|
1 second
|
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
|
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation"
] |
4ca13794471831953f2737ca9d4ba853
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
| 800
|
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.