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
89afb00e5be07f28b33247ddf288e0c3
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.codeforces.Round766; import java.io.*; public class pr01 { // Consistency will always give you great Result // Don't Confuse Always make things simple // Don't afraid after seeing the problem instead dive deep into it to get better understanding //Experience is the name of the game // You won't fail until you stop trying....... // you can solve one problem by many approaches. when you stuck you are going to learn something new okk // Everything is easy. you feel its hard because of you don't know, and you not understand it very well. //// How to Improve Your problem-solving skill ??( By practise ). ***simple /// ==>> Solve problems slightly above of your level (to know some logic and how to approach cp problems) // Otherwise You will stay as it is okk. Learn from other code as well. ///////////////////////////////////////////////////////////////////////// /// How to Solve Problem in CP ? (you need to come up with brainstorm) ( if you feel problem is hard then your approach is wrong ) // ==>> Step01 :- Understanding problem statement clearly. Then Only Move Forward (Because Everything is mentioned in problem statement). // Step02 :- Think a lot about Solution. if you are confident that your solution might be correct Then only Move Forward // Step03 :- First think of brute force then move to optimal approach // Step04 :- Finally Code ( there is no any sense to code. if you not follow about steps okk) /////////////////////////////////////////////////////////////////////////// public static void main(String[] args)throws IOException { Reader scan=new Reader(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t=scan.nextInt(); while (t-->0) { int n=scan.nextInt(); int m=scan.nextInt(); int r=scan.nextInt(); int c=scan.nextInt(); char[][] arr=new char[n][m]; boolean flag=true; for (int i = 0; i < n; i++) { String s=scan.next(); for (int j = 0; j < s.length(); j++) { arr[i][j]=s.charAt(j); if(arr[i][j]=='B'){ flag=false; } } } int ans=2; if(flag){ ans=-1; } else { //check is it already black if(arr[r-1][c-1]=='B'){ ans=0; } else { //check is black contain in its row and column //check in row for (int i = 0; i < m; i++) { if(arr[r-1][i]=='B'){ ans=1; } } //check in column for (int i = 0; i < n; i++) { if (arr[i][c - 1] == 'B') { ans = 1; } } } } bw.write(ans+""); bw.newLine(); bw.flush(); } } //FAST READER static class Reader { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public Reader() { in = new BufferedInputStream(System.in, BS); } public Reader(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ 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 long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+(cur < 0 ? -1*nextLong()/num : nextLong()/num); } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } } }
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
f18865e282d4e93dd9c7e34ad5524eed
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 Solution{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0) { int m=sc.nextInt(); int n=sc.nextInt(); int r=sc.nextInt(); int c=sc.nextInt(); char[][] arr=new char[m][n]; int flag=0; for(int i=0;i<m;i++) { String str=sc.next(); for(int j=0;j<n;j++) { arr[i][j]=str.charAt(j); if(arr[i][j]=='B') flag+=1; } } if(flag==0) System.out.println("-1"); else { int i=0,j=0; while(i<m) { if(arr[i][c-1]=='B') break; i+=1; } while(j<n) { if(arr[r-1][j]=='B') break; j+=1; } if(arr[r-1][c-1]=='B') System.out.println("0"); else if(i!=m || j!=n) 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
62b6c787559464ba3175f0c43312313d
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 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(); r -= 1; c -= 1; char[][] ar = new char[n][m]; for (int i = 0; i < n; i++){ ar[i] = s.next().toCharArray(); } if (ar[r][c] == 'B'){ System.out.println("0"); continue; } boolean white = true; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ if (ar[i][j] == 'B') white = false; } } if (white){ System.out.println("-1"); continue; } boolean black = false; for (int i = 0; i < n; i++){ if (ar[i][c] == 'B') black = true; } for (int i = 0; i < m; i++){ if (ar[r][i] == 'B') black = true; } if (black) { 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
7c7cbafd6b8cdf10839be3437f289bb1
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 Not_shading { public static void main(String[] args) { int num, n, m, r, c, cnt; MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out), true); ArrayList<String> arr = new ArrayList<String>(); boolean one; num = sc.nextInt(); for(int i=0;i<num;i++) { n = sc.nextInt(); m = sc.nextInt(); r = sc.nextInt(); c = sc.nextInt(); r--; c--; one = false; cnt = 0; for(int j=0;j<n;j++) { arr.add(sc.nextLine()); } if(arr.get(r).charAt(c) == 'B') out.println(0); else { for(int j=0;j<n;j++) { for(int k=0;k<m;k++) { if(arr.get(j).charAt(k) == 'B') { if((j == r || k == c)) { one = true; break; } else cnt++; } } if(one == true) break; } if(one == true) out.println(1); else if(cnt == 0) out.println(-1); else out.println(2); } arr.clear(); } } } 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; } }
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
c7abc1659fd7c7e186b18b612da9e5c4
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.Scanner; public class A { public static void main(String[] args) { Scanner sr = new Scanner(System.in); int t = sr.nextInt(); while(t!=0){ t--; int n,m,x,y; n=sr.nextInt(); m=sr.nextInt(); x=sr.nextInt(); y=sr.nextInt(); x--; y--; String a[]=new String[n]; for(int i=0;i<n;i++){ a[i]=sr.next(); } if(a[x].charAt(y)=='B'){ System.out.println(0); continue; } boolean flag=false; for(int i=0;i<n;i++){ if(a[i].charAt(y)=='B'){ flag=true; break; } } for(int j=0;j<m;j++){ if(a[x].charAt(j)=='B'){ flag=true; break; } } if(flag) { System.out.println(1); continue; } int i; for(i=0;i<n;i++){ int j; for(j=0;j<m;j++){ if(a[i].charAt(j)=='B'){ System.out.println(2); break; } } if(j!=m) break;; } if(i==n) 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
cb29e7866d42c0f3d73c01ce7790e171
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 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(); String[] arr = new String[n]; boolean noAns = true; for (int i = 0; i < n; i++) { arr[i] = sc.next(); if (arr[i].contains("B")) { noAns = false; } } r--; c--; if (noAns) { System.out.println("-1"); continue; } if (arr[r].charAt(c) == 'B') { System.out.println("0"); continue; } boolean found = false; for (int i = 0; i < n; i++) { if (arr[i].charAt(c) == 'B') { found = true; break; } } if (found) { System.out.println("1"); continue; } for (int i = 0; i < m; i++) { if (arr[r].charAt(i) == 'B') { found = true; break; } } System.out.println(found ? "1" : "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
3f1b309d6f5b2dd9c49dbb8f3cb9ce85
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 = Integer.parseInt(scn.next()); for(int cases = 0; cases < t; cases++) { int n = Integer.parseInt(scn.next()); int m = Integer.parseInt(scn.next()); int r = Integer.parseInt(scn.next()); int c = Integer.parseInt(scn.next()); String[] arr = new String[n]; int count = 0; boolean found = false; r--; c--; int ans = -1; for(int i = 0; i<n; i++){ arr[i] = scn.next(); for(int j = 0; j<m; j++){ if (arr[i].charAt(j) == 'B'){ count++; } } } if (count == 0){ System.out.println(-1); } else if (arr[r].charAt(c) == 'B'){ System.out.println(0); } else{ boolean flag = false; for(int i = 0; i<n; i++){ if (arr[i].charAt(c) == 'B'){ System.out.println(1); flag = true; break; } } if (!flag){ for(int j = 0; j<m; j++){ if (arr[r].charAt(j) == 'B'){ System.out.println(1); flag = true; break; } } } if(!flag){ 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
0753dfdb3d96f94cca75272fa79977d5
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.Comparator; import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int yCol = sc.nextInt(); int xCol = sc.nextInt(); int y = sc.nextInt(); int x = sc.nextInt(); sc.nextLine(); ArrayList<String> strs = new ArrayList<String>(); Integer fl = -1; for (int i = 0; i < yCol; i++){ String str = sc.nextLine(); strs.add(str); char[] result = str.toCharArray(); if (fl != 0){ if(i == y-1){ if (result[x-1] == 'B'){ fl = 0; //break; } else if (str.contains("B")){ fl = 1; //break; } } if (fl != 1 && fl != 0){ if (str.contains("B")){ if(result[x-1] == 'B'){ fl = 1; //break; } else { fl = 2; } } } } } System.out.println(fl); } } }
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
e2fa199f96deb67441a8cf0b40ff56bb
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.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; /** * * @author eslam */ public class JavaApplication1005 { 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 input = new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t = input.nextInt(); loop: while (t-- > 0) { int n = input.nextInt(); int m = input.nextInt(); int y = input.nextInt(); int x = input.nextInt(); boolean ca = false; String w[] = new String[n]; for (int i = 0; i < n; i++) { w[i] = input.next(); for (int j = 0; j < m; j++) { if (w[i].charAt(j) == 'B') { ca = true; break; } } } if (!ca) { System.out.println("-1"); } else { if (w[y - 1].charAt(x - 1) == 'B') { System.out.println(0); } else { for (int i = 0; i < n; i++) { if (w[i].charAt(x-1) == 'B') { System.out.println(1); continue loop; } } for (int i = 0; i < m; i++) { if (w[y - 1].charAt(i) == 'B') { System.out.println(1); continue loop; } } 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
aa2380ab5127af89a41e6ebeeb6a2053
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 cf; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.*; import java.util.StringTokenizer; public class cftt { 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()); } } public static void main(String[] args) { FastScanner r = new FastScanner(); PrintWriter out=new PrintWriter(System.out); // System.out.println("input"); int tc=r.nextInt(); while(tc-->0) { int rr=r.nextInt(); int cc=r.nextInt(); int fr=r.nextInt()-1; int fc=r.nextInt()-1; int bl=0,wh=0; char ch[][]=new char[rr][cc]; for(int i=0;i<rr; i++) { String s=r.next(); for(int j=0; j<s.length(); j++) { ch[i][j]=s.charAt(j); if(ch[i][j]=='W')wh++; else bl++; } } if(bl==0)System.out.println("-1"); else if(ch[fr][fc]=='B')System.out.println("0"); else { boolean f=false; for(int i=0; i<cc; i++) { if(ch[fr][i]=='B') { f=true; break; } } for(int i=0; i<rr; i++) { if(ch[i][fc]=='B') { f=true; break; } } if(f)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
773ba13bd3b678c3867930d78ecc99c3
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 scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ int n = scn.nextInt(); int m = scn.nextInt(); int r = scn.nextInt(); int c = scn.nextInt(); int[] row = new int[n]; int[] col = new int[m]; scn.nextLine(); boolean ansZero = false, zero = false; for(int i=0;i<n;i++){ String s = scn.nextLine(); for(int j=0;j<m;j++){ char p = s.charAt(j); if(p=='B'){ zero = true; if(i==r-1&&j==c-1){ System.out.println(0); ansZero = true; break; } row[i]++; col[j]++; } } // if(ansZero) break; } if(!ansZero){ if(row[r-1]>0||col[c-1]>0){ System.out.println(1); }else if(zero){ 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
32b283d05f52476788a2ec7c4ea5c278
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.*; // public class A_Not_Shading { public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ int n = f.nextInt(); int m = f.nextInt(); int r = f.nextInt()-1; int c = f.nextInt()-1; HashSet<Integer> x = new HashSet<>(); HashSet<Integer> y = new HashSet<>(); char arr[][] = new char[n][m]; for(int i = 0; i < n; i++) { String s = f.nextLine(); for(int j = 0; j < m; j++) { if(s.charAt(j) == 'B') { x.add(i); y.add(j); } arr[i][j] = s.charAt(j); } } if(x.size() == 0) { out.println(-1); } else if(arr[r][c] == 'B') { out.println(0); } else if(x.contains(r) == false && y.contains(c) == false) { out.println(2); } else { out.println(1); } } out.close(); } public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } public static int Gcd(int a, int b) { int dividend = a > b ? a : b; int divisor = a < b ? a : b; while(divisor > 0) { int reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } public static int lcm1(int a, int b) { int lcm = Gcd(a, b); int hcf = (a * b) / lcm; return hcf; } 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()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */
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
89a0debbc29234f160833b771cb92a64
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.*; public class A_Not_Shading { public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ int n = f.nextInt(); int m = f.nextInt(); int r = f.nextInt()-1; int c = f.nextInt()-1; HashSet<Integer> x = new HashSet<>(); HashSet<Integer> y = new HashSet<>(); char arr[][] = new char[n][m]; for(int i = 0; i < n; i++) { String s = f.nextLine(); for(int j = 0; j < m; j++) { if(s.charAt(j) == 'B') { x.add(i); y.add(j); } arr[i][j] = s.charAt(j); } } if(x.size() == 0) { out.println(-1); } else if(arr[r][c] == 'B') { out.println(0); } else if(x.contains(r) == false && y.contains(c) == false) { out.println(2); } else { out.println(1); } } out.close(); } public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } public static int Gcd(int a, int b) { int dividend = a > b ? a : b; int divisor = a < b ? a : b; while(divisor > 0) { int reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } public static int lcm1(int a, int b) { int lcm = Gcd(a, b); int hcf = (a * b) / lcm; return hcf; } 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()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */
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
2c8f1fa1c188d22d304681bda8caba1a
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 Codeforces { final static int mod = 1000000007; final static String yes = "YES"; final static String no = "NO"; public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); outer: while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int t1 = sc.nextInt(); int t2 = sc.nextInt(); boolean check_col = false; boolean check_row = false; boolean contains_black = false; char[][] c = new char[n + 1][m + 1]; for (int i = 1; i <= n; i++) { String s = sc.next(); for (int j = 1; j <= m; j++) { c[i][j] = s.charAt(j - 1); if (c[i][j] == 'B') { if (!contains_black) contains_black = true; if (i == t1 && !check_row) { check_col = true; } if (j == t2 && !check_col) { check_col = true; } } } } if (!contains_black) { System.out.println(-1); continue outer; } if (c[t1][t2] == 'B') { System.out.println(0); continue outer; } System.out.println(check_col || check_row ? 1 : 2); } } static void sortLong(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sortInt(Integer[] a) // check for int { 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); } public static boolean isSorted(int[] nums, int n) { for (int i = 1; i < n; i++) { if (nums[i] < nums[i - 1]) return false; } return true; } public static boolean isPalindrome(String s) { int i = 0, j = s.length() - 1; while (i <= j) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } public static void sortByColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); } public static void backtrack(String[] letters, int index, String digits, StringBuilder build, List<String> result) { if (build.length() >= digits.length()) { result.add(build.toString()); return; } char[] key = letters[digits.charAt(index) - '2'].toCharArray(); for (int j = 0; j < key.length; j++) { build.append(key[j]); backtrack(letters, index + 1, digits, build, result); build.deleteCharAt(build.length() - 1); } } public static String get(String s, int k) { int n = s.length(); int rep = k % n == 0 ? k / n : k / n + 1; s = s.repeat(rep); return s.substring(0, k); } public static int diglen(Long y) { int a = 0; while (y != 0L) { y /= 10; a++; } return a; } static class FastReader { BufferedReader br; StringTokenizer st; // StringTokenizer() is used to read long strings 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 class Pair implements Comparable<Pair> { public final int index; public final int value; public Pair(int index, int value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { // multiplied to -1 as the author need descending sort order return -1 * Integer.valueOf(this.value).compareTo(other.value); } } static String reverseString(String str) { StringBuilder input = new StringBuilder(); return input.append(str).reverse().toString(); } static void printArray(int[] nums) { for (int i = 0; i < nums.length; i++) { System.out.println(nums[i] + " "); } } static long factorial(int n, int b) { if (n == b) return 1; return n * factorial(n - 1, b); } static int lcm(int ch, int b) { return ch * b / gcd(ch, b); } static int gcd(int ch, int b) { return b == 0 ? ch : gcd(b, ch % b); } static double ceil(double n, double k) { return Math.ceil(n / k); } static int sqrt(double n) { return (int) Math.sqrt(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 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
89e00b01fcbd22a3c1194b44828b032b
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 Codeforces { final static int mod = 1000000007; final static String yes = "YES"; final static String no = "NO"; public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); outer: while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int t1 = sc.nextInt(); int t2 = sc.nextInt(); boolean check_col = false; boolean check_row = false; boolean contains_black = false; char[][] c = new char[n + 1][m + 1]; for (int i = 1; i <= n; i++) { String s = sc.next(); for (int j = 1; j <= m; j++) { c[i][j] = s.charAt(j - 1); if (c[i][j] == 'B') { if (!contains_black) contains_black = true; if (i == t1 && !check_row) { check_col = true; } if (j == t2 && !check_col) { check_col = true; } } } } if (!contains_black) { System.out.println(-1); continue outer; } if (c[t1][t2] == 'B') { System.out.println(0); continue outer; } System.out.println(check_col || check_row ? 1 : 2); } } static void sortLong(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sortInt(Integer[] a) // check for int { 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); } public static boolean isSorted(int[] nums, int n) { for (int i = 1; i < n; i++) { if (nums[i] < nums[i - 1]) return false; } return true; } public static boolean isPalindrome(String s) { int i = 0, j = s.length() - 1; while (i <= j) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } public static void sortByColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); } public static void backtrack(String[] letters, int index, String digits, StringBuilder build, List<String> result) { if (build.length() >= digits.length()) { result.add(build.toString()); return; } char[] key = letters[digits.charAt(index) - '2'].toCharArray(); for (int j = 0; j < key.length; j++) { build.append(key[j]); backtrack(letters, index + 1, digits, build, result); build.deleteCharAt(build.length() - 1); } } public static String get(String s, int k) { int n = s.length(); int rep = k % n == 0 ? k / n : k / n + 1; s = s.repeat(rep); return s.substring(0, k); } public static int diglen(Long y) { int a = 0; while (y != 0L) { y /= 10; a++; } return a; } static class FastReader { BufferedReader br; StringTokenizer st; // StringTokenizer() is used to read long strings 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 class Pair implements Comparable<Pair> { public final int index; public final int value; public Pair(int index, int value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { // multiplied to -1 as the author need descending sort order return -1 * Integer.valueOf(this.value).compareTo(other.value); } } static String reverseString(String str) { StringBuilder input = new StringBuilder(); return input.append(str).reverse().toString(); } static void printArray(int[] nums) { for (int i = 0; i < nums.length; i++) { System.out.println(nums[i] + " "); } } static long factorial(int n, int b) { if (n == b) return 1; return n * factorial(n - 1, b); } static int lcm(int ch, int b) { return ch * b / gcd(ch, b); } static int gcd(int ch, int b) { return b == 0 ? ch : gcd(b, ch % b); } static double ceil(double n, double k) { return Math.ceil(n / k); } static int sqrt(double n) { return (int) Math.sqrt(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 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
071f6c143ba8a65e80968f3f53b6142f
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 yoyo{ 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; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static boolean bpresinr(String x) { for (int i = 0; i < x.length(); i++) { if (x.charAt(i)=='B') { return true; } } return false; } public static boolean bpresinc(String w[],int m) { for (int i = 0; i < w.length; i++) { if (w[i].charAt(m-1)=='B') { return true; } } return false; } public static boolean bpresornot(String h[]) { for (int i = 0; i < h.length; i++) { for (int j = 0; j < h[i].length(); j++) { if(h[i].charAt(j)=='B'){ return true; } } } return false; } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); int t=in.nextInt(); while (t-->0) { int n=in.nextInt(); int m=in.nextInt(); int r=in.nextInt(); int c=in.nextInt(); String[] a=new String[n]; for (int i = 0; i < n; i++) { a[i]=in.next(); } if (a[r-1].charAt(c-1)=='B') { System.out.println(0); } else { if(bpresinr(a[r-1])||bpresinc(a,c)){ System.out.println(1); } else { if(bpresornot(a)) System.out.println(2); else System.out.println(-1); } } } out.close(); } 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
PASSED
26b388e84f28253abc5940accd9a5eb9
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 { static Scanner sc = new Scanner (System.in); public static void main (String[]args) { int t = sc.nextInt(); for(int i = 0 ; i < t ;i++){ int r = sc.nextInt(); int c = sc.nextInt(); int y =sc.nextInt(); int x =sc.nextInt(); String[] b = new String[r]; for(int j = 0 ; j < r ; j++){ b[j]=sc.next(); } System.out.println(count(b,y,x)); } } static int count(String[]bo , int r,int c){ if(bo[r-1].charAt(c-1)=='B') return 0; //check line for(int i = 0; i< bo[0].length();i++){ if(bo[r-1].charAt(i)=='B') return 1; } for(int i = 0; i< bo.length;i++){ if(bo[i].charAt(c-1)=='B') return 1; } for(int i = 0 ; i < bo.length;i++){ for(int j = 0 ;j<bo[i].length();j++){ if(bo[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 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
386935df0d5bde7cfc39475d31d6a808
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 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][m]; String ar[] = new String[n]; for (int i = 0; i <n ; i++) { ar[i] = s.next(); } for (int i = 0; i <n ; i++) { for(int j = 0; j <m; j++) { arr[i][j] = ar[i].charAt(j); } } if(arr[r-1][c-1] == 'B') { System.out.println(0); continue; } boolean isBlack = false; for (int i = 0; i <n ; i++) { for(int j = 0; j <m; j++) { if(arr[i][j] =='B') { isBlack = true; break; } } if(isBlack) { break; } } if(!isBlack) { System.out.println(-1); continue; } boolean row = false; boolean col = false; for(int i = 0; i<m; i++) { if(arr[r-1][i] == 'B') { row = true; } if(row) { break; } } for(int i = 0; i<n; i++) { if(arr[i][c-1] == 'B') { col = true; } if(col) { break; } } if(row || col) { 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 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
0bc482cacb7366b323109a968601f5bf
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.LinkedList; import java.util.Queue; import java.util.Scanner; import java.awt.Point; public class Main { public static void solve(Scanner sc) { int height, width, row, col; height = sc.nextInt(); width = sc.nextInt(); row = sc.nextInt(); col = sc.nextInt(); String dummy = sc.nextLine(); Int2DArray board = new Int2DArray(height, width); Queue<Point> q = new LinkedList<>(); for(int y = 1; y <= height; ++y) { String line = sc.nextLine(); for(int x = 1; x <= width; ++x) { if (line.charAt(x-1) == 'B') { board.set(y, x, 0); q.add(new Point(x, y)); } else { board.set(y, x, Integer.MAX_VALUE); } } } while(!q.isEmpty() && board.get(row, col) == Integer.MAX_VALUE) { Point p = q.remove(); int current_value = board.get(p.y, p.x); for(int x = 1; x <= width; ++x) { if (board.get(p.y, x) > current_value + 1) { board.set(p.y, x, current_value + 1); q.add(new Point(x, p.y)); } } for(int y = 1; y <= height; ++y) { if (board.get(y, p.x) > current_value + 1) { board.set(y, p.x, current_value + 1); q.add(new Point(p.x, y)); } } } if (board.get(row, col) == Integer.MAX_VALUE) { System.out.println("-1"); } else { System.out.println(board.get(row, col)); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; ++i) { solve(sc); } sc.close(); } } class Int2DArray { private int[][] data; public Int2DArray(int height, int width) { data = new int[height][width]; } public int get(int y, int x) { return data[y-1][x-1]; } public void set(int y, int x, int value) { data[y-1][x-1] = value; } }
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
1a3248a5289966194955665eb8e07dfb
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 Trial; import java.util.*; import java.io.*; public class codeforceA1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int a=1; int t = in.nextInt(); for(int i=0;i<t;i++) { int n=in.nextInt(); int m=in.nextInt(); int r=in.nextInt(); int c=in.nextInt(); int b=0; char[][] input=new char [n][m]; for(int j=0;j<n;j++) { String s=in.next(); for( int k=0;k<m;k++) { input[j][k]=s.charAt(k); if(input[j][k]=='B') { b++; } } } if( b==0)System.out.println(-1); else if(input[r-1][c-1]=='B')System.out.println(0); else { boolean check=false; for(int j=0;j<n;j++) { if(input[j][c-1]=='B') { check=true; break; } } if(!check) { for(int j=0;j<m;j++) { if(input[r-1][j]=='B') { check=true; break; } } } if( check)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
e3b029584dc94c20577f2ad308262e04
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 Trial; import java.util.*; import java.io.*; public class codeforceA1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int t = in.nextInt(); for(int i=0;i<t;i++) { int n=in.nextInt(); int m=in.nextInt(); int r=in.nextInt(); int c=in.nextInt(); int b=0; char[][] input=new char [n][m]; for(int j=0;j<n;j++) { String s=in.next(); for( int k=0;k<m;k++) { input[j][k]=s.charAt(k); if(input[j][k]=='B') { b++; } } } if( b==0)System.out.println(-1); else if(input[r-1][c-1]=='B')System.out.println(0); else { boolean check=false; for(int j=0;j<n;j++) { if(input[j][c-1]=='B') { check=true; break; } } if(!check) { for(int j=0;j<m;j++) { if(input[r-1][j]=='B') { check=true; break; } } } if( check)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
e9c0672b093eeeeeeb357f7e1ff032f6
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 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(); while(t-->0){ int n=in.nextInt();int m=in.nextInt(); int r=in.nextInt();int c=in.nextInt(); char[][] arr=new char[n+1][m+1]; Set<Integer> br=new HashSet<>(); Set<Integer> bc=new HashSet<>(); for(int i=1;i<=n;i++){ String s=in.next(); for(int j=1;j<=m;j++){ arr[i][j]=s.charAt(j-1); if(arr[i][j]=='B'){ br.add(i);bc.add(j); } } System.out.println(); } if(br.size()==0 && bc.size()==0) System.out.println(-1); else if(br.contains(r) && bc.contains(c) && arr[r][c]=='B') System.out.println(0); else if(br.contains(r) || bc.contains(c)){ System.out.println(1); } else { System.out.println(2); } } } static boolean isPalindrome(String a){ int n=a.length(); for(int i=0;i<a.length();i++){ if(a.charAt(i)!=a.charAt(n-i-1)) return false; } return true; } static int lengthOfLIS(int[] nums) { int n=nums.length; int[] lisi = new int[n]; for(int i=0;i<n;i++) lisi[i]=1; for(int i=1;i<n;i++){ for(int j=0;j<i;j++){ if(nums[j]<=nums[i]){ lisi[i]=lisi[j]+1; } } } return lisi[n-1]; } // static int lengthOfLIS(int[] nums) // { // int[] tails = new int[nums.length]; // int size = 0; // for (int x : nums) { // int i = 0, j = size; // while (i != j) { // int m = (i + j) / 2; // if (tails[m] <= x) // i = m + 1; // else // j = m; // } // tails[i] = x; // if (i == size) ++size; // } // return size; // } static void swap(int[] arr,int a,int b){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } static int max(int a, int b) { if(a<b) return b; return a; } static int min(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 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 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 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
e15675ae20f18cd85b0e0990085f639a
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(); for (int i = 0; i < t; i++) { int m = scn.nextInt(); int n = scn.nextInt(); int r = scn.nextInt(); int c = scn.nextInt(); char[][]arr = new char[m][n]; int countb = 0 ; int rcountb = 0 ; int rcountc = 0 ; for (int j = 0; j < m; j++) { String str = scn.next(); for (int j2 = 0; j2 < n; j2++) { arr[j][j2] = str.charAt(j2); if(arr[j][j2] == 'B'){ countb++; } } } if(countb == 0){ System.out.println("-1"); } else if(arr[r-1][c-1] == 'B'){ System.out.println("0"); } else{ for (int j = 0; j < n; j++) { if(arr[r-1][j] == 'B'){ rcountb++; } } for (int j = 0; j < m; j++) { if(arr[j][c-1] == 'B'){ rcountc++; } } if(rcountb > 0 || rcountc > 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 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
853113ef6a42f1b79a11e99622072568
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.List; import java.util.Scanner; public class Solution3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 1; i <= t; i++) { int n = scan.nextInt(); int m = scan.nextInt(); int r = scan.nextInt() - 1; int c = scan.nextInt() - 1; boolean flag = false; List<String> x = new ArrayList<String>(); for (int j = 1; j <= n; j++) x.add(scan.next()); if (x.get(r).charAt(c) == 'B') System.out.println(0); else { for (int j = 0; j < n; j++) { for (int k = 0; k < m; k++) { if (x.get(j).charAt(k) == 'B') flag = true; } } if (!flag) System.out.println(-1); else { boolean flag_2 = false; for (int k = 0; k < m; k++) { if (x.get(r).charAt(k) == 'B') flag_2 = true; } for (int k = 0; k < n; k++) { if (x.get(k).charAt(c) == 'B') flag_2 = true; } if (flag_2) 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
fea42dd410cb2a87f9482ca39bcbde55
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 A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, m, r, c = 0; int t = in.nextInt(); for(int yu = 1; yu <= t; yu++) { n = in.nextInt(); m = in.nextInt(); r = in.nextInt(); c = in.nextInt(); int ans = 2; boolean kc = false; boolean kc2 = false; String asc; r--; c--; for(int i = 0 ; i < n; i++){ asc = in.next(); for(int j = 0 ;j < m; j++) { char ch = asc.charAt(j); if (ch == 'B') { kc2 = true; if (i == r && c == j) kc = true; else if (i == r) ans = 1; else if (j == c) ans = 1; } } } if(!kc2) { System.out.println("-1"); continue; } if(kc) System.out.println("0"); else if(ans == 1) 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
905f62c2f7b35164fc6d37e0c6680aed
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.Scanner; import static java.lang.Math.*; public class NotShading { static Scanner in; public static void main(String[] args) { in = new Scanner(System.in);// for online judge if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setOut(new PrintStream(new FileOutputStream("output.txt"))); in = new Scanner(new File("input.txt")); } catch (Exception e) { } } // Your Code Start Here int t = in.nextInt(); while(t-- > 0) { System.out.println(solve()); } in.close(); } static int solve() { int m = in.nextInt(), n = in.nextInt(); int r = in.nextInt()-1, c = in.nextInt()-1; char[][] arr = new char[m][n]; for(int i = 0; i < m; i++) arr[i] = in.next().toCharArray(); if(arr[r][c] == 'B') return 0; boolean sameRowOrColumn = false, anyRowOrColumn = false; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(arr[i][j] == 'B') { if(i == r || j == c) sameRowOrColumn = true; anyRowOrColumn = true; } } } if(sameRowOrColumn) return 1; else if(anyRowOrColumn) 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 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
4875a2641aab72499cf2b6e704b38368
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.Scanner; import static java.lang.Math.*; public class NotShading { static Scanner in; public static void main(String[] args) { in = new Scanner(System.in);// for online judge if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setOut(new PrintStream(new FileOutputStream("output.txt"))); in = new Scanner(new File("input.txt")); } catch (Exception e) { } } // Your Code Start Here int t = in.nextInt(); while(t-- > 0) { System.out.println(solve()); } in.close(); } static int solve() { int m = in.nextInt(), n = in.nextInt(); int r = in.nextInt(), c = in.nextInt(); char[][] arr = new char[m][n]; for(int i = 0; i < m; i++) arr[i] = in.next().toCharArray(); if(arr[r-1][c-1] == 'B') return 0; for(int i = 0; i < n; i++) if(arr[r-1][i] == 'B') return 1; for(int i = 0; i < m; i++) if(arr[i][c-1] == 'B') return 1; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) if(arr[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 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
4ff0d05c36deb1b7cc02538c195a5f00
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 input = new Scanner(System.in); int t = input.nextInt(); for(int a = 0; a < t; a++){ int n = input.nextInt(); int m = input.nextInt(); int r = input.nextInt()-1; int c = input.nextInt()-1; input.nextLine(); int[][] grid = new int[n][m]; // black = 0, white = 1 String line; char chr; for(int i = 0; i < n; i++){ line = input.nextLine(); for(int j = 0; j < m; j++) { chr = line.charAt(j); if(chr == 'B') grid[i][j] = 0; else grid[i][j] = 1; } } int output = 3; if(grid[r][c] == 0) output = 0; else{ for(int i = 0; i < m; i++){ if(grid[r][i] == 0){ output = 1; break; } for(int j = 0; j < n; j++){ if(grid[j][i] == 0){ //System.out.println(i+" "+j+" "+grid[j][i]); if(i == c) output = 1; else output = Math.min(output, 2); break; } } } } if(output == 3) output = -1; System.out.println(output); } } }
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
e1fe649c392e56cf964a47853d200f7c
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.Arrays; import java.util.InputMismatchException; import java.util.StringTokenizer; public class D { { MULTI_TEST = true; FILE_NAME = ""; NEED_FILE_IO = false; INF = (long) 1e18 + 1; } // fields void solve() { int n = ri(); int m = ri(); int column = ri() - 1, row = ri() - 1; char[][] a = new char[n][m]; for(int i = 0; i < n; i++) { a[i] = rs().toCharArray(); } if(a[column][row] == 'B') { out.println(0); return; } for(int i = 0; i < n; i++) { if(a[i][row] =='B') { out.println(1); return; } } for(int i = 0; i < m; i++) { if(a[column][i] == 'B') { out.println(1); return; } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(a[i][j] == 'B') { out.println(2); return; } } } out.println(-1); } public static void main(String[] args) { new D().run(); } //main @SuppressWarnings("unused") long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } @SuppressWarnings("unused") long lcm(long a, long b) { return a / gcd(a, b) * b; } @SuppressWarnings("unused") int gcd(int a, int b) { return (int) gcd((long)a, b); } @SuppressWarnings("unused") int lcm(int a, int b) { return (int) lcm(a, (long)b); } @SuppressWarnings("unused") int sqrtInt(int x) { return (int) sqrtLong(x); } @SuppressWarnings("unused") long sqrtLong(long x) { long root = (long) Math.sqrt(x); while (root * root > x) --root; while ((root + 1) * (root + 1) <= x) ++root; return root; } @SuppressWarnings("unused") long binpow(long a, long power) { return binpow(a, power, INF); } @SuppressWarnings("unused") long binpow(long a, long power, long modulo) { if(power == 0) return 1%modulo; if(power % 2 == 1) { long b = binpow(a, power - 1, modulo) % modulo; return ((a % modulo) * b) % modulo; } else { long b = binpow(a, power / 2, modulo) % modulo; return (b * b) % modulo; } } @SuppressWarnings("unused") long fastMod(String s1, long n2) { long num = 0; for (int i = 0; i < s1.length() - 1; i++) { num += Integer.parseInt(String.valueOf(s1.charAt(i))); num *= 10; if (num >= n2) { num = num % n2; } } return (num + Integer.parseInt(String.valueOf(s1.charAt(s1.length() - 1)))) % n2; } @SuppressWarnings("unused") long factorialMod(long n, long p) { long res = 1; while (n > 1) { res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p; for (int i = 2; i <= n % p; ++i) res = (res * i) % p; n /= p; } return res % p; } @SuppressWarnings("unused") boolean isPrime(int number) { for (int i = 2; i * i <= number; i++) { if (number % i == 0) { return false; } } return number > 1; } @SuppressWarnings("unused") boolean[] primes(int border) { boolean[] isPrimes = new boolean[border + 1]; Arrays.fill(isPrimes, true); isPrimes[0] = false; isPrimes[1] = false; for (int i = 2; i < border + 1; i++) { if (!isPrimes[i]) continue; for (int k = i * 2; k < border; k += i) { isPrimes[k] = false; } } return isPrimes; } //Number theory @SuppressWarnings("unused") void sort(int[] a) { int n = a.length; Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") void sort(long[] a) { int n = a.length; Long[] arr = new Long[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") int max(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; for (int j : a) { if (j > max) max = j; } return max; } @SuppressWarnings("unused") long max(long[] a) { int n = a.length; long max = Long.MIN_VALUE; for (long l : a) { if (l > max) max = l; } return max; } @SuppressWarnings("unused") int maxIndex(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int maxIndex(long[] a) { int n = a.length; long max = Long.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int min(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; for (int j : a) { if (j < min) min = j; } return min; } @SuppressWarnings("unused") int minIndex(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int minIndex(long[] a) { int n = a.length; long min = Long.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") long min(long[] a) { int n = a.length; long min = Long.MAX_VALUE; for (long l : a) { if (l < min) min = l; } return min; } @SuppressWarnings("unused") long sum(int[] a) { int n = a.length; long sum = 0; for (int j : a) { sum += j; } return sum; } @SuppressWarnings("unused") long sum(long[] a) { int n = a.length; long sum = 0; for (long l : a) { sum += l; } return sum; } //Arrays Operations @SuppressWarnings("unused") String readLine() { try { return in.readLine(); } catch (Exception e) { throw new InputMismatchException(); } } @SuppressWarnings("unused") String rs() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(readLine()); } return tok.nextToken(); } @SuppressWarnings("unused") int ri() { return Integer.parseInt(rs()); } @SuppressWarnings("unused") long rl() { return Long.parseLong(rs()); } @SuppressWarnings("unused") double rd() { return Double.parseDouble(rs()); } @SuppressWarnings("unused") int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri(); } return a; } @SuppressWarnings("unused") int[] riawd(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri() - 1; } return a; } @SuppressWarnings("unused") long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = rl(); } return a; } @SuppressWarnings("unused") private boolean yesNo(boolean yes, String yesString, String noString) { out.println(yes ? yesString : noString); return yes; } //fastIO void run() { try { long start = System.currentTimeMillis(); initIO(); if (MULTI_TEST) { long t = rl(); while (t-- > 0) { solve(); } } else { solve(); } out.close(); System.err.println("Time(ms) - " + (System.currentTimeMillis() - start)); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void initConsoleIO() { out = new PrintWriter(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } void initFileIO(String inName, String outName) throws FileNotFoundException { out = new PrintWriter(outName); in = new BufferedReader(new FileReader(inName)); } void initIO() throws FileNotFoundException { if (!FILE_NAME.isEmpty()) { initFileIO(FILE_NAME + ".in", FILE_NAME + ".out"); } else { if (NEED_FILE_IO && new File("input.txt").exists()) { initFileIO("input.txt", "output.txt"); } else { initConsoleIO(); } } tok = new StringTokenizer(""); } //initIO private final String FILE_NAME; private final boolean MULTI_TEST; private final boolean NEED_FILE_IO; private final long INF; BufferedReader in; PrintWriter out; StringTokenizer tok; //fields }
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
0a7420ce4af7e767d44ba607e716dfc7
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 funct { public static void main(String args[]) { Scanner sc = new Scanner(System.in); funct ob = new funct(); int T = sc.nextInt(); while (T > 0) { boolean possible = false; int m = sc.nextInt(), n = sc.nextInt(), r = sc.nextInt()-1, c = sc.nextInt()-1; String s; char a[][] = new char[m][n]; for (int i = 0; i < m; i++) { s= sc.next(); for (int j = 0; j < n; j++) { a[i][j] = s.charAt(j); if (a[i][j] == 'B')possible = true; } } if (possible) { if (a[r][c] == 'B') { System.out.println(0); } else if (ob.sameRow(r, a) || ob.sameColumn(c, a)) { System.out.println(1); } else { System.out.println(2); } } else { System.out.println(-1); } T--; } } boolean sameRow(int r, char a[][]) { for (int j = 0; j < a[r].length; j++) { if (a[r][j] == 'B') return true; } return false; } boolean sameColumn(int c, char a[][]) { for (int j = 0; j < a.length; j++) { if (a[j][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 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
eafe2e6e9672b3359e670629fb1e0e29
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 cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e6+5); while(tc-->0) { 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++) { mat[i]=sc.next().toCharArray(); } r--;c--; if(mat[r][c]=='B') { out.println(0); continue; } int ans=-1; boolean sameRowOrCol=false; for(int i=0;i<n;i++) { if(mat[i][c]=='B') { sameRowOrCol=true; break; } } for(int i=0;i<m;i++) { if(mat[r][i]=='B') { sameRowOrCol=true; break; } } if(sameRowOrCol) { out.println(1); continue; } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mat[i][j]=='B') { ans=2; break; } } if(ans!=-1) break; } out.println(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static class A implements Comparable<A>{ int a; int b; int c; int sum; public A(int a,int b,int c) { // TODO Auto-generated constructor stub this.a=a; this.b=b; this.c=c; this.sum=a+b+c; } @Override public int compareTo(cp.A o) { // TODO Auto-generated method stub return this.sum-o.sum; } } static boolean isPowerOfTwo(int n) { return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static int util(char a,char b) { int A=a-'0'; int B=b-'0'; return A+B; } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find first index >= y static int upper(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (h-l>1) { int mid = (l + h) / 2; if (arr.get(mid) <= x) l=mid+1; else { h=mid; } } if(arr.get(l)>x) { return l; } if(arr.get(h)>x) return h; return -1; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (h-l>1) { int mid = (l + h) / 2; if (arr.get(mid) < x) l=mid+1; else { h=mid; } } if(arr.get(l)>=x) { return l; } if(arr.get(h)>=x) return h; return -1; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long 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 ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.max(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.max(left, right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l<=h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid -1 ; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static int UpperBound(long a[], long x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y) { this.x=x; this.y=y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } 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 void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // static long modInverse(long a, long m) // { // long g = gcd(a, m); // // return power(a, m - 2, m); // // } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } 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') break; 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(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
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
6e71da0ede6052067998474a1af49d0d
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
// Imports import java.io.*; import java.util.*; public class A1627 { public static void main(String[] args) throws IOException { // Test once done BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(f.readLine()); for(int i = 0; i < T; i++) { // Do stuff StringTokenizer st = new StringTokenizer(f.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[] rows = new boolean[N]; boolean[] cols = new boolean[M]; boolean black = false; boolean found = false; for(int j = 0; j < N; j++) { String s = f.readLine(); for(int k = 0; k < M; k++) { if(s.charAt(k) == 'B') { black = true; if(j == R && k == C) { found = true; System.out.println("0"); break; } else { rows[j] = true; cols[k] = true; } } } } if(!black) { System.out.println("-1"); } else { if((rows[R] || cols[C]) && !found) { System.out.println("1"); } else if(!found) { 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
dbe81f4f596033de6acb27f0ce526eba
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 hi{ 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(); int c = sc.nextInt(); String[][] a = new String[n][m]; for(int j=0;j<n;j++) { String s = sc.next(); for(int h=0;h<m;h++) { a[j][h] = s.charAt(h)+""; } } int ans=0; boolean check=true; if(a[r-1][c-1].equals("B")) { System.out.println(ans); check = false; } if(check) { for(int j=0;j<n;j++) { if(a[j][c-1].equals("B")) { ans=1; System.out.println(ans); check=false; break; } } if(check) { for(int j=0;j<m;j++) { if(a[r-1][j].equals("B")) { ans=1; System.out.println(ans); check=false; break; } } } } if(check) { for(int j=0;j<n;j++) { if(!check) { break; } for(int g=0;g<m;g++) { if(a[j][g].equals("B")) { ans=2; System.out.println(ans); check = false; break; } } } } if(check) { 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
e6cbe568582f9cf518453c624d6d6940
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
689d9fe197dc5806fdb6299e94ef0dd4
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
/* ID: rajneesh.osho LANG: JAVA PROG: Not Shading */ import java.util.*; import java.io.*; public class NotShading { static Osho sc = new Osho(); public static void main(String[] args) { // TODO Auto-generated method stub int t = sc.nextInt(); while(t-->0) { solve(); } } private static void solve() { // TODO Auto-generated method stub int n = sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); char ch[][] = new char[n][m]; for(int i=0;i<n;i++) { String s = sc.next(); for(int j=0;j<m;j++) { ch[i][j] = s.charAt(j); } } if(ch[r-1][c-1]=='B') { System.out.println(0); return; } boolean x = false; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(ch[i][j]=='B') { x =true; int diffx = Math.abs(r-1-i); int diffy = Math.abs(c-1-j); if(diffx==0||diffy==0) { System.out.println(1); return; } } } } if(x) { System.out.println(2); return; } System.out.println(-1); } static class Osho extends PrintWriter { private BufferedReader r; private StringTokenizer st = new StringTokenizer(""); private String token; // standard input public Osho() { this(System.in,System.out); } public Osho(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Osho(String problemName) throws IOException { super(new FileWriter(problemName+".out")); r = new BufferedReader(new FileReader(problemName+".in")); } private String peek() { if (token == null) try { while (!st.hasMoreTokens()) { String line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } public boolean hasMoreTokens() { return peek() != null; } public String next() { String ans = peek(); token = null; return ans; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public 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
ed5cabf224b8faa946160faeb71a89ef
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
/* JAIKARA SHERAWAALI DA BOLO SACHE DARBAR KI JAI HAR HAR MAHADEV JAI BHOLENAATH Rohit Kumar "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." */ 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.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken();} long nextLong(){ return Long.parseLong(next());} int nextInt(){ return Integer.parseInt(next());} } static int mod=(int)1e9+7; public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } FastReader sc = new FastReader(); int tt=sc.nextInt(); while(tt-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int r=sc.nextInt(); int c=sc.nextInt(); char ch[][]=new char[n][m]; boolean black=false; for(int i=0;i<ch.length;i++) { String s=sc.next(); for(int j=0;j<ch[0].length;j++) { ch[i][j]=s.charAt(j); if(ch[i][j]=='B') { black=true; } } } if(ch[r-1][c-1]=='B') { print(0); continue; } if(!black) { print(-1); continue; } boolean colpres=false,rowpres=false; for(int i=0;i<ch.length;i++) { if(ch[i][c-1]=='B') { rowpres=true; break; } } for(int i=0;i<ch[0].length;i++) { if(ch[r-1][i]=='B') { colpres=true; break; } } if(rowpres||colpres) { print(1); } else{ print(2); } } } static <t>void print(t o){ System.out.println(o); } static void reverse(int[] arr, int from, int till) { for (int i = from, j = till; i < j; i++, j--) { swap(arr, i, j); } } static void swap(int[] arr, int a, int b) { int tmp = arr[a]; arr[a] = arr[b]; arr[b] = tmp; } void sieveOfEratosthenes(int n) { 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) System.out.print(i + " "); } } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static boolean isprime(int n) { if(n<=1) return false; if(n<=3) return true; 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; } }
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
ff1887f78e52df8efa408e0a2b602cab
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.math.BigInteger; import java.text.DateFormat; import java.text.Format; import java.text.SimpleDateFormat; import java.util.*; public class ex { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Solution s = new Solution(); int t = scan.nextInt(); while (t-->0){ int n = scan.nextInt(); int m = scan.nextInt(); int r = scan.nextInt()-1; int c = scan.nextInt()-1; String[] grid = new String[n]; for (int i = 0; i <n; i++) { grid[i]=scan.next(); } System.out.println(s.solve(grid,n,m,r,c)); } } } class Solution{ public int solve(String[] grid,int n,int m,int r,int c){ if(grid[r].charAt(c)=='B'){ return 0; } for (int i = 0; i < m; i++) { if(grid[r].charAt(i)=='B'){ return 1; } } for (int i = 0; i <n; i++) { if(grid[i].charAt(c)=='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 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
1726ff00b66a10eb2bd74fd2c2acdde3
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 Solve_766_div_2 { public static void main(String[] args) { var io = new Kattio(System.in, System.out); int t = io.nextInt(); for(int i=0; i<t; i++){ solve_A(io); } io.close(); } public static void solve_A(Kattio io){ int n, m, r, c; n = io.nextInt(); m = io.nextInt(); r = io.nextInt()-1; c = io.nextInt()-1; boolean hasBlack = false; boolean[][] grid = new boolean[n][m]; for(int i=0; i<n; i++){ String line = io.next(); for(int j=0; j<m; j++){ grid[i][j] = (line.charAt(j) == 'B'); hasBlack |= grid[i][j]; } } if(!hasBlack){ io.println(-1); } else if(grid[r][c]){ io.println(0); } else{ boolean flag = false; for(int i=0; i<m; i++){ flag |= grid[r][i]; } for(int i=0; i<n; i++){ flag |= grid[i][c]; } if(flag){ io.println(1); } else{ io.println(2); } } } public static void solve_B(Kattio io){ } public static void solve_C(Kattio io){ } public static void solve_D(Kattio io){ } public static void solve_E(Kattio io){ } public static void solve_F(Kattio io){ } static class Kattio extends PrintWriter { public Kattio(InputStream i) { super(new BufferedOutputStream(System.out)); r = new BufferedReader(new InputStreamReader(i)); } public Kattio(InputStream i, OutputStream o) { super(new BufferedOutputStream(o)); r = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public String next() { return nextToken(); } private BufferedReader r; private String line; private StringTokenizer st; private String token; private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return 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
ce411b8996468356bf0bc2c17952e0ec
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 jdk.swing.interop.SwingInterOpUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Median { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); for (int k = 0; k < t; k++) { int rows = sc.nextInt(); int cols = sc.nextInt(); int r = sc.nextInt()-1; int c = sc.nextInt()-1; int B = 0; int res = 2; String s; char a[][] = new char[rows][cols]; for (int i = 0; i < rows; i++) { s = sc.nextLine(); for (int j = 0; j < cols; j++) { a[i][j] = s.charAt(j); if(a[i][j] == 'B'){ B++; } } } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if(i == r && j == c && a[i][j] == 'B'){ res = 0; break; } if((i == r || j == c) && a[i][j] == 'B'){ res = 1; } } if(res == 0){ break; } } // if(B == 0){ res = -1; } System.out.println(res); } } } 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[] 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 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
26b5fe518eecc9918486f372686463ea
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 MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0) { int m=sc.nextInt(); int n=sc.nextInt(); int r=sc.nextInt(); int c=sc.nextInt(); char[][] arr=new char[m][n]; int flag=0; for(int i=0;i<m;i++) { String str=sc.next(); for(int j=0;j<n;j++) { arr[i][j]=str.charAt(j); if(arr[i][j]=='B') flag+=1; } } if(flag==0) System.out.println("-1"); else { int i=0,j=0; while(i<m) { if(arr[i][c-1]=='B') break; i+=1; } while(j<n) { if(arr[r-1][j]=='B') break; j+=1; } if(arr[r-1][c-1]=='B') System.out.println("0"); else if(i!=m || j!=n) 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
834a32fb85310e4d6f97ac12f77b3741
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
/** * 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 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /* */ public class Main { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t = fs.nextInt(); for (int i = 0; i < t; i++) { int n = fs.nextInt(); int m = fs.nextInt(); int r = fs.nextInt(); int c = fs.nextInt(); r-=1; c-=1; boolean ans = false ,oneshot = false; char [][]x = new char[n][m]; for (int j = 0; j < n ; j++) { String s = fs.next(); for (int k = 0; k < m; k++) { x[j][k] = s.charAt(k); if(x[j][k]=='B') ans = true; } } for (int j = 0; j < n; j++) { if(x[j][c]=='B') oneshot = true; } for (int j = 0; j < m; j++) { if(x[r][j]=='B') oneshot = true; } if(ans==false) out.println(-1); else if(x[r][c]=='B') { out.println(0); } else if(oneshot==true) out.println(1); else out.println(2); } out.close(); } 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 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
6ad114d39fdc39ef70681decad5dcfca
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.shroom; import java.util.*; public class dec { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-->0) { LinkedList<Character> ll = new LinkedList<>(); int n=in.nextInt(); int m=in.nextInt(); int r=in.nextInt(); int c=in.nextInt(); String []a = new String[n]; int cnt1=0, cnt2=0, cnt3=0; int g = n*m; for(int i=0 ; i<n; i++){ a[i] = in.next(); for(int j=0; j<m; j++){ if(a[i].charAt(j)=='B') cnt1++; if(a[i].charAt(j)=='B' && (i==r-1 && j==c-1)) cnt2++; if(a[i].charAt(j)=='B' && (i==r-1 || j==c-1)) cnt3++; // if(a[i].charAt(j)=='B') cnt1++; } } if(cnt1==0){ System.out.println(-1); }else if(cnt2>0){ System.out.println(0); }else if(cnt3>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 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
4a9e4dacfbecf410ec9bbcc3b8118a30
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 { static int t ; public static void main(String[] args) { Scanner sc = new Scanner(System.in); t = sc.nextInt(); int n[] = new int[t]; int m[]= new int[t]; int r[]= new int[t]; int c[]= new int[t]; String[][] a = new String[t][]; int res[] = new int[t]; for(int i=0;i<t;i++){ n[i] = sc.nextInt(); m[i] = sc.nextInt(); r[i] = sc.nextInt(); c[i] = sc.nextInt(); a[i] = new String[n[i]]; for(int j=0;j<n[i];j++){ a[i][j] = sc.next(); } } for(int i =0;i<t;i++){ res[i] = findRes(i, n, r, c, a, res ); } for(int i =0;i<t;i++){ System.out.println(res[i]); } } static int findRes(int i, int n[],int r[], int c[],String a[][], int res[]){ if(a[i][r[i]-1].charAt(c[i]-1) == 'B') { return 0; } else if(a[i][r[i]-1].contains("B")) return 1; else{ for(int j=0;j<n[i];j++){ if (a[i][j].charAt(c[i]-1) == 'B'){ return 1; } } for(int j=0;j<n[i];j++){ if(a[i][j].contains("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 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
38fee2afef3e729da725706280f4e599
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.HashMap; import java.util.Scanner; public class FirstProject { public static void main(String[] args) { Scanner src=new Scanner(System.in); int t=src.nextInt(); while(t-- > 0) { int r=src.nextInt(); int c=src.nextInt(); int makeRow=src.nextInt(); int makeCol=src.nextInt(); src.nextLine(); ArrayList<String>strs=new ArrayList<>(); for(int i=0;i<r;i++) { strs.add(src.next()); } if(strs.get(makeRow-1).charAt(makeCol-1) == 'B') { System.out.println(0); }else { boolean isAnyBlack=false; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { if(strs.get(i).charAt(j)=='B') { isAnyBlack=true; } } } if(isAnyBlack == false) { System.out.println(-1); }else { boolean isBlack=false; for(int i=0;i<r;i++) { if(strs.get(i).charAt(makeCol-1) == 'B') { isBlack=true; } } boolean isBlackAgain=false; for(int i=0;i<c;i++) { if(strs.get(makeRow-1).charAt(i)=='B') { isBlackAgain=true; } } if(isBlack == false && isBlackAgain==false) { 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
d087a1ef0cf26c995da51b6657f608cd
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 boolean checkForBlackInGrid(char[][] arr, int n, int m){ boolean hasBlack=false; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ char ch=arr[i][j]; if(ch=='B'){ hasBlack=true; } } } return hasBlack; } public static boolean checkColumn(char[][] arr, int row, int col){ for(int i=0;i<row;i++){ if(arr[i][col]=='B'){ return true; } } return false; } public static boolean checkRow(char[][] arr, int row, int col){ for(int i=0;i<col;i++){ if(arr[row][i]=='B'){ return true; } } return false; } public static boolean checkBlack(char[][] arr, int row, int col){ if(arr[row][col]=='B'){ return true; } else{ return false; } } public static int helper(char[][] arr, int n, int m, int row, int col){ if(checkForBlackInGrid(arr, n, m)==false){ return -1; } if(checkBlack(arr, row-1, col-1)){ return 0; } if(checkColumn(arr, n, col-1)){ return 1; } if(checkRow(arr, row-1, m)){ return 1; } return 2; } public static void main(String[] args){ Scanner sc=new Scanner(System.in); int testCases=sc.nextInt(); while(testCases>0){ // System.out.println(testCases); int n=sc.nextInt(); int m=sc.nextInt(); int r=sc.nextInt(); int c=sc.nextInt(); // System.out.println(n+" "+m+" "+r+" "+c); char[][] arr=new char[n][m]; for(int i=0;i<n;i++){ String str=sc.next(); for(int j=0;j<m;j++){ arr[i][j]=str.charAt(j); } } System.out.println(helper(arr, n, m, r, c)); testCases--; } 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
e09928067f00ec39e7a315d04e85a408
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 Solution { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int t = in.nextInt() ; while(t>0) { int n = in.nextInt() ; int m = in.nextInt() ; int r = in.nextInt() ; int c = in.nextInt() ; boolean[]row = new boolean[n] ; boolean[]col = new boolean[m] ; int flag = 0 ; char[][] grid = new char[n][m] ; String[] temp = new String[n] ; for(int i=0 ;i<n ;i++) { temp[i]=in.next() ; } for(int i=0 ;i<n ;i++) { grid[i] = temp[i].toCharArray() ; } for(int i=0 ;i<n ;i++) { for(int j=0 ;j<m ;j++) { if(grid[i][j]=='B') { flag = 1 ; row[i] = true ; col[j] = true ; } } } if(flag==0)System.out.println(-1) ; else { if(grid[r-1][c-1]=='B') System.out.println(0) ; else if(row[r-1] || col[c-1])System.out.println(1) ; else System.out.println(2) ; } 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 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
ec8bdd74bfd035caf263deee154233d5
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 r766; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int testCases = scan.nextInt(); String output = ""; while (testCases > 0) { int n = scan.nextInt(); int m = scan.nextInt(); int r = scan.nextInt(); int c = scan.nextInt(); char[][] grid = new char[n][m]; boolean allWhite = true; for(int i = 0; i < n; i++) { String temp = scan.next(); for(int j = 0; j < m; j++) { grid[i][j] = temp.charAt(j); if(grid[i][j] == ('B')) { allWhite = false; } } } boolean foundAnswer = false; if(allWhite) { output += "-1\n"; testCases--; foundAnswer = true; continue; } if(grid[r-1][c-1] == 'B') { output += "0\n"; testCases--; foundAnswer = true; continue; } for(int j = 0; j < m; j++) { if(grid[r-1][j] ==('B')) { output += "1\n"; foundAnswer = true; break; } } if(foundAnswer) { testCases--; continue; } for(int i = 0; i < n; i++) { if(grid[i][c-1] == ('B')) { output += "1\n"; foundAnswer = true; break; } } if(foundAnswer) { testCases--; continue; } output += "2\n"; testCases--; } System.out.println(output); } }
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
9065d1355d52f2d05cef54bd9c92d847
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.ArrayList; import java.util.List; public class NotShaded { 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') break; 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(); } } static Reader sc = new Reader(); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String args[]) throws IOException { /* * For integer input: int n=inputInt(); * For long input: long n=inputLong(); * For double input: double n=inputDouble(); * For String input: String s=inputString(); * For printing without space: print(a+""); where a is a variable of any datatype * For printing with space: printSp(a+""); where a is a variable of any datatype * For printing with new line: println(a+""); where a is a variable of any datatype */ int num = inputInt(); while (num-- > 0) { int n = inputInt(); int m = inputInt(); int r = inputInt(); int c = inputInt(); r = r-1; c = c-1; int min = 3; inputString(); for (int i = 0; i < n; i++) { String temp = inputString(); for (int j = 0; j < m; j++) { if(temp.charAt(j) == 'B'){ if(r == i && c == j){ min = 0; break; } else if(r == i || c == j){ if(min > 1) min = 1; } else { if(min > 2) min = 2; } } } } if(min != 3) println(min + ""); else println("-1"); } bw.flush(); bw.close(); } public static int inputInt() throws IOException { return sc.nextInt(); } public static long inputLong() throws IOException { return sc.nextLong(); } public static double inputDouble() throws IOException { return sc.nextDouble(); } public static String inputString() throws IOException { return sc.readLine(); } public static void print(String a) throws IOException { bw.write(a); } public static void printSp(String a) throws IOException { bw.write(a + " "); } public static void println(String a) throws IOException { bw.write(a + "\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 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
237d9f470739ee0617dacf60883b37d5
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 A_Not_Shading { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } // end of fast i/o code public static void main(String[] args) { FastReader reader = new FastReader(); int t = reader.nextInt(); while(t-->0) { int n = reader.nextInt(), m = reader.nextInt(), r = reader.nextInt(); int c = reader.nextInt(); char [][]color = new char[n][m]; int flag = 0; for(int i=0; i<n; i++) { String str = reader.next(); for(int j = 0; j<m; j++) { color[i][j] = str.charAt(j); if(color[i][j] == 'B') { flag = 1; } } } int flag1 = 0, flag2=0; for(int i=0; i<n; i++) { if(color[i][c-1] == 'B') { flag1 = 1; break; } } for(int i=0; i<m; i++) { if(color[r-1][i] == 'B') { flag2 = 1; break; } } //System.out.println(flag + " "+ flag1 + " "+flag2); if(flag == 0) System.out.println("-1"); else if(color[r-1][c-1] == 'B') System.out.println("0"); else if(flag1 == 1 || flag2 == 1) 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
90fc767f13cf41b3b5abb7eb1fe33cbe
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.*; public class Contest { static boolean check(char arr[][],int n,int m){ for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(arr[i][j]=='B')return false; if(i==arr.length-1&&j==arr[i].length-1)return true; } } return false; } static int solve(char arr[][],int n,int m,int r,int c) { boolean status = check(arr,n,m); if(status)return -1; if(arr[r-1][c-1]=='B')return 0; for(int i=0;i<m;i++){ if(arr[r-1][i]=='B') return 1; } for(int i=0;i<n;i++){ if(arr[i][c-1]=='B')return 1; } return 2; } 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(); int c = sc.nextInt(); char [][] arr = new char[n][m]; String s = ""; for(int j=0;j<n;j++){ s = sc.next(); for(int k=0;k<m;k++){ arr[j][k] = s.charAt(k); } } int ans = solve(arr,n,m,r,c); // for(int j=0;j<n;j++){ // for(int k=0;k<m;k++){ // System.out.print(arr[j][k]+" "); // } // System.out.println(); // } System.out.println(ans); } } } //Important Stuff //Integer range is from -2*10^10 - 2*10^10
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
7ec5486f325342f76a938119d0780af6
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
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class NotShading { public static void main(String[] args) { FastReader fs = new FastReader(); int t = fs.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = fs.nextInt(), m = fs.nextInt(), r = fs.nextInt(), c = fs.nextInt(); char[][] arr = new char[n][m]; for (int i = 0; i < n; i++) { arr[i] = fs.nextLine().toCharArray(); } int count = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == 'B') { count++; } } } if (count == -1) { sb.append(-1).append("\n"); } else if (arr[r - 1][c - 1] == 'B') { sb.append(0).append("\n"); } else { boolean flag = true; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == 'B') { if (i + 1 == r || j + 1 == c) { sb.append(1).append("\n"); flag = false; i = n; break; } } } } if (flag) { sb.append(2).append("\n"); } } } System.out.println(sb); } private static int getGcd(int a, int b) { if (b == 0) return a; return getGcd(b, a % b); } private static long binaryExponentiation(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) == 1) res = res * a; a = a * a; b >>= 1; } return res; } 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[] readArray(int n) { 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 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
8bf04c82116acaab161b475f4636873b
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 NotShading { static PrintWriter out = new PrintWriter((System.out)); static Reader sc = new Reader(); public static void main(String args[]) throws Exception { int t = sc.nextInt(); // int i = 0; while (t-- > 0) { // out.print("Case #" + (i + 1) + ": "); solve(); // i++; } out.close(); } public static void solve() throws Exception { int n = sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); boolean cekw = false; boolean cekb = false; String bw[][] = new String[n][m]; for (int i = 0; i < n; i++) { String temp = sc.nextLine(); for (int j = 0; j < m; j++) { bw[i][j] = String.valueOf(temp.charAt(j)); if (bw[i][j].equals("B")) { cekw = true; } if ((i + 1) == r && (j + 1) == c) { if (bw[i][j].equals("B")) { cekb = true; } } } } if (cekb == true) { out.println(0); return; } if (cekw == false) { out.println(-1); } else { ArrayList<Integer> value = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (bw[i][j].equals("B")) { if (i + 1 == r || j + 1 == c) { value.add(1); } else { value.add(2); } } } } out.println(Collections.min(value)); } } static class Reader { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null) { return false; } st = new StringTokenizer(next); 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 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
c9608b99fcfcafeb84f38f0a10165731
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 y = new BufferedReader (new InputStreamReader (System.in)); int t = Integer.parseInt(y.readLine()); List <Integer> list = new ArrayList <Integer>(); for(int loop = 0; loop < t; loop++) { String str1 = y.readLine(); String s1[] = str1.split(" "); int n = Integer.parseInt(s1[0]); int m = Integer.parseInt(s1[1]); int r = Integer.parseInt(s1[2]); int c = Integer.parseInt(s1[3]); String arr[] = new String[n]; for(int i = 0; i < n; i++) { arr[i] = y.readLine(); } if(arr[r - 1].charAt(c - 1) == 'B') { list.add(0); } else if(arr[r - 1].contains("B")) { list.add(1); } else { int count = 0; for(int i = 0; i < n; i++) { if(arr[i].charAt(c - 1) == 'B') count++; if(count > 0) break; } if(count == 1) { list.add(1); } else { count = 0; for(int i = 0; i < n; i++) { if(arr[i].contains("B")) { count++; } if(count > 1) break; } if(count > 0) list.add(2); else list.add(-1); } } } for(int i = 0; i < t; i++) { System.out.println(list.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 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
4bec998e48689697c1b9ce8d88e78bbb
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 SolnA { static boolean checkPerfectSquare(double number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean checkPerfectCube(double number) { double cbrt=Math.cbrt(number); return ((cbrt - Math.floor(cbrt)) == 0); } public static int reverseInt(int i) { int n = 0; while(i!=0) { n=n*10+i%10; i=i/10; } return n; } public static Long reverseLong(long i) { long n = 0; while(i!=0) { n=n*10+i%10; i=i/10; } return n; } public static long gcdl(long a,long b) { if(b==0) return a; return gcdl(b,a%b); } public static long gcdi(int a,int b) { if(b==0) return a; return gcdi(b,a%b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0) { t--; run(sc); } sc.close(); } public static void run(Scanner sc) { int n=sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c= sc.nextInt(); sc.nextLine(); r--; c--; String[] ingrid = new String[n]; char[][] grid = new char[n][m]; for(int i=0;i<n;i++) { ingrid[i]=sc.nextLine(); for(int j=0;j<m;j++) { grid[i][j]=ingrid[i].charAt(j); } } if(grid[r][c]=='B') { System.out.println(0); return; } for(int i=0;i<n;i++) { if(grid[i][c]=='B') { System.out.println(1); return; } } for(int i=0;i<m;i++) { if(grid[r][i]=='B') { System.out.println(1); return; } } int hasB = -1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(grid[i][j]=='B') { hasB=2; } } } System.out.println(hasB); } }
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
55f21c73dd8c6830e3a9d7d04cc5b339
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 Rough { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test > 0) { int row = sc.nextInt() ; int col = sc.nextInt() ; int rpos = sc.nextInt(); // int cpos = sc.nextInt() ; char [][] arr = new char[row][col]; boolean oneblack = false ; for (int i = 0; i < row; i++) { String str = sc.next() ; char ch [] = str.toCharArray() ; for (int j = 0; j < col; j++) { arr[i][j] = ch[j]; if(arr[i][j] == 'B') { oneblack = true ; } } } // for (int i = 0; i < row; i++) { // for (int j = 0; j < col; j++) { // System.out.print(arr[i][j]); // } // System.out.println(); // } if(oneblack == false) { System.out.println(-1); }else if(arr[rpos-1][cpos-1] == 'B') { System.out.println(0); }else { if(checkBlackCol(arr,rpos-1,col)) { System.out.println(1); } else if(checkBlackRow(arr,row,cpos-1)) { System.out.println(1); } else { System.out.println(2); } } test--; } } private static boolean checkBlackCol(char[][] arr, int row, int col) { // TODO Auto-generated method stub for(int c = 0 ; c < col ; c++) { if(arr[row][c] == 'B') { return true ; } } return false ; } private static boolean checkBlackRow(char[][] arr, int row, int col) { // TODO Auto-generated method stub for(int i = 0 ; i < row ; i++) { if(arr[i][col] == '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
85f6ba7a7629cde3b07d40dc61f9b1ce
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 Blacked { public static void main(String[] args) throws IOException { 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[][] grid=new char[n][m]; int black=0; int white=0; for(int i=0;i<grid.length;i++) { grid[i]=sc.next().toCharArray(); } for(char[] arr: grid) { for(char ch: arr) { if(ch=='B') black++; else{ white++; } } } if(black==0) { System.out.println("-1"); }else if(grid[r-1][c-1]=='B') { System.out.println("0"); }else{ boolean flag=false; for(int i=0;i<grid[0].length && flag==false;i++) { if(grid[r-1][i]=='B') { flag=true; System.out.println("1"); } } for(int i=0;i<grid.length && flag==false;i++) { if(grid[i][c-1]=='B') { flag=true; System.out.println("1"); } } if(flag==false) { 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
c92a82689319641454bb3780df613bc3
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.StringTokenizer; public class A { static class RealScanner { 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()); } } public static void main(String[] args) { RealScanner sc = new RealScanner(); int t = sc.nextInt(); while (t-- > 0) { int n, m, r, c; n = sc.nextInt(); m = sc.nextInt(); r = sc.nextInt(); c = sc.nextInt(); char[][] ch = new char[n+1][m+1]; int[] arr1 = new int[n+1]; int[] arr2 = new int[m+1]; boolean check = false; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < m; j++) { ch[i][j] = s.charAt(j); if (ch[i][j] == 'B') { check = true; arr1[i] = 1; arr2[j] = 1; } } } // for (int i=1;i<=n;i++){ // for (int j=1;j<=m;j++){ // System.out.print(ch[i][j]); // } // System.out.println(); // } // if (ch[r][c] == 'B') { // System.out.println(0); // continue; // } // if (!check) { // System.out.println(-1); // continue; // } // int cm1 = 0; // int my = 1; // for (int i = 1; i <= m; i++) { // if (ch[my][i] == 'B') { // cm1++; // break; // } // } // int co = 1; // for (int i = 1; i <= n; i++) { // // if (ch[i][co] == 'B') { // cm1++; // break; // } // // } // if (cm1 > 0) { // System.out.println(1); // } else { // System.out.println(2); // } if (!check) { System.out.println(-1); } else if (ch[r - 1][c - 1] == 'B') System.out.println(0); else if (arr1[r - 1] == 1 || arr2[c - 1] == 1) 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
8989e7d3ce1f5504dea0dd94e27819b6
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.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) throws IOException{ // Scanner scanner = new Scanner(System.in); Reader.init(System.in); int t, n,m, r, c, ans; String s; Boolean fb, fbr; t = Reader.nextint(); while (t-->0){ fb = false; fbr = false; ans=-1; n = Reader.nextint(); m = Reader.nextint(); r = Reader.nextint(); c = Reader.nextint(); Boolean done = false; for (int i = 0; i < n; i++) { s = Reader.next(); for (int j = 0; j < m; j++) { if(s.charAt(j)=='B' && done == false){ fb=true; if(i==r-1 && j==c-1) { ans = 0; done = true; // break; } else if (i==r-1 || j==c-1){ ans=1; } } } } if (fb == true && ans == -1) ans = 2; System.out.println(ans); } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextint() throws IOException { return Integer.parseInt( next() ); } static long nextlong() throws IOException { return Long.parseLong( next() ); } static double nextdouble() throws IOException { return Double.parseDouble( 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
c2eb066dcda4de89dc8bd474fb983c2d
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.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) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextFloat() { return Float.parseFloat(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[] a = new int[n]; for (int i=0; i<n; i++) { a[i] = this.nextInt(); } return a; } long[] nextArrayLong(int n) { long[] a = new long[n]; for (int i=0; i<n; i++) { a[i] = this.nextLong(); } return a; } } public static void solve(int n, int m, int r, int c, char[][] grid) { if (grid[r-1][c-1] == 'B') { System.out.println("0"); return; } boolean possible = false; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { if (grid[i][j] == 'B') { possible = true; if (i == r-1 || j == c-1) { System.out.println("1"); return; } } } } if (possible) { System.out.println("2"); } else { System.out.println("-1"); } } 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(); char[][] grid = new char[n][m]; for (int i=0; i<n; i++) { grid[i] = in.next().toCharArray(); } solve(n, m, r, c, grid); } } }
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
eee413de80dad1e940f8410ad9ea2556
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
//Siddhartha Bose import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class h { public static int r1=0; 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; } } static long[][] f=new long[501][501]; public static void main(String[] args) { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); 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(); r--; c--; int g=0; int d=0; boolean p=false; char[][] a=new char[n][m]; for(int i=0;i<n;i++) { String str=s.next(); for(int j=0;j<m;j++) { a[i][j]=str.charAt(j); if(a[i][j]=='B') { g++; if(i==r && j==c) { d=2; } else if(j==c || i==r && d==0) { p=true; } } } } if(g==0) { System.out.println(-1); }else if(d==2) { System.out.println(0); }else if(p) { System.out.println(1); }else { System.out.println(2); } t--; } out.close(); } // static long ans(int[] a,int[] b,long till,long[] post,int n,int k,int i) { // if(k==0 || i==n) { // return till+post[i]; // } // if(f[i][k]!=-1) { // return f[i][k]; // } // // } // static class pair static class pair1 implements Comparable<pair1>{ private int a; private int b; pair1(int a,int b){ this.a=a; this.b=b; } public int compareTo(pair1 o) { return Integer.compare(this.b, o.b); } } static class pair3 implements Comparable<pair3>{ private int a; private int b; private int d; private int c; pair3(int a,int b,int c,int d){ this.a=a; this.d=d; this.b=b; this.c=c; } @Override public int compareTo(pair3 c) { if(c.b-c.a!=this.b-this.a) { return Integer.compare(c.b-c.a, this.b-this.a); }else { return Integer.compare(this.c, c.c); } } } static class pair2 implements Comparable<pair2>{ private int a; private int b; private int d; private int c; pair2(int a,int b,int c,int d){ this.a=a; this.d=d; this.b=b; this.c=c; } public int compareTo(pair2 c) { if(c.b!=this.b) { return Integer.compare(c.b, this.b); }else { if(c.c!=this.c) { return Integer.compare(this.c, c.c); }else { return Integer.compare(this.a, c.a); } } } } static boolean r(int k) { for(int i=2;i<=k/2;i++) { if(k%i==0) { return false; } } return true; } public static int[] is_prime=new int[100001]; public static ArrayList<Long> primes=new ArrayList<>(); public static void sieve() { long maxN=100; for(long i=1;i<=maxN;i++) { is_prime[(int) i]=1; } is_prime[0]=0; is_prime[1]=0; for(long i=2;i*i<=maxN;i++) { if(is_prime[(int) i]!=0) { primes.add( i); long c=1; for(long j=i*i;j<=maxN;j+=i) { is_prime[(int) j]++; } } } for(long i=2;i<=maxN;i++) { is_prime[(int) i]=is_prime[(int) (i-1)]+is_prime[(int) i]; } // int count=0; // for(long i=1;i<=maxN;i++) { // if(is_prime[(int) i]==1) { // count++; // } // if(is_prime[count]==1) { // arr[(int) i]=1; // }else { // arr[(int)i]=0; // } // } } public static long[] merge_sort(long[] A, int start, int end) { if (end > start) { int mid = (end + start) / 2; long[] v = merge_sort(A, start, mid); long[] o = merge_sort(A, mid + 1, end); return (merge(v, o)); } else { long[] y = new long[1]; y[0] = A[start]; return y; } } public static long[] merge(long a[], long b[]) { // int count=0; long[] temp = new long[a.length + b.length]; int m = a.length; int n = b.length; int i = 0; int j = 0; int c = 0; while (i < m && j < n) { if (a[i] < b[j]) { temp[c++] = a[i++]; } else { temp[c++] = b[j++]; } } while (i < m) { temp[c++] = a[i++]; } while (j < n) { temp[c++] = b[j++]; } return temp; } }
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
585912c1ecb6604e6414007db72d2068
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 Codeforces { public static void main(String[] args) throws java.lang.Exception { /* your code goes here */ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(buf.readLine()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < t; i++) { String st1[]=(buf.readLine()).split(" "); int n=Integer.parseInt(st1[0]); int m=Integer.parseInt(st1[1]); int r=Integer.parseInt(st1[2]); int c=Integer.parseInt(st1[3]); char mat[][]=new char[n][m]; int flag=0; for(int j=0;j<n;j++) { char ch[]=(buf.readLine()).toCharArray(); for(int z=0;z<m;z++) { mat[j][z] = ch[z]; } } int ct=0; for(int j=0;j<n;j++) { for(int z=0;z<m;z++) { if(mat[j][z]=='B') { ct++; if(j==r-1 && z==c-1) { flag=1; break; } else if(j==r-1 || z==c-1) { flag=2; } } } if(flag==1) break; } if(flag==1) sb.append(0); else if(flag==2) sb.append(1); else { if(ct>0) sb.append(2); else sb.append(-1); } sb.append("\n"); } System.out.println(sb); } }
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
5f2ecbcd93abf559949a2bc66135faee
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.*; public class NotShading { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int numOfCases = Integer.parseInt(br.readLine()); for(int curCase = 0; curCase < numOfCases; curCase++){ String[] line = br.readLine().split(" "); int n, m, r, c; n = Integer.parseInt(line[0]); m = Integer.parseInt(line[1]); r = Integer.parseInt(line[2])-1; c = Integer.parseInt(line[3])-1; char[][] grid = new char[n][m]; boolean allWhite = true; for(int i = 0; i < n; i++){ grid[i] = br.readLine().toCharArray(); if(allWhite){ for(char letter : grid[i]){ if(letter == 'B') allWhite = false; } } } if(allWhite){ sb.append(-1).append("\n"); } else if(grid[r][c] == 'B'){ sb.append(0).append("\n"); } else{ boolean blackFound = false; for(int i = 0; i < n && !blackFound; i++){ if(grid[i][c] == 'B') blackFound = true; } for(int i = 0; i < m && !blackFound; i++){ if(grid[r][i] == 'B') blackFound = true; } if(blackFound){ sb.append(1).append("\n"); } else { sb.append(2).append("\n"); } } } System.out.print(sb); } }
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
cb765641b5508617cbecc84cd8706a88
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.io.*; import java.util.*; public class Main{ static boolean[] primecheck = new boolean[1000002]; static ArrayList<Integer>[] adj; static int[] vis; static long mod = (long)1e9 + 7; public static void main(String[] args) { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; t = in.nextInt(); for (int i = 0; i < t; i++) { solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in, PrintWriter out){ int n = in.nextInt(), m = in.nextInt(), r = in.nextInt(), c = in.nextInt(); char[][] s = new char[n][m]; for (int i = 0; i < n; i++) { s[i] = in.nextLine().toCharArray(); } int b = 0, same = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(s[i][j] == 'B'){ b = 1; if(i == r-1){ same = 1; break; } if(j == c-1){ same = 1; break; } } } } if(b == 0) out.println(-1); else if(s[r-1][c-1] == 'B') out.println(0); else if(same == 1) out.println(1); else out.println(2); } } static long sigmaK(long k){ return (k*(k+1))/2; } static void swap(int[] a, int l, int r) { int temp = a[l]; a[l] = a[r]; a[r] = temp; } static void dfs(int i){ vis[i] = 1; for(int j: adj[i]){ if(vis[j] == 0) dfs(j); } } static HashMap<Integer, Integer> initializeMap(int n){ HashMap<Integer,Integer> hm = new HashMap<>(); for (int i = 0; i <= n; i++) { hm.put(i, 0); } return hm; } static boolean isRegular(char[] c){ Stack<Character> s = new Stack<>(); for (char value : c) { if (s.isEmpty() && value == ')') return false; if (value == '(') s.push(value); else s.pop(); } return s.isEmpty(); } static ArrayList<ArrayList<Integer>> createAdj(int n, int e){ FastReader in = new FastReader(); ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); for (int i = 0; i < n + 1; i++) { adj.add(new ArrayList<>()); } for (int i = 0; i < e; i++) { int a = in.nextInt(), b = in.nextInt(); System.out.println(a); adj.get(a).add(b); adj.get(b).add(a); } return adj; } static int binarySearch(int[] a, int l, int r, int x){ if(r>=l){ int mid = l + (r-l)/2; if(a[mid] == x) return mid; if(a[mid] > x) return binarySearch(a, l, mid-1, x); else return binarySearch(a,mid+1, r, x); } return -1; } static boolean isPalindromeI(int n){ int d = 0; int y = n; while(y>0){ d++; y/=10; } int[] a = new int[d]; for (int i = 0; i < d; i++) { a[i] = n%10; n/=10; } for (int i = 0; i < d / 2; i++) { if(a[i] != a[d-i-1]) return false; } return true; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static long fast_pow(long a, long b) { //Jeel bhai OP if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } // If B is odd else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } static class Pair implements Comparable<Pair>{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } public int compareTo(Pair o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; // int ans = Integer.compare(y, o.y); // if(o.y == y) ans = Integer.compare(x, o.x); // // return ans; } } static class Tuple implements Comparable<Tuple>{ int x, y, id; Tuple(int x, int y, int id){ this.x = x; this.y = y; this.id = id; } public int compareTo(Tuple o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } // public static class Pair<U extends Comparable<U>, V extends Comparable<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 hashCode() { // return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); // } // // public boolean equals(Object o) { // if (this == o) // return true; // if (o == null || getClass() != o.getClass()) // return false; // Pair<U, V> p = (Pair<U, V>) o; // return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); // } // // public int compareTo(Pair<U, V> b) { // int cmpU = x.compareTo(b.x); // return cmpU != 0 ? cmpU : y.compareTo(b.y); // } // // public int compareToY(Pair<U, V> b) { // int cmpU = y.compareTo(b.y); // return cmpU != 0 ? cmpU : x.compareTo(b.x); // } // // public String toString() { // return String.format("(%s, %s)", x.toString(), y.toString()); // } // // } 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()); } char nextChar() { return next().charAt(0); } boolean nextBoolean() { return !(nextInt() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } private static int[] mergeSort(int[] array) { //array.length replaced with ctr int ctr = array.length; if (ctr <= 1) { return array; } int midpoint = ctr / 2; int[] left = new int[midpoint]; int[] right; if (ctr % 2 == 0) { right = new int[midpoint]; } else { right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } left = mergeSort(left); right = mergeSort(right); int[] result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while (leftPointer < left.length || rightPointer < right.length) { if (leftPointer < left.length && rightPointer < right.length) { if (left[leftPointer] < right[rightPointer]) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } else if (leftPointer < left.length) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } return result; } public static void Sieve(int n) { Arrays.fill(primecheck, true); primecheck[0] = false; primecheck[1] = false; for (int i = 2; i * i < n + 1; i++) { if (primecheck[i]) { for (int j = i * 2; j < n + 1; j += i) { primecheck[j] = 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
7a21a0ffda134b500a512e9cad3c527f
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 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
96fb5f8c99af91638a061b04a3cee17e
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 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
42ce6fd3cb7c6c45217300e212b69053
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(), n, m, r, c; String s[]; boolean k1, k2, k3; while(t -- > 0){ k1 = true; k2 = true; k3 = true; n = sc.nextInt(); m = sc.nextInt(); r = sc.nextInt(); c = sc.nextInt(); s = new String[n]; for(int i = 0; i < n; i ++){ s[i] = sc.next(); for(int j = 0; j < m; j ++){ if(s[i].charAt(j) == 'B'){ k1 = false; break; } } } if(k1) System.out.println(-1); else{ if(s[r - 1].charAt(c - 1) == 'B') System.out.println(0); else{ for(int i = 0; i < m; i ++){ if(s[r - 1].charAt(i) == 'B'){ k2 = false; k3 = false; System.out.println(1); break; } } if(k2){ for(int i = 0; i < n; i ++){ if(s[i].charAt(c - 1) == 'B'){ k3 = false; System.out.println(1); break; } } } if(k3) 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
5549f7b60eff99c621943f1b8e724745
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
//<———My cp———— import java.util.*; import java.io.*; public class A_Not_Shading{ public static void main(String[] args) throws Exception{ FastReader fr = new FastReader(System.in); int t = fr.nextInt(); while(t-->0){ int r = fr.nextInt(); int c = fr.nextInt(); char[][] mat = new char[r][c]; int row = fr.nextInt(); int col = fr.nextInt(); for(int i=0;i<r;i++){ mat[i]=fr.next().toCharArray(); } if(mat[row-1][col-1]=='B'){ System.out.println(0); }else{ boolean blackFound = false; for(int cR=0;cR<r;cR++){ for(int cC = 0;cC<c;cC++){ if(mat[cR][cC]=='B'){ blackFound=true; } } } if(blackFound){ boolean atSameRowOrCol = false; for(int i = 0;i<c;i++){ if(mat[row-1][i]=='B'){ atSameRowOrCol=true; } } for(int i = 0;i<r;i++){ if(mat[i][col-1]=='B'){ atSameRowOrCol=true; } } if(atSameRowOrCol){ System.out.println(1); }else{ System.out.println(2); } }else{ System.out.println(-1); } } } } public static void print(Object val){ System.out.print(val); } public static void println(Object val){ System.out.println(val); } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static void reverseArray(long[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ long temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } public static void reverseArray(int[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ int temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
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
f091b977fa82e1e62c1a90903cae170e
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 { static boolean arrMax(long[] arr, int i, int n){ long max = Long.MIN_VALUE; for ( int j=0; j<n-i-1; j++){ if ( arr[j]>max) max = arr[j]; } if ( max==arr[n-i-2]) return false; return true; } static boolean lol(int[][]arr,int n,int m){ int ff=0; for (int i=0; i<n; i++){ for ( int j=0; j<m; j++){ if ( arr[i][j]==1){ ff = 1; break; } } if ( ff==1) break; } if ( ff==1) return true; return false; } static void swap(int[]arr, int a,int b){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } static long abs(int[] arr, int a,int b){ if ( arr[a]>=arr[b]) return arr[a]-arr[b]; return arr[b]-arr[a]; } static int absOfNum(int a, int b){ if ( a>=b) return (a-b); return (b-a); } static String oddOrEven(int n){ int a = n & 1; if ( a==0) return "Even"; return "Odd"; } static long gcd(long a,long b){ if ( b==0) return a; return gcd(b,a%b); } static long lcm(long a,long b){ return (a*b)/gcd(a,b); } static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int test = sc.nextInt(); for ( int t=0; t<test; t++){ long n = sc.nextLong(); long m = sc.nextLong(); long x = sc.nextLong(); long y = sc.nextLong(); String[] str = new String[(int)n]; for ( int i=0;i<n; i++) str[i] = sc.next(); int cc = 0; for ( int i=0; i<n; i++){ for ( int j=0; j<m; j++){ if ( str[i].charAt(j)=='B'){ cc = 1; break; } } if ( cc == 1) break; } if ( cc==0) System.out.println(-1); else{ if ( str[(int)x-1].charAt((int)y-1)=='B') System.out.println(0); else { int ans = 2; for ( int i=0; i<m; i++){ if ( str[(int)x-1].charAt(i)=='B') ans =1; } for ( int i=0; i<n; i++){ if ( str[i].charAt((int)y-1)=='B') ans = 1; } 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 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
bd7a6e9cf401d022ef577aa3b982acdd
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 A_Not_Shading{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); try { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); int y=sc.nextInt(); String [] str=new String[n]; boolean flag=false,f=false; for(int j=0;j<n;j++){ str[j]=sc.next(); if(str[j].contains("B")) flag = true; if(str[j].charAt(y-1)=='B') f=true; } if(!flag) System.out.println("-1"); else if(str[x-1].charAt(y-1)=='B') System.out.println("0"); else if(f || str[x-1].contains("B")) System.out.println("1"); else System.out.println("2"); } } finally{ 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
b7f3075337aa8f08ba2ac05f54a4b216
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 A { 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(); char ch[][] = new char[n][m]; for(int i = 0; i<n; i++) { ch[i] = sc.next().toCharArray(); } boolean b = false; for(int i = 0; i<n; i++) { for(int j = 0; j<m; j++) { if(ch[i][j]=='B') { b = true; break; } } } boolean f1 = false; boolean f2 = false; for(int i = 0; i<m; i++) { if(ch[r-1][i]=='B') { f1 = true; break; } } for(int i = 0; i<n; i++) { if(ch[i][c-1]=='B') { f2 = true; break; } } System.out.println(ch[r-1][c-1]=='B'?0:b?(f1||f2?1: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
3353f10e8c31bea1d376c61bf1d50a84
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.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static String OUTPUT = ""; //global //private final static long BASE = 998244353L; private final static int ALPHABET = (int)('z') - (int)('a') + 1; private final static long BASE = 1000000007l; private final static int INF_I = (1<<31)-1; private final static long INF_L = (1l<<63)-1; private final static int MAXN = 200100; private final static int MAXK = 31; private final static int[] DX = {-1,0,1,0}; private final static int[] DY = {0,1,0,-1}; static void solve() { //int ntest = 1; int ntest = readInt(); for (int test=0;test<ntest;test++) { int N = readInt(), M = readInt(); int r = readInt(), c = readInt(); boolean[] row = new boolean[N]; boolean[] col = new boolean[M]; boolean isExist = false; boolean isPainted = false; for (int i=0;i<N;i++) { char[] S = readString().toCharArray(); for (int j=0;j<M;j++) { if (S[j]=='B') { isExist = true; row[i] = true; col[j] = true; if (i==r-1 && j==c-1) isPainted=true; } } } if (!isExist) out.println(-1); else if (isPainted) out.println(0); else if (row[r-1] || col[c-1]) out.println(1); else out.println(2); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
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
269dc425d0781eac0217c7106067efac
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
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n, m, r, c; static char[][] a; public static void main(String[] args) throws IOException { t = in.iscan(); outer : while (t-- > 0) { n = in.iscan(); m = in.iscan(); r = in.iscan()-1; c = in.iscan()-1; a = new char[n][m]; boolean haveBlack = false; for (int i = 0; i < n; i++) { a[i] = in.sscan().toCharArray(); for (int j = 0; j < m; j++) { if (a[i][j] == 'B') { haveBlack = true; } } } if (a[r][c] == 'B') { out.println(0); continue outer; } if (!haveBlack) { out.println(-1); continue outer; } boolean flag = false; for (int row = 0; row < n; row++) { if (a[row][c] == 'B') { flag = true; break; } } for (int col = 0; col < m; col++) { if (a[r][col] == 'B') { flag = true; break; } } out.println(flag ? 1 : 2); } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
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
ca766f74bd0fbd40e7c1ce9b972a4c1f
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 { public static void main(String[] args) throws IOException { 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 < arr.length; i++) { arr[i] = sc.next().toCharArray(); } boolean flag = true; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { if(arr[i][j] == 'B') flag = false; } } if(flag) { pw.println(-1); continue; } if(arr[r-1][c-1]=='B') { pw.println(0); continue; } boolean inRowOrCol = false; for (int i = 0; i < arr[0].length; i++) { if(arr[r-1][i]=='B') inRowOrCol = true; } for (int i = 0; i < arr.length; i++) { if(arr[i][c-1]=='B') inRowOrCol = true; } pw.println(inRowOrCol ? 1 : 2); } pw.close(); } static long getDiff(pair start, pair end) { return Math.abs(start.x - end.x) + Math.abs(start.y - end.y); } static HashMap Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static HashMap Hash(char[] arr) { HashMap<Character, Integer> map = new HashMap<>(); for (char i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return n * factorial(n - 1); } static boolean isPalindrome(char[] str, int i, int j) { // While there are characters to compare while (i < j) { // If there is a mismatch if (str[i] != str[j]) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static double log2(int N) { double result = (Math.log(N) / Math.log(2)); return result; } public static double log4(int N) { double result = (Math.log(N) / Math.log(4)); return result; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static int clearBit(int mask, int idx) { return mask ^ (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } 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; } } } public static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(long[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(Integer[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(String[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } 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 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
d1b8d676f9f2402df4e3565cde1c571d
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.*; /* 9 3 5 1 4 WBWWW BBBWB WWBBB 4 3 2 1 BWW BBW WBB WWB 2 3 2 2 WWW WWW 2 2 1 1 WW WB 5 9 5 9 WWWWWWWWW WBWBWBBBW WBBBWWBWW WBWBWBBBW WWWWWWWWW 1 1 1 1 B 1 1 1 1 W 1 2 1 1 WB 2 1 1 1 W B */ public class Main { 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(); char[][] arr = new char[n][m]; boolean tr=false; for(int i=0;i<n;i++){ String str=scan.next(); for(int j=0;j<m;j++){ arr[i][j]=str.charAt(j); if(arr[i][j]=='B'){ tr=true; } } } if(arr[r-1][c-1]=='B'){ System.out.println(0); } else if(!tr){ System.out.println(-1); } else{ boolean tr1=false; for(int i=0;i<n;i++){ if(arr[i][c-1]=='B'){ tr1=true; i=n; } } for(int i=0;i<m;i++){ if(arr[r-1][i]=='B'){ tr1=true; i=m; } } if(tr1==true){ System.out.println(1); } else{ System.out.println(2); } } 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 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
e4393d81c43ccffcf68055966f8d0470
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.lang.reflect.Array; import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream; public class Main { public static void main(String[] args) { in = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); try { int t = in.nextInt(); while(t-- > 0) { solve(); out.println();} // solve(); } finally { out.close(); } return; } public static void solve() { int n = in.nextInt(), m = in.nextInt(), r = in.nextInt(), c = in.nextInt(); r--; c--; char[][] s = new char[n][m]; for(int i = 0; i < n; i++) { s[i] = in.next().toCharArray(); } if(s[r][c] == 'B') { out.print("0"); return; } int min = 3; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(s[i][j] != 'B') { continue; } if(i == r || j == c) { min = 1; } min = Math.min(min,2); } } out.print(min < 3 ? min : -1); return; } //-------------- Helper methods-------------------® public static int[] fillArray(int n) { int[] array = new int[n]; for(int i = 0; i < n; i++) { array[i] = in.nextInt(); } return array; } public static char[] fillArray() { char[] array = in.next().toCharArray(); return array; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static MyScanner in; //-----------MyScanner class for faster input---------- 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; } } //-------------------------------------------------------- }
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
c2b3a0146d8f3bac6f1867e1e172d5ad
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.util.Arrays; import java.util.Collections; // import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.ArrayList; import java.util.Set; import java.util.Stack; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; // import java.util.Set; // import java.util.TreeSet; // import java.util.stream.Collectors; // import java.util.stream.Stream; // import java.util.Stack; // import java.util.Optional; // import java.util.HashSet; import java.io.*; public class sol { 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(); char ar[][] = new char [n][m]; boolean allwhite[]={true}; IntStream.range(0, n).forEach(i->{ ar[i]=sc.next().toCharArray(); for(int j=0;j<m&&allwhite[0];j++){ if(allwhite[0]&&ar[i][j]=='B') { allwhite[0]=false; } } }); if(ar[r-1][c-1]=='B'){ System.out.println("0"); } else if(allwhite[0]){ System.out.println("-1"); } else{ boolean rowb=false; boolean colb=false; // System.out.println("c="+c); for(int i=0;i<m&&rowb==false;i++){ // System.out.print(ar[n-1][i]+"--"); if(ar[r-1][i]=='B'){ // System.out.println("trued"); rowb=true; } } // System.out.println("r="+r); for(int i=0;i<n&&rowb==false;i++){ // System.out.print(ar[i][m-1]+"++"); if(ar[i][c-1]=='B'){ // System.out.println("trued"); rowb=true; } } System.out.println(rowb?1: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
e5e1a2560cdfe75669c98fd659a5d2cd
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 testing4 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int w = 0; w < t; w++){ int n = in.nextInt(); int m = in.nextInt(); int r = in.nextInt(); int c = in.nextInt(); in.nextLine(); boolean white = true; char[][] a = new char[n][m]; boolean[] br = new boolean[n]; boolean[] bc = new boolean[m]; for(int i = 0; i < n; i++){ a[i] = in.nextLine().toCharArray(); for (int j = 0; j < m; j++) { if (a[i][j] == 'B') { br[i] = true; white = false; break; } } } for(int j = 0; j < m; j++){ for(int i = 0; i < n; i++){ if (a[i][j] == 'B') { bc[j] = true; break; } } } if(white) System.out.println(-1); else if(a[r-1][c-1]=='B') System.out.println(0); else if(br[r-1]) System.out.println(1); else if(bc[c-1]) 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
e84cd34b31d550d920fb7a908093f4e1
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.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner in = new Scanner(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for (int t = 0; t < T; t++) { int n = in.nextInt(); int m = in.nextInt(); int r = in.nextInt(); int c = in.nextInt(); int ans = -1; for (int i = 0; i < n; i++) { String s = in.next(); for (int j = 0; j < m; j++) { if (s.charAt(j) == 'B') { if (i + 1 == r && j + 1 == c) { ans = 0; } else if (ans != 0 && (i + 1 == r || j + 1 == c)) { ans = 1; } else if (ans == -1) { ans = 2; } } } } out.println(ans); } out.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
ce381e51964140184edc85356e21b403
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(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); int totalBlack =0; int clmBlack =0; String[] arr = new String[n]; for (int j = 0; j < n; j++) { arr[j] = sc.next(); } for (int j = 0; j < n; j++) { for (int k = 0; k < m; k++) { if (arr[j].charAt(k) == 'B'){ totalBlack++; break; } } } if (totalBlack == 0){ System.out.println(-1); }else { if (arr[r-1].charAt(c-1) == 'B'){ System.out.println(0); }else { if ((arr[r-1].indexOf('B')) != -1){ System.out.println(1); } else { for (int j = 0; j < n; j++) { if (arr[j].charAt(c-1) == 'B'){ clmBlack++; } } if (clmBlack > 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 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
2ab005997ad386ecc1a6ecf5f7e14db3
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.stream.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); StringBuilder result = new StringBuilder(); for(int i = 0; i < t; i++) { int n = scan.nextInt(); int m = scan.nextInt(); int r = scan.nextInt()-1; int c = scan.nextInt()-1; scan.nextLine(); Set<Cell> black = new HashSet<>(); for(int j = 0; j < n; j++) { String s = scan.nextLine(); for(int k = 0; k < m; k++) { if('B' == s.charAt(k)) { black.add(new Cell(j, k)); } } } int res = -1; if(!black.isEmpty()) { if(black.contains(new Cell(r,c))) { res = 0; } else { Optional<Cell> opt = black.stream().filter((elem) -> elem.row == r || elem.column == c).findAny(); if(opt.isPresent()) { res = 1; } else { res = 2; } } } result.append(res + "\n"); } System.out.println(result); } } class Cell { public int row; public int column; public Cell(int row, int column) { this.row = row; this.column = column; } public boolean equals(Object o) { Cell other = (Cell) o; return other.row == row && other.column == column; } public int hashCode() { return 31*row + column; } }
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
78213304831919d0a7dc16358c96f176
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.Scanner; public class Not_sitting { 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 d = 0; int mid = n % 2 == 0 ? 2 : 1; int l = m % 2 == 0 ? 2 : 1; int tmid = mid; int tl = l; d = n / 2 + m / 2; int p = l*mid; int ll=l; int rr=mid; for (int j = 0; j < n * m; j++) { int c = 0; if (mid * 2 - tmid > n) { int temp = (mid * 2 - tmid - n) / 2; // while (temp > 0) { // c += tl * 2; // tl += 2; // temp--; // } c+=temp*(tl+tl+(temp-1)*2); // tl = m % 2 == 0 ? 2 : 1; } if (l * 2 - tl > m) { int temp = (l * 2 - tl - m) / 2; // while (temp > 0) { // c += tmid * 2; // tmid += 2; // temp--; // } c+=temp*(tmid+tmid+(temp-1)*2); tmid= n % 2 == 0 ? 2 : 1; } if (j < p - c) { System.out.print(d + " "); } else { if (tl % 2 == 1 || tmid % 2 == 1) { mid++; l++; } else { mid++; l++; int temp=ll; ll = ll < rr ? ll + 2 : ll; rr = temp < rr ? rr : rr + 2; } if (tl % 2 == 1 || tmid % 2 == 1) { p = l * mid + (mid - 1) * (l - 1); } else { int bl = ll < rr ? ll: ll - 2; int br= l < rr ? rr - 2 : rr; p = ll * rr + bl * br; } d += 1; System.out.print(d + " "); } } System.out.println(); } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
6001896f7aca561a6f7fa73caaebf8aa
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); Integer[] arr = new Integer[n * m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i * m + j] = Math.max(i, n - i - 1) + Math.max(j, m - j - 1); } } Arrays.sort(arr); pw.println(toString(arr)); } pw.close(); } static long getDiff(pair start, pair end) { return Math.abs(start.x - end.x) + Math.abs(start.y - end.y); } static HashMap Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static HashMap Hash(char[] arr) { HashMap<Character, Integer> map = new HashMap<>(); for (char i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return n * factorial(n - 1); } static boolean isPalindrome(char[] str, int i, int j) { // While there are characters to compare while (i < j) { // If there is a mismatch if (str[i] != str[j]) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static double log2(int N) { double result = (Math.log(N) / Math.log(2)); return result; } public static double log4(int N) { double result = (Math.log(N) / Math.log(4)); return result; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static int clearBit(int mask, int idx) { return mask ^ (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } 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; } } } public static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(long[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(Integer[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(String[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
8b4fe822c2ba14d7fa954eef2a40ed42
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.*; import java.io.*; import java.util.stream.Collectors; 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(); List<Integer> list = new ArrayList<Integer>(); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ int max = Math.max(i,n-1-i)+Math.max(j,m-1-j); list.add(max); } } Collections.sort(list); String res = list.stream() .map(Object::toString) .collect(Collectors.joining(" ")); System.out.println(res); } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
77835f02ec3b9297c065e24b91edf39f
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.*; import java.io.*; import java.util.stream.Collectors; 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(); List<Integer> list = new ArrayList<Integer>(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ int max = Math.max(Math.max(Math.abs(1-i)+Math.abs(1-j),Math.abs(1-i)+Math.abs(m-j)), Math.max(Math.abs(n-i)+Math.abs(m-j),Math.abs(n-i)+Math.abs(1-j))); list.add(max); } } Collections.sort(list); String res = list.stream() .map(Object::toString) .collect(Collectors.joining(" ")); System.out.println(res); } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
39080e60f1f4edea31ac8cc014dea320
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.io.*; import java.util.*; public class A { static FastScanner sc; static PrintWriter pw; public static void main(String[] args) throws Exception{ sc = new FastScanner(); pw = new PrintWriter(System.out); try{ int T = sc.ni(); while(T-->0){ int n = sc.ni(); int m = sc.ni(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ list.add(Math.max(i, n-i-1)+Math.max(j, m-j-1)); } } Collections.sort(list); for(int num: list){ pw.print(num+" "); } pw.println(); } }catch(Exception e){ return; } pw.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni(); return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl(); return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
0990b72677bd5a9e7f8416991d7c26df
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.PriorityQueue; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.AbstractCollection; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author saikat021 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int T = in.nextInt(); while (T-- > 0) { int n = in.nextInt(); int m = in.nextInt(); PriorityQueue<Integer> pq = new PriorityQueue<>(); for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { int max = Integer.MIN_VALUE; max = Math.max(dist(r, c, n - 1, m - 1), max); max = Math.max(dist(r, c, 0, m - 1), max); max = Math.max(dist(r, c, 0, 0), max); max = Math.max(dist(r, c, n - 1, 0), max); pq.add(max); } } while (!pq.isEmpty()) { out.print(pq.poll() + " "); } out.println(); //// // Queue<Pair> q=new LinkedList<>(); // boolean [][] visited =new boolean[n][m]; // if (n % 2 == 0 && m % 2 == 0) { // int midRow=n/2; // int midCol=m/2; // q.add(new Pair(midRow,midCol)); // visited[midRow][midCol]=true; // if(midRow-1>=0) { // q.add(new Pair(midRow - 1, midCol)); // visited[midRow-1][midCol]=true; // } // if(midCol-1>=0) { // q.add(new Pair(midRow, midCol - 1)); // visited[midRow][midCol-1]=true; // } // if(midCol-1>=0&&midRow-1>=0) { // q.add(new Pair(midRow - 1, midCol - 1)); // visited[midRow-1][midCol-1]=true; // } // // // // // } else if (n % 2 != 0 && m % 2 == 0) { // int midRow=n/2; // int midCol=m/2; // q.add(new Pair(midRow,midCol)); // visited[midRow][midCol]=true; // if(midCol-1>=0) { // q.add(new Pair(midRow, midCol - 1)); // visited[midRow][midCol - 1] = true; // } // // // } else if (m % 2 != 0 && n % 2 == 0) { // int midRow=n/2; // int midCol=m/2; // q.add(new Pair(midRow,midCol)); // visited[midRow][midCol]=true; // if(midRow-1>=0) { // q.add(new Pair(midRow - 1, midCol)); // visited[midRow - 1][midCol] = true; // } // // // } else { // q.add(new Pair(m/2,n/2)); // visited[m/2][n/2]=true; // // } // // ArrayList<Integer> res=new ArrayList<>(); // while(!q.isEmpty()){ // int size=q.size(); // // ArrayList<Integer> arrayList=new ArrayList<>(); // for(int i=0;i<size;i++){ // Pair u=q.poll(); // int r=u.i; // int c=u.j; // int max=Integer.MIN_VALUE; // max=Math.max(dist(r,c,n-1,m-1),max); // max=Math.max(dist(r,c,0,m-1),max); // max=Math.max(dist(r,c,0,0),max); // max=Math.max(dist(r,c,n-1,0),max); // arrayList.add(max); // // if(r-1>=0&&visited[r-1][c]==false){ // q.add(new Pair(r-1,c)); // visited[r-1][c]=true; // // } // if(r+1<n&&visited[r+1][c]==false){ // q.add(new Pair(r+1,c)); // visited[r+1][c]=true; // } // if(c+1<m&&visited[r][c+1]==false){ // q.add(new Pair(r,c+1)); // visited[r][c+1]=true; // } // if(c-1>=0&&visited[r][c-1]==false){ // q.add(new Pair(r,c-1)); // visited[r][c-1]=true; // } // if(r+1<n&&c+1<m&&visited[r+1][c+1]==false){ // q.add(new Pair(r+1,c+1)); // visited[r+1][c+1]=true; // } // if(r-1>=0&&c-1>=0&&visited[r-1][c-1]==false){ // q.add(new Pair(r-1,c-1)); // visited[r-1][c-1]=true; // } // if(r-1>=0&&c+1<m&&visited[r-1][c+1]==false){ // q.add(new Pair(r-1,c+1)); // visited[r-1][c+1]=true; // } // if(c-1>=0&&r+1<n&&visited[r+1][c-1]==false){ // q.add(new Pair(r+1,c-1)); // visited[r+1][c-1]=true; // } // // // // } // res.addAll(arrayList); // } // Collections.sort(res); // for(int i=0;i<res.size();i++){ // out.print(res.get(i)+" "); // } // out.println(); // } } private int dist(int r, int c, int n, int m) { return Math.abs(r - n) + Math.abs(m - c); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println() { writer.println(); } public void close() { writer.close(); } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
513cf9ba98499f5878ed8bee5821b424
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.io.*; import java.util.*; public class ComdeFormces { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub FastReader sc=new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); int m=sc.nextInt(); int ans=n+m; ArrayList<Integer> ar=new ArrayList<>(); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i<=n/2) { if(j<=m/2) { ar.add(Math.abs(n-i)+Math.abs(m-j)); } else { ar.add(Math.abs(n-i)+Math.abs(1-j)); } } else { if(j<=m/2) { ar.add(Math.abs(1-i)+Math.abs(m-j)); } else { ar.add(Math.abs(1-i)+Math.abs(1-j)); } } } } Collections.sort(ar); for(int i=0;i<ar.size();i++) { log.write(ar.get(i)+" "); } log.write("\n"); log.flush(); } } static ArrayList<Integer> divisors(int n){ ArrayList<Integer> ar=new ArrayList<>(); for (int i=2; i<=Math.sqrt(n); i++){ if (n%i == 0){ if (n/i == i) { ar.add(i); } else { ar.add(i); ar.add(n/i); } } } return ar; } static int primeDivisor(int n){ ArrayList<Integer> ar=new ArrayList<>(); int cnt=0; boolean pr=false; while(n%2==0) { pr=true; n/=2; } if(pr)ar.add(2); for(int i=3;i*i<=n;i+=2) { pr=false; while(n%i==0) { n/=i; pr=true; } if(pr)ar.add(i); } if(n>2) ar.add(n); return ar.size(); } static int mdlg(int num, int base) { int val=((int)(Math.ceil(Math.log(num)/Math.log(base)))); int val2=(int)Math.pow(base,val); // System.out.println(num+" "+Math.pow(base,val)); if(num/val2==0)return val; return val+1; // return val; } static String rev(String s) { char temp[]=s.toCharArray(); for(int i=0;i<temp.length/2;i++) { char tp=temp[i]; temp[i]=temp[temp.length-1-i]; temp[temp.length-1-i]=tp; } return String.valueOf(temp); } static long bsv(long a[],long min,long max) { long ans=-1; while(min<=max) { long mid=min+(max-min)/2; long an=solvess(a,mid); // System.out.println("an="+an+"mid="+mid); if(an==mid) { ans=Math.max(ans,an); min=mid+1; } else if(an>mid)min=mid+1; else if(an<mid)max=mid-1; } return ans; } static long solvess(long aa[],long el) { long a[]=new long[aa.length]; long min=Long.MAX_VALUE; for(int i=0;i<a.length;i++ ) { a[i]=aa[i]; min=Math.min(min, aa[i]); } boolean ans=true; long val[]=new long[a.length]; for(int i=a.length-1;i>=2;i--) { if(a[i]+val[i]>el) { if(val[i]>=el) { long df=a[i]; a[i]-=df; val[i-1]+=(df/3); val[i-2]+=2*(df/3); a[i]+=df%3; } else { long df=a[i]+val[i]-el; val[i-1]+=(df/3); val[i-2]+=2*(df/3); a[i]-=df; a[i]+=df%3; } } } for(int i=0;i<a.length;i++) { a[i]+=val[i]; } long min2=Long.MAX_VALUE; for(int i=0;i<a.length;i++) { min2=Math.min(min2, a[i]); } return min2; } static int bsd(ArrayList<LinkedList<Integer>> arr, int val,int el) { int s=0; int e=arr.size()-1; int ptr=0; if(val==1)ptr=1; while(s<=e) { int m=s+(e-s)/2; if(arr.get(m).get(ptr)==el)return m; else if(arr.get(m).get(ptr)<el)s=m+1; else e=m-1; } return -1; } static int bs(ArrayList<pair> arr,int el) { int start=0; int end=arr.size()-1; while(start<=end) { int mid=start+(end-start)/2; if(arr.get(mid).a==el)return mid; else if(arr.get(mid).a<el)start=mid+1; else end=mid-1; } if(start>arr.size()-1)return -2; return -1; } static String eval(String s, long bs, long be,long k) { String ans=""; for(int i=s.length()-1;i>=0;i--) { if(s.charAt(i)=='a') { ans="a"+ans; } else if(be>0){ if(be>k) { for(int j=0;j<k;j++)ans="b"+ans; be-=k; } else { for(int j=0;j<be;j++)ans="b"+ans; be=0; } } } for(int i=0;i<bs;i++)ans="b"+ans; return ans; } static int[][] slv() { int r=200001; int bits=(int)(Math.log(r)/Math.log(2))+1; int cnt[][]=new int[r+1][bits]; for(int i=1;i<=r;i++) { int temp=i; int j=0; int min=Integer.MAX_VALUE; while(j<bits) { int tval=1&temp; temp>>=1; if(tval==0)cnt[i][j]=cnt[i-1][j]+1; else { cnt[i][j]=cnt[i-1][j]; } j++; } } return cnt; } static long find(int s,long a[]) { if(s>=a.length)return -1; long num=a[s]; for(int i=s;i<a.length;i+=2) { num=gcd(num,a[i]); if(num==1 || num==0)return -1; } return num; } static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } static long factmod(long n,long mod,long img) { if(n==0)return 1; long ans=1; long temp=1; while(n--!=0) { if(temp!=img) { ans=((ans%mod)*((temp)%mod))%mod; } temp++; } return ans%mod; } static long eval(ArrayList<Integer> a, int k) { if(a.size()<1 )return 0; int i=a.size()-1; long ans=0; while(i>=0) { int temp=k; int prev=0; while(temp--!=0 && i>=0) { ans+=Math.abs(a.get(i)-prev); if(temp==0 || i==0)ans+=a.get(i); prev=a.get(i); i--; } } return ans; } static int bs(long a[] ,long num) { int start=0; int end=a.length-1; while(start<=end) { int mid=start+(end-start)/2; if(a[mid]==num) { return mid; } else if(a[mid]<num)start=mid+1; else end=mid-1; } return start; } //utils static int ncr(int n, int r){ if(r>n-r)r=n-r; int ans=1; for(int i=0;i<r;i++){ ans*=(n-i); ans/=(i+1); } return ans; } public static class trip{ int a,b,c; public trip(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } public int compareTo(trip q) { return this.c-q.c; } } static int mergesort(int[] a,int start,int end) { if(start>=end)return a[end]; int mid=start+(end-start)/2; int min1=mergesort(a,start,mid); int min2=mergesort(a,mid+1,end); merge(a,start,mid,end); return Math.min(min1, min2); } static void merge(int []a, int start,int mid,int end) { int ptr1=start; int ptr2=mid+1; int b[]=new int[end-start+1]; int i=0; while(ptr1<=mid && ptr2<=end) { if(a[ptr1]<=a[ptr2]) { b[i]=a[ptr1]; ptr1++; i++; } else { b[i]=a[ptr2]; ptr2++; i++; } } while(ptr1<=mid) { b[i]=a[ptr1]; ptr1++; i++; } while(ptr2<=end) { b[i]=a[ptr2]; ptr2++; i++; } for(int j=start;j<=end;j++) { a[j]=b[j-start]; } } public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (s != null && s.hasMoreTokens()) { return true; } String tmp; try { b.mark(1000); tmp = b.readLine(); if (tmp == null) { return false; } b.reset(); } catch (IOException e) { return false; } return true; } } public static class pair{ int a; int b; public pair(int a,int b) { this.a=a; this.b=b; } public int compareTo(pair b) { return b.b-this.b; } public int compareToo(pair b) { if(this.a!=b.a)return this.a-b.a; else { return b.b-this.b; } } } static long pow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } static int pow(int a, int pw) { int temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
6bb70cc56730bad1218b7bb8962b48da
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
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; } } static void sort(int a[]){ // int -> long ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long for(int i=0;i<a.length;i++) arr.add(a[i]); Collections.sort(arr); for(int i=0;i<a.length;i++) a[i]=arr.get(i); } private static long gcd(long a, long b){ if(b==0)return a; return gcd(b,a%b); } private static long pow(long x,long y){ if(y==0)return 1; long temp = pow(x, y/2); if(y%2==1){ return x*temp*temp; } else{ return temp*temp; } } static int log(long n){ int res = 0; while(n>0){ res++; n/=2; } return res; } static int mod = (int)1e9+7; static int INF = Integer.MAX_VALUE; static PrintWriter out; static FastReader sc ; public static void main(String[] args) throws IOException { sc = new FastReader(); out = new PrintWriter(System.out); // primes(); // ================================ // int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); solver( n, m); } // ================================ // // int n = sc.nextInt(); // solver(); // ================================ // out.flush(); } public static void solver( int n, int m) { PriorityQueue<Integer> pq =new PriorityQueue<>(); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ pq.offer( Math.max(i+j,Math.max(n-1-i+j, Math.max(m-1-j+i, n+m-2-i-j))) ); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ out.print( pq.poll()+" "); } } out.println( ); } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
ed61628b56c738a5e5014302f9f89ca6
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { // pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); // sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); solve(); } pw.close(); } public long mod = 1_000_000_007; public void solve() { int n = sc.nextInt(); int m = sc.nextInt(); PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2)->o1 - o2); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int dis1 = i + j; int dis2 = i + (m - 1 - j); int dis3 = (n - 1 - i) + j; int dis4 = (n - 1 - i) + (m - 1 - j); pq.add(Math.max(dis1, Math.max(dis2, Math.max(dis3, dis4)))); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { pw.print(pq.poll() + " "); } } pw.println(); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : ans; } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
40b40bfc8ff3d69ae8918c253e462f4d
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.io.IOException; public class Main { public static void main(String... args) throws IOException { int t = readInt(); for (int i = 0; i < t; i++) { int m = readInt(), n = readInt(); int j = m / 2 + n / 2; int ringSize = 0; if (n % 2 == 0 || m % 2 == 0) { ringSize = ((1 - n & 1) + (1 - m & 1)) * 2; for (int l = 0; l < ringSize; l++) { System.out.print(j); System.out.print(' '); } } else { System.out.print(j); System.out.print(' '); } j++; for (; ringSize >= 0; j++) { ringSize += 4; if (j == n + m / 2) { ringSize -= m % 2 == 0 ? 4 : 2; } else if (j > n + m / 2) { ringSize -= 4; } if (j == m + n / 2) { ringSize -= n % 2 == 0 ? 4 : 2; } else if (j > m + n / 2) { ringSize -= 4; } for (int l = 0; l < ringSize; l++) { System.out.print(j); System.out.print(' '); } } System.out.println(); } } static int readInt() throws IOException { int i = System.in.read(); while (i == ' ' || i == '\n' || i == '\r') { i = System.in.read(); } boolean negative = false; if (i == '-') { negative = true; i = System.in.read(); } int result = 0; while (i != ' ' && i != '\n' && i != '\r' && i != -1) { result *= 10; result += i - '0'; i = System.in.read(); } return negative ? -result : result; } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
f5db82a5c8fca8f4e3468dc448541a7c
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.io.*; import java.util.*; public class zia { static boolean prime[] = new boolean[25001]; static void shuffleArray(long[] arr){ int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } public static int Lcm(int a,int b) { int max=Math.max(a,b); for(int i=1;;i++) { if((max*i)%a==0&&(max*i)%b==0) return (max*i); } } static void sieve(int n,boolean prime[]) { // 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 =i+ p) prime[i] = false; } } } // public static String run(int ar[],int n) // { // } public static long lcm(long a,long b) { long max=Math.max(a, b); long min=Math.min(a, b); for(int i=1;;i++) { if((max*i)%min==0) return max; } } public static long calculate(long a,long b,long x,long y,long n) { if(a-x>=n) { a-=n; } else { b=b-(n-(a-x)); a=x; if(b<y) b=y; } return a*b; } public static int upperbound(int s,int e, long ar[],long x) { int res=-1; while(s<=e) { int mid=((s-e)/2)+e; if(ar[mid]>x) {e=mid-1;res=mid;} else if(ar[mid]<x) {s=mid+1;} else {e=mid-1;res=mid; if(mid>0&&ar[mid]==ar[mid-1]) e=mid-1; else break; } } return res; } public static int lowerbound(int s,int e, long ar[],long x) { int res=-1; while(s<=e) { int mid=((s-e)/2)+e; if(ar[mid]>x) {e=mid-1;} else if(ar[mid]<x) {s=mid+1;res=mid;} else {res=mid; if(mid+1<ar.length&&ar[mid]==ar[mid+1]) s=mid+1; else break;} } return res; } static long modulo=1000000007; public static long power(long a, long b) { if(b==0) return 1; long temp=power(a,b/2)%modulo; if((b&1)==0) return (temp*temp)%modulo; else return (((temp*temp)%modulo)*a)%modulo; } public static int log2(int a) { double d=Math.log(a)/Math.log(2); return (int)d+1; } public static int log10(long a) { double d=Math.log(a)/Math.log(10); return (int)d; } public static int find(int ar[],int x) { int count=0; for(int i=0;i<ar.length;i+=2) { if((ar[i]&1)!=x) count++; } return count; } public static void main(String[] args) throws Exception { FastIO sc = new FastIO(); //sc.println(); //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CODE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx int test=sc.nextInt(); // // double c=Math.log(10); // boolean prime[]=new boolean[1000001]; // sieve(1000000, prime); while(test-->0) { int n=sc.nextInt(); int m=sc.nextInt(); ArrayList<Integer> ar=new ArrayList<>(); for(int i=0;i<n;i++) { int maxdistanace=Math.max(i,n-1-i); for(int j=0;j<m;j++) { ar.add(Math.max(j,m-1-j)+maxdistanace); } } Collections.sort(ar); for(int x:ar) sc.print(x+" "); sc.println(); } // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CODE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx sc.close(); } } class pair implements Comparable<pair>{ int first,second; pair(int first,int second) {this.first=first; this.second=second; } public int compareTo(pair p) {return -this.first+p.first;} } class triplet implements Comparable<triplet>{ int first,second,third; triplet(int first,int second,int third) {this.first=first; this.second=second; this.third=third; } public int compareTo(triplet p) {return this.third-p.third;} } // class triplet // { // int x1,x2,i; // triplet(int a,int b,int c) // {x1=a;x2=b;i=c;} // } class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1<<16]; private int curChar, numChars; // standard input public FastIO() { this(System.in,System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } public String nextLine() { int c; do { c = nextByte(); } while (c <= '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10*res+c-'0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); long sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10*res+c-'0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
13461065b050e00aa03ccbd5a2879613
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
//package com.company; import java.io.*; import java.util.*; public class Main{ static boolean[] primecheck = new boolean[1000002]; static ArrayList<Integer>[] adj; static int[] vis; static long mod = (long)1e9 + 7; public static void main(String[] args) { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; t = in.nextInt(); for (int i = 0; i < t; i++) { solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in, PrintWriter out){ int n = in.nextInt(), m = in.nextInt(); int[] dis = new int[n*m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dis[i*m + j] = Math.max(i, n-i-1) + Math.max(j, m-j-1); } } dis = mergeSort(dis); for (int i = 0; i < dis.length; i++) { out.print(dis[i] + " "); } out.println(); } } static void dfs(int i){ vis[i] = 1; for(int j: adj[i]){ if(vis[j] == 0) { dfs(j); } } } static long sigmaK(long k){ return (k*(k+1))/2; } static void swap(int[] a, int l, int r) { int temp = a[l]; a[l] = a[r]; a[r] = temp; } static HashMap<Integer, Integer> initializeMap(int n){ HashMap<Integer,Integer> hm = new HashMap<>(); for (int i = 0; i <= n; i++) { hm.put(i, 0); } return hm; } static boolean isRegular(char[] c){ Stack<Character> s = new Stack<>(); for (char value : c) { if (s.isEmpty() && value == ')') return false; if (value == '(') s.push(value); else s.pop(); } return s.isEmpty(); } static ArrayList<ArrayList<Integer>> createAdj(int n, int e){ FastReader in = new FastReader(); ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); for (int i = 0; i < n + 1; i++) { adj.add(new ArrayList<>()); } for (int i = 0; i < e; i++) { int a = in.nextInt(), b = in.nextInt(); System.out.println(a); adj.get(a).add(b); adj.get(b).add(a); } return adj; } static int binarySearch(int[] a, int l, int r, int x){ if(r>=l){ int mid = l + (r-l)/2; if(a[mid] == x) return mid; if(a[mid] > x) return binarySearch(a, l, mid-1, x); else return binarySearch(a,mid+1, r, x); } return -1; } static boolean isPalindromeI(int n){ int d = 0; int y = n; while(y>0){ d++; y/=10; } int[] a = new int[d]; for (int i = 0; i < d; i++) { a[i] = n%10; n/=10; } for (int i = 0; i < d / 2; i++) { if(a[i] != a[d-i-1]) return false; } return true; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static long fast_pow(long a, long b) { //Jeel bhai OP if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } // If B is odd else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } // static class Pair implements Comparable<Pair>{ // // int x; // int y; // // Pair(int x, int y){ // this.x = x; // this.y = y; // } // // public int compareTo(Pair o){ // // int ans = Integer.compare(x, o.x); // if(o.x == x) ans = Integer.compare(y, o.y); // // return ans; // //// int ans = Integer.compare(y, o.y); //// if(o.y == y) ans = Integer.compare(x, o.x); //// //// return ans; // } // } static class Tuple implements Comparable<Tuple>{ int x, y, id; Tuple(int x, int y, int id){ this.x = x; this.y = y; this.id = id; } public int compareTo(Tuple o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } public static class Pair<U extends Comparable<U>, V extends Comparable<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 hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public int compareToY(Pair<U, V> b) { int cmpU = y.compareTo(b.y); return cmpU != 0 ? cmpU : x.compareTo(b.x); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } 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()); } char nextChar() { return next().charAt(0); } boolean nextBoolean() { return !(nextInt() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } private static int[] mergeSort(int[] array) { //array.length replaced with ctr int ctr = array.length; if (ctr <= 1) { return array; } int midpoint = ctr / 2; int[] left = new int[midpoint]; int[] right; if (ctr % 2 == 0) { right = new int[midpoint]; } else { right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } left = mergeSort(left); right = mergeSort(right); int[] result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while (leftPointer < left.length || rightPointer < right.length) { if (leftPointer < left.length && rightPointer < right.length) { if (left[leftPointer] < right[rightPointer]) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } else if (leftPointer < left.length) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } return result; } public static void Sieve(int n) { Arrays.fill(primecheck, true); primecheck[0] = false; primecheck[1] = false; for (int i = 2; i * i < n + 1; i++) { if (primecheck[i]) { for (int j = i * 2; j < n + 1; j += i) { primecheck[j] = false; } } } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
0882c75fd6e0ec8bc54f0b56f657abf0
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
//Harsh Gour code import java.util.*; import java.io.*; import java.lang.*; public class Solution { // Arrays.sort(arr, (a, b) -> a[1] - b[1]); // this is used to to sort value pairs /* String str ="3"; int p = Integer.parseInt(str); System.out.println(p); */ // Using stringBuilder functions /* StringBuilder sb =new StringBuilder(); sb.append("Hii my name is Harsh "); int age = 21; sb.append("My age is :"+age+"\n"); System.out.println(sb); */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub FastReader sc = new FastReader(System.in); int t =sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int m = sc.nextInt(); int[] dd = new int[n * m]; for (int h = 0, i = 0; i < n; i++) for (int j = 0; j < m; j++) dd[h++] = Math.max(i, n - 1 - i) + Math.max(j, m - 1 - j); Arrays.sort(dd); for (int h = 0; h < n * m; h++) System.out.print(dd[h] + " "); System.out.println(); } } // largest possible divisor of an number static int largestDivisor(int n) { if (n % 2 == 0) { return n / 2; } final int sqrtn = (int) Math.sqrt(n); for (int i = 3; i <= sqrtn; i += 2) { if (n % i == 0) { return n / i; } } return 1; } // for finding inverse static long calculate(long p, long q) { long mod = 998244353, expo; expo = mod - 2; // Loop to find the value // until the expo is not zero while (expo != 0) { // Multiply p with q // if expo is odd if ((expo & 1) == 1) { p = (p * q) % mod; } q = (q * q) % mod; // Reduce the value of // expo by 2 expo >>= 1; } return p; } //GCD static int gcd(int a, int b) { if (a == 0) return b; else return gcd(b % a, a); } // optimal power function static int power(int x, int y) { if (y == 0) return 1; else if (y % 2 == 0) return power(x, y / 2) * power(x, y / 2); else return x * power(x, y / 2) * power(x, y / 2); } // optimal power function end static int minimumadjacentswapstomakearraysidentical(char []s1, char[] s2, int size) { int i = 0, j = 0; int result = 0; // Iterate over the first string and convert // every element equal to the second string while (i < size) { j = i; // Find index element of first string which // is equal to the ith element of second string while (s1[j] != s2[i]) { j += 1; } // Swap adjacent elements in first string so // that element at ith position becomes equal while (i < j) { // Swap elements using temporary variable char temp = s1[j]; s1[j] = s1[j - 1]; s1[j - 1] = temp; j -= 1; result += 1; } i += 1; } return result; } //end// // sorted pair by its key value /* StringBuilder sb =new StringBuilder(); Map<Integer , Integer> map = new HashMap<>(); TreeMap<Integer,Integer> sorted=new TreeMap<Integer,Integer>(); sorted.putAll(map); for(Map.Entry<Integer , Integer>entry : sorted.entrySet()) { sb.append(entry.getKey()+" "+ entry.getValue()+ "\n"); } */ } //Pair class class Pair { int x; int y; //Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } // Fast Reader // FastReader in = new FastReader(System.in); class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
2aa3234ef15997443736c139c52b2227
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.*; import java.util.stream.*; import java.io.*; import java.lang.*; import java.math.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.log; import static java.lang.System.out; import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.MIN_VALUE; public class Codeforces { static int garr[] ; static FastReader fs; static boolean isPrimes[]; static int countprimes[]; static int mod= 1_000_000_000+7; static int max_seive = 1000000; static ArrayList<Integer> arr; public static void main(String args[]) throws Exception{ try{ System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch(Exception e){ } fs =new FastReader(); int tt = 1; tt = fs.nextInt(); while(tt--!=0) solve(); } public static void solve(){ int m = fs.nextInt(),n = fs.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); int dist[][] = new int[][]{{0,0},{0,n-1},{m-1,0},{m-1,n-1}}; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ int val = Integer.MIN_VALUE; for(int[] check : dist){ val = Math.max(val,Math.abs(i-check[0])+Math.abs(j-check[1])); } arr.add(val); } } Collections.sort(arr); for(int i=0;i<arr.size();i++){ print(arr.get(i)+" "); } println(""); } public static boolean isPallindrome(String val){ StringBuilder sb =new StringBuilder(val); return val.equals(sb.reverse().toString()); } public static boolean isVowel(char c){ return c=='a' || c=='e' || c=='i' || c=='o' || c=='u'; } public static void seive(){ countprimes = new int[max_seive]; isPrimes = new boolean[max_seive]; Arrays.fill(isPrimes,true); isPrimes[0] = false; isPrimes[1] = false; for(int i =2;i*i<=max_seive;i++){ if(isPrimes[i]){ for(int j =i*i;j<max_seive;j+=i){ isPrimes[j] = false; } } } int count =0; for(int i = 2;i<max_seive;i++){ if(isPrimes[i]){ count++; } countprimes[i] = count; } } static int gcd(int a,int b){ return (b==0)?a: gcd(b,a%b); } static void print(Object line){ System.out.print(line); } static void println(Object line){ System.out.println(line); } 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; } } static int[] readArray(int n){ int arr[]= new int[n]; for(int i=0;i<arr.length;i++){ arr[i] = fs.nextInt(); } return arr; } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
035c2e571530b651656ce5b7dff5d4f4
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import javax.management.MBeanRegistration; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; 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[] nm = Input.getArray(2); int n = nm[0]; int m = nm[1]; List<Integer> a = new ArrayList<>(); for(int i = 0 ; i < n ; i ++){ for(int j =0 ; j < m ; j++){ int tr = j + i; int tl = m - j + i-1; int br = n - i + j-1; int bl = n -i + m - j-2; a.add(Math.max(tr,Math.max(tl,Math.max(br,bl)))); } } Collections.sort(a); for(int i:a){ System.out.print( i + " "); } System.out.println(); } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
b959af83772269bc36e344bfd4ef03ee
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; 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 sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int k=0; int prev=(n)/2 +(m)/2; Queue<int[]> q=new LinkedList<>(); boolean vis[][]=new boolean[n][m]; int dx[]= {0,0,-1,1}; int dy[]= {-1,1,0,0}; if(n%2!=0 && m%2!=0) { q.add(new int[] {(n)/2,(m)/2}); vis[n/2][m/2]=true; } else if(n%2==0 && m%2==0) { int r=(n/2)-1,c=(m/2)-1; q.add(new int[] {r,c}); vis[r][c]=true; if(r+1<n) { q.add(new int[]{r+1,c}); vis[r+1][c]=true; } if(c+1<m) { q.add(new int[]{r,c+1}); vis[r][c+1]=true; } if(r+1<n && c+1<m) { q.add(new int[]{r+1,c+1}); vis[r+1][c+1]=true; } } else if(n%2==0 && m%2!=0){ int r=(n/2)-1,c=m/2; q.add(new int[] {r,c}); vis[r][c]=true; if(r+1<n) { q.add(new int[] {r+1,c}); vis[r+1][c]=true; } }else { int r=(n/2),c=(m/2)-1; q.add(new int[] {r,c}); vis[r][c]=true; if(c+1<m) { q.add(new int[] {r,c+1}); vis[r][c+1]=true; } } b:while(k<n*m) { int size=q.size(); for(int i=0;i<size;i++) { int x=q.peek()[0]; int y=q.peek()[1]; q.poll(); System.out.print(prev+" "); k++; if(k==m*n) break b; for(int j=0;j<4;j++) { if(x+dx[j]<n && x+dx[j]>=0 && y+dy[j]<m && y+dy[j]>=0 && !vis[x+dx[j]][y+dy[j]]) { q.add(new int[] {x+dx[j],y+dy[j]}); vis[x+dx[j]][y+dy[j]]=true; } } } prev++; } System.out.println(); } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
d2e434da724c6440db2a37b1e86ab9f4
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class CF766B { 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); } } 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(); Queue<Pair> q = new LinkedList<>(); boolean arr[][] = new boolean[n][m]; if(n%2==1&&m%2==1){ q.add(new Pair(n/2,m/2)); arr[n/2][m/2] = true; }else if(n%2==1&&m%2==0){ q.add(new Pair(n/2,m/2)); q.add(new Pair(n/2,m/2-1)); arr[n/2][m/2] = true; arr[n/2][m/2-1] = true; }else if(n%2==0&&m%2==1){ q.add(new Pair(n/2,m/2)); q.add(new Pair(n/2-1,m/2)); arr[n/2][m/2] = true; arr[n/2-1][m/2] = true; }else{ q.add(new Pair(n/2,m/2)); q.add(new Pair(n/2-1,m/2)); q.add(new Pair(n/2,m/2-1)); q.add(new Pair(n/2-1,m/2-1)); arr[n/2][m/2] = true; arr[n/2-1][m/2] = true; arr[n/2][m/2-1] = true; arr[n/2-1][m/2-1] = true; } ArrayList<Integer> ls = new ArrayList<>(); while(q.size()>0){ ls.add(q.size()); int x = q.size(); for(int i=0;i<x;i++){ Pair p = q.poll(); if(isValid(p.x,p.y+1,n,m)&&!arr[p.x][p.y+1]){ q.add(new Pair(p.x,p.y+1)); arr[p.x][p.y+1] = true; } if(isValid(p.x,p.y-1,n,m)&&!arr[p.x][p.y-1]){ q.add(new Pair(p.x,p.y-1)); arr[p.x][p.y-1] = true; } if(isValid(p.x-1,p.y,n,m)&&!arr[p.x-1][p.y]){ q.add(new Pair(p.x-1,p.y)); arr[p.x-1][p.y] = true; } if(isValid(p.x+1,p.y,n,m)&&!arr[p.x+1][p.y]){ q.add(new Pair(p.x+1,p.y)); arr[p.x+1][p.y] = true; } } } int num = n/2+m/2; for(int i=0;i<ls.size();i++){ for(int j=0;j<ls.get(i);j++){ System.out.print(num+" "); } num++; } System.out.println(); } } public static boolean isValid(int x,int y,int n,int m){ if(x<0||y<0||x==n||y==m){ return false; } return true; } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
6b0e945898c42e3946f4d5fd322047da
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.util.ArrayDeque; import java.util.Queue; import java.util.Scanner; public class pb4 { 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 nr = n/2 + n%2; int nc = m / 2 + m%2; int currDis = Math.abs(n - nr) + Math.abs(m - nc); Queue<int[]> q = new ArrayDeque<>(); int[][] visited = new int[n][m]; if (n%2 != 0 && m%2 != 0){ // odd odd q.add(new int[]{n/2 , m/2 , currDis}); visited[n/2][m/2] = 1; } else if (n%2 == 0 && m%2 != 0){ // even odd q.add(new int[]{n/2 , m/2 , currDis}); q.add(new int[]{n/2 - 1, m/2 , currDis}); visited[n/2][m/2] = 1; visited[n/2 - 1 ][m/2] = 1; } else if (n%2 != 0 && m%2 == 0){ // odd even q.add(new int[]{n/2 , m/2 , currDis}); q.add(new int[]{n/2 , m/2 - 1 , currDis}); visited[n/2][m/2] = 1; visited[n/2][m/2 - 1] = 1; } else{ q.add(new int[]{n/2 , m/2 , currDis}); q.add(new int[]{n/2 - 1, m/2 , currDis}); q.add(new int[]{n/2 , m/2 - 1 , currDis}); q.add(new int[]{n/2 - 1 , m/2 - 1 , currDis}); visited[n/2][m/2] = 1; visited[n/2][m/2 - 1] = 1; visited[n/2 - 1 ][m/2] = 1; visited[n/2 - 1][m/2 - 1] = 1; } while (!q.isEmpty()){ int[] arr = q.poll(); System.out.print(arr[2] + " "); int x = arr[0] , y = arr[1]; if (y + 1 < m && visited[x][y+1] == 0){ visited[x][y+1] = 1; q.add(new int[]{x , y+1 , arr[2] + 1}); } if (y-1 >= 0 && visited[x][y-1] == 0){ visited[x][y-1] = 1; q.add(new int[]{x , y-1 , arr[2]+1}); } if (x + 1 < n && visited[x+1][y] == 0){ visited[x+1][y] = 1; q.add(new int[]{x+1 , y , arr[2]+1}); } if (x - 1 >= 0 && visited[x-1][y] == 0){ visited[x-1][y] = 1; q.add(new int[]{x-1 , y , arr[2]+1}); } } System.out.println(); } } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
3ff2b8b8f050354333fe91659357b6ea
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
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.Collections; import java.util.List; public class NotSitting { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t!=0){ solve(br,pr); t--; } pr.flush(); pr.close(); } public static void solve(BufferedReader br,PrintWriter pr) throws IOException{ String[] temp=br.readLine().split(" "); int m=Integer.parseInt(temp[0]); int n=Integer.parseInt(temp[1]); List<Integer> list=new ArrayList<>(); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ int dist=Math.max(i-1,m-i)+Math.max(j-1,n-j); list.add(dist); } } Collections.sort(list); for(int i:list){ pr.print(i+" "); } pr.print('\n'); } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output
PASSED
2a6a8a88871f4981a77b9972d82ec836
train_109.jsonl
1642257300
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
256 megabytes
import java.io.*; import java.util.*; public class Q1672B { static int mod = (int) (1e9 + 7); static StringBuilder sb = new StringBuilder(); static void solve() { long n = l(); long m = l(); long start = m + n - 2; ArrayList<Long> list = new ArrayList<>(); // long[][] mat = new long[(int) n][(int) m]; if (n % 2 == 0 && m % 2 == 0) { for (int i = 0; i < n; i++) { long val = start; for (int j = 0; j < m; j++) { // mat[i][j] = val; list.add(val); if (j == (m / 2) - 1) { } else if (j > (m / 2) - 1) { val++; } else {// j<m/2 val--; } } if (i == (n / 2) - 1) { // nothnig to do } else if (i < (n / 2) - 1) { start--; } else { start++; } } } else if (m % 2 == 0) { for (int i = 0; i < n; i++) { long val = start; for (int j = 0; j < m; j++) { // mat[i][j] = val; list.add(val); if (j == (m / 2) - 1) { // nothing } else if (j > (m / 2) - 1) { val++; } else { val--; } } if (i < n / 2) { start--; } else { start++; } } } else if (n % 2 == 0) { for (int i = 0; i < n; i++) { long val = start; for (int j = 0; j < m; j++) { // mat[i][j] = val; list.add(val); if (j < m / 2) { val--; } else { val++; } } if (i == (n / 2) - 1) { // nothing } else if (i > (n / 2) - 1) { start++; } else { start--; } } } else { for (int i = 0; i < n; i++) { long val = start; for (int j = 0; j < m; j++) { // mat[i][j] = val; list.add(val); if (j < m / 2) { val--; } else { val++; } } if (i < n / 2) { start--; } else { start++; } } } // for (int i = 0; i < n; i++) { // for (int j = 0; j < m; j++) { // System.out.print(mat[i][j] + " "); // } // System.out.println(); // } Collections.sort(list); for(long val:list){ System.out.print(val+" "); } System.out.println(); } public static void main(String[] args) { int test = i(); while (test-- > 0) { solve(); } System.out.println(sb); } // -----> POWER ---> long power(long x, long y) <---- // -----> LCM ---> long lcm(long x, long y) <---- // -----> GCD ---> long gcd(long x, long y) <---- // -----> SIEVE --> ArrayList<Integer> sieve(int N) <----- // -----> NCR ---> long ncr(int n, int r) <---- // -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) <---- // -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<-- // ----> DFS ---> void dfs(ArrayList<ArrayList<Integer>>edges,int child,int // parent)<--- // ---> NODETOROOT --> ArrayList<Integer> // node2Root(ArrayList<ArrayList<Integer>>edges,int child,int parent,int tofind) // <-- // ---> LEVELS_TREE -->levels_Trees(ArrayList<HashSet<Integer>> edges, int // child, int parent,int[]level,int currLevel) <-- // ---> K_THPARENT --> int k_thparent(int node,int[][]parent,int k) <--- // ---> TWOPOWERITHPARENTFILL --> void twoPowerIthParentFill(int[][]parent) <--- // -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<- // tempstart static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static InputReader in = new InputReader(System.in); public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } }
Java
["2\n\n4 3\n\n1 2"]
1 second
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
Java 11
standard input
[ "games", "greedy", "sortings" ]
dc1dc5e5dd17d19760c772739ce244a7
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
1,300
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
standard output