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
40d9c3f2e9ef58c8615f87f64e9fd662
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.time.Clock; import java.time.LocalDateTime; import java.util.*; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; public class Solution { private void solve() throws IOException { int n = nextInt(); int m = nextInt(); char[][] s = new char[n][]; for (int i = 0; i < n; i++) { s[i] = nextTokenChars(); } int shapeId = 1; int[][] a = new int[n][m]; for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { int c = 0; for (int di = -1; di < 1; di++) { for (int dj = -1; dj < 1; dj++) { if (a[i + di][j + dj] != 0) c = -100; if (s[i + di][j + dj] == '*') c++; } } if (c == 3) { shapeId++; for (int di = -1; di < 1; di++) { for (int dj = -1; dj < 1; dj++) { if (s[i + di][j + dj] == '*') a[i + di][j + dj] = shapeId; } } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] == '*') { if (a[i][j] == 0) { out.println("NO"); return; } for (int di = -1; di < 2; di++) { for (int dj = -1; dj < 2; dj++) { if (i + di >= 0 && j + dj >= 0 && i + di < n && j + dj < m) { if (a[i + di][j + dj] != 0 && a[i + di][j + dj] != a[i][j]) { out.println("NO"); return; } } } } } } } out.println("YES"); } private static final boolean runNTestsInProd = true; private static final boolean printCaseNumber = false; private static final boolean assertInProd = false; private static final boolean logToFile = false; private static final boolean readFromConsoleInDebug = false; private static final boolean writeToConsoleInDebug = true; private static final boolean testTimer = false; private static Boolean isDebug = null; private BufferedReader in; private StringTokenizer line; private PrintWriter out; public static void main(String[] args) throws Exception { isDebug = Arrays.asList(args).contains("DEBUG_MODE"); if (isDebug) { log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out); clock = Clock.systemDefaultZone(); } new Solution().run(); } private void run() throws Exception { in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt"))); out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt"); try (Timer totalTimer = new Timer("total")) { int t = runNTestsInProd || isDebug ? nextInt() : 1; for (int i = 0; i < t; i++) { if (printCaseNumber) { out.print("Case #" + (i + 1) + ": "); } if (testTimer) { try (Timer testTimer = new Timer("test #" + (i + 1))) { solve(); } } else { solve(); } if (isDebug) { out.flush(); } } } in.close(); out.flush(); out.close(); } private void println(Object... objects) { boolean isFirst = true; for (Object o : objects) { if (!isFirst) { out.print(" "); } else { isFirst = false; } out.print(o.toString()); } out.println(); } private int[] nextIntArray(int n) throws IOException { return nextIntArray(n, 0); } private int[] nextIntArray(int n, int delta) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt() + delta; } return res; } private long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private char[] nextTokenChars() throws IOException { return nextToken().toCharArray(); } private String nextToken() throws IOException { while (line == null || !line.hasMoreTokens()) { line = new StringTokenizer(in.readLine()); } return line.nextToken(); } private static void assertPredicate(boolean p) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(); } } private static void assertPredicate(boolean p, String message) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(message); } } private static <T> void assertNotEqual(T unexpected, T actual) { if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) { throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual); } } private static <T> void assertEqual(T expected, T actual) { if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) { throw new RuntimeException("assertEqual: " + expected + " != " + actual); } } private static PrintWriter log = null; private static Clock clock = null; private static void log(Object... objects) { log(true, objects); } private static void logNoDelimiter(Object... objects) { log(false, objects); } private static void log(boolean printDelimiter, Object[] objects) { if (isDebug) { StringBuilder sb = new StringBuilder(); sb.append(LocalDateTime.now(clock)).append(" - "); boolean isFirst = true; for (Object o : objects) { if (!isFirst && printDelimiter) { sb.append(" "); } else { isFirst = false; } sb.append(o.toString()); } log.println(sb); log.flush(); } } private static class Timer implements Closeable { private final String label; private final long startTime = isDebug ? System.nanoTime() : 0; public Timer(String label) { this.label = label; } @Override public void close() throws IOException { if (isDebug) { long executionTime = System.nanoTime() - startTime; String fraction = Long.toString(executionTime / 1000 % 1_000_000); logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's'); } } } private static <T> T timer(String label, Supplier<T> f) throws Exception { if (isDebug) { try (Timer timer = new Timer(label)) { return f.get(); } } else { return f.get(); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 8
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
c7918fd3afbb33672e28bdc6e2d1c0ad
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static char[][][] comp = { {{'-', '.', '.', '.'}, {'.', '.', '*', '.'}, {'.', '*', '*', '.'}, {'.', '.', '.', '.'}}, {{'.', '.', '.', '.'}, {'.', '*', '.', '.'}, {'.', '*', '*', '.'}, {'.', '.', '.', '.'}}, {{'.', '.', '.', '.'}, {'.', '*', '*', '.'}, {'.', '.', '*', '.'}, {'-', '.', '.', '.'}}, {{'.', '.', '.', '.'}, {'.', '*', '*', '.'}, {'.', '*', '.', '.'}, {'.', '.', '.', '-'}} }; static int[] dr = {-1, -1, -1, -1}; static int[] dc = {-2, -1, -1, -1}; static int[] fr = {-1, 0, 1, 0}; static int[] fc = {0, -1, 0, 1}; static int R; static int C; static char[][] map; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); tcase : for (int tNum = 1; tNum <= t; tNum++) { StringTokenizer st = new StringTokenizer(br.readLine()); R = Integer.parseInt(st.nextToken()); C = Integer.parseInt(st.nextToken()); map = new char[R + 4][C + 4]; for (int i = 0; i < R + 4; i++) { for (int j = 0; j < C + 4; j++) { map[i][j] = '.'; } } for (int i = 2; i <= R + 1; i++) { String str = br.readLine(); for (int j = 2; j <= C + 1; j++) { map[i][j] = str.charAt(j - 2); } } for (int i = 2; i <= R + 1; i++) { cells : for (int j = 2; j <= C + 1; j++) { if (map[i][j] == '*') { boolean ok = false; for(int c = 0; c < 4; c++) { ok = check(c, i, j); if (ok) { fill(i, j); continue cells; } } if (!ok) { sb.append("NO\n"); continue tcase; } } } } sb.append("YES\n"); } System.out.print(sb.toString()); } public static boolean check(int c, int row, int col) { int sR = row + dr[c]; int sC = col + dc[c]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { switch (comp[c][i][j]) { case '*': if (map[sR + i][sC + j] == '.') { return false; } break; case '.': if (map[sR + i][sC + j] == '*') { return false; } break; } } } return true; } public static void fill(int row, int col) { if (map[row][col] == '.') { return; } map[row][col] = '.'; for (int f = 0; f < 4; f++) { fill(row + fr[f], col + fc[f]); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 8
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
5729ac8a95159380d3d0e71d20e1ce4a
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class F { static FastReader sc; static void solve() { StringBuilder res = new StringBuilder(); int n = sc.nextInt(); int m = sc.nextInt(); char[][] arr = new char[n][m]; for (int i = 0; i < n; i++) { arr[i] = sc.next().toCharArray(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == '*') { if (j + 1 < m && i + 1 < n && arr[i][j + 1] == '*' && arr[i + 1][j + 1] == '*') { arr[i][j] = '.'; arr[i][j + 1] = '.'; arr[i + 1][j + 1] = '.'; if (check(arr, i, j) || check(arr, i, j + 1) || check(arr, i + 1, j + 1)) { print("NO"); return; } } else if (j + 1 < m && i + 1 < n && arr[i + 1][j] == '*' && arr[i + 1][j + 1] == '*') { arr[i][j] = '.'; arr[i + 1][j] = '.'; arr[i + 1][j + 1] = '.'; if (check(arr, i, j) || check(arr, i + 1, j) || check(arr, i + 1, j + 1)) { print("NO"); return; } } else if (j - 1 >= 0 && i + 1 < n && arr[i + 1][j - 1] == '*' && arr[i + 1][j] == '*') { arr[i][j] = '.'; arr[i + 1][j - 1] = '.'; arr[i + 1][j] = '.'; if (check(arr, i, j) || check(arr, i + 1, j - 1) || check(arr, i + 1, j)) { print("NO"); return; } } else if (j + 1 < m && i + 1 < n && arr[i + 1][j] == '*' && arr[i][j + 1] == '*') { arr[i][j] = '.'; arr[i + 1][j] = '.'; arr[i][j + 1] = '.'; if (check(arr, i, j) || check(arr, i + 1, j) || check(arr, i, j + 1)) { print("NO"); return; } } else { print("NO"); return; } } } } print("YES"); } static boolean check(char[][] arr, int i, int j) { for (int j2 = i - 1; j2 <= i + 1; j2++) { for (int k = j - 1; k <= j + 1; k++) { if (j2 >= 0 && j2 < arr.length && k >= 0 && k < arr[0].length && arr[j2][k] == '*') return true; } } return false; } public static void main(String[] args) throws IOException { sc = new FastReader(); int tt = sc.nextInt(); while (tt-- > 0) { solve(); } } static <E> void debug(E a) { System.err.println(a); } static void debug(int... a) { System.err.println(Arrays.toString(a)); } static int maxOf(int... array) { return Arrays.stream(array).max().getAsInt(); } static int minOf(int... array) { return Arrays.stream(array).parallel().reduce(Math::min).getAsInt(); } static <E> void print(E res) { System.out.println(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[] 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
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 8
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
a915d8de4c87de009f1215813841d192
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
//package codeforces.round817div4; import java.io.*; import java.util.*; import static java.lang.Math.*; public class F { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; 6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial. If getting stuck, skip it and solve other similar level problems. Wait for 1 week then try to solve again. Only read editorial after you solved a problem. 7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already. 8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you rushed to coding! */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a thorough idea steps! 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? */ static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(), m = in.nextInt(); char[][] g = new char[n][]; for(int i = 0; i < n; i++) { g[i] = in.next().toCharArray(); } boolean ans = true; //int[][] v = new int[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(g[i][j] == '*') { List<int[]> p = new ArrayList<>(); p.add(new int[]{i, j}); //int cnt = 1; for(int k1 = -1; k1 <= 1; k1++) { for(int k2 = -1; k2 <= 1; k2++) { int x = i + k1, y = j + k2; if(x >= 0 && x < n && y >= 0 && y < m && !(x == i && y == j) && g[x][y] == '*') { p.add(new int[]{x, y}); } } } if(p.size() != 3) { ans = false; break; } ans &= check(p); if(!ans) { break; } } } if(!ans) { break; } } out.println(ans ? "YES" : "NO"); } out.close(); } static boolean check(List<int[]> p) { Collections.sort(p, (e1,e2)-> { if(e1[0] != e2[0]) { return e1[0] - e2[0]; } return e1[1] - e2[1]; }); int i = p.get(0)[0], j = p.get(0)[1]; return p.get(1)[0] == i + 1 && p.get(1)[1] == j && p.get(2)[0] == i + 1 && p.get(2)[1] == j + 1 || p.get(1)[0] == i + 1 && p.get(1)[1] == j - 1 && p.get(2)[0] == i + 1 && p.get(2)[1] == j || p.get(1)[0] == i && p.get(1)[1] == j + 1 && p.get(2)[0] == i + 1 && p.get(2)[1] == j + 1 || p.get(1)[0] == i && p.get(1)[1] == j + 1 && p.get(2)[0] == i + 1 && p.get(2)[1] == j; } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } 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(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building an undirected weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildUndirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]][0] = end2[i]; adj[end1[i]][idxForEachNode[end1[i]]][1] = weight[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]][0] = end1[i]; adj[end2[i]][idxForEachNode[end2[i]]][1] = weight[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } /* A more efficient way of building a directed weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildDirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]][0] = to[i]; adj[from[i]][idxForEachNode[from[i]]][1] = weight[i]; idxForEachNode[from[i]]++; } return adj; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 8
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
6934b73d1bb6950a21073e41bfcfb34f
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args){ MyScanner scanner = new MyScanner(); int numOfTests = scanner.nextInt(); for(int t = 1; t <= numOfTests; t++){ int m = scanner.nextInt(), n = scanner.nextInt(); char[][] grid = new char[m][n]; for(int i = 0; i < m; i++){ String s = scanner.nextLine(); for(int j = 0; j < n; j++){ grid[i][j] = s.charAt(j); } } boolean res = solveF(grid, m, n); String output = res? "YES" : "NO"; out.println(output); } out.close(); } static int[] dx = {-1,1,0,0,1,1,-1,-1}; static int[] dy = {0,0,-1,1,-1,1,1,-1}; private static boolean containsInL(List<int[]> all, int x, int y){ for(int[] l: all){ if(l[0] == x && l[1] == y){ return true; } } return false; } public static boolean solveF(char[][] grid, int m, int n){ boolean[][] visited = new boolean[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(grid[i][j] == '*' && !visited[i][j]){ Queue<int[]> q = new LinkedList<>(); q.offer(new int[]{i, j}); List<int[]> all = new ArrayList<>(); visited[i][j] = true; while(!q.isEmpty()){ int[] curr = q.poll(); all.add(curr); for(int d = 0; d < 4; d++){ int nx = curr[0] + dx[d]; int ny = curr[1] + dy[d]; if(nx >= 0 && ny >= 0 && nx < m && ny < n && grid[nx][ny] == '*' && !visited[nx][ny]){ q.offer(new int[]{nx, ny}); visited[nx][ny] = true; } } } if(all.size() != 3){ // out.println(all.size()); // for(int[] x: all){ // out.println(Arrays.toString(x)); // } // out.println(i + " " + j); return false; } if(all.get(0)[0] == all.get(1)[0] && all.get(2)[0] == all.get(1)[0]){ return false; } if(all.get(0)[1] == all.get(1)[1] && all.get(2)[1] == all.get(1)[1]){ return false; } for(int d = 4; d < 8; d++){ for(int[] l: all){ int nx = l[0] + dx[d]; int ny = l[1] + dy[d]; if(nx >= 0 && ny >= 0 && nx < m && ny < n && grid[nx][ny] == '*' && !containsInL(all, nx, ny)){ //out.println("here"); return false; } } } } } } return true; } 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
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 8
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
b859c44da14781080c058a2df13dfe06
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class myclass { 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()); } } static class node { int a , b; node(int x , int y) { a = x; b = y; } } static ArrayList<node> al; 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(); char arr[][] = new char[n][m]; for(int i=0 ; i<n ; i++) { String s = sc.next(); for(int j=0 ; j<m ; j++) { arr[i][j] = s.charAt(j); } } boolean ans = true; for(int i=0 ; i<n && ans; i++) { for(int j=0 ; j<m ; j++) { if(arr[i][j] == '*') { al = new ArrayList<>(); if(!check(arr , i , j , n , m)) { ans = false; break; } } } } System.out.println(ans ? "YES" : "NO"); } } static int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; static int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; static boolean check(char arr[][] , int a , int b , int n , int m) { dfs(arr , a , b , n , m); if(al.size()!=3) return false; int ar[] = {al.get(0).a , al.get(1).a , al.get(2).a}; int br[] = {al.get(0).b , al.get(1).b , al.get(2).b}; Arrays.sort(ar); Arrays.sort(br); if(ar[2]-ar[0]!=1) return false; if(br[2]-br[0]!=1) return false; return true; } static void dfs(char arr[][] , int a , int b , int n , int m) { arr[a][b] = '.'; al.add(new node(a , b)); for(int i=0 ; i<8 ; i++) { int x = a + dx[i]; int y = b + dy[i]; if(x>=0 && y>=0 && x<n && y<m && arr[x][y]=='*') { dfs(arr , x , y , n , m); } } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 8
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
5b36bbd0651c52114b0e4843f31e105e
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { Reader in = new Reader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for (int t = 0; t < T; t++) { int N = in.nextInt(), M = in.nextInt(); char[][] mat = new char[N][M]; for (int i = 0; i < N; i++) { mat[i] = in.next().toCharArray(); } boolean[][] visited = new boolean[N][M]; int[] x = new int[]{-1, 1, 0, 0}, y = new int[]{0, 0, -1, 1}; boolean valid = true; i: for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (!visited[i][j] && mat[i][j] == '*') { Queue<int[]> q = new LinkedList<>(); q.offer(new int[]{i, j}); visited[i][j] = true; int size = 0, minR = Integer.MAX_VALUE, maxR = Integer.MIN_VALUE, minC = Integer.MAX_VALUE, maxC = Integer.MIN_VALUE; while (!q.isEmpty()) { int[] current = q.poll(); int r = current[0], c = current[1]; size++; minR = Math.min(minR, r); maxR = Math.max(maxR, r); minC = Math.min(minC, r); maxC = Math.max(maxC, r); for (int k = 0; k < 4; k++) { if (r + x[k] >= 0 && r + x[k] < N && c + y[k] >= 0 && c + y[k] < M) { if (mat[r + x[k]][c + y[k]] == '*' && !visited[r + x[k]][c + y[k]]) { visited[r + x[k]][c + y[k]] = true; q.offer(new int[]{r + x[k], c + y[k]}); } } } } if (size != 3 || maxR - minR != 1 || maxC - minC != 1) { valid = false; break i; } } if (i + 1 < N && j + 1 < M) { if (mat[i][j] == '*' && mat[i + 1][j + 1] == '*' && mat[i + 1][j] == '.' && mat[i][j + 1] == '.') { valid = false; break i; } if (mat[i][j] == '.' && mat[i + 1][j + 1] == '.' && mat[i + 1][j] == '*' && mat[i][j + 1] == '*') { valid = false; break i; } } } } out.println(valid ? "YES" : "NO"); } out.close(); } static class Reader { BufferedReader in; StringTokenizer st; public Reader() { in = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } public static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 8
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
e61b1bf5f12e9e78d3583a75d4cd8178
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class P1722F { public static class Point { int r; int c; public Point(int r, int c) { this.r = r; this.c = c; } } static int[] dr = {-1, 0, 1, 0, -1, -1, 1, 1}; static int[] dc = {0, -1, 0, 1, -1, 1, -1, 1}; public static void solve (int m, int n, boolean[][] grid){ List<List<Point>> cc = new ArrayList<>(); boolean[][] vis = new boolean[m][n]; for (int i=0; i < m; i++){ for (int j=0; j < n; j++){ if (grid[i][j] && !vis[i][j]){ vis[i][j] = true; Queue<Point> q = new LinkedList<>(); q.add(new Point (i, j)); List<Point> curList = new ArrayList<>(); while (!q.isEmpty()){ Point cur = q.remove(); curList.add(cur); for (int k=0; k < 8; k++){ int nr = cur.r + dr[k]; int nc = cur.c + dc[k]; if (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] && !vis[nr][nc]){ vis[nr][nc] = true; q.add(new Point (nr, nc)); } } } cc.add(curList); } } } if (cc.size() == 0){ System.out.println("YES"); return; } for (List<Point> i : cc){ if (i.size() != 3){ System.out.println("NO"); return; } Point p1 = i.get(0); Point p2 = i.get(1); Point p3 = i.get(2); if (!(works (p1, p2, p3) || works (p1, p3, p2) || works (p2, p1, p3) || works (p2, p3, p1) || works (p3, p1, p2) || works (p3, p2, p1))){ System.out.println("NO"); return; } } System.out.println("YES"); } public static boolean works (Point p1, Point p2, Point p3){ if (Math.abs(p1.r-p2.r) == 1 && p1.c == p2.c){ if (Math.abs(p1.c-p3.c) == 1 && p1.r == p3.r){ return true; } } return false; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int test = 0; test < t; test++){ StringTokenizer st = new StringTokenizer(br.readLine()); int m = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(st.nextToken()); boolean[][] grid = new boolean[m][n]; for (int i=0; i < m; i++){ String s = br.readLine(); for (int j=0; j < n; j++){ grid[i][j] = s.charAt(j) == '*'; } } solve (m, n, grid); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
78b91e2b3cd6db044b92f03bde41c02b
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class f { //FastReader public 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; } } //QuickSort public static class QuickSort { int array[]; int length; public void sort(int[] inputArr) { if (inputArr == null || inputArr.length == 0) { return; } this.array = inputArr; length = inputArr.length; quickSort(0, length - 1); } public void quickSort(int lowerIndex, int higherIndex) { int i = lowerIndex; int j = higherIndex; // calculate pivot number, I am taking pivot as middle index number int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2]; // Divide into two arrays while (i <= j) { /** * In each iteration, we will identify a number from left side which * is greater then the pivot value, and also we will identify a number * from right side which is less then the pivot value. Once the search * is done, then we exchange both numbers. */ while (array[i] < pivot) { i++; } while (array[j] > pivot) { j--; } if (i <= j) { exchangeNumbers(i, j); //move index to next position on both sides i++; j--; } } // call quickSort() method recursively if (lowerIndex < j) quickSort(lowerIndex, j); if (i < higherIndex) quickSort(i, higherIndex); } private void exchangeNumbers(int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } //Power Algo public static long power(long x, long n) { long pow = 1; // loop till `n` become 0 while (n > 0) { // if `n` is odd, multiply the result by `x` if ((n & 1) == 1) { pow *= x; } // divide `n` by 2 n = n >> 1; // multiply `x` by itself x = x * x; } // return result return pow; } //Graph & DFS public static class Graph { int V; //number of nodes LinkedList<Integer>[] adj; //adjacency list Graph(int V) { this.V = V; adj = new LinkedList[V]; for (int i = 0; i < adj.length; i++) adj[i] = new LinkedList<Integer>(); } public void addEdge(int v, int w) { adj[v].add(w); //adding an edge to the adjacency list (edges are bidirectional in this example) } public List<Integer> DFS(int n) { boolean nodes[] = new boolean[V]; List<Integer> ll=new ArrayList<>(); ll.add(0); Stack<Integer> stack = new Stack<>(); stack.push(n); //push root node to the stack int a = 0; while(!stack.empty()) { n = stack.peek(); //extract the top element of the stack stack.pop(); //remove the top element from the stack if(nodes[n] == false) { nodes[n] = true; } for (int i = 0; i < adj[n].size(); i++) //iterate through the linked list and then propagate to the next few nodes { a = adj[n].get(i); if (!nodes[a]) //only push those nodes to the stack which aren't in it already { stack.push(a); ll.add(a); //push the top element to the stack } } } return ll; } } //Dijkstra's Algo public static class Dijkstra { // Member variables of this class private int dist[]; private Set<Integer> settled; private PriorityQueue<Node> pq; // Number of vertices private int V; List<List<Node> > adj; // Constructor of this class public Dijkstra(int V) { // This keyword refers to current object itself this.V = V; dist = new int[V]; settled = new HashSet<Integer>(); pq = new PriorityQueue<Node>(V, new Node()); } // Method 1 // Dijkstra's Algorithm public void dijkstra(List<List<Node>> adj2, int src) { this.adj = adj2; for (int i = 0; i < V; i++) dist[i] = Integer.MAX_VALUE; // Add source node to the priority queue pq.add(new Node(src, 0)); // Distance to the source is 0 dist[src] = 0; while (settled.size() != V) { // Terminating condition check when // the priority queue is empty, return if (pq.isEmpty()) return; // Removing the minimum distance node // from the priority queue int u = pq.remove().node; // Adding the node whose distance is // finalized if (settled.contains(u)) // Continue keyword skips exwcution for // following check continue; // We don't have to call e_Neighbors(u) // if u is already present in the settled set. settled.add(u); e_Neighbours(u); } } // Method 2 // To process all the neighbours // of the passed node private void e_Neighbours(int u) { int edgeDistance = -1; int newDistance = -1; // All the neighbors of v for (int i = 0; i < adj.get(u).size(); i++) { Node v = adj.get(u).get(i); // If current node hasn't already been processed if (!settled.contains(v.node)) { edgeDistance = v.cost; newDistance = dist[u] + edgeDistance; // If new distance is cheaper in cost if (newDistance < dist[v.node]) dist[v.node] = newDistance; // Add the current node to the queue pq.add(new Node(v.node, dist[v.node])); } } } } //Necessary Supporting F/n for Dijkstra's Algo public static class Node implements Comparator<Node> { // Member variables of this class public int node; public int cost; // Constructors of this class // Constructor 1 public Node() {} // Constructor 2 public Node(int node, int cost) { // This keyword refers to current instance itself this.node = node; this.cost = cost; } // Method 1 @Override public int compare(Node node1, Node node2) { if (node1.cost < node2.cost) return -1; if (node1.cost > node2.cost) return 1; return 0; } } //BinarySearch to find exact or closest element public static class BinarySearch{ public static int k=0; public static int binarySearch(int arr[], int first, int last, int key){ k = (first + last)/2; while( first <= last ){ if ( arr[k] < key ){ first = k + 1; }else if (arr[k] == key ){ break; }else{ last = k - 1; } k = (first + last)/2; } return k; } } //GCD public static int gcd(int a,int b) { int min=Math.min(a, b); int max=Math.max(a, b); int mod=max%min; if(mod==0) return min; else return gcd(mod,min); } //highest Power of 2 less than or equal to the no. public static int highestPowerof2(int n) { int p = (int)(Math.log(n) / Math.log(2)); return (int)power(2,p); } public static void main(String[] args) { try { FastReader fr=new FastReader(); QuickSort qs=new QuickSort(); StringBuilder sb=new StringBuilder(); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int t=fr.nextInt(); while(t-->0) { int n=fr.nextInt(); int m=fr.nextInt(); char mat[][]=new char[n][m]; String s=""; for(int i=0;i<n;i++) { s=fr.next(); for(int j=0;j<m;j++) { mat[i][j]=s.charAt(j); } } int val[][]=new int[n][m]; int adj[][]=new int[n][m]; int diag[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(i>0 && mat[i-1][j]=='*') adj[i][j]++; if(j>0 && mat[i][j-1]=='*') adj[i][j]++; if(i<n-1 && mat[i+1][j]=='*') adj[i][j]++; if(j<m-1 && mat[i][j+1]=='*') adj[i][j]++; if(i>0 && j>0 && mat[i-1][j-1]=='*') { diag[i][j]++; } if(i>0 && j<m-1 && mat[i-1][j+1]=='*' ) { diag[i][j]++; } if(i<n-1 && j<m-1 && mat[i+1][j+1]=='*' ) { diag[i][j]++; } if(i<n-1 && j>0 && mat[i+1][j-1]=='*' ) { diag[i][j]++; } } } int p=0; int r=1; int cnt=0; int dir=0; int u=0,v=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(adj[i][j]>2) { p=1; break; } else if(adj[i][j]==2 && mat[i][j]=='*') { cnt=0; dir=0; if(i>0 && mat[i-1][j]=='*') { if(adj[i-1][j]==1 && val[i-1][j]==0) { val[i-1][j]=r; r++; dir=1; cnt++; if(diag[i-1][j]>1) { p=1; break; } } } if(i<n-1 && mat[i+1][j]=='*' && dir!=1) { if(adj[i+1][j]==1 && val[i+1][j]==0) { val[i+1][j]=r; r++; cnt++; if(diag[i+1][j]>1) { p=1; break; } } } if(j>0 && mat[i][j-1]=='*') { if(adj[i][j-1]==1 && val[i][j-1]==0) { val[i][j-1]=r; r++; dir=2; cnt++; if(diag[i][j-1]>1) { p=1; break; } } } if(j<m-1 && mat[i][j+1]=='*' && dir!=2) { if(adj[i][j+1]==1 && val[i][j+1]==0) { val[i][j+1]=r; r++; cnt++; if(diag[i][j+1]>1) { p=1; break; } } } if(cnt==2 && mat[i][j]=='*') { val[i][j]=r; r++; } if(i>0 && j>0 && adj[i-1][j-1]>0 && mat[i-1][j-1]=='*' && val[i][j]!=0) { p=1; break; } if(i>0 && j<m-1 && adj[i-1][j+1]>0 && mat[i-1][j+1]=='*' && val[i][j]!=0) { p=1; break; } if(i<n-1 && j<m-1 && adj[i+1][j+1]>0 && mat[i+1][j+1]=='*' && val[i][j]!=0) { p=1; break; } if(i<n-1 && j>0 && adj[i+1][j-1]>0 && mat[i+1][j-1]=='*' && val[i][j]!=0) { p=1; break; } } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mat[i][j]=='*' && val[i][j]==0) { p=1; break; } } } if(p==0) sb.append("YES\n"); else sb.append("NO\n"); } output.write(sb+""); output.close(); } catch(Exception e) { return; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
3b5fdf3cab8b07fb09bdf0a57c367261
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.math.BigInteger; public class Main{ static class Node { long sum, pre; Node(long a, long b) { this.sum = a; this.pre = b; } } static class SegmentTree { int l , r; // range responsible for SegmentTree left , right; long val; SegmentTree(int l,int r,long a[]) { this.l = l; this.r = r; // list= new ArrayList<>(); if(l == r) { val = a[l]; return ; } int mid = l + (r-l)/2; this.left = new SegmentTree(l ,mid , a); this.right = new SegmentTree(mid + 1 , r,a); this.val = Math.max(this.left.val , this.right.val); } public long query(int left ,int right) { if(this.l > right || this.r < left) { return 0l; } if(this.l >= left && this.r <= right) { return this.val; } return this.left.query(left , right ) + this.right.query(left , right ); } // public void pointUpdate(int index , long val) { // if(this.l > index || this.r < index) return; // if(this.l == this.r && this.l == index) { // this.val = val; // return ; // } // this.left.pointUpdate(index ,val); // this.right.pointUpdate(index , val); // this.val = join(this.left.val , this.right.val); // } // public void rangeUpdate(int left , int right,long val) { // if(this.l > right || this.r < left) return ; // if(this.l >= left && this.r <= right) { // this.val += val; // } // if(this.l == this.r) return; // this.left.rangeUpdate(left , right , val); // this.right.rangeUpdate(left , right , val); // } // public long valueAtK(int k) { // if(this.l > k || this.r < k) return 0; // if(this.l == this.r && this.l == k) { // return this.val; // } // return join(this.left.valueAtK(k) , this.right.valueAtK(k)); // } public int join(int a ,int b) { return a + b; } } static class Hash { long hash[] ,mod = (long)1e9 + 7 , powT[] , prime , inverse[]; Hash(char []s) { prime = 131; int n = s.length; powT = new long[n]; hash = new long[n]; inverse = new long[n]; powT[0] = 1; inverse[n-1] = pow(pow(prime , n-1 ,mod), mod-2 , mod); for(int i = 1;i < n; i++ ) { powT[i] = (powT[i-1]*prime)%mod; } for(int i = n-2; i>= 0;i-=1) { inverse[i] = (inverse[i+1]*prime)%mod; } hash[0] = (s[0] - 'a' + 1); for(int i = 1; i < n;i++ ) { hash[i] = hash[i-1] + ((s[i]-'a' + 1)*powT[i])%mod; hash[i] %= mod; } } public long hashValue(int l , int r) { if(l == 0) return hash[r]%mod; long ans = hash[r] - hash[l-1] +mod; ans %= mod; // System.out.println(inverse[l] + " " + pow(powT[l], mod- 2 , mod)); ans *= inverse[l]; ans %= mod; return ans; } } static class ConvexHull { Stack<Integer>stack; Stack<Integer>stack1; int n; Point arr[]; ConvexHull(Point arr[]) { n = arr.length; this.arr = arr; Arrays.sort(arr , (a , b)-> { if(a.x == b.x) return (int)(b.y-a.y); return (int)(a.x-b.x); }); Point min = arr[0]; stack = new Stack<>(); stack1 = new Stack<>(); stack.push(0); stack1.push(0); Point ob = new Point(2,2); for(int i =1;i < n;i++) { if(stack.size() < 2) stack.push(i); else { while(stack.size() >= 2) { int a = stack.pop() , b = stack.pop() ,c = i; int dir = ob.cross(arr[b] , arr[a] , arr[c]); if(dir < 0) { stack.push(b); stack.push(a); stack.push(c); break; } stack.push(b); } if(stack.size() < 2) { stack.push(i); } } } for(int i =1;i < n;i++) { if(stack1.size() < 2) stack1.push(i); else { while(stack1.size() >= 2) { int a = stack1.pop() , b = stack1.pop() ,c = i; int dir = ob.cross(arr[b] , arr[a] , arr[c]); if(dir > 0) { stack1.push(b); stack1.push(a); stack1.push(c); break; } stack1.push(b); } if(stack1.size() < 2) { stack1.push(i); } } } } public List<Point> getPoints() { boolean vis[] = new boolean[n]; List<Point> list = new ArrayList<>(); // for(int x : stack) { // list.add(arr[x]); // vis[x] = true; // } for(int x : stack1) { // if(vis[x]) continue; list.add(arr[x]); } return list; } } public static class Suffix implements Comparable<Suffix> { int index; int rank; int next; public Suffix(int ind, int r, int nr) { index = ind; rank = r; next = nr; } public int compareTo(Suffix s) { if (rank != s.rank) return Integer.compare(rank, s.rank); return Integer.compare(next, s.next); } } static class Point { long x , y; Point(long x , long y) { this.x = x; this.y = y; } // public String toString() { // return this.x + " " + this.y; // } public Point sub(Point a,Point b) { return new Point(a.x - b.x , a.y-b.y); } public int cross(Point a ,Point b , Point c) { Point g = sub(b,a) , l = sub(c, b); long ans = g.y*l.x - g.x*l.y; if(ans == 0) return 0; if(ans < 0) return -1; return 1; } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } static Kattio sc = new Kattio(); static long mod = 998244353l; // static Kattio out = new Kattio(); //Heapify function to maintain heap property. public static void swap(int i,int j,int arr[]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static long max(long ...a) { return maxArray(a); } public static void swap(int i,int j,long arr[]) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void swap(int i,int j,char arr[]) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static String endl = "\n" , gap = " "; static int size[]; static int parent[]; static HashMap<Integer , Long> value; // static HashMap<String , Boolean > dp; // static HashMap<Integer , List<int[]>> graph; static boolean vis[]; static int answer; static HashSet<String> guess; static long primePow[]; // static long dp[]; static int N; static int dis[]; static int height[]; static long p[]; // static long fac[]; // static long inv[]; static HashMap<Integer ,List<Integer>> graph; public static long rd() { return (long)((Math.random()*10) + 1); } public static void main(String[] args)throws IOException { long t = ri(); // List<Integer> list = new ArrayList<>(); // int MAX = (int)4e4; // for(int i =1;i<=MAX;i++) { // if(isPalindrome(i + "")) list.add(i); // } // // System.out.println(list); // long dp[] = new long[MAX +1]; // dp[0] = 1; // long mod = (long)(1e9+7); // for(int x : list) { // for(int i =1;i<=MAX;i++) { // if(i >= x) { // dp[i] += dp[i-x]; // dp[i] %= mod; // } // } // } // int MAK = (int)1e6 + 10; // boolean seive[] = new boolean[MAK]; // Arrays.fill(seive , true); // seive[1] = false; // seive[0] = false; // for (int i = 1; i < MAK; i++) { // if(seive[i]) { // for (int j = i+i; j < MAK; j+=i) { // seive[j] = false; // } // } // } // TreeSet<Long>primeSet = new TreeSet<>(); // for(long i = 2;i <= (long)1e6;i++) { // if(seive[(int)i])primeSet.add(i); // } // List<Integer> list = new ArrayList<>(); // for (int i = 1; i < seive.length; i++) { // if(seive[i]) list.add(i); // } int test_case = 1; while(t-->0) { // sc.print("Case #"+(test_case++)+": "); solve(); } sc.close(); } static int endTime[]; static int time; public static int dis(int a[] , int b[]) { return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]); } // static Long dp[][][]; static long fac[], inv[]; public static long ncr(int a , int b) { if(a == b) return 1l; return (((fac[a]*inv[b])%mod)*inv[a-b])%mod; } static Long dp[][]; static HashMap<String , Long> dmap; static long dirpair[][]; static HashSet<String> not; // long subsum[]; public static void solve() throws IOException { int n = ri() , m = ri(); char s[][] = new char[n][m]; for(int i = 0;i < n;i++) { s[i] = rac(); } if(n <= 1 || m <= 1) { for(int i = 0;i < n;i++) { for(int j =0 ;j < m;j++) { if(s[i][j] == '*') { System.out.println("NO"); return ; } } } System.out.println("YES"); return ; } for(int i =0;i < n;i++) { for(int j= 0;j < m;j++) { if(s[i][j] == '*') { boolean ans = true; for(int k = j-1;k <= j + 2;k++) if(i-1 >= 0 && k >= 0 && k <m && s[i-1][k] == '*') ans = false; for(int k = j-1;k<= j + 1;k++) if(i + 2 < n && k >= 0 && k <m && s[i+2][k] == '*') ans = false; for(int k = i-1;k <= i + 2;k++) if(j-1>= 0 && k >= 0 && k <n && s[k][j-1] == '*') ans = false; for(int k = i-1;k <= i + 1;k++) if(j + 2 < m && k >= 0 && k < n && s[k][j+2] == '*') ans = false; if(ans && i + 1 < n && j + 1 < m && s[i][j + 1] == '*' && s[i + 1][j]== '*' && s[i + 1][j+ 1] == '.') { s[i][j] = '.'; s[i][j + 1] = '.'; s[i + 1][j]= '.'; continue; } ans = true; for(int k = j-1;k <= j + 2;k++) if(i-1 >= 0 && k >= 0 && k <m && s[i-1][k] == '*') ans = false; for(int k = j;k <= j + 2;k++) if(i + 2 < n && k >= 0 && k <m && s[i+2][k] == '*') ans = false; for(int k = i-1;k <= i + 1;k++) if(j-1>= 0 && k >= 0 && k <n && s[k][j-1] == '*') ans = false; for(int k = i-1;k <= i + 2;k++) if(j + 2 < m && k >= 0 && k <n && s[k][j+2] == '*') ans = false; if(ans && i + 1 < n && j + 1 < m && s[i][j + 1] == '*' && s[i + 1][j + 1] == '*' && s[i + 1][j] == '.') { s[i][j] = '.'; s[i][j + 1] = '.'; s[i + 1][j + 1]= '.'; continue; } ans = true; for(int k = j-1;k <= j + 1;k++) if(i-1 >= 0 && k >= 0 && k <m && s[i-1][k] == '*') ans = false; // System.out.println(ans); for(int k = j-1;k <= j + 2;k++) if(i + 2 < n && k >= 0 && k <m && s[i+2][k] == '*') ans = false; // System.out.println(ans); for(int k = i-1;k <= i + 2;k++) if(j - 1 >= 0 && k >= 0 && k <n && s[k][j - 1] == '*') ans = false; // System.out.println(ans); // for(int k = i;k <= i + 1;k++) if(j + 1 < m && k >= 0 && k <n && s[k][j+1] == '*') ans = false; // System.out.println(ans); for(int k = i;k <= i + 2;k++) if(j + 2 < m && k >= 0 && k <n && s[k][j+2] == '*') ans = false; // System.out.println(ans); if(ans && i + 1 < n && j + 1 < m && s[i + 1][j] == '*' && s[i+1][j + 1] == '*' && s[i][j+1] == '.') { s[i][j] = '.'; s[i + 1][j+1]= '.'; s[i + 1][j]= '.'; // System.out.println("in"); continue; } ans = true; for(int k = j-2;k <= j + 1;k++) if(i-1 >= 0 && k >= 0 && k <m && s[i-1][k] == '*') ans = false; // System.out.println(ans); for(int k = j-2;k <= j + 1;k++) if(i + 2 < n && k >= 0 && k <m && s[i+2][k] == '*') ans = false; // System.out.println(ans); for(int k = i;k <= i + 2;k++) if(j - 2 >= 0 && k >= 0 && k <n && s[k][j - 2] == '*') ans = false; // System.out.println(ans); for(int k = i-1;k <= i + 2;k++) if(j + 1 < m && k >= 0 && k <n && s[k][j+1] == '*') ans = false; // System.out.println(ans); if(ans && i + 1 < n && j - 1 >= 0 && s[i + 1][j-1] == '*' && s[i+1][j] == '*' && s[i][j-1] == '.') { s[i][j] = '.'; s[i + 1][j]= '.'; s[i + 1][j - 1]= '.'; continue; } // System.out.println("AT " + i + " " + j); System.out.println("NO"); return ; } } } System.out.println("YES"); } public static long solve(long r , long c, int m) { if(m == 0) return 1l; String key = r + " " + c + " " + m; if(dmap.containsKey(key)) return dmap.get(key); long ans = 0 , mod = 998244353L; for(int i =0;i < 3;i++) { long nr = r + dirpair[i][0] , nc = c + dirpair[i][1]; if(not.contains(nr + " " + nc)) continue; ans += solve(nr , nc , m-1); ans %= mod; } dmap.put(key , ans); return ans; } public static long lcm(long x , long y) { return x/gcd(x , y)*y; } public static void print(PriorityQueue<long[]> que ) { for(long a[] : que) { System.out.print(a[1] + " "); } System.out.println(); } public static String slope(Point a , Point b) { if((a.x-b.x) == 0) return "inf"; long n = a.y- b.y; if(n == 0) return "0"; long m = a.x-b.x; boolean neg = (n*m < 0?true:false); n = Math.abs(n); m = Math.abs(m); long g = gcd(Math.abs(n),Math.abs(m)); n /= g; m /= g; String ans = n+"/"+m; if(neg) ans = "-" + ans; return ans; } public static int lis(int A[], int size) { int[] tailTable = new int[size]; int len; tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) tailTable[len++] = A[i]; else tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; } public static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] >= key) r = m; else l = m; } return r; } public static int lcs(char a[] , char b[]) { int n = a.length , m = b.length; int dp[][] = new int[n + 1][m + 1]; for(int i =1;i <= n;i++) { for(int j =1;j <= m;j++) { if(a[i-1] == b[j-1]) dp[i][j] = 1 + dp[i-1][j-1]; else dp[i][j] = Math.max(dp[i-1][j] , dp[i][j-1]); } } return dp[n][m]; } public static int find(int node) { if(node == parent[node]) return node; return parent[node] = find(parent[node]); } public static void merge(int a ,int b ) { a = find(a); b = find(b); if(a == b) return; if(size[a] >= size[b]) { parent[b] = a; size[a] += size[b]; // primePow[a] += primePow[b]; // primePow[b] = 0; } else { parent[a] = b; size[b] += size[a]; // primePow[b] += primePow[a]; // primePow[a] = 0; } } public static void processPowerOfP(long arr[]) { int n = arr.length; arr[0] = 1; long mod = (long)1e9 + 7; for(int i =1;i<n;i++) { arr[i] = arr[i-1]*51; arr[i] %= mod; } } public static long hashValue(char s[]) { int n = s.length; long powerOfP[] = new long[n]; processPowerOfP(powerOfP); long ans =0; long mod = (long)1e9 + 7; for(int i =0;i<n;i++) { ans += (s[i]-'a'+1)*powerOfP[i]; ans %= mod; } return ans; } public static void dfs(int r,int c,char arr[][]) { int n = arr.length , m = arr[0].length; arr[r][c] = '#'; for(int i =0;i<4;i++) { int nr = r + colx[i] , nc = c + coly[i]; if(nr < 0 || nc < 0 || nc >= m || nr>=n) continue; if(arr[nr][nc] == '#') continue; dfs(nr,nc,arr); } } public static double getSlope(int a , int b,int x,int y) { if(a-x == 0) return Double.MAX_VALUE; if(b-y == 0) return 0.0; return ((double)b-(double)y)/((double)a-(double)x); } public static boolean collinearr(long a[] , long b[] , long c[]) { return (b[1]-a[1])*(b[0]-c[0]) == (b[0]-a[0])*(b[1]-c[1]); } public static boolean isSquare(long sum) { long root = (int)Math.sqrt(sum); return root*root == sum; } public static int[] suffixArray(String s) { int n = s.length(); Suffix[] su = new Suffix[n]; for (int i = 0; i < n; i++) { su[i] = new Suffix(i, s.charAt(i) - '$', 0); } for (int i = 0; i < n; i++) su[i].next = (i + 1 < n ? su[i + 1].rank : -1); Arrays.sort(su); int[] ind = new int[n]; for (int length = 4; length < 2 * n; length <<= 1) { int rank = 0, prev = su[0].rank; su[0].rank = rank; ind[su[0].index] = 0; for (int i = 1; i < n; i++) { if (su[i].rank == prev && su[i].next == su[i - 1].next) { prev = su[i].rank; su[i].rank = rank; } else { prev = su[i].rank; su[i].rank = ++rank; } ind[su[i].index] = i; } for (int i = 0; i < n; i++) { int nextP = su[i].index + length / 2; su[i].next = nextP < n ? su[ind[nextP]].rank : -1; } Arrays.sort(su); } int[] suf = new int[n]; for (int i = 0; i < n; i++) suf[i] = su[i].index; return suf; } public static boolean isPalindrome(String s) { int i =0 , j = s.length() -1; while(i <= j && s.charAt(i) == s.charAt(j)) { i++; j--; } return i>j; } // digit dp hint; public static long callfun(String num , int N, int last ,int secondLast ,int bound ,long dp[][][][]) { if(N == 1) { if(last == -1 || secondLast == -1) return 0; long answer = 0; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i = 0;i<=max;i++) { if(last > secondLast && last > i) { answer++; } if(last < secondLast && last < i) { answer++; } } return answer; } if(secondLast == -1 || last == -1 ){ long answer = 0l; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i =0;i<=max;i++) { int nl , nsl , newbound = bound==0?0:i==max?1:0; if(last == - 1&& secondLast == -1 && i == 0) { nl = -1 ; nsl = -1; } else { nl = i;nsl = last; } long temp = callfun(num , N-1 , nl , nsl ,newbound, dp); answer += temp; if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++; } return answer; } if(dp[N][last][secondLast][bound] != -1) return dp[N][last][secondLast][bound]; long answer = 0l; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i =0;i<=max;i++) { int nl , nsl , newbound = bound==0?0:i==max?1:0; if(last == - 1&& secondLast == -1 && i == 0) { nl = -1 ; nsl = -1; } else { nl = i;nsl = last; } long temp = callfun(num , N-1 , nl , nsl ,newbound,dp); answer += temp; if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++; } return dp[N][last][secondLast][bound] = answer; } public static Long callfun(int index ,int pair,int arr[],Long dp[][]) { long mod = 998244353l; if(index >= arr.length) return 1l; if(dp[index][pair] != null) return dp[index][pair]; Long sum = 0l , ans = 0l; if(arr[index]%2 == pair) { return dp[index][pair] = callfun(index + 1,pair^1 , arr,dp)%mod + callfun(index + 1 ,pair , arr , dp)%mod; } else { return dp[index][pair] = callfun(index + 1,pair , arr,dp)%mod; } // for(int i =index;i<arr.length;i++) { // sum += arr[i]; // if(sum%2 == pair) { // ans = ans + callfun(i + 1,pair^1,arr , dp)%mod; // ans%=mod; // } // } // return dp[index][pair] = ans; } public static boolean callfun(int index , int n,int neg , int pos , String s) { if(neg < 0 || pos < 0) return false; if(index >= n) return true; if(s.charAt(0) == 'P') { if(neg <= 0) return false; return callfun(index + 1,n , neg-1 , pos , s.charAt(1) + "N"); } else { if(pos <= 0) return false; return callfun(index + 1 , n , neg , pos-1 , s.charAt(1) + "P"); } } public static void getPerm(int n , char arr[] , String s ,List<String>list) { if(n == 0) { list.add(s); return; } for(char ch : arr) { getPerm(n-1 , arr , s+ ch,list); } } public static int getLen(int i ,int j , char s[]) { while(i >= 0 && j < s.length && s[i] == s[j]) { i--; j++; } i++; j--; if(i>j) return 0; return j-i + 1; } public static int getMaxCount(String x) { char s[] = x.toCharArray(); int max = 0; int n = s.length; for(int i =0;i<n;i++) { max = Math.max(max,Math.max(getLen(i , i,s) , getLen(i ,i+1,s))); } return max; } public static double getDis(int arr[][] , int x, int y) { double ans = 0.0; for(int a[] : arr) { int x1 = a[0] , y1 = a[1]; ans += Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1)); } return ans; } public static boolean valid(String x ) { if(x.length() == 0) return true; if(x.length() == 1) return false; char s[] = x.toCharArray(); if(x.length() == 2) { if(s[0] == s[1]) { return false; } return true; } int r = 0 , b = 0; for(char ch : x.toCharArray()) { if(ch == 'R') r++; else b++; } return (r >0 && b >0); } public static void primeDivisor(HashMap<Long , Long >cnt , long num) { for(long i = 2;i*i<=num;i++) { while(num%i == 0) { cnt.put(i ,(cnt.getOrDefault(i,0l) + 1)); num /= i; } } if(num > 2) { cnt.put(num ,(cnt.getOrDefault(num,0l) + 1)); } } public static boolean isSubsequene(char a[], char b[] ) { int i =0 , j = 0; while(i < a.length && j <b.length) { if(a[i] == b[j]) { j++; } i++; } return j >= b.length; } public static long fib(int n ,long M) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { long[][] mat = {{1, 1}, {1, 0}}; mat = pow(mat, n-1 , M); return mat[0][0]; } } public static long[][] pow(long[][] mat, int n ,long M) { if (n == 1) return mat; else if (n % 2 == 0) return pow(mul(mat, mat , M), n/2 , M); else return mul(pow(mul(mat, mat,M), n/2,M), mat , M); } static long[][] mul(long[][] p, long[][] q,long M) { long a = (p[0][0]*q[0][0] + p[0][1]*q[1][0])%M; long b = (p[0][0]*q[0][1] + p[0][1]*q[1][1])%M; long c = (p[1][0]*q[0][0] + p[1][1]*q[1][0])%M; long d = (p[1][0]*q[0][1] + p[1][1]*q[1][1])%M; return new long[][] {{a, b}, {c, d}}; } public static long[] kdane(long arr[]) { int n = arr.length; long dp[] = new long[n]; dp[0] = arr[0]; long ans = dp[0]; for(int i = 1;i<n;i++) { dp[i] = Math.max(dp[i-1] + arr[i] , arr[i]); ans = Math.max(ans , dp[i]); } return dp; } public static void update(int low , int high , int l , int r, int val , int treeIndex ,int tree[]) { if(low > r || high < l || high < low) return; if(l <= low && high <= r) { System.out.println("At " +low + " and " + high + " ans ttreeIndex " + treeIndex); tree[treeIndex] += val; return; } int mid = low + (high - low)/2; update(low , mid , l , r , val , treeIndex*2 + 1, tree); update(mid + 1 , high , l , r , val , treeIndex*2 + 2 , tree); } // static int colx[] = {1 ,1, -1,-1 , 2,2,-2,-2}; // static int coly[] = {-2 ,2, 2,-2,1,-1,1,-1}; static int colx[] = {1 ,-1, 0,0 , 1,1,-1,-1}; static int coly[] = {0 ,0, 1,-1,1,-1,1,-1}; public static void reverse(char arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static void reverse(long arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static void reverse(int arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static long inverse(long x , long mod) { return pow(x , mod -2 , mod); } public static int maxArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static long maxArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static int minArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static long minArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static int sumArray(int arr[]) { int ans = 0; for(int x : arr) { ans += x; } return ans; } public static long sumArray(long arr[]) { long ans = 0; for(long x : arr) { ans += x; } return ans; } public static long rl() { return sc.nextLong(); } public static char[] rac() { return sc.next().toCharArray(); } public static String rs() { return sc.next(); } public static char rc() { return sc.next().charAt(0); } public static int [] rai(int n) { int ans[] = new int[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextInt(); } return ans; } public static long [] ral(int n) { long ans[] = new long[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextLong(); } return ans; } public static int ri() { return sc.nextInt(); } public static int getValue(int num ) { int ans = 0; while(num > 0) { ans++; num = num&(num-1); } return ans; } public static boolean isValid(int x ,int y , int n,char arr[][],boolean visited[][][][]) { return x>=0 && x<n && y>=0 && y <n && !(arr[x][y] == '#'); } // public static Pair join(Pair a , Pair b) { // Pair res = new Pair(Math.min(a.min , b.min) , Math.max(a.max , b.max) , a.count + b.count); // return res; // } // segment tree query over range // public static int query(int node,int l , int r,int a,int b ,Pair tree[] ) { // if(tree[node].max < a || tree[node].min > b) return 0; // if(l > r) return 0; // if(tree[node].min >= a && tree[node].max <= b) { // return tree[node].count; // } // int mid = l + (r-l)/2; // int ans = query(node*2 ,l , mid ,a , b , tree) + query(node*2 +1,mid + 1, r , a , b, tree); // return ans; // } // // segment tree update over range // public static void update(int node, int i , int j ,int l , int r,long value, long arr[] ) { // if(l >= i && j >= r) { // arr[node] += value; // return; // } // if(j < l|| r < i) return; // int mid = l + (r-l)/2; // update(node*2 ,i ,j ,l,mid,value, arr); // update(node*2 +1,i ,j ,mid + 1,r, value , arr); // } public static long pow(long a , long b , long mod) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2 , mod)%mod; if(b%2 == 0) { return (ans*ans)%mod; } else { return ((ans*ans)%mod*a)%mod; } } public static long pow(long a , long b ) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2); if(b%2 == 0) { return (ans*ans); } else { return ((ans*ans)*a); } } public static boolean isVowel(char ch) { if(ch == 'a' || ch == 'e'||ch == 'i' || ch == 'o' || ch == 'u') return true; if((ch == 'A' || ch == 'E'||ch == 'I' || ch == 'O' || ch == 'U')) return true; return false; } // public static int getFactor(int num) { // if(num==1) return 1; // int ans = 2; // int k = num/2; // for(int i = 2;i<=k;i++) { // if(num%i==0) ans++; // } // return Math.abs(ans); // } public static int[] readarr()throws IOException { int n = sc.nextInt(); int arr[] = new int[n]; for(int i =0;i<n;i++) { arr[i] = sc.nextInt(); } return arr; } public static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); } public static boolean isPrime(long num) { if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(long i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } public static boolean isPrime(int num) { // System.out.println("At pr " + num); if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(int i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } // public static boolean isPrime(long num) { // if(num==1) return false; // if(num<=3) return true; // if(num%2==0||num%3==0) return false; // for(int i =5;i*i<=num;i+=6) { // if(num%i==0) return false; // } // return true; // } public static void allMultiple() { // int MAX = 0 , n = nums.length; // for(int x : nums) MAX = Math.max(MAX ,x); // int cnt[] = new int[MAX + 1]; // int ans[] = new int[MAX + 1]; // for (int i = 0; i < n; ++i) cnt[nums[i]]++; // for (int i = 1; i <= MAX; ++i) { // for (int j = i; j <= MAX; j += i) ans[i] += cnt[j]; // } } public static long gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } public static int gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static int get_gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static long get_gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } // public static long fac(long num) { // long ans = 1; // int mod = (int)1e9+7; // for(long i = 2;i<=num;i++) { // ans = (ans*i)%mod; // } // return ans; // } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
7294a94a4217e375cdcfd2f16bbf4c83
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
// package c1722; // // Codeforces Round #817 (Div. 4) 2022-08-30 07:50 // F. L-shapes // https://codeforces.com/contest/1722/problem/F // time limit per test 1 second; memory limit per test 256 megabytes // public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*' // // An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape // contains exactly three shaded cells (denoted by *), which can be rotated in any way. // https://espresso.codeforces.com/ae29aac2dae807baedbba86d92c71e48ac6606eb.png // // You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't // touch an edge or corner. More formally: // * Each shaded cell in the grid is part of exactly one L-shape, and // * no two L-shapes are adjacent by edge or corner. // // For example, the last two grids in the picture above satisfy the condition because the two // L-shapes touch by corner and edge, respectively. // // Input // // The input consists of multiple test cases. The first line contains an integer t (1 <= t <= 100)-- // the number of test cases. The description of the test cases follows. // // The first line of each test case contains two integers n and m (1 <= n, m <= 50)-- the number of // rows and columns in the grid, respectively. // // Then n lines follow, each containing m characters. Each of these characters is either '.' or // '*'-- an empty cell or a shaded cell, respectively. // // Output // // For each test case, output "YES" if the grid is made up of L-shape that don't share edges or // corners, and "NO" otherwise. // // You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" // will be recognized as a positive answer). // // Example /* input: 10 6 10 ........** .**......* ..*..*.... .....**... ...*.....* ..**....** 6 10 ....*...** .**......* ..*..*.... .....**... ...*.....* ..**....** 3 3 ... *** ... 4 4 .*.. **.. ..** ..*. 5 4 .*.. **.. .... ..** ..*. 3 2 .* ** *. 2 3 *.. .** 3 2 .. ** *. 3 3 .** *.* **. 3 3 ..* .** ..* output: YES NO NO NO YES NO NO YES NO NO */ // import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.Random; import java.util.StringTokenizer; public class C1722F { static final int MOD = 998244353; static final Random RAND = new Random(); static boolean solve(char[][] grid) { int n = grid.length; int m = grid[0].length; if (n == 1) { for (int j = 0; j < m; j++) { if (grid[0][j] == '*') { return false; } } return true; } else if (m == 1) { for (int i = 0; i < n; i++) { if (grid[i][0] == '*') { return false; } } return true; } myAssert(n >= 2 && m >= 2); // System.out.println(Utils.traceGrid(grid)); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) { // skip if // .? OR .. // .? ?? if ((grid[i][j] == '.' && grid[i+1][j] == '.') || (grid[i][j] == '.' && grid[i][j+1] == '.')) { continue; } // .* *. ** ** // ** ** .* *. // check whether the inner square has 3 * int cnt = 0; for (int h = i; h < i + 2; h++) { for (int w = j; w < j + 2; w++) { cnt += grid[h][w] == '*' ? 1 : 0; } } if (cnt != 3) { // System.out.format(" i:%d j:%d cnt %d \n", i, j, cnt); return false; } // check all cell in the outer 4x4 boundary except the corner connect to '.' are '.' // // i-1 ? . . . // i . . * . // i+1 . * * . // i+2 . . . . // ^ ^ ^ ^ // j-1 j j+1 j+2 for (int a = i-1; a < i + 3; a++) { if (a < 0 || a >= n) { continue; } for (int b = j-1; b < j + 3; b++) { if (b < 0 || b >= m) { continue; } if ((a == i || a == i+1) && (b == j || b == j+1)) { // the inner square continue; } if (a == i - 1 && b == j - 1 && grid[i][j] == '.') { continue; } if (a == i - 1 && b == j + 2 && grid[i][j+1] == '.') { continue; } if (a == i + 2 && b == j - 1 && grid[i+1][j] == '.') { continue; } if (a == i + 2 && b == j + 2 && grid[i+1][j+1] == '.') { continue; } if (grid[a][b] == '*') { if (test) { System.out.format(" i:%d j:%d a:%d b:%d\n", i, j, a, b); } return false; } } } for (int h = i; h < i + 2; h++) { for (int w = j; w < j + 2; w++) { grid[h][w] = '.'; } } } } // check whether grid is all '.' by now for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == '*') { return false; } } } return true; } static boolean test = false; static void doTest() { if (!test) { return; } long t0 = System.currentTimeMillis(); String[] arr = new String[] { "..........", "..........", "..........", "..........", "..........", "..........", ".........*", ".........*", "..........", "..........", }; int n = arr.length; int m = arr[0].length(); char[][] grid = new char[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { grid[i][j] = arr[i].charAt(j); } } System.out.println(traceGrid(grid)); boolean ans = solve(grid); System.out.println(ans? "YES" : "NO"); System.out.println(traceGrid(grid)); System.out.format("%d msec\n", System.currentTimeMillis() - t0); System.exit(0); } public static String traceGrid(char[][] grid) { StringBuilder sb = new StringBuilder(); // If more than 32, trace the first 16 and last 16 int m = grid.length; for (int i = 0; i < m; i++) { if (m > 32) { if (i == 16) { sb.append(" ...\n"); } if (i >= 16 && i < m - 16) { continue; } } sb.append(String.format("%3d:", i)); for (int j = 0; j < grid[i].length; j++) { sb.append(' '); sb.append(grid[i][j]); } if (i < m - 1) { sb.append("\n"); } } return sb.toString(); } public static void main(String[] args) { doTest(); MyScanner in = new MyScanner(); int T = in.nextInt(); for (int t = 1; t <= T; t++) { int n = in.nextInt(); int m = in.nextInt(); char[][] grid = new char[n][m]; for (int i = 0; i < n; i++) { String s = in.next(); for (int j = 0; j < m; j++) { grid[i][j] = s.charAt(j); } } boolean ans = solve(grid); System.out.println(ans? "YES" : "NO"); } } static void output(int[] a) { if (a == null) { System.out.println("-1"); return; } StringBuilder sb = new StringBuilder(); for (int v : a) { sb.append(v); sb.append(' '); if (sb.length() > 4000) { System.out.print(sb.toString()); sb.setLength(0); } } System.out.println(sb.toString()); } static void myAssert(boolean cond) { if (!cond) { throw new RuntimeException("Unexpected"); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
0d49ae16d1be49a101656a488e82b8c1
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { static int[] dx = {-1,0,1}; static int[] dy = {-1,0,1}; public static void main(String arg[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int T = Integer.parseInt(br.readLine()); while(T --> 0) { boolean answer = false; StringTokenizer st = new StringTokenizer(br.readLine(), " "); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); char[][] arr = new char[n][m]; for(int i = 0; i < n; i++) { String str = br.readLine(); for(int j = 0; j < m; j++) { arr[i][j] = str.charAt(j); } } int curr = 1; int[][] id = new int[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(answer) break; if(arr[i][j] == '*') { ArrayList<Integer> tx = new ArrayList<>(); ArrayList<Integer> ty = new ArrayList<>(); if(i > 0 && arr[i-1][j] == '*') { tx.add(i-1); tx.add(j); } if(i < n-1 && arr[i+1][j] == '*') { tx.add(i+1); tx.add(j); } if(j > 0 && arr[i][j-1] == '*') { ty.add(i); ty.add(j-1); } if(j < m-1 && arr[i][j+1] == '*') { ty.add(i); ty.add(j+1); } if(tx.size() == 2 && ty.size() == 2) { if(id[i][j] == 0) id[i][j] = curr; else answer = true; if(id[tx.get(0)][tx.get(1)] == 0) id[tx.get(0)][tx.get(1)] = curr; else answer = true; if(id[ty.get(0)][ty.get(1)] == 0) id[ty.get(0)][ty.get(1)] = curr; else answer = true; curr++; } else if(tx.size() > 2 || ty.size() > 2) { answer = true; break; } } } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(answer) break; if(arr[i][j] == '*') { if(id[i][j] == 0) { answer = true; break; } for(int x = 0; x < 3; x++) { for(int y = 0; y < 3; y++) { int adx = i+dx[x]; int ady = j+dy[y]; if(adx >= 0 && adx < n && ady >= 0 && ady < m) { if(id[adx][ady] != id[i][j] && id[adx][ady] != 0) { answer = true; } } } } } } } if(answer) sb.append("NO").append('\n'); else sb.append("YES").append('\n'); } System.out.println(sb); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
ee90daa94226eb9069762cf8f94e4a3a
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; //4 public class Main { static int[] dx = {-1,0,-1}; static int[] dy = {-1,0,1}; public static void main(String arg[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); while(T --> 0) { boolean answer = false; StringTokenizer st = new StringTokenizer(br.readLine(), " "); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); char[][] arr = new char[n][m]; for(int i = 0; i < n; i++) { String str = br.readLine(); for(int j = 0; j < m; j++) { arr[i][j] = str.charAt(j); } } int curr = 1; int[][] id = new int[n][m]; for(int i = 0; i < n; i++) { if(answer) break; for(int j = 0; j < m; j++) { if(arr[i][j] == '*') { ArrayList<Integer> adjh = new ArrayList<>(); ArrayList<Integer> adjv = new ArrayList<>(); if(i > 0 && arr[i-1][j] == '*') { adjh.add(i-1); adjh.add(j); } if(i < n-1 && arr[i+1][j] == '*') { adjh.add(i+1); adjh.add(j); } if(j > 0 && arr[i][j-1] == '*') { adjv.add(i); adjv.add(j-1); } if(j < m-1 && arr[i][j+1] == '*') { adjv.add(i); adjv.add(j+1); } if(adjh.size() == 2 && adjv.size() == 2) { if(id[i][j] == 0) { id[i][j] = curr; } else { answer = true; break; } if(id[adjh.get(0)][adjh.get(1)] == 0) { id[adjh.get(0)][adjh.get(1)] = curr; } else { answer = true; break; } if(id[adjv.get(0)][adjv.get(1)] == 0) { id[adjv.get(0)][adjv.get(1)] = curr; } else { answer = true; break; } curr++; } else if(adjh.size() > 2 || adjv.size() > 2) { answer = true; break; } } } } for(int i = 0; i < n; i++) { if(answer) break; for(int j = 0; j < m; j++) { if(arr[i][j] == '*') { if(id[i][j] == 0) { answer = true; break; } else { for(int x = 0; x < 3; x++) { for(int y = 0; y < 3; y++) { if(0 <= i + dx[x] && i + dx[x] < n) { if(0 <= j + dy[y] && j + dy[y] < m) { if(id[i + dx[x]][j + dy[y]] != id[i][j] && id[i + dx[x]][j + dy[y]] != 0) { answer = true; break; } } } } } } } } } if(answer) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
54c7415e0dcb6c8fd646765047ae23c0
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static int[][] dirs =new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; public static int[][] cdir = new int[][]{{0, 0}, {0, 1}, {1, 0}, {1, 1}}; public static HashSet<List<Integer>> count(char[][] arr, int idx, int jdx) { HashSet<List<Integer>> ans = new HashSet<>(); for(int i=0; i<arr.length; i++) { for(int j=0; j<arr.length; j++) { List<Integer> x = new ArrayList<>(); if(arr[i][j] == '*') { x.add(idx+i); x.add(jdx+j); ans.add(x); } } } return ans; } public static boolean check2(char[][] arr, int i, int j, HashSet<List<Integer>> xxx) { if(i >= 0 && j >= 0 && i < arr.length && j < arr[0].length) { if(arr[i][j] == '.') return true; List<Integer> x = new ArrayList<>(); x.add(i); x.add(j); if(!xxx.contains(x)) return false; } return true; } public static boolean check(char[][] arr, int idx, int jdx, HashSet<List<Integer>> xxx) { for(int i=0; i<cdir.length; i++) { int x = idx+cdir[i][0]; int y = jdx+cdir[i][1]; if(arr[x][y] == '*') { for(int[] dir : dirs) { if(!check2(arr, x+dir[0], y+dir[1], xxx)) return false; } } } return true; } public static void s() { int n = sc.nextInt(); int m = sc.nextInt(); char[][] arr = new char[n][m]; for(int i=0; i<n; i++) { String s = sc.nextLine(); for(int j=0; j<m; j++) { arr[i][j] = s.charAt(j); } } for(int i=0; i<n-1; i++) { for(int j=0; j<m-1; j++) { char[][] copy = new char[2][2]; copy[0][0] = arr[i][j]; copy[0][1] = arr[i][j+1]; copy[1][0] = arr[i+1][j]; copy[1][1] = arr[i+1][j+1]; HashSet<List<Integer>> ans = count(copy, i, j); if(ans.size() == 4) { p.writeln("NO"); return; } else if(ans.size() == 3) { if(check(arr, i, j, ans)) { arr[i][j] = '.'; arr[i+1][j] = '.'; arr[i][j+1] = '.'; arr[i+1][j+1] = '.'; } else { p.writeln("NO"); return; } } } } // for(int i=0; i<n; i++) { // for(int j=0; j<m; j++) { // p.write(arr[i][j]); // } // p.writeln(); // } for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(arr[i][j] == '*') { p.writeln("NO"); return; } } } p.writeln("YES"); } public static void main(String[] args) { int t = 1; t = sc.nextInt(); while (t-- != 0) { s(); } p.print(); } public static boolean debug = false; static void debug(String st) { if(debug) p.writeln(st); } static final Integer MOD = (int) 1e9 + 7; static final FastReader sc = new FastReader(); static final Print p = new Print(); static class Functions { 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 int max(int... a) { int max = Integer.MIN_VALUE; for (int val : a) max = Math.max(val, max); return max; } static int min(int... a) { int min = Integer.MAX_VALUE; for (int val : a) min = Math.min(val, min); return min; } static long min(long... a) { long min = Long.MAX_VALUE; for (long val : a) min = Math.min(val, min); return min; } static long max(long... a) { long max = Long.MIN_VALUE; for (long val : a) max = Math.max(val, max); return max; } static long sum(long... a) { long sum = 0; for (long val : a) sum += val; return sum; } static int sum(int... a) { int sum = 0; for (int val : a) sum += val; return sum; } public static long mod_add(long a, long b) { return (a % MOD + b % MOD + MOD) % MOD; } public static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = mod_mul(res, a); a = mod_mul(a, a); b >>= 1; } return res; } public static long mod_mul(long a, long b) { long res = 0; a %= MOD; while (b > 0) { if ((b & 1) > 0) { res = mod_add(res, a); } a = (2 * a) % MOD; b >>= 1; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(long n) { long res = 1; for (int i = 1; i <= n; i++) { res = (i % MOD * res % MOD) % MOD; } return res; } public static int count(int[] arr, int x) { int count = 0; for (int val : arr) if (val == x) count++; return count; } public static ArrayList<Integer> generatePrimes(int n) { boolean[] primes = new boolean[n]; for (int i = 2; i < primes.length; i++) primes[i] = true; for (int i = 2; i < primes.length; i++) { if (primes[i]) { for (int j = i * i; j < primes.length; j += i) { primes[j] = false; } } } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < primes.length; i++) { if (primes[i]) arr.add(i); } return arr; } } static class Print { StringBuffer strb = new StringBuffer(); public void write(Object str) { strb.append(str); } public void writes(Object str) { char c = ' '; strb.append(str).append(c); } public void writeln(Object str) { char c = '\n'; strb.append(str).append(c); } public void writeln() { char c = '\n'; strb.append(c); } public void yes() { char c = '\n'; writeln("YES"); } public void no() { writeln("NO"); } public void writes(int... arr) { for (int val : arr) { write(val); write(' '); } } public void writes(long... arr) { for (long val : arr) { write(val); write(' '); } } public void writeln(int... arr) { for (int val : arr) { writeln(val); } } public void print() { System.out.print(strb); } public void println() { System.out.println(strb); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double[] readArrayDouble(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = new String(); try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
11cec21eee7b267124ca3fd714c2e7aa
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.*; import java.util.StringTokenizer; public class copy { static int log=30; static int[][] ancestor; static int[] depth; static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) { 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] is not changed, then it is a // prime if (prime[p]) { // 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]) { arr.add(i); } } } public static long fac(long N, long mod) { if (N == 0) return 1; if(N==1) return 1; return ((N % mod) * (fac(N - 1, mod) % mod)) % mod; } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static long nCrModPFermat(int n, int r, long p,long[] fac) { if (n < r) return 0; // Base case if (r == 0) return 1; return ((fac[n] % p * (modInverse(fac[r], p) % p)) % p * (modInverse(fac[n-r], p) % p)) % p; } public static int find(int[] parent, int x) { if (parent[x] == x) return x; return find(parent, parent[x]); } public static void merge(int[] parent, int[] rank, int x, int y,int[] child) { int x1 = find(parent, x); int y1 = find(parent, y); if (rank[x1] > rank[y1]) { parent[y1] = x1; child[x1]+=child[y1]; } else if (rank[y1] > rank[x1]) { parent[x1] = y1; child[y1]+=child[x1]; } else { parent[y1] = x1; child[x1]+=child[y1]; rank[x1]++; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int power(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p,int[] fac) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r // int[] fac = new int[n + 1]; // fac[0] = 1; // // for (int i = 1; i <= n; i++) // fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } public static long[][] ncr(int n,int r) { long[][] dp=new long[n+1][r+1]; for(int i=0;i<=n;i++) dp[i][0]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=r;j++) { if(j>i) continue; dp[i][j]=dp[i-1][j-1]+dp[i-1][j]; } } return dp; } public static boolean prime(long N) { int c=0; for(int i=2;i*i<=N;i++) { if(N%i==0) ++c; } return c==0; } public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child) { int c=0; for(int i:arr.get(x)) { if(i!=parent) { // System.out.println(i+" hello "+x); depth[i]=depth[x]+1; ancestor[i][0]=x; // if(i==2) // System.out.println(parent+" hello"); for(int j=1;j<log;j++) ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1]; c+=sparse_ancestor_table(arr,i,x,child); } } child[x]=1+c; return child[x]; } public static int lca(int x,int y) { if(depth[x]<depth[y]) { int temp=x; x=y; y=temp; } x=get_kth_ancestor(depth[x]-depth[y],x); if(x==y) return x; // System.out.println(x+" "+y); for(int i=log-1;i>=0;i--) { if(ancestor[x][i]!=ancestor[y][i]) { x=ancestor[x][i]; y=ancestor[y][i]; } } return ancestor[x][0]; } public static int get_kth_ancestor(int K,int x) { if(K==0) return x; int node=x; for(int i=0;i<log;i++) { if(K%2!=0) { node=ancestor[node][i]; } K/=2; } return node; } public static ArrayList<Integer> primeFactors(int n) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) factors.add(2); while (n%2==0) { n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n if(n%i==0) factors.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { factors.add(n); } return factors; } static long ans=1,mod=1000000007; public static void recur(long X,long N,int index,ArrayList<Integer> temp) { // System.out.println(X); if(index==temp.size()) { System.out.println(X); ans=((ans%mod)*(X%mod))%mod; return; } for(int i=0;i<=60;i++) { if(X*Math.pow(temp.get(index),i)<=N) recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp); else break; } } public static int upper(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)<X) l=mid+1; else { if(mid-1>=0 && temp.get(mid-1)>=X) r=mid-1; else return mid; } } return -1; } public static int lower(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)>X) r=mid-1; else { if(mid+1<temp.size() && temp.get(mid+1)<=X) l=mid+1; else return mid; } } return -1; } public static int[] check(String shelf,int[][] queries) { int[] arr=new int[queries.length]; ArrayList<Integer> indices=new ArrayList<>(); for(int i=0;i<shelf.length();i++) { char ch=shelf.charAt(i); if(ch=='|') indices.add(i); } for(int i=0;i<queries.length;i++) { int x=queries[i][0]-1; int y=queries[i][1]-1; int left=upper(indices,x); int right=lower(indices,y); if(left<=right && left!=-1 && right!=-1) { arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1); } else arr[i]=0; } return arr; } static boolean check; public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited) { visited[x]=true; PriorityQueue<Integer> pq=new PriorityQueue<>(); for(int i:arr.get(x)) { if(color[i]<color[x]) pq.add(color[i]); if(color[i]==color[x]) check=false; if(!visited[i]) check(arr,i,color,visited); } int start=1; while(pq.size()>0) { int temp=pq.poll(); if(temp==start) ++start; else break; } if(start!=color[x]) check=false; } static boolean cycle; public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr) { if(stack[x]) { cycle=true; return; } visited[x]=true; for(int i:arr.get(x)) { if(!visited[x]) { cycle(stack,visited,i,arr); } } stack[x]=false; } public static int check(char[][] ch,int x,int y) { int cnt=0; int c=0; int N=ch.length; int x1=x,y1=y; while(c<ch.length) { if(ch[x][y]=='1') ++cnt; // if(x1==0 && y1==3) // System.out.println(x+" "+y+" "+cnt); x=(x+1)%N; y=(y+1)%N; ++c; } return cnt; } public static void s(char[][] arr,int x) { char start=arr[arr.length-1][x]; for(int i=arr.length-1;i>0;i--) { arr[i][x]=arr[i-1][x]; } arr[0][x]=start; } public static void shuffle(char[][] arr,int x,int down) { int N= arr.length; down%=N; char[] store=new char[N-down]; for(int i=0;i<N-down;i++) store[i]=arr[i][x]; for(int i=0;i<arr.length;i++) { if(i<down) { // Printing rightmost // kth elements arr[i][x]=arr[N + i - down][x]; } else { // Prints array after // 'k' elements arr[i][x]=store[i-down]; } } } public static String form(int C1,char ch1,char ch2) { char ch=ch1; String s=""; for(int i=1;i<=C1;i++) { s+=ch; if(ch==ch1) ch=ch2; else ch=ch1; } return s; } public static void printArray(long[] arr) { for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } public static boolean check(long mid,long[] arr,long K) { long[] arr1=Arrays.copyOfRange(arr,0,arr.length); long ans=0; for(int i=0;i<arr1.length-1;i++) { if(arr1[i]+arr1[i+1]>=mid) { long check=(arr1[i]+arr1[i+1])/mid; // if(mid==5) // System.out.println(check); long left=check*mid; left-=arr1[i]; if(left>=0) arr1[i+1]-=left; ans+=check; } // if(mid==5) // printArray(arr1); } // if(mid==5) // System.out.println(ans); ans+=arr1[arr1.length-1]/mid; return ans>=K; } public static long search(long sum,long[] arr,long K) { long l=1,r=sum/K; while(l<=r) { long mid=(l+r)/2; if(check(mid,arr,K)) { if(mid+1<=sum/K && check(mid+1,arr,K)) l=mid+1; else return mid; } else r=mid-1; } return -1; } public static void primeFactors(int n,HashSet<Integer> hp) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) hp.add(2); while (n%2==0) { n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n if(n%i==0) hp.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { hp.add(n); } } public static boolean check(String s) { HashSet<Character> hp=new HashSet<>(); char ch=s.charAt(0); for(int i=1;i<s.length();i++) { // System.out.println(hp+" "+s.charAt(i)); if(hp.contains(s.charAt(i))) { // System.out.println(i); // System.out.println(hp); // System.out.println(s.charAt(i)); return false; } if(s.charAt(i)!=ch) { hp.add(ch); ch=s.charAt(i); } } return true; } public static int check_end(String[] arr,boolean[] st,char ch) { for(int i=0;i<arr.length;i++) { if(ch==arr[i].charAt(0) && !st[i] && ch==arr[i].charAt(arr[i].length()-1)) return i; } for(int i=0;i<arr.length;i++) { if(ch==arr[i].charAt(0) && !st[i]) return i; } return -1; } public static int check_start(String[] arr,boolean[] st,char ch) { for(int i=0;i<arr.length;i++) { // if(ch=='B') // { // if(!st[i]) // System.out.println(arr[i]+" hello"); // } if(ch==arr[i].charAt(arr[i].length()-1) && !st[i] && ch==arr[i].charAt(0)) return i; } for(int i=0;i<arr.length;i++) { // if(ch=='B') // { // if(!st[i]) // System.out.println(arr[i]+" hello"); // } if(ch==arr[i].charAt(arr[i].length()-1) && !st[i]) return i; } return -1; } public static boolean palin(int N) { String s=""; while(N>0) { s+=N%10; N/=10; } int l=0,r=s.length()-1; while(l<=r) { if(s.charAt(l)!=s.charAt(r)) return false; ++l; --r; } return true; } public static boolean check(long org_s,long org_d,long org_n,long check_ele) { if(check_ele<org_s) return false; if((check_ele-org_s)%org_d!=0) return false; long num=(check_ele-org_s)/org_d; // if(check_ele==5) // System.out.println(num+" "+org_n); return num+1<=org_n; } public static long check(long c,long c_diff,long mod,long b_start,long c_start, long c_end,long b_end,long b_diff) { // System.out.println(c); long max=Math.max(c,b_diff); long min=Math.min(c,b_diff); long lcm=(max/gcd(max,min))*min; // System.out.println(lcm); // System.out.println(c); // System.out.println(c_diff); // if(b_diff>c) // { long start_point=c_diff/c-c_diff/lcm; // System.out.println(start_point); // } // else // { // start_point=c_diff/b_diff-c_diff/c; // } // System.out.println(c+" "+start_point); return (start_point%mod*start_point%mod)%mod; } public static boolean check_bounds(int x, int y, int N, int M) { return x>=0 && x<N && y>=0 && y<M; } static boolean found=false; public static void check(int x,int y,int[][] arr,boolean status[][]) { if(arr[x][y]==9) { found=true; return; } status[x][y]=true; if(check_bounds(x-1,y, arr.length,arr[0].length)&& !status[x-1][y]) check(x-1,y,arr,status); if(check_bounds(x+1,y, arr.length,arr[0].length)&& !status[x+1][y]) check(x+1,y,arr,status); if(check_bounds(x,y-1, arr.length,arr[0].length)&& !status[x][y-1]) check(x,y-1,arr,status); if(check_bounds(x,y+1, arr.length,arr[0].length)&& !status[x][y+1]) check(x,y+1,arr,status); } public static int check(String s1,String s2,int M) { int ans=0; for(int i=0;i<M;i++) { ans+=Math.abs(s1.charAt(i)-s2.charAt(i)); } return ans; } public static int check(int[][] arr,int dir1,int dir2,int x1,int y1) { int sum=0,N=arr.length,M=arr[0].length; int x=x1+dir1,y=y1+dir2; while(x<N && x>=0 && y<M && y>=0) { sum+=arr[x][y]; x=x+dir1; y+=dir2; } return sum; } public static int check(long[] pref,long X,int N) { if(X>pref[N-1]) return -1; // System.out.println(pref[0]); if(X<=pref[0]) return 1; int l=0,r=N-1; while(l<=r) { int mid=(l+r)/2; if(pref[mid]>=X) { if(mid-1>=0 && pref[mid-1]<X) return mid+1; else r=mid-1; } else l=mid+1; } return -1; } private static long mergeAndCount(long[] arr, int l, int m, int r) { // Left subarray long[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray long[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l;long swaps = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static long mergeSortAndCount(long[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree long count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } public static long check(long L,long R) { long ans=0; for(int i=1;i<=Math.pow(10,8);i++) { long A=i*(long)i; if(A<L) continue; long upper=(long)Math.floor(Math.sqrt(A-L)); long lower=(long)Math.ceil(Math.sqrt(Math.max(A-R,0))); if(upper>=lower) ans+=upper-lower+1; } return ans; } public static int check(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[]store) { int index=0; ArrayList<Integer> temp=arr.get(x); for(int i:temp) { if(i!=parent) { index+=check(arr,i,x,store); } } store[x]=index; return index+1; } public static void finans(int[][] store,ArrayList<ArrayList<Integer>> arr,int x,int parent) { // ++delete; // System.out.println(x); if(store[x][0]==0 && store[x][1]==0) return; if(store[x][0]!=0 && store[x][1]==0) { ++delete; ans+=store[x][0]; return; } if(store[x][0]==0 && store[x][1]!=0) { ++delete; ans+=store[x][1]; return; } ArrayList<Integer> temp=arr.get(x); if(store[x][0]!=0 && store[x][1]!=0) { ++delete; if(store[x][0]>store[x][1]) { ans+=store[x][0]; for(int i=temp.size()-1;i>=0;i--) { if(temp.get(i)!=parent) { finans(store,arr,temp.get(i),x); break; } } } else { ans+=store[x][1]; for(int i=0;i<temp.size();i++) { if(temp.get(i)!=parent) { finans(store,arr,temp.get(i),x); break; } } } } } public static int dfs(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] store) { int index1=-1,index2=-1; for(int i=0;i<arr.get(x).size();i++) { if(arr.get(x).get(i)!=parent) { if(index1==-1) { index1=i; } else index2=i; } } if(index1==-1) { return 0; } if(index2==-1) { return store[arr.get(x).get(index1)]; } // System.out.println(x); // System.out.println();; return Math.max(store[arr.get(x).get(index1)]+dfs(arr,arr.get(x).get(index2),x,store),store[arr.get(x).get(index2)]+dfs(arr,arr.get(x).get(index1),x,store)); } static int delete=0; public static boolean bounds(int x,int y,int N,int M) { return x>=0 && x<N && y>=0 && y<M; } public static int gcd_check(ArrayList<Integer> temp,char[] ch, int[] arr) { ArrayList<Integer> ini=new ArrayList<>(temp); for(int i=0;i<temp.size();i++) { for(int j=0;j<temp.size();j++) { int req=temp.get(j); temp.set(j,arr[req-1]); } boolean status=true; for(int j=0;j<temp.size();j++) { if(ch[ini.get(j)-1]!=ch[temp.get(j)-1]) status=false; } if(status) return i+1; } return temp.size(); } static long LcmOfArray(int[] arr, int idx) { // lcm(a,b) = (a*b/gcd(a,b)) if (idx == arr.length - 1){ return arr[idx]; } int a = arr[idx]; long b = LcmOfArray(arr, idx+1); return (a*b/gcd(a,b)); // } public static boolean check(ArrayList<Integer> arr,int sum) { for(int i=0;i<arr.size();i++) { for(int j=i+1;j<arr.size();j++) { for(int k=j+1;k<arr.size();k++) { if(arr.get(i)+arr.get(j)+arr.get(k)==sum) return true; } } } return false; } // Returns true if str1 is smaller than str2. static boolean isSmaller(String str1, String str2) { // Calculate lengths of both string int n1 = str1.length(), n2 = str2.length(); if (n1 < n2) return true; if (n2 < n1) return false; for (int i = 0; i < n1; i++) if (str1.charAt(i) < str2.charAt(i)) return true; else if (str1.charAt(i) > str2.charAt(i)) return false; return false; } public static int check(List<String> history) { int[][] arr=new int[history.size()][history.get(0).length()]; for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[0].length;j++) { arr[i][j]=history.get(i).charAt(j)-48; } } for(int i=0;i<arr.length;i++) Arrays.sort(arr[i]); int sum=0; for(int i=0;i<arr[0].length;i++) { int max=0; for(int j=0;j<arr.length;j++) max=Math.max(max,arr[j][i]); sum+=max; } return sum; } // Function for find difference of larger numbers static String findDiff(String str1, String str2) { // Before proceeding further, make sure str1 // is not smaller if (isSmaller(str1, str2)) { String t = str1; str1 = str2; str2 = t; } // Take an empty string for storing result String str = ""; // Calculate length of both string int n1 = str1.length(), n2 = str2.length(); // Reverse both of strings str1 = new StringBuilder(str1).reverse().toString(); str2 = new StringBuilder(str2).reverse().toString(); int carry = 0; // Run loop till small string length // and subtract digit of str1 to str2 for (int i = 0; i < n2; i++) { // Do school mathematics, compute difference of // current digits int sub = ((int)(str1.charAt(i) - '0') - (int)(str2.charAt(i) - '0') - carry); // If subtraction is less then zero // we add then we add 10 into sub and // take carry as 1 for calculating next step if (sub < 0) { sub = sub + 10; carry = 1; } else carry = 0; str += (char)(sub + '0'); } // subtract remaining digits of larger number for (int i = n2; i < n1; i++) { int sub = ((int)(str1.charAt(i) - '0') - carry); // if the sub value is -ve, then make it // positive if (sub < 0) { sub = sub + 10; carry = 1; } else carry = 0; str += (char)(sub + '0'); } // reverse resultant string return new StringBuilder(str).reverse().toString(); } static int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } // Returns factorial of n static int fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static void fill(int[][] dp,int[] arr,int N) { for(int i=1;i<=N;i++) dp[i][0]=arr[i]; for (int j = 1; j <= dp[0].length; j++) for (int i = 1; i + (1 << j) <= N; i++) dp[i][j] = Math.max(dp[i][j-1], dp[i + (1 << (j - 1))][j - 1]); } static int start=0; // static boolean status; public static void dfs(ArrayList<ArrayList<Integer>> arr,int[] A,int[] B,int x,int time,int parent,int K,int coins) { if(time>K) return; ans=Math.max(ans,coins); for(int i:arr.get(x)) { if(i!=parent) { dfs(arr,A,B,i,time+1,x,K,coins); dfs(arr,A,B,i,time+1+B[i],x,K,coins+A[i]); } } } public static boolean dfs_diameter(ArrayList<ArrayList<Integer>> arr,int x,int parent,int node1,int node2,int[] child) { boolean status=false; for(int i:arr.get(x)) { if(i!=parent) { boolean ch=dfs_diameter(arr,i,x,node1,node2,child); status= status || ch; if(!ch && x==node1) ans+=child[i]; child[x]+=child[i]; } } child[x]+=1; return status || (x==node2); } public static boolean check_avai(int x,int y,int sx,int sy,int di) { // if(x==4 && y==4) // System.out.println((Math.abs(x-sx)+Math.abs(y-sy))<=di); return (Math.abs(x-sx)+Math.abs(y-sy))<=di; } public static int lower(ArrayList<div> arr, long x,long y) { int l=0,r=arr.size()-1; while(l<=r) { int mid=(l+r)/2; if(arr.get(mid).x<=x || arr.get(mid).y<=y) l=mid+1; else { if(mid-1>=0 && arr.get(mid-1).x> x && arr.get(mid-1).y> y) r=mid-1; else return mid; } } return -1; } public static int higher(ArrayList<div> arr, long x,long y) { int l=0,r=arr.size()-1; while(l<=r) { int mid=(l+r)/2; if(arr.get(mid).x>=x || arr.get(mid).y>=y) r=mid-1; else { if(mid+1<arr.size() && arr.get(mid+1).x<x && arr.get(mid+1).y<y ) l=mid+1; else return mid; } } return -1; } public static void recur(HashSet<Integer> a,HashSet<Integer> b,long[] aa,long []ab,int bit,boolean[] bits) { // System.out.println("hello"); // System.out.println(bit); if(a.size()==0) return; if(bit<0) return; // if(bit<=4) // { // System.out.println(bit+" hello"); // System.out.println(a); // System.out.println(b); // } HashSet<Integer> b0=new HashSet<>(); HashSet<Integer> b1=new HashSet<>(); HashSet<Integer> a0=new HashSet<>(); HashSet<Integer> a1=new HashSet<>(); for(int i:a) { if((aa[i]>>bit)%2==0) a0.add(i); else a1.add(i); } for(int i:b) { if((ab[i]>>bit)%2==0) b0.add(i); else b1.add(i); } // if(bit==0 && a.size()==2) // { // System.out.println(a0); // System.out.println(a1); // System.out.println(b0); // System.out.println(b1); // } if(a0.size()!=b1.size()) { bits[bit] = false; // System.out.println(bit+" MCBC"); } else if(bits[bit]){ recur(a0, b1, aa, ab, bit - 1, bits); recur(a1, b0, aa, ab, bit - 1, bits); } recur(a, b, aa, ab, bit - 1, bits); } public static void check(char[][] arr,int x,int y,int[][] freq,int N,int M,int[][] status,int index) { if(x-1>=0 && y-1>=0) { if(arr[x-1][y]=='*' && arr[x][y-1]=='*') { freq[x][y]+=1; freq[x-1][y]+=1; freq[x][y-1]+=1; status[x][y]=index; status[x-1][y]=index; status[x][y-1]=index; } } if(x+1<N && y+1<M) { if(arr[x][y+1]=='*' && arr[x+1][y]=='*') { freq[x][y]+=1; freq[x+1][y]+=1; freq[x][y+1]+=1; status[x][y]=index; status[x+1][y]=index; status[x][y+1]=index; } } if(x+1<N && y-1>=0) { if(arr[x][y-1]=='*' && arr[x+1][y]=='*') { freq[x][y]+=1; freq[x][y-1]+=1; freq[x+1][y]+=1; status[x][y]=index; status[x+1][y]=index; status[x][y-1]=index; } } if(x-1>=0 && y+1<M) { if(arr[x-1][y]=='*' && arr[x][y+1]=='*') { freq[x][y]+=1; freq[x-1][y]+=1; freq[x][y+1]+=1; status[x][y]=index; status[x-1][y]=index; status[x][y+1]=index; } } } public static void main(String[] args) throws IOException{ Reader.init(System.in); // BufferedWriter output = new BufferedWriter(new FileWriter("C:/Users/asus/Downloads/test2.txt")); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int T=Reader.nextInt(); long[][] dp=new long[1001][1001]; long[][] check=new long[1001][1001]; for(int m=1;m<=T;m++) { int N=Reader.nextInt(); int M=Reader.nextInt(); char[][] arr=new char[N][M]; for(int i=0;i<N;i++) arr[i]=Reader.next().toCharArray(); int[][] freq=new int[N][M]; int[][] status=new int[N][M]; int index=1; for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(arr[i][j]=='*') { check(arr,i,j,freq,N,M,status,index++); } } } boolean status1=true; for(int i=0;i<N-1;i++) { for(int j=0;j<M-1;j++) { // output.write(freq[i][j]+" "); int cnt=0; HashSet<Integer> hp=new HashSet<>(); if(arr[i+1][j+1]=='*') { cnt+=1; hp.add(status[i+1][j+1]); } if( arr[i][j]=='*') { cnt+=1; hp.add(status[i][j]); } if(arr[i+1][j]=='*') { cnt+=1; hp.add(status[i+1][j]); } if(arr[i][j+1]=='*') { cnt+=1; hp.add(status[i][j+1]); } if(cnt==2) { if(hp.size()>1) status1=false; } } // output.write("\n"); } for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(arr[i][j]=='*' && freq[i][j]!=1) status1=false; } } if(status1) output.write("YES"+"\n"); else output.write("NO"+"\n"); } // output.close(); output.flush(); } } 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()) { 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()); } } class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { left=null; right=null; this.data=data; } } class div { long x; long y; long ar; div(long x,long y,long ar) { this.x=x; this.y=y; this.ar=ar; // this.coins=coins; } } class kar implements Comparator<div> { public int compare(div o1,div o2) { if (o1.x < o2.x) return -1; else if (o1.x > o2.x) return 1; else { if (o1.y < o2.y) return -1; else if (o1.y > o2.y) return 1; else return 0; } } } class trie_node { trie_node[] arr; trie_node() { arr=new trie_node[26]; } public static void insert(trie_node root,String s) { trie_node tmp=root; for(int i=0;i<s.length();i++) { if(tmp.arr[s.charAt(i)-97]!=null) { tmp=tmp.arr[s.charAt(i)-97]; } else { tmp.arr[s.charAt(i)-97]=new trie_node(); tmp=tmp.arr[s.charAt(i)-97]; } } } public static boolean search(trie_node root,String s) { trie_node tmp=root; for(int i=0;i<s.length();i++) { if(tmp.arr[s.charAt(i)-97]!=null) { tmp=tmp.arr[s.charAt(i)-97]; } else { return false; } } return true; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
865c3a34743fa519a0a9e9ab0e33792d
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = true; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static int[][] moves = new int[][]{{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}}; static int[][] a; static int count; static int n,m; static void solve(PrintWriter o) { try { n = fReader.nextInt(); m = fReader.nextInt(); a = new int[n][m]; for(int i=0;i<n;i++) { String s = fReader.nextString(); for(int j=0;j<m;j++) { a[i][j] = s.charAt(j) == '.' ? 0 : 1; } } count = 2; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(a[i][j] == 1) { if(i-1 >= 0 && j-1 >= 0) { if(a[i-1][j] == 1 && a[i][j-1] == 1) { a[i-1][j] = count; a[i][j-1] = count; a[i][j] = count++; } else if(a[i-1][j-1] == 1 && a[i-1][j] == 1) { a[i-1][j-1] = count; a[i-1][j] = count; a[i][j] = count++; } else if(a[i-1][j-1] == 1 && a[i][j-1] == 1) { a[i-1][j-1] = count; a[i][j-1] = count; a[i][j] = count++; } } if(i+1 < n && j+1 < m) { if(a[i+1][j] == 1 && a[i][j+1] == 1) { a[i+1][j] = count; a[i][j+1] = count; a[i][j] = count++; } else if(a[i+1][j+1] == 1 && a[i+1][j] == 1) { a[i+1][j+1] = count; a[i+1][j] = count; a[i][j] = count++; } else if(a[i+1][j+1] == 1 && a[i][j+1] == 1) { a[i+1][j+1] = count; a[i][j+1] = count; a[i][j] = count++; } } if(i+1 < n && j-1 >= 0) { if(a[i+1][j] == 1 && a[i][j-1] == 1) { a[i+1][j] = count; a[i][j-1] = count; a[i][j] = count++; } else if(a[i+1][j-1] == 1 && a[i+1][j] == 1) { a[i+1][j-1] = count; a[i+1][j] = count; a[i][j] = count++; } else if(a[i+1][j-1] == 1 && a[i][j-1] == 1) { a[i+1][j-1] = count; a[i][j-1] = count; a[i][j] = count++; } } if(i-1 >= 0 && j+1 < m) { if(a[i-1][j] == 1 && a[i][j+1] == 1) { a[i-1][j] = count; a[i][j+1] = count; a[i][j] = count++; } else if(a[i-1][j+1] == 1 && a[i-1][j] == 1) { a[i-1][j+1] = count; a[i-1][j] = count; a[i][j] = count++; } else if(a[i-1][j+1] == 1 && a[i][j+1] == 1) { a[i-1][j+1] = count; a[i][j+1] = count; a[i][j] = count++; } } } } for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(a[i][j] == 0) continue; if(a[i][j] == 1) { o.println("NO"); return; } for(int[] move: moves) { int nx = move[0]+i; int ny = move[1]+j; if(nx >= 0 && nx < n && ny >= 0 && ny < m) { if(a[nx][ny] != 0 && a[nx][ny] != a[i][j]) { o.println("NO"); return; } } } } o.println("YES"); } catch (Exception e){e.printStackTrace();} } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } 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 / gcd(a,b)*b; } public static boolean isPrime(long x){ boolean ok = true; for(long i=2;i<=Math.sqrt(x);i++){ if(x % i == 0){ ok = false; break; } } return ok; } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static long qpow(long a, long n){ long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % mod; } n >>= 1; a = a * a % mod; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] tree; public FenWick(int n){ this.n = n; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
c3e370cc2fe05331587189acba5204e0
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) throws IOException{ FastReader s = new FastReader(); int t = s.nextInt(); outer: while(t-->0){ int n = s.nextInt(); int m = s.nextInt(); char[][] grid = new char[n][m]; for(int i = 0; i < n; i++){ grid[i] = s.nextLine().toCharArray(); } for(int i = 0; i < n - 1; i++){ for(int j = 0; j < m - 1; j++){ int count = 0; int hash = 0; if(grid[i][j] == '*'){ count++; } if(grid[i][j + 1] == '*'){ count++; } if(grid[i + 1][j] == '*'){ count++; } if(grid[i + 1][j + 1] == '*'){ count++; } if(grid[i][j] == '#'){ hash++; } if(grid[i][j + 1] == '#'){ hash++; } if(grid[i + 1][j] == '#'){ hash++; } if(grid[i + 1][j + 1] == '#'){ hash++; } if((count > 0 && hash > 0) || count == 4){ System.out.println("No"); continue outer; } if(count == 2 && ((grid[i][j] == '*' && grid[i + 1][j + 1] == '*') || (grid[i][j + 1] == '*' && grid[i + 1][j] == '*'))){ System.out.println("No"); continue outer; } if(hash == 2 && ((grid[i][j] == '#' && grid[i + 1][j + 1] == '#') || (grid[i][j + 1] == '#' && grid[i + 1][j] == '#'))){ System.out.println("No"); continue outer; } if(count == 3){ if(grid[i][j] == '*'){ grid[i][j] = '#'; } if(grid[i][j + 1] == '*'){ grid[i][j + 1] = '#'; } if(grid[i + 1][j] == '*'){ grid[i + 1][j] = '#'; } if(grid[i + 1][j + 1] == '*'){ grid[i + 1][j + 1] = '#'; } } } } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(grid[i][j] == '*'){ System.out.println("No"); continue outer; } } } for(int i = 0; i < n - 2; i++){ for(int j = 0; j < m; j++){ if(grid[i][j] == '#' && grid[i + 1][j] == '#' && grid[i + 2][j] == '#'){ System.out.println("No"); continue outer; } } } for(int i = 0; i < n; i++){ for(int j = 0; j < m - 2; j++){ if(grid[i][j] == '#' && grid[i][j + 1] == '#' && grid[i][j + 2] == '#'){ System.out.println("No"); continue outer; } } } System.out.println("Yes"); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException{ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next()); } long nextLong() throws IOException{ return Long.parseLong(next()); } double nextDouble() throws IOException{return Double.parseDouble(next());} String nextLine() throws IOException{ String str = ""; if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } return str; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
f0d6d8daa3122fb43c4945920880f4c5
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; public class Solution{ public 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 swap(int i, int j) { int temp = i; i = j; j = temp; } static class Pair{ int s; int end; public Pair(int a, int e) { end = e; s = a; } } 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(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } public static void main(String[] args) throws Exception { FastReader scn = new FastReader(); OutputStream outputStream = System.out; OutputWriter output = new OutputWriter(outputStream); int t = scn.nextInt(); long mod = 1000000007L; // String[] pal = {"0000", "0110","0220","0330","0440","0550","1001","1111","1221","1331","1441","1551","2002","2112","2222","2332"}; while(t>0) { // int n = scn.nextInt(); // long max =0, min= Integer.MAX_VALUE; // long[] a = new long[n]; // for(int i=0;i<n;i++) { // a[i]= scn.nextLong(); // max = Math.max(max,a[i]); // min = Math.min(min, a[i]); // } int n = scn.nextInt(); int m = scn.nextInt(); char[][] a = new char[n][m]; for(int i=0;i<n;i++) { String s = scn.next(); for(int j=0;j<m;j++) { a[i][j] = s.charAt(j); } } boolean b = true; boolean[][] mark = new boolean[n][m]; for(int i=0;i<n-1;i++) { boolean f = false; for(int j=0;j<m-1;j++) { if(checkL(a,i,j, mark)==1) { f = true; } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]=='*') { if(!mark[i][j]) { b=false; } } // output.print(mark[i][j]+" "); } //output.println(); } if(b==true) { output.println("YES"); }else { output.println("NO"); } t--; } output.close(); } public static int checkL(char[][] a, int r, int c, boolean[][] mark) { int cnt=0; for(int i=Math.max(r-1,0);i<=Math.min(r+2,a.length-1);i++) { for(int j= Math.max(c-1,0);j<=Math.min(c+2,a[0].length-1);j++) { if(a[i][j]=='*') { cnt++; } } } char ch11 = a[r][c]; char ch12 = a[r][c+1]; char ch21 = a[r+1][c]; char ch22 = a[r+1][c+1]; int n = a.length; int m = a[0].length; if(cnt==4) { if(ch22!='*' && r+2<n && c+2<m && a[r+2][c+2]=='*') { cnt--; }else if(ch21!='*' && r+2<n && c-1>=0 && a[r+2][c-1]=='*') { cnt--; }else if(ch11!='*' && r-1>=0 && c-1>=0 && a[r-1][c-1]=='*') { cnt--; }else if(ch12!='*' && r-1>=0 && c+2<m && a[r-1][c+2]=='*') { cnt--; } } if(cnt==0) { return 1; } if(cnt>3) { return 4; } if(cnt<3) { return 2; } if((ch11=='*' && ch12=='*' && ch22=='*' && ch21!='*')|| (ch11=='*' && ch21=='*' && ch22=='*' && ch12!='*') || (ch12=='*'&&ch22=='*'&&ch21=='*'&&ch11!='*')||(ch11=='*'&&ch12=='*'&&ch21=='*'&&ch22!='*')) { for(int i=r;i<r+2;i++) { for(int j=c;j<c+2;j++) { if(a[i][j]=='*') { mark[i][j] = true; } } } return 1; } return 1; } public static void addTime(int h, int m, int ah, int am) { int rm = m+am; int rh = (h+ah+(rm/60))%24; rm = rm%60; } public static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
4fba2ec0f01f1c4ceb51d09e9d6529c9
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class sol { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static int floodfill(char[][] grid, boolean[][] visited, int i, int j) { int visitedCnt = 1; Stack<int[]> s = new Stack<>(); s.push(new int[] {i,j}); int[][] dirs = new int[][] {{0,1},{1,0},{-1,0},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}}; while(!s.isEmpty()) { int[] nxt = s.pop(); for (int[] x : dirs) { int[] p = new int[] {x[0]+nxt[0],x[1]+nxt[1]}; if(p[0]<0 || p[0] >= grid.length || p[1]<0 || p[1] >= grid[0].length) { continue; } if(grid[p[0]][p[1]] != '*' || visited[p[0]][p[1]]) { continue; } visited[p[0]][p[1]] = true; s.push(p); visitedCnt++; } } return visitedCnt; } public static void solve() throws IOException { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); char[][] grid = new char[n][m]; for (int i = 0; i < n; i++) { grid[i] = br.readLine().toCharArray(); } int regions = 0; boolean[][] visited = new boolean[grid.length][grid[0].length]; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if(grid[i][j] == '*' && !visited[i][j]) { regions++; visited[i][j] = true; int res = floodfill(grid,visited,i,j); if(res != 3) { System.out.println("NO"); return; } } } } int lCnt = 0; for (int i = 0; i < grid.length - 1; i++) { for (int j = 0; j < grid[0].length - 1; j++) { int cnt = 0; for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { if(grid[i+a][j+b]=='*') cnt++; } } if(cnt == 3) lCnt++; } } if(lCnt == regions) { System.out.println("YES"); } else { System.out.println("NO"); } } public static void main(String[] args) throws IOException { int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) solve(); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
cdff9f0d829a58f8fbbfad5546736cfe
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import javax.print.attribute.IntegerSyntax; import java.util.*; import java.io.*; public class Solution { private static class FastIO { private static class FastReader { BufferedReader br; StringTokenizer st; 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; } } private static PrintWriter out = new PrintWriter(System.out); private static FastReader in = new FastReader(); public void print(String s) {out.print(s);} public void println(String s) {out.println(s);} public void println() { println(""); } public void print(int i) {out.print(i);} public void print(long i) {out.print(i);} public void print(char i) {out.print(i);} public void print(double i) {out.print(i);} public void println(int i) {out.println(i);} public void println(long i) {out.println(i);} public void println(char i) {out.println(i);} public void println(double i) {out.println(i);} public void printIntArrayWithoutSpaces(int[] a) { for(int i : a) { out.print(i); } out.println(); } public void printIntArrayWithSpaces(int[] a) { for(int i : a) { out.print(i + " "); } out.println(); } public void printIntArrayNewLine(int[] a) { for(int i : a) { out.println(i); } } public int[] getIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public List<Integer> getIntList(int n) { List<Integer> list = new ArrayList<>(); for(int i = 0; i < n; i++) { list.add(in.nextInt()); } return list; } public static void printKickstartCase(int i) { out.print("Case #" + i + ": "); } public String next() {return in.next();} int nextInt() { return in.nextInt(); } char nextChar() {return in.next().charAt(0);} long nextLong() { return in.nextLong(); } double nextDouble() { return in.nextDouble(); } String nextLine() {return in.nextLine();} public void close() { out.flush(); out.close(); } } private static final FastIO io = new FastIO(); private static class MathUtil { public static final int MOD = 1000000007; public static int gcd(int a, int b) { if(b == 0) { return a; } return gcd(b, a % b); } public static int gcd(int[] a) { if(a.length == 0) { return 0; } int res = a[0]; for(int i = 1; i < a.length; i++) { res = gcd(res, a[i]); } return res; } public static int gcd(List<Integer> a) { if(a.size() == 0) { return 0; } int res = a.get(0); for(int i = 1; i < a.size(); i++) { res = gcd(res, a.get(i)); } return res; } public static int modular_mult(int a, int b, int M) { long res = (long)a * b; return (int)(res % M); } public static int modular_mult(int a, int b) { return modular_mult(a, b, MOD); } public static int modular_add(int a, int b, int M) { long res = (long)a + b; return (int)(res % M); } public static int modular_add(int a, int b) { return modular_add(a, b, MOD); } public static int modular_sub(int a, int b, int M) { long res = ((long)a - b) + M; return (int)(res % M); } public static int modular_sub(int a, int b) { return modular_sub(a, b, MOD); } //public static int modular_div(int a, int b, int M) {} //public static int modular_div(int a, int b) {return modular_div(a, b, MOD);} public static int pow(int a, int b, int M) { int res = 1; while (b > 0) { if ((b & 1) == 1) { res = modular_mult(res, a, M); } a = modular_mult(a, a, M); b = b >> 1; } return res; } public static int pow(int a, int b) { return pow(a, b, MOD); } /*public static int fact(int i, int M) { } public static int fact(int i) { } public static void preComputeFact(int i) { } public static int mod_mult_inverse(int den, int mod) { } public static void C(int n, int r) { }*/ } private static class ArrayUtil { @FunctionalInterface private static interface NumberPairComparator { boolean test(int a, int b); } public static int[] nextGreaterOrSmallerRight(int[] a, NumberPairComparator npc) { int n = a.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for(int i = 0; i < n; i++) { int cur = a[i]; while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) { res[stack.pop()] = i; } stack.push(i); } while(!stack.isEmpty()) { res[stack.pop()] = n; } return res; } public static int[] nextGreaterOrSmallerLeft(int[] a, NumberPairComparator npc) { int n = a.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for(int i = n - 1; i >= 0; i--) { int cur = a[i]; while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) { res[stack.pop()] = i; } stack.push(i); } while(!stack.isEmpty()) { res[stack.pop()] = n; } return res; } public static Map<Integer, Integer> getFreqMap(int[] a) { Map<Integer, Integer> map = new HashMap<>(); for(int i : a) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } public static long arraySum(int[] a) { long sum = 0; for(int i : a) { sum += i; } return sum; } private static int maxIndex(int[] a) { int max = Integer.MIN_VALUE; int max_index = -1; for(int i = 0; i < a.length; i++) { if(a[i] > max) { max = a[i]; max_index = i; } } return max_index; } } private static final int M = 1000000007; private static final String yes = "YES"; private static final String no = "NO"; private static int MAX = M / 100; private static final int MIN = -MAX; private static final int UNVISITED = -1; private static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return "x: " + x + " y: " + y; } } private static List<List<Integer>> getAdj(int n, int e, boolean directed, boolean shift) { List<List<Integer>> res = new ArrayList<>(); for(int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for(int i = 0; i < e; i++) { int u = io.nextInt(); int v = io.nextInt(); if(shift) { u --; v --; } res.get(u).add(v); if(!directed) { res.get(v).add(u); } } return res; } private static class TreeUtil { private static List<List<Integer>> getTreeInputWithOffset(int n) { List<List<Integer>> res = new ArrayList<>(); for(int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for(int i = 0; i < n - 1; i++) { int u = io.nextInt() - 1; int v = io.nextInt() - 1; res.get(u).add(v); res.get(v).add(u); } return res; } private static int subtreeSizeDfs(int root, int parent, List<List<Integer>> tree, List<Integer> res) { int sum = 0; for(int child : tree.get(root)) { if(child == parent) { continue; } sum += subtreeSizeDfs(child, root, tree, res); } res.set(root, 1 + sum); return 1 + sum; } private static List<Integer> getSubtreeSizes(List<List<Integer>> tree, int root) { List<Integer> res = new ArrayList<>(); for(int i = 0; i < tree.size(); i++) { res.add(0); } subtreeSizeDfs(root, -1, tree, res); return res; } } private static final long e3 = 1_000; private static final long e6 = 1_000_000; private static final long e9 = 1_000_000_000; private static final long e12 = e9 * e3; private static final long e15 = e9 * e6; private static final long e18 = e9 * e9; String mod = "%"; private static int f(int node, int parent, List<List<Integer>> tree, List<Integer> subtreeSizes) { tree.get(node).remove(Integer.valueOf(parent)); int numChildren = tree.get(node).size(); if(numChildren == 0) { return 0; } if(numChildren == 1) { return subtreeSizes.get(tree.get(node).get(0)) - 1; } int left = tree.get(node).get(0); int right = tree.get(node).get(1); int leftAns = subtreeSizes.get(left) - 1 + f(right, node, tree, subtreeSizes); int rightAns = subtreeSizes.get(right) - 1 + f(left, node, tree, subtreeSizes); return Math.max(leftAns, rightAns); } private static final int[] dr = new int[]{-1, 0, 1, -1, 1, -1, 0, 1}; private static final int[] dc = new int[]{-1, -1, -1, 0, 0, 1, 1, 1}; private static void dfs(int i, int j, char[][] a, boolean[][] visited, List<int[]> res) { visited[i][j] = true; int[] cur = new int[]{i, j}; res.add(cur); for(int k = 0; k < dr.length; k++) { int newI = i + dr[k]; int newJ = j + dc[k]; boolean outOfBounds = newI < 0 || newJ < 0 || newI >= a.length || newJ >= a[0].length; if(outOfBounds || a[newI][newJ] == '.' || visited[newI][newJ]) { continue; } dfs(newI, newJ, a, visited, res); } } private static boolean isRectangle(List<int[]> component) { component.sort((a, b) -> a[0] - b[0]); if(component.get(2)[0] - component.get(0)[0] == 2) { return false; } component.sort((a, b) -> a[1] - b[1]); return component.get(2)[1] - component.get(0)[1] != 2; } public static void main(String[] args) { int testcases = io.nextInt(); // int testcases = 1; for(int zqt = 0; zqt < testcases; zqt ++) { int n = io.nextInt(); int m = io.nextInt(); char[][] a = new char[n][m]; for(int i = 0; i < n; i++) { String s = io.next(); for (int j = 0; j < m; j++) { a[i][j] = s.charAt(j); } } boolean[][] visited = new boolean[n][m]; boolean res = true; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(visited[i][j] || a[i][j] == '.') { continue; } List<int[]> component = new ArrayList<>(); dfs(i, j, a, visited, component); if(component.size() != 3 || !isRectangle(component)) { res = false; break; } } if(!res) { break; } } io.println(res ? yes : no); } io.close(); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
10a53e4a380ddaa7140e86d6829436c1
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws Exception { int T = sc.nextInt(); for(int i = 0; i < T; i++)solve(); //solve(); pw.flush(); } public static void solve() { int n = sc.nextInt(); int m = sc.nextInt(); int[][] map = new int[n][m]; for(int i = 0; i < n; i++){ char[] s = sc.next().toCharArray(); for(int j = 0; j < m; j++){ if(s[j] == '*'){ map[i][j]++; } } } int[] cy = {-1,-1,-1,-1,0,0,1,1,2,2,2,2}; int[] cx = {-1,0,1,2,-1,2,-1,2,-1,0,1,2}; //0,3,8,11 for(int i = 0; i < n-1; i++){ for(int j = 0; j < m-1; j++){ int val = map[i][j] + map[i][j+1] + map[i+1][j] + map[i+1][j+1]; if(val == 4){ pw.println("NO"); return; }else if(val == 3){ for(int c = 0; c < 12; c++){ if(c == 0 && map[i][j] == 0){ continue; } if(c == 3 && map[i][j+1] == 0){ continue; } if(c == 8 && map[i+1][j] == 0){ continue; } if(c == 11 && map[i+1][j+1] == 0){ continue; } int ci = i+cy[c]; int cj = j+cx[c]; if(0 <= ci && 0 <= cj && ci < n && cj < m && map[ci][cj] == 1){ pw.println("NO"); return; } } map[i][j] = map[i][j+1] = map[i+1][j] = map[i+1][j+1] = 0; } } } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(map[i][j] == 1){ pw.println("NO"); return; } } } pw.println("YES"); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
5b8f853fb10c9049ad707aff9598ca45
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class F1722 { static int[] dx; static int[] dy; static boolean[][] seen; static boolean[][] arr; static int[][] val; public static void main(String[] args) throws IOException{ StringBuffer ans = new StringBuffer(); StringTokenizer st; BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(; t > 0; t--){ dx = new int[]{-1,0,0,1}; dy = new int[]{0,1,-1,0}; st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); arr = new boolean[n][m]; val = new int[n][m]; for(int i = 0; i < n; i++){ st = new StringTokenizer(f.readLine()); String str = st.nextToken(); for(int x = 0; x < m; x++){ if(str.charAt(x) == '*') arr[i][x] = true; } } int curr = 1; seen = new boolean[n][m]; boolean broken = false; for(int i = 0; i < n; i++){ for(int x = 0; x < m; x++){ if(i != 0 && i < n-1 && arr[i][x] && arr[i-1][x] && arr[i+1][x]) broken = true; if(x != 0 && x < m-1 && arr[i][x] && arr[i][x-1] && arr[i][x+1]) broken = true; if(!seen[i][x] && arr[i][x]){ val[i][x] = curr; if(dfs(i,x,curr) != 3) broken = true; curr++; } } } //System.out.println(broken); dx = new int[]{0,0,1,1,1,-1,-1,-1}; dy = new int[]{1,-1,0,1,-1,-1,0,1}; for(int i = 0; i < n; i++){ for(int x = 0; x < m; x++){ if(!arr[i][x]) continue; for(int j = 0; j < 8; j++){ int ni = dx[j]+i; int nx = dy[j]+x; if(ni < 0 || nx < 0 || ni == n || nx == m) continue; if(val[i][x] < val[ni][nx]) broken = true; } } } //for(int i = 0; i < n; i++) System.out.println(Arrays.toString(val[i])); if(broken) ans.append("NO").append("\n"); else ans.append("YES").append("\n"); } System.out.println(ans); f.close(); } public static int dfs(int i, int x, int curr){ seen[i][x] = true; int hmany = 1; for(int p = 0; p < 4; p++){ int ji = i+dx[p]; int jy = x+dy[p]; if(ji >= 0 && jy >= 0 && ji < arr.length && jy < arr[0].length && !seen[ji][jy] && arr[ji][jy]){ val[ji][jy] = curr; hmany+=dfs(ji,jy,curr); } } return hmany; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
8da161fdb25f76f55431bb4bc9b2dfdc
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static boolean surrup(String[] ar, int n, int m, int r, int c) { if (r > 0) { if (ar[r-1].charAt(c) == '*') { return true; } } return false; } public static boolean surrdown(String[] ar, int n, int m, int r, int c) { if (r < n-1) { if (ar[r+1].charAt(c) == '*') { return true; } } return false; } public static boolean surrleft(String[] ar, int n, int m, int r, int c) { if (c > 0) { if (ar[r].charAt(c-1) == '*') { return true; } } return false; } public static boolean surrright(String[] ar, int n, int m, int r, int c) { if (c < m-1) { if (ar[r].charAt(c+1) == '*') { return true; } } return false; } public static boolean upright(String[] ar, int n, int m, int r, int c) { if (c < m-1 && r > 0) { if (ar[r-1].charAt(c+1) == '*') { return true; } } return false; } public static boolean upleft(String[] ar, int n, int m, int r, int c) { if (c > 0 && r > 0) { if (ar[r-1].charAt(c-1) == '*') { return true; } } return false; } public static boolean downright(String[] ar, int n, int m, int r, int c) { if (c < m-1 && r < n-1) { if (ar[r+1].charAt(c+1) == '*') { return true; } } return false; } public static boolean downleft(String[] ar, int n, int m, int r, int c) { if (c > 0 && r < n-1) { if (ar[r+1].charAt(c-1) == '*') { return true; } } return false; } public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); MyReader in = new MyReader(); int t = in.nextInt(); for (int i = 1; i < t+1; i++) { int n = in.nextInt(); int m = in.nextInt(); String[] ar = new String[n]; HashSet<Integer> hs = new HashSet<>(); LinkedList<Integer> ll = new LinkedList<>(); // row * 100 + col for (int j = 0; j < n; j++) { ar[j] = in.next(); for (int k = 0; k < m; k++) { if (ar[j].charAt(k) == '*') { hs.add(100 * j + k); ll.add(100 * j + k); } } } if (hs.size() % 3 != 0) { out.println("NO"); continue; } boolean broken = false; while (!ll.isEmpty()) { int curr = ll.pop(); if (!hs.contains(curr)) { continue; } int r = curr / 100; int c = curr % 100; hs.remove(curr); HashSet<Integer> adj = new HashSet<>(); if (surrup(ar, n, m, r, c)) { adj.add((r-1) * 100 + c); } if (surrdown(ar, n, m, r, c)) { adj.add((r+1) * 100 + c); } if (surrleft(ar, n, m, r, c)) { adj.add(r * 100 + c-1); } if (surrright(ar, n, m, r, c)) { adj.add(r * 100 + c + 1); } if (adj.size() > 2 || adj.size() == 0) { broken = true; break; } if (adj.size() == 2) { if (upleft(ar, n, m, r, c)) { broken = true; break; } if (upright(ar, n, m, r, c)) { broken = true; break; } if (downleft(ar, n, m, r, c)) { broken = true; break; } if (downright(ar, n, m, r, c)) { broken = true; break; } boolean bad = false; for (int check : adj) { int rr = check / 100; int cc = check % 100; int cnt = 0; if (surrup(ar, n, m, rr, cc)) { cnt++; if (!adj.contains((rr-1) * 100 + cc) && ((rr-1) * 100 + cc) != curr) { bad = true; break; } } if (surrdown(ar, n, m, rr, cc)) { cnt++; if (!adj.contains((rr+1) * 100 + cc) && ((rr+1) * 100 + cc) != curr) { bad = true; break; } } if (surrright(ar, n, m, rr, cc)) { cnt++; if (!adj.contains(rr * 100 + cc + 1) && (rr * 100 + cc + 1) != curr) { bad = true; break; } } if (surrleft(ar, n, m, rr, cc)) { cnt++; if (!adj.contains(rr * 100 + cc - 1) && (rr * 100 + cc - 1) != curr) { bad = true; break; } } if (downleft(ar, n, m, rr, cc)) { cnt++; if (!adj.contains((rr+1) * 100 + cc - 1) && ((rr+1) * 100 + cc - 1) != curr) { bad = true; break; } } if (downright(ar, n, m, rr, cc)) { cnt++; if (!adj.contains((rr+1) * 100 + cc + 1) && ((rr+1) * 100 + cc + 1) != curr) { bad = true; break; } } if (upleft(ar, n, m, rr, cc)) { cnt++; if (!adj.contains((rr-1) * 100 + cc - 1) && ((rr-1) * 100 + cc - 1) != curr) { bad = true; break; } } if (upright(ar, n, m, rr, cc)) { cnt++; if (!adj.contains((rr-1) * 100 + cc + 1) && ((rr-1) * 100 + cc + 1) != curr) { bad = true; break; } } if (cnt != 2) { bad = true; break; } hs.remove(check); } if (bad) { broken = true; break; } } if (adj.size() == 1) { } } if (broken) { out.println("NO"); continue; } if (!hs.isEmpty()) { out.println("NO"); } else { out.println("YES"); } } out.flush(); } public static class MyReader { private BufferedReader br; private StringTokenizer st; public MyReader() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(this.next()); } public long nextLong() throws IOException { return Long.parseLong(this.next()); } public double nextDouble() throws IOException { return Double.parseDouble(this.next()); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
cef4997abf1352983e6ab5ac167e1b9d
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static boolean surrup(String[] ar, int n, int m, int r, int c) { if (r > 0) { if (ar[r-1].charAt(c) == '*') { return true; } } return false; } public static boolean surrdown(String[] ar, int n, int m, int r, int c) { if (r < n-1) { if (ar[r+1].charAt(c) == '*') { return true; } } return false; } public static boolean surrleft(String[] ar, int n, int m, int r, int c) { if (c > 0) { if (ar[r].charAt(c-1) == '*') { return true; } } return false; } public static boolean surrright(String[] ar, int n, int m, int r, int c) { if (c < m-1) { if (ar[r].charAt(c+1) == '*') { return true; } } return false; } public static boolean upright(String[] ar, int n, int m, int r, int c) { if (c < m-1 && r > 0) { if (ar[r-1].charAt(c+1) == '*') { return true; } } return false; } public static boolean upleft(String[] ar, int n, int m, int r, int c) { if (c > 0 && r > 0) { if (ar[r-1].charAt(c-1) == '*') { return true; } } return false; } public static boolean downright(String[] ar, int n, int m, int r, int c) { if (c < m-1 && r < n-1) { if (ar[r+1].charAt(c+1) == '*') { return true; } } return false; } public static boolean downleft(String[] ar, int n, int m, int r, int c) { if (c > 0 && r < n-1) { if (ar[r+1].charAt(c-1) == '*') { return true; } } return false; } public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); MyReader in = new MyReader(); int t = in.nextInt(); for (int i = 1; i < t+1; i++) { int n = in.nextInt(); int m = in.nextInt(); String[] ar = new String[n]; HashSet<Integer> hs = new HashSet<>(); LinkedList<Integer> ll = new LinkedList<>(); // row * 100 + col for (int j = 0; j < n; j++) { ar[j] = in.next(); for (int k = 0; k < m; k++) { if (ar[j].charAt(k) == '*') { hs.add(100 * j + k); ll.add(100 * j + k); } } } if (hs.size() % 3 != 0) { out.println("NO"); continue; } boolean broken = false; while (!ll.isEmpty()) { int curr = ll.pop(); if (!hs.contains(curr)) { continue; } int r = curr / 100; int c = curr % 100; hs.remove(curr); LinkedList<Integer> adj = new LinkedList<>(); if (surrup(ar, n, m, r, c)) { adj.add((r-1) * 100 + c); } if (surrdown(ar, n, m, r, c)) { adj.add((r+1) * 100 + c); } if (surrleft(ar, n, m, r, c)) { adj.add(r * 100 + c-1); } if (surrright(ar, n, m, r, c)) { adj.add(r * 100 + c + 1); } if (adj.size() > 2 || adj.size() == 0) { broken = true; break; } if (adj.size() == 2) { if (upleft(ar, n, m, r, c)) { broken = true; break; } if (upright(ar, n, m, r, c)) { broken = true; break; } if (downleft(ar, n, m, r, c)) { broken = true; break; } if (downright(ar, n, m, r, c)) { broken = true; break; } boolean bad = false; for (int check : adj) { int rr = check / 100; int cc = check % 100; int cnt = 0; if (surrup(ar, n, m, rr, cc)) { cnt ++; } if (surrdown(ar, n, m, rr, cc)) { cnt++; } if (surrright(ar, n, m, rr, cc)) { cnt++; } if (surrleft(ar, n, m, rr, cc)) { cnt++; } if (downleft(ar, n, m, rr, cc)) { cnt++; } if (downright(ar, n, m, rr, cc)) { cnt++; } if (upleft(ar, n, m, rr, cc)) { cnt++; } if (upright(ar, n, m, rr, cc)) { cnt++; } if (cnt != 2) { bad = true; break; } hs.remove(check); } if (bad) { broken = true; break; } } if (adj.size() == 1) { } } if (broken) { out.println("NO"); continue; } if (!hs.isEmpty()) { out.println("NO"); } else { out.println("YES"); } } out.flush(); } public static class MyReader { private BufferedReader br; private StringTokenizer st; public MyReader() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(this.next()); } public long nextLong() throws IOException { return Long.parseLong(this.next()); } public double nextDouble() throws IOException { return Double.parseDouble(this.next()); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
cfb2cf4c4f27db1e9ce6d54b1a392709
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { static Reader r = new Reader(); static StringBuilder sb = new StringBuilder(); static int n,m; static int grid[][]; static int cnt; static int nb[][]; static boolean visited[][]; static int[] dx = {-1,1,0,0,-1,-1,1,1}; static int[] dy = {0,0,-1,1,-1,1,1,-1}; public static void main(String args[]) throws IOException { int t = r.readInt(); while(t-->0){ solve(); } System.out.println(sb); } static void solve() throws IOException{ n = r.readInt(); m = r.readInt(); grid = new int[n][m]; visited = new boolean[n][m]; for(int i=0;i<n;i++){ String s= r.readLine(); for(int j=0;j<m;j++) { if(s.charAt(j)=='*'){ grid[i][j] = 1; } } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(grid[i][j]==1 && !visited[i][j]) { cnt = 0; dfs(i,j,0,0); if(cnt!=3){ sb.append("NO\n"); return; } } } } nb = new int[n][m]; int ni,nj; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(grid[i][j]==0) continue; for(int k=0;k<8;k++){ ni = i+dx[k]; nj = j+dy[k]; if(ni<0 || nj<0 || ni>=n || nj>=m || grid[ni][nj]==0) continue; nb[i][j] ++; } } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(grid[i][j]==1 && nb[i][j]!=2){ sb.append("NO\n"); return; } } } sb.append("YES\n"); } static void dfs(int x, int y, int px, int py){ visited[x][y] = true; cnt++; if(cnt>3) return; for(int k=0;k<4;k++){ int nx = x+dx[k]; int ny = y+dy[k]; if(nx<0 || nx>=n || ny<0 || ny>=m) continue; if(grid[nx][ny]==0 || visited[nx][ny]) continue; if(cnt==2 && nx+px==2*x && ny+py ==2*y){ cnt = 4; return; } dfs(nx,ny,x,y); } } } 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 String readLine() throws IOException { byte[] buf = new byte[5000]; // line length int cnt = 0, c; while((c=read())!=-1){ if(c=='\n'){ if(cnt!=0) break; else continue; } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int readInt() throws IOException { int ret = 0; byte c = read(); while(c <= ' '){ c = read();} boolean neg = (c == '-'); if(neg) c = read(); do{ ret = (ret<<3) + (ret<<1) + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg ? -ret : ret; } public long readLong() throws IOException { long ret = 0; byte c = read(); while(c <= ' '){ c = read();} boolean neg = (c == '-'); if(neg) c = read(); do{ ret = (ret<<3) + (ret<<1) + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg ? -ret : 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(); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
16193755d9e38e9d27cace932266f335
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; import java.util.StringTokenizer; import java.util.concurrent.ThreadLocalRandom; /* 3 2 1 4 3 1 4 2 //1 3 4 2 1 //2 4 2 1 3 //3 000 001 010 011 100 101 110 111 1 2 3 4 5 6 1 3 6 10 15 21 */ public class CF { private static void sport(StringBuilder[] a) { int n = a.length; int m = a[0].length(); char c = 44; /* / // */ for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) { if (a[i].charAt(j) == '*' && a[i + 1].charAt(j) == '*' && a[i + 1].charAt(j + 1) == '*') { a[i].setCharAt(j, c); a[i + 1].setCharAt(j, c); a[i + 1].setCharAt(j + 1, c); int[][] coord = new int[][] {{i, j}, {i + 1, j}, {i + 1, j + 1}}; if (!check(a, coord, c)) { System.out.println("NO"); return; } c++; } } } /* / // */ for (int i = 0; i < n - 1; i++) { for (int j = 1; j < m; j++) { if (a[i].charAt(j) == '*' && a[i + 1].charAt(j) == '*' && a[i+1].charAt(j - 1) == '*') { a[i].setCharAt(j, c); a[i + 1].setCharAt(j, c); a[i + 1].setCharAt(j - 1, c); int[][] coord = new int[][] {{i, j}, {i + 1, j}, {i + 1, j - 1}}; if (!check(a, coord, c)) { System.out.println("NO"); return; } c++; } } } /* // / */ for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) { if (a[i].charAt(j) == '*' && a[i].charAt(j + 1) == '*' && a[i + 1].charAt(j + 1) == '*') { a[i].setCharAt(j, c); a[i].setCharAt(j + 1, c); a[i + 1].setCharAt(j + 1, c); int[][] coord = new int[][] {{i, j}, {i, j + 1}, {i + 1, j + 1}}; if (!check(a, coord, c)) { System.out.println("NO"); return; } c++; } } } /* // / */ for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) { if (a[i].charAt(j) == '*' && a[i].charAt(j + 1) == '*' && a[i + 1].charAt(j) == '*') { a[i].setCharAt(j, c); a[i].setCharAt(j + 1, c); a[i + 1].setCharAt(j, c); int[][] coord = new int[][] {{i, j}, {i, j + 1}, {i + 1, j}}; if (!check(a, coord, c)) { System.out.println("NO"); return; } c++; } } } for (StringBuilder builder : a) { for (char c1 : builder.toString().toCharArray()) { if (c1=='*'){ System.out.println("NO"); return; } } } System.out.println("YES"); } static boolean check(StringBuilder[] a, int[][] coord, char c) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (i == 0 && j == 0) { continue; } for (int[] ints : coord) { int nr = ints[0] + i; int nc = ints[1] + j; if (nr < 0 || nr >= a.length || nc < 0 || nc >= a[0].length()) { continue; } if (a[nr].charAt(nc) == c || a[nr].charAt(nc) == '.') { continue; } else { return false; } } } } return true; } static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); StringBuilder[] a = new StringBuilder[n]; for (int j = 0; j < n; j++) { a[j] = new StringBuilder(sc.next()); } sport(a); } } // 1 2 3 4 5 6 | 2 static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } static class BIT { // The size of the array holding the Fenwick tree values final int N; // This array contains the Fenwick tree ranges private long[] tree; // Create an empty Fenwick Tree with 'sz' parameter zero based. public BIT(int sz) { tree = new long[(N = sz + 1)]; } // Construct a Fenwick tree with an initial set of values. // The 'values' array MUST BE ONE BASED meaning values[0] // does not get used, O(n) construction. public BIT(long[] values) { if (values == null) { throw new IllegalArgumentException("Values array cannot be null!"); } N = values.length; values[0] = 0L; // Make a clone of the values array since we manipulate // the array in place destroying all its original content. tree = values.clone(); for (int i = 1; i < N; i++) { int parent = i + lsb(i); if (parent < N) { tree[parent] += tree[i]; } } } // Returns the value of the least significant bit (LSB) // lsb(108) = lsb(0b1101100) = 0b100 = 4 // lsb(104) = lsb(0b1101000) = 0b1000 = 8 // lsb(96) = lsb(0b1100000) = 0b100000 = 32 // lsb(64) = lsb(0b1000000) = 0b1000000 = 64 private static int lsb(int i) { // Isolates the lowest one bit value return i & -i; // An alternative method is to use the Java's built in method // return Integer.lowestOneBit(i); } // Computes the prefix sum from [1, i], O(log(n)) private long prefixSum(int i) { long sum = 0L; while (i != 0) { sum += tree[i]; i &= ~lsb(i); // Equivalently, i -= lsb(i); } return sum; } // Returns the sum of the interval [left, right], O(log(n)) public long sum(int left, int right) { if (right < left) { throw new IllegalArgumentException("Make sure right >= left"); } return prefixSum(right) - prefixSum(left - 1); } // Get the value at index i public long get(int i) { return sum(i, i); } // Add 'v' to index 'i', O(log(n)) public void add(int i, long v) { while (i < N) { tree[i] += v; i += lsb(i); } } // Set index i to be equal to v, O(log(n)) public void set(int i, long v) { add(i, v - sum(i, i)); } @Override public String toString() { return java.util.Arrays.toString(tree); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int[] readArrayInt(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()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
82e62ba5bc323b1a61d3ebc89ca294fd
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; public class Solution{ public 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 swap(int i, int j) { int temp = i; i = j; j = temp; } static class Pair{ int s; int end; public Pair(int a, int e) { end = e; s = a; } } 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(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } public static void main(String[] args) throws Exception { FastReader scn = new FastReader(); OutputStream outputStream = System.out; OutputWriter output = new OutputWriter(outputStream); int t = scn.nextInt(); long mod = 1000000007L; // String[] pal = {"0000", "0110","0220","0330","0440","0550","1001","1111","1221","1331","1441","1551","2002","2112","2222","2332"}; while(t>0) { // int n = scn.nextInt(); // long max =0, min= Integer.MAX_VALUE; // long[] a = new long[n]; // for(int i=0;i<n;i++) { // a[i]= scn.nextLong(); // max = Math.max(max,a[i]); // min = Math.min(min, a[i]); // } int n = scn.nextInt(); int m = scn.nextInt(); char[][] a = new char[n][m]; for(int i=0;i<n;i++) { String s = scn.next(); for(int j=0;j<m;j++) { a[i][j] = s.charAt(j); } } boolean b = true; boolean[][] mark = new boolean[n][m]; for(int i=0;i<n-1;i++) { boolean f = false; for(int j=0;j<m-1;j++) { if(checkL(a,i,j, mark)==1) { f = true; } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]=='*') { if(!mark[i][j]) { b=false; } } // output.print(mark[i][j]+" "); } //output.println(); } if(b==true) { output.println("YES"); }else { output.println("NO"); } t--; } output.close(); } public static int checkL(char[][] a, int r, int c, boolean[][] mark) { int cnt=0; for(int i=Math.max(r-1,0);i<=Math.min(r+2,a.length-1);i++) { for(int j= Math.max(c-1,0);j<=Math.min(c+2,a[0].length-1);j++) { if(a[i][j]=='*') { cnt++; } } } char ch11 = a[r][c]; char ch12 = a[r][c+1]; char ch21 = a[r+1][c]; char ch22 = a[r+1][c+1]; int n = a.length; int m = a[0].length; if(cnt==4) { if(ch22!='*' && r+2<n && c+2<m && a[r+2][c+2]=='*') { cnt--; }else if(ch21!='*' && r+2<n && c-1>=0 && a[r+2][c-1]=='*') { cnt--; }else if(ch11!='*' && r-1>=0 && c-1>=0 && a[r-1][c-1]=='*') { cnt--; }else if(ch12!='*' && r-1>=0 && c+2<m && a[r-1][c+2]=='*') { cnt--; } } if(cnt==0) { return 1; } if(cnt>3) { return 4; } if(cnt<3) { return 2; } if((ch11=='*' && ch12=='*' && ch22=='*' && ch21!='*')|| (ch11=='*' && ch21=='*' && ch22=='*' && ch12!='*') || (ch12=='*'&&ch22=='*'&&ch21=='*'&&ch11!='*')||(ch11=='*'&&ch12=='*'&&ch21=='*'&&ch22!='*')) { for(int i=r;i<r+2;i++) { for(int j=c;j<c+2;j++) { if(a[i][j]=='*') { mark[i][j] = true; } } } return 1; } return 1; } public static void addTime(int h, int m, int ah, int am) { int rm = m+am; int rh = (h+ah+(rm/60))%24; rm = rm%60; } public static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
10d72dcd76953ac86f042ee248fc9285
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { static FastReader scanner = new FastReader(); static int[] dx = new int[]{-1, -1, -1, 0, 0, 1, 1, 1}; static int[] dy = new int[]{-1, 0, 1, -1, 1, -1, 0, 1}; static int[] topLeft = new int[]{0, 1, 2, 3, 5}; static int[] topRight = new int[]{0, 1, 2, 4, 7}; static int[] bottomLeft = new int[]{0, 3, 5, 6, 7}; static int[] bottomRight = new int[]{2, 4, 5, 6, 7}; private static void solve() { int n = scanner.nextInt(); int m = scanner.nextInt(); char[][] a = new char[n][m]; int countStar = 0; for (int i = 0; i < n; i++) { String s = scanner.nextLine(); for (int j = 0; j < s.length(); j++) { a[i][j] = s.charAt(j); if (a[i][j] == '*') { countStar++; } } } if (countStar % 3 != 0) { System.out.println("NO"); return; } boolean[][] flag = new boolean[n][m]; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) { int count = 0; if (a[i][j] == '*') { count++; } if (a[i][j + 1] == '*') { count++; } if (a[i + 1][j] == '*') { count++; } if (a[i + 1][j + 1] == '*') { count++; } if (count == 4) { System.out.println("NO"); return; } if (count != 3) { continue; } if (a[i][j] == '*') { if (!check(a, n, m, i, j, topLeft)) { System.out.println("NO"); return; } } if (a[i][j + 1] == '*') { if (!check(a, n, m, i, j + 1, topRight)) { System.out.println("NO"); return; } } if (a[i + 1][j] == '*') { if (!check(a, n, m, i + 1, j, bottomLeft)) { System.out.println("NO"); return; } } if (a[i + 1][j + 1] == '*') { if (!check(a, n, m, i + 1, j + 1, bottomRight)) { System.out.println("NO"); return; } ; } flag[i][j] = true; flag[i][j + 1] = true; flag[i + 1][j] = true; flag[i + 1][j + 1] = true; } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if (a[i][j] == '*' && !flag[i][j]) { System.out.println("NO");return; } } } System.out.println("YES"); } static boolean check(char[][] a, int n, int m, int i, int j, int[] arr) { for (int p : arr) { int newX = i + dx[p]; int newY = j + dy[p]; if (newX >= 0 && newX < n && newY >= 0 && newY < m && a[newX][newY] == '*') { return false; } } return true; } public static void main(String[] args) throws Exception { // int n = scanner.nextInt(); // while (n-- > 0) { // solve(); // } int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { solve(); } // bufferedWriter.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
c176d80b388b88ab73cff04905d951b7
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class L_shapes { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dirff = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int[][] dirss = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; private static final int iMax = Integer.MAX_VALUE, iMin = Integer.MIN_VALUE; private static final long lMax = Long.MAX_VALUE, lMin = Long.MIN_VALUE; private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); int t = 1; t = fs.nextInt(); for (int tt = 1; tt <= t; tt++) { // fw.out.print("Case #" + tt + ": "); solve(); } fw.out.close(); } private static void solve() { int n = fs.nextInt(), m = fs.nextInt(); char[][] grid = new char[n][m]; for (int i = 0; i < n; i++) grid[i] = fs.nextLine().toCharArray(); boolean[][] vis = new boolean[n][m]; int islands = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == '*' && !vis[i][j]) { int cnt = dfs(grid, vis, i, j, n, m); if (cnt != 3) { fw.out.println("NO"); return; } islands++; } } } int elbows = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == '*' && util(i, j, grid, n, m)) elbows++; } } fw.out.println((islands == elbows) ? "YES" : "NO"); } private static boolean util(int i, int j, char[][] grid, int n, int m) { int cnt = 0; int[] rec = new int[4]; int idx = 0; for (int[] dd : dirff) { int rr = i + dd[0], cc = j + dd[1]; if (check(rr, cc, n, m, grid)) { cnt++; rec[idx] = rr; rec[idx + 2] = cc; idx = (idx + 1) % 4; } } return (cnt == 2 && rec[0] != rec[1] && rec[2] != rec[3]); } private static boolean check(int i, int j, int n, int m, char[][] grid) { return i >= 0 && j >= 0 && i != n && j != m && grid[i][j] != '.'; } private static int dfs(char[][] grid, boolean[][] vis, int i, int j, int n, int m) { vis[i][j] = true; int cnt = 1; for (int[] dd : dirss) { int rr = i + dd[0]; int cc = j + dd[1]; if (rr < 0 || cc < 0 || rr == n || cc == m || grid[rr][cc] == '.' || vis[rr][cc]) continue; cnt += (dfs(grid, vis, rr, cc, n, m)); } return cnt; } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } private static class SCC { private final int n; private final List<List<Integer>> adjList; SCC(int _n, List<List<Integer>> _adjList) { n = _n; adjList = _adjList; } private List<List<Integer>> getSCC() { List<List<Integer>> ans = new ArrayList<>(); Stack<Integer> stack = new Stack<>(); boolean[] vis = new boolean[n]; for (int i = 0; i < n; i++) { if (!vis[i]) dfs(i, adjList, vis, stack); } vis = new boolean[n]; List<List<Integer>> rev_adjList = rev_graph(n, adjList); while (!stack.isEmpty()) { int curr = stack.pop(); if (!vis[curr]) { List<Integer> scc_list = new ArrayList<>(); dfs2(curr, rev_adjList, vis, scc_list); ans.add(scc_list); } } return ans; } private void dfs(int curr, List<List<Integer>> adjList, boolean[] vis, Stack<Integer> stack) { vis[curr] = true; for (int x : adjList.get(curr)) { if (!vis[x]) dfs(x, adjList, vis, stack); } stack.add(curr); } private void dfs2(int curr, List<List<Integer>> adjList, boolean[] vis, List<Integer> scc_list) { vis[curr] = true; scc_list.add(curr); for (int x : adjList.get(curr)) { if (!vis[x]) dfs2(x, adjList, vis, scc_list); } } } private static List<List<Integer>> rev_graph(int n, List<List<Integer>> adjList) { List<List<Integer>> ans = new ArrayList<>(); for (int i = 0; i < n; i++) ans.add(new ArrayList<>()); for (int i = 0; i < n; i++) { for (int x : adjList.get(i)) { ans.get(x).add(i); } } return ans; } private static class Calc_nCr { private final long[] fact; private final long[] invfact; private final int p; Calc_nCr(int n, int prime) { fact = new long[n + 5]; invfact = new long[n + 5]; p = prime; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = (i * fact[i - 1]) % p; } invfact[n] = pow_with_mod(fact[n], p - 2, p); for (int i = n - 1; i >= 0; i--) { invfact[i] = (invfact[i + 1] * (i + 1)) % p; } } private long nCr(int n, int r) { if (r > n || n < 0 || r < 0) return 0; return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p; } } private static int random_between_two_numbers(int l, int r) { Random ran = new Random(); return ran.nextInt(r - l) + l; } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a); } a = (a * a); b >>= 1; } return result; } private static long pow_with_mod(long a, long b, int mod) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) % mod; } a = (a * a) % mod; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static int noOfSetBits(long x) { int cnt = 0; while (x != 0) { x = x & (x - 1); cnt++; } return cnt; } private static int sumOfDigits(long num) { int cnt = 0; while (num > 0) { cnt += (num % 10); num /= 10; } return cnt; } private static int noOfDigits(long num) { int cnt = 0; while (num > 0) { cnt++; num /= 10; } return cnt; } private static boolean isPerfectSquare(long num) { long sqrt = (long) Math.sqrt(num); return ((sqrt * sqrt) == num); } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static void randomizeIntArr(int[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInIntArr(arr, i, j); } } private static void randomizeLongArr(long[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInLongArr(arr, i, j); } } private static void swapInIntArr(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static void swapInLongArr(long[] arr, int a, int b) { long temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextLong(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static List<Long> readLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextLong()); return list; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
ff6327e1e70df5e4e219aa6df122847b
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.sort; public class Codeforces { public static void main(String[] args) { FastReader fastReader = new FastReader(); PrintWriter out = new PrintWriter(System.out); // int tt = 1; int tt = fastReader.nextInt(); while(tt -- > 0){ int n = fastReader.nextInt(); int m = fastReader.nextInt(); char c[][] = new char[n][m]; for(int i = 0 ; i < n; i++){ c[i] =fastReader.nextLine().toCharArray(); } boolean ans = true; boolean [][] vis = new boolean[n][m]; outer:for(int i = 0; i < n; i++){ for(int j = 0 ; j < m; j++){ if(c[i][j] != '*') continue; if((i-1>=0 && j+1 < m && c[i-1][j] == '*' && c[i][j+1] == '*') || (i-1>=0 && j-1 >= 0 && c[i-1][j] == '*' && c[i][j-1] == '*') || (i+1< n && j+1 < m && c[i+1][j] == '*' && c[i][j+1] == '*') || (i+1 < n && j-1 >= 0 && c[i+1][j] == '*' && c[i][j-1] == '*')){ // out.println(i + " " + j); vis[i][j] = true; if((i-1>=0 && j+1 < m && c[i-1][j] == '*' && c[i][j+1] == '*')){ vis[i-1][j] = true; vis[i][j+1] = true; Pair p [] = new Pair[3]; p[0] = new Pair(i-1,j); p[1] = new Pair(i,j); p[2] = new Pair(i,j+1); ans&=valid(p,c,vis); } if(i-1>=0 && j-1 >= 0 && c[i-1][j] == '*' && c[i][j-1] == '*'){ vis[i-1][j] = true; vis[i][j-1] = true; Pair p [] = new Pair[3]; p[0] = new Pair(i-1,j); p[1] = new Pair(i,j); p[2] = new Pair(i,j-1); ans&=valid(p,c,vis); } if((i+1< n && j+1 < m && c[i+1][j] == '*' && c[i][j+1] == '*')){ vis[i][j+1] = true; vis[i+1][j] = true; Pair p [] = new Pair[3]; p[0] = new Pair(i+1,j); p[1] = new Pair(i,j); p[2] = new Pair(i,j+1); ans&=valid(p,c,vis); } if((i+1< n && j-1 >= 0 && c[i+1][j] == '*' && c[i][j-1] == '*')){ vis[i][j-1] = true; vis[i+1][j] = true; Pair p [] = new Pair[3]; p[0] = new Pair(i+1,j); p[1] = new Pair(i,j); p[2] = new Pair(i,j-1); ans&=valid(p,c,vis); } } if(!ans){ // out.println(i + " " + j); break outer; } } } // out.println(ans); if(!ans){ out.println("NO"); }else{ outer: for(int i = 0 ; i < n; i++){ for(int j = 0 ; j < m ; j++){ if(c[i][j] == '*' && !vis[i][j]){ ans = false; break outer; } } } out.println(ans ?"YES" : "NO"); } } out.close(); } public static boolean valid(Pair p [] , char c[][] , boolean vis[][]){ int n = c.length; int m = c[0].length; int[][] d = new int[][]{{-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0, -1}}; for(int i = 0; i < 3 ; i++){ int x = p[i].x; int y = p[i].y; for(int j = 0; j < 8 ; j++){ int curx = x + d[j][0]; int cury = y + d[j][1]; if(curx >= 0 && curx < n && cury >= 0 && cury < m){ if(c[curx][cury] == '*' && !vis[curx][cury]){ return false; } } } } return true; } static class Pair{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[] { 1, 0 }; int[] y = exgcd(b, a % b); return new int[] { y[1], y[0] - y[1] * (a / b) }; } static long[] exgcd(long a, long b) { if (b == 0) return new long[] { 1, 0 }; long[] y = exgcd(b, a % b); return new long[] { y[1], y[0] - y[1] * (a / b) }; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() { return Long.parseLong(next()); } long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
7761a63c5d6488d4206d9da978f7b2c5
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
///WizardAP - 当你休息的时候,很多人比你付出更多的努力!不放弃 ! /// Time : 2022-10-12, Wed, 16:54 import java.io.*; import java.util.*; import static java.lang.Double.parseDouble; import static java.lang.System.in; import static java.lang.System.out; public class Main { static final int MOD = (int) 1e9 + 7; public static boolean check(int x,int y,int n,int m) { return x>=0 &&x<n && y>=0 && y<m; } public static void main(String[] args) throws Exception { FastIO fs = new FastIO(); //Scanner fs = new Scanner(System.in); int t=fs.nextInt(); final int dx[] = {-1,0,1,0}; final int dy[] ={0,1,0,-1}; while(t-->0) { int n = fs.nextInt(); int m = fs.nextInt(); char[][] a = new char[n][m]; for (int i = 0; i < n; i++) { String s = fs.next(); for (int j = 0; j < m; j++) { a[i][j] = s.charAt(j); } } int[][] d = new int[n][m]; int cnt =0; boolean ok =true; for (int i = 0; i < n && ok; i++) for (int j = 0; j <m && ok; j++) if (a[i][j] == '*') { // System.out.println(i + " " +j); for (int xx = -1; xx <= 1 &&ok; xx++) for (int yy = -1; yy <= 1 && ok; yy++) if (xx != 0 || yy != 0) { int u = i + xx; int v = j + yy; if (check(u,v,n,m)==false) continue; if (a[u][v] == '*' && d[i][j]!=d[u][v] && d[u][v] != 0) { ok = false; break; } } if (d[i][j] !=0) continue; // if (i==1 && j==0) System.out.println(ok); if (ok==false) break; boolean found=false; int x =i,y=j+1; int p = i+1,q=j+1; if (check(x,y,n,m) && check(p,q,n,m) ) { if (a[x][y] == '*' && a[p][q] =='*') { if (d[x][y] ==0 && d[p][q] == 0) { if (found) { ok = false; // System.out.println(1); break; } cnt++; d[i][j] =cnt; d[x][y] = cnt; d[p][q] =cnt; found=true; } } } // System.out.println(i + " " +j); x=i+1; y=j; p=i+1;q=j+1; if (check(x,y,n,m) && check(p,q,n,m) ) { if (a[x][y] == '*' && a[p][q] =='*') { if (d[x][y] ==0 && d[p][q] == 0) { if (found) { ok = false; // System.out.println(2); break; } cnt++; d[i][j] =cnt; d[x][y] = cnt; d[p][q] =cnt; found=true; } } } x=i+1;y=j; p=i+1;q=j-1; if (check(x,y,n,m) && check(p,q,n,m) ) { if (a[x][y] == '*' && a[p][q] =='*') { if (d[x][y] ==0 && d[p][q] == 0) { if (found) { ok = false; // System.out.println(3); break; } cnt++; d[i][j] =cnt; d[x][y] = cnt; d[p][q] =cnt; found=true; } } } x=i+1;y=j; p=i;q=j+1; if (check(x,y,n,m) && check(p,q,n,m) ) { if (a[x][y] == '*' && a[p][q] =='*') { if (d[x][y] ==0 && d[p][q] == 0) { if (found) { ok = false; // System.out.println(3); break; } cnt++; d[i][j] =cnt; d[x][y] = cnt; d[p][q] =cnt; found=true; } } }if (!found) ok=false; } // for (int i =0 ;i<n;i++,System.out.println()) // for (int j =0 ;j<m;j++) // System.out.print(d[i][j]); System.out.println(ok ? "YES" : "NO" ); } fs.close(); } //BeginCodeSnip{FastIO} static class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; // standard input public FastIO() { this(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++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break 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() { // nextLong() would be implemented similarly 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() { // nextLong() would be implemented similarly int c; do { c = nextByte(); } while (c <= ' '); int 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 parseDouble(next()); } } //EndCodeSnip }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
c8c5845eead150d46b29df32aea57fcc
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.lang.Math; import java.util.*; import java.util.regex.Pattern; import javax.swing.text.DefaultStyledDocument.ElementSpec; public final class Solution { private static class FastScanner { private final int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private FastScanner(boolean usingFile) throws IOException { if (usingFile) din = new DataInputStream(new FileInputStream("path")); else din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private short nextShort() throws IOException { short ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = (short) (ret * 10 + c - '0'); while ( (c = read()) >= '0' && c <= '9' ); if (neg) return (short) -ret; return ret; } private 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; } private char nextChar() throws IOException { byte c = read(); while (c <= ' ') c = read(); return (char) c; } private String nextString() throws IOException { StringBuilder ret = new StringBuilder(); byte c = read(); while (c <= ' ') c = read(); do { ret.append((char) c); } while ((c = read()) > ' '); return ret.toString(); } 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++]; } } static StringTokenizer st; static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(System.out) ); static int mod = 1000000007; static String is(int no) { return Integer.toString(no); } static String ls(long no) { return Long.toString(no); } static int pi(String s) { return Integer.parseInt(s); } static long pl(String s) { return Long.parseLong(s); } /*write your constructor and global variables here*/ static class Rec<f, s, t> { f a; s b; t c; Rec(f a, s b, t c) { this.a = a; this.b = b; this.c = c; } } static class Quad<f, s, t, fo> { f a; s b; t c; fo d; Quad(f a, s b, t c, fo d) { this.a = a; this.b = b; this.c = c; this.d = d; } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } } static int findPow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = modMul.mod(res, a, mod); } a = modMul.mod(a, a, mod); b = b / 2; } return res; } interface modOperations { int mod(int a, int b, int mod); } static modOperations modAdd = (int a, int b, int mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (int a, int b, int mod) -> { return ((a % mod - b % mod + mod) % mod); }; static modOperations modMul = (int a, int b, int mod) -> { return (int) ((1l * a % mod * b % mod)) % mod; }; static modOperations modDiv = (int a, int b, int mod) -> { return modMul.mod(a, findPow(b, mod - 2, mod), mod); }; static int gcd(int a, int b) { int div = b; int rem = a % b; while (rem != 0) { int temp = rem; rem = div % rem; div = temp; } return div; } static HashSet<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; HashSet<Integer> list = new HashSet<>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { list.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { list.add(i); } } return list; } static int[] factorialList(int MAXI, int mod) { int[] factorial = new int[MAXI + 1]; factorial[0] = 1; for (int i = 1; i < MAXI + 1; i++) { factorial[i] = modMul.mod(factorial[i - 1], i, mod); } return factorial; } static void put(HashMap<Integer, Integer> cnt, int key, int val) { if (cnt.containsKey(key)) { cnt.replace(key, cnt.get(key) + val); } else { cnt.put(key, val); } } static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getKey()); } return vals; } static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getValue()); } return vals; } static int getMax(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) > max) { max = arr.get(i); } } return max; } static int getMin(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) < max) { max = arr.get(i); } } return max; } static int[][] fill_arr(int m, int n, int fill) { int arr[][] = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr[i][j] = fill; } } return arr; } static class sortCond implements Comparator<Pair<Integer, Long>> { @Override public int compare(Pair<Integer, Long> p1, Pair<Integer, Long> p2) { if (p1.b >= p2.b) { return 1; } else { return -1; } } } static class sortCondRec implements Comparator<Rec<Integer, Integer, Integer>> { @Override public int compare( Rec<Integer, Integer, Integer> p1, Rec<Integer, Integer, Integer> p2 ) { return p1.b - p2.b; } } /*write your methods and classes here*/ static int cor[][] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 }, { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 }, }; static boolean check_L( char arr[][], boolean is_par[][], int x1, int y1, int x2, int y2, int x3, int y3 ) { int n = arr.length; int m = arr[0].length; if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= m) return false; if (x2 < 0 || x2 >= n || y2 < 0 || y2 >= m) { return false; } if (x3 < 0 || x3 >= n || y3 < 0 || y3 >= m) { return false; } if (arr[x1][y1] == '*' && arr[x2][y2] == '*' && arr[x3][y3] == '*') { return true; } return false; } static void change( char arr[][], boolean is_par[][], int x1, int y1, int x2, int y2, int x3, int y3 ) { int n = arr.length; int m = arr[0].length; if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= m) return; if (x2 < 0 || x2 >= n || y2 < 0 || y2 >= m) { return; } if (x3 < 0 || x3 >= n || y3 < 0 || y3 >= m) { return; } if (arr[x1][y1] == '*' && arr[x2][y2] == '*' && arr[x3][y3] == '*') { is_par[x1][y1] = true; is_par[x2][y2] = true; is_par[x3][y3] = true; } } static boolean check( char arr[][], boolean is_par[][], boolean vis[][], int x1, int y1, int x2, int y2, int x3, int y3 ) { int n = arr.length; int m = arr[0].length; if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= m) return true; if (x2 < 0 || x2 >= n || y2 < 0 || y2 >= m) { return true; } if (x3 < 0 || x3 >= n || y3 < 0 || y3 >= m) { return true; } if (arr[x1][y1] != '*' || arr[x2][y2] != '*' || arr[x3][y3] != '*') { return true; } int rec[][] = { { x1, y1 }, { x2, y2 }, { x3, y3 } }; //System.out.println("x : " + x1 + " " + "y: " + y1); for (int i = 0; i < 3; i++) { //System.out.println("rec_x: " + rec[i][0] + " " + "rec_y: " + rec[i][1]); for (int j = 0; j < 8; j++) { int dx = rec[i][0] + cor[j][0]; int dy = rec[i][1] + cor[j][1]; if (dx < 0 || dx >= n || dy < 0 || dy >= m) { continue; } boolean is = false; for (int k = 0; k < 3; k++) { if (dx == rec[k][0] && dy == rec[k][1]) { is = true; } } if (is) { continue; } if (is_par[dx][dy]) { return false; } } } return true; } public static void main(String[] args) throws IOException { FastScanner input = new FastScanner(false); PrintWriter out = new PrintWriter(System.out); int cases = input.nextInt(), n, m, j, i; while (cases-- != 0) { n = input.nextInt(); m = input.nextInt(); char arr[][] = new char[n][m]; for (i = 0; i < n; i++) { arr[i] = input.nextString().toCharArray(); } boolean is_par[][] = new boolean[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (arr[i][j] == '*') { change(arr, is_par, i, j, i - 1, j, i - 1, j - 1); change(arr, is_par, i, j, i - 1, j, i - 1, j + 1); change(arr, is_par, i, j, i, j - 1, i - 1, j - 1); change(arr, is_par, i, j, i, j + 1, i - 1, j + 1); } } } /*for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { out.print(is_par[i][j] + " "); } out.println(); }*/ boolean ok = true; boolean vis[][] = new boolean[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (arr[i][j] == '*' && is_par[i][j] == false) { ok = false; } else if (arr[i][j] == '*') { int cnt = 0; cnt = check(arr, is_par, vis, i, j, i - 1, j, i - 1, j - 1) ? cnt + 1 : 0; cnt = check(arr, is_par, vis, i, j, i - 1, j, i - 1, j + 1) ? cnt + 1 : cnt; cnt = check(arr, is_par, vis, i, j, i, j - 1, i - 1, j - 1) ? cnt + 1 : cnt; cnt = check(arr, is_par, vis, i, j, i, j + 1, i - 1, j + 1) ? cnt + 1 : cnt; if (cnt != 4) { ok = false; } //System.out.println(i + " " + j + " " + cnt); } } } if (ok) { out.println("YES"); } else { out.println("NO"); } } out.flush(); out.close(); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
386f85914c1bf0e1f35738e1ffc76112
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main{ static int n = 0,m = 0; static boolean [][]v = new boolean [55][55]; static char[][]g = new char[55][55]; static StreamTokenizer st ; static BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); st = new StreamTokenizer(re); int t = sc.nextInt(); while(t-->0) { n = sc.nextInt();m = sc.nextInt(); for (int i = 0 ;i < 55 ; i++) Arrays.fill(g[i], '.'); for (int i = 1 ; i <= n ; i++) { char[] s = sc.next().toCharArray(); for (int j = 1 ; j <= m ; j++) g[i][j] = s[j-1]; } for (int i = 1 ; i< n ; i++) { for (int j = 1 ; j <= m ; j++) { if(!v[i][j] && g[i][j] == '*') { if(c1(i,j)) continue; if(c2(i,j)) continue; if(c3(i,j)) continue; if(c4(i,j)) continue; } } } boolean f = false; p:for (int i = 1 ; i <= n ; i++) for (int j = 1 ; j <= m ; j++) { if(g[i][j] == '*' && !v[i][j]) { f = true; break p; } } if(f) pw.println("NO"); else pw.println("YES"); for (int i = 0 ; i < 55 ; i++) Arrays.fill(v[i], false); } pw.flush(); } static boolean c1(int i,int j) { /* * * * ** * * * */ boolean f = false; int x[] = {-1,-1,-1,0,0,0,1,1,2,2,2,2}; int y[] = {-1,0,1,-1,1,2,-1,2,-1,0,1,2}; if(g[i+1][j] == '*' && g[i+1][j+1] == '*' && !v[i+1][j] && !v[i+1][j+1]) { for (int k = 0 ; k < 12 ; k++) { int xx = i+x[k]; int yy = j+y[k]; if(g[xx][yy] != '.') return false; } v[i+1][j] = true;v[i][j] = true;v[i+1][j+1] = true; return true; } return f; } static boolean c2(int i,int j) { /* * * * ** * * * */ boolean f = false; int x[] = {-1,-1,-1,0,0,0,1,1,2,2,2,2}; int y[] = {-1,0,1,-1,1,-2,1,-2,-1,0,1,-2}; if(g[i+1][j] == '*' && g[i+1][j-1] == '*' && !v[i+1][j] && !v[i+1][j-1]) { for (int k = 0 ; k < 12 ; k++) { int xx = i+x[k]; int yy = j+y[k]; if(g[xx][yy] != '.') return false; } v[i+1][j] = true;v[i][j] = true; v[i+1][j-1] = true; return true; } return f; } static boolean c3(int i,int j) { /* * ** * * * * * */ boolean f = false; int x[] = {2,2,2,0,0,1,1,1,-1,-1,-1,-1}; int y[] = {-1,0,1,-1,2,1,-1,2,-1,0,1,2}; if(g[i][j+1] == '*' && g[i+1][j] == '*' && !v[i][j+1] && !v[i+1][j]) { for (int k = 0 ; k < 12 ; k++) { int xx = i+x[k]; int yy = j+y[k]; if(g[xx][yy] != '.') return false; } v[i][j+1] =true; v[i+1][j] = true;v[i][j] = true; return true; } return f; } static boolean c4(int i,int j) { /* * ** * * * * * */ boolean f = false; int x[] = {-1,-1,-1,-1,0,0,1,1,1,2,2,2}; int y[] = {-1,0,1,2,-1,2,-1,0,2,0,1,2}; if(g[i][j+1] == '*' && g[i+1][j+1] == '*' && !v[i][j+1] && !v[i+1][j+1]) { for (int k = 0 ; k < 12 ; k++) { int xx = i+x[k]; int yy = j+y[k]; if(g[xx][yy] != '.') return false; } v[i][j+1] =true; v[i+1][j+1]=true;v[i][j] = true; return true; } return f; } static int I() throws IOException { st.nextToken(); return (int)st.nval; } static long L() throws IOException { st.nextToken(); return (long)st.nval; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
528b002babff08a93a8c3e74cbf32655
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class F { public static void main(String[] args)throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(bf.readLine()); while (t --> 0) { String[] line0=bf.readLine().split(" "); int n=Integer.parseInt(line0[0]); int m=Integer.parseInt(line0[1]); char[][] g=new char[n+2][m+2]; for (int i=0;i<n+2;i++) for (int j=0;j<m+2;j++) g[i][j]='.'; for (int i=0;i<n;i++) { char[] r=bf.readLine().toCharArray(); for (int j=0;j<m;j++) g[i+1][j+1]=r[j]; } int[][] comp=new int[n+2][m+2]; for (int i=0;i<n+2;i++) for (int j=0;j<m+2;j++) comp[i][j]=-1; int cComp=0; for (int i=1;i<n+1;i++) { for (int j=1;j<m+1;j++) { if (g[i][j]=='*' && comp[i][j]==-1) dfs(comp, g, i, j, cComp++); } } int[] cnt=new int[cComp]; for (int i=0;i<n+2;i++) { for (int j=0;j<m+2;j++) { if (comp[i][j]==-1) continue; cnt[comp[i][j]]++; } } boolean touches=false; for (int i=0;i<cComp;i++) if (cnt[i]!=3) touches=true; if (touches) { System.out.println("NO"); continue; } boolean[] works=new boolean[cComp]; for (int i=0;i<n+1;i++) { for (int j=0;j<m+1;j++) { int num=0; int c=0; if (g[i][j]=='*') { num++; c=comp[i][j]; } if (g[i+1][j]=='*') { num++; c=comp[i+1][j]; } if (g[i][j+1]=='*') { num++; c=comp[i][j+1]; } if (g[i+1][j+1]=='*') { num++; c=comp[i+1][j+1]; } if (num==3) works[c]=true; } } boolean good=true; for (boolean b : works) good=good&&b; System.out.println(good ? "YES":"NO"); } } static void dfs(int[][] comp, char[][] g, int i, int j, int cComp) { if (comp[i][j]!=-1) return; comp[i][j]=cComp; for (int iN=i-1;iN<=i+1;iN++) { for (int jN=j-1;jN<=j+1;jN++) { if (g[iN][jN]!='*') continue; dfs(comp, g, iN, jN, cComp); } } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
cb63e4924a492e84f977d990b5b98385
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class lshapes { static char[][] arr; static boolean[][] visited; static int n; static int m; static boolean valid; static final int R_CHANGE[] = {0, 1, 0, -1}; static final int C_CHANGE[] = {1, 0, -1, 0}; static final int R_CHANGE2[] = {-1, 1, -1, 1}; static final int C_CHANGE2[] = {-1, -1, 1, 1}; public static void main(String[] args) throws IOException { Reader in = new Reader(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); For: for(int z = 0; z < t; z++) { n = in.nextInt(); m = in.nextInt(); arr = new char[n][m]; visited = new boolean[n][m]; valid = true; for(boolean[] row : visited) { Arrays.fill(row, false); } for(int i = 0; i < n; i++) { String line = in.readLine(); for(int j = 0; j < m; j++) { arr[i][j] = line.charAt(j); } } // out.println(Arrays.deepToString(arr)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(arr[i][j] == '*' && !visited[i][j]) { floodfill(i, j); if(!valid) { out.println("NO"); continue For; } } } } // floodfill(1, 2); out.println("YES"); } in.close(); out.close(); } static void floodfill(int row, int col) { int num = 0; HashSet<Pos> positionSet = new HashSet<>(); Stack<Pos> stack = new Stack<>(); stack.push(new Pos(row, col)); while(!stack.isEmpty()) { Pos curr = stack.pop(); row = curr.row; col = curr.col; if(row < 0 || row >= n || col < 0 || col >= m || visited[row][col] || arr[row][col] == '.') { continue; } visited[row][col] = true; if(num < 3) { positionSet.add(new Pos(row, col)); } num++; for(int i = 0; i < 4; i++) { int rowChange = row + R_CHANGE[i]; int colChange = col + C_CHANGE[i]; stack.add(new Pos(rowChange, colChange)); } } if(num != 3) { valid = false; return; } // for(Pos position : positionSet) { // System.out.println(position.row + " " + position.col); // } int minX = Integer.MAX_VALUE, maxX = 0, minY = Integer.MAX_VALUE, maxY = 0; for(Pos position : positionSet) { for(int i = 0; i < 4; i++) { int rowChange = position.row + R_CHANGE2[i]; int colChange = position.col + C_CHANGE2[i]; if(rowChange < 0 || rowChange >= n || colChange < 0 || colChange >= m || positionSet.contains(new Pos(rowChange, colChange))) { continue; } if(arr[rowChange][colChange] == '*') { valid = false; return; } } minX = Math.min(minX, position.row); maxX = Math.max(maxX, position.row); minY = Math.min(minY, position.col); maxY = Math.max(maxY, position.col); } if(maxX - minX != 1 || maxY - minY != 1) { valid = false; return; } // System.out.println(num); } static class Pos { int row, col; Pos(int row, int col) { this.row = row; this.col = col; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null) return false; if(getClass() != obj.getClass()) return false; Pos other = (Pos) obj; if(row != other.row) return false; if(col != other.col) return false; return true; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + row; result = prime * result + col; return result; } } 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]; int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
8bacc9514de420f70dc01916ea4f7ba2
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class Lshapes { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int N = Integer.parseInt(br.readLine()); out: while (N-- > 0) { StringTokenizer token = new StringTokenizer(br.readLine()); int n = Integer.parseInt(token.nextToken()); int m = Integer.parseInt(token.nextToken()); char[][] grid = new char[n][]; boolean[][] v = new boolean[n][m]; for (int i = 0; i < n; i++) { grid[i] = br.readLine().toCharArray(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == '.' || v[i][j]) continue; if (!fill(grid, v, i, j, new Group()).isValid()) { pw.println("NO"); continue out; } } } pw.println("YES"); } pw.close(); br.close(); } static int[] dx = {1, 1, 1, 0, -1, -1, -1, 0}; static int[] dy = {1, 0, -1, -1, -1, 0, 1, 1}; static private Group fill(char[][] g, boolean[][] v, int x, int y, Group gr) { if (x < 0 || y < 0 || x >= v.length || y >= v[0].length || g[x][y] == '.' || v[x][y]) return gr; v[x][y] = true; gr.add(x, y); for (int i = 0; i < 8; i++) { fill(g, v, x+dx[i], y+dy[i], gr); } return gr; } } class Group { int size; int[] rows; int[] cols; public Group() { size = 0; rows = new int[4]; cols = new int[4]; } public boolean isValid() { return size == 3 && (rows[0] != rows[1] || rows[1] != rows[2]) && (rows[0] == rows[1] || rows[0] == rows[2] || rows[1] == rows[2]) && (cols[0] != cols[1] || cols[1] != cols[2]) && (cols[1] == cols[2] || cols[0] == cols[1] || cols[0] == cols[2]); } public void add(int a, int b) { if (size == 4) return; rows[size] = a; cols[size] = b; size++; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
45ffb1395eba788385bcfe98088ebfe3
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
//some updates in import stuff 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.*; //key points learned //max space ever that could be alloted in a program to pass in cf //int[][] prefixSum = new int[201][200_005]; -> not a single array more!!! //never allocate memory again again to such bigg array, it will give memory exceeded for sure //believe in your fucking solution and keep improving it!!! (sometimes) ///common mistakes // didn't read the question properly public class Main{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List<Integer> primeNumbers = new ArrayList<>(); public static void main(String[] args) { MyScanner sc = new MyScanner(); //pretty important for sure - out = new PrintWriter(new BufferedOutputStream(System.out)); //dope shit output for sure //code here int test = sc.nextInt(); while(test --> 0){ int n = sc.nextInt(); int m = sc.nextInt(); char[][] inp = new char[n][m]; for(int i = 0; i < n; i++){ inp[i] = sc.nextLine().toCharArray(); } int[][] vis = new int[n][m]; ArrayList<Set<Pair>> shapes = new ArrayList<>(); boolean finalFlag = true; for(int i= 0; i < n; i++){ for(int j = 0; j < m; j++){ boolean[] flag = new boolean[1]; flag[0] = true; if(inp[i][j] == '*' && vis[i][j] != 1){ Set<Pair> shape = new HashSet<>(); addShapes(i, j, vis, shapes, shape, m, n, flag, inp); shapes.add(shape); } if(flag[0] == false) finalFlag = false; } } //now just one more check is going to be placed on this stuff for sure //that is to never give up no matter what boi!! (always striving to be the best you can) //final check has to be implemented for(Set<Pair> cset : shapes){ //think about this for once if(cset.size() != 3) finalFlag = false; Set<Integer> row = new HashSet<>(); Set<Integer> col = new HashSet<>(); for(Pair p: cset){ row.add(p.a); col.add(p.b); } if(row.size() == 1 || col.size() == 1) finalFlag = false; } out.println(finalFlag ? "YES" : "NO"); // // // System.out.println(shapes); // System.out.println(finalFlag); } out.close(); } public static void addShapes(int x, int y, int[][] vis, ArrayList<Set<Pair>> shapes, Set<Pair> cshape, int m, int n, boolean[] flag, char[][] inp){ vis[x][y] = 1; cshape.add(new Pair(x, y)); for(int i = 0; i < 8; i++){ int cx = x + dx8[i]; int cy = y + dy8[i]; if(cx >= n || cx < 0 || cy >= m || cy < 0 || inp[cx][cy] != '*') continue; if(dx8[i] != 0 && dy8[i] != 0){ if(vis[cx][cy] == 1 && !cshape.contains(new Pair(cx, cy))) flag[0] = false; }else{ if(vis[cx][cy] != 1) { addShapes(cx, cy, vis, shapes, cshape, m, n, flag, inp); } } } } //new stuff to learn (whenever this is need for them, then only) //Persistent Segment Trees //Square Root Decomposition //Geometry & Convex Hull //High Level DP -- yk yk //String Matching Algorithms //Heavy light Decomposition //-----CURRENTLY PRESENT-------// //Graph //DSU //powerMODe //power //Segment Tree (work on this one) //Prime Sieve //Count Divisors //Next Permutation //Get NCR //isVowel //Sort (int) //Sort (long) //Binomial Coefficient //Pair //Triplet //lcm (int & long) //gcd (int & long) //gcd (for binomial coefficient) //swap (int & char) //reverse //primeExponentCounts //Fast input and output //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //GRAPH (basic structure) public static class Graph{ public int V; public ArrayList<ArrayList<Integer>> edges; //2 -> [0,1,2] (current) Graph(int V){ this.V = V; edges = new ArrayList<>(V+1); for(int i= 0; i <= V; i++){ edges.add(new ArrayList<>()); } } public void addEdge(int from , int to){ edges.get(from).add(to); edges.get(to).add(from); } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ //union by size if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod public static long powerMOD(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ x %= mod; res %= mod; res = (res * x)%mod; } // y must be even now y = y >> 1; // y = y/2 x%= mod; x = (x * x)%mod; // Change x to x^2 } return res%mod; } //without mod public static long power(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ res = (res * x); } // y must be even now y = y >> 1; // y = y/ x = (x * x); } return res; } public static class segmentTree{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query -> O(logn) //update -> O(logn) //update-range -> O(n) (worst case) //simple iteration and stuff for sure public segmentTree(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long query(int s, int e, int qs , int qe, int index){ //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //no overlap if(qe < s || qs > e){ return Long.MAX_VALUE; } //partial overlap int mid = (s + e)/2; long left = query( s, mid , qs, qe, 2*index); long right = query( mid + 1, e, qs, qe, 2*index + 1); return min(left, right); } //gonna do range updates for sure now!! //let's do this bois!!! (solve this problem for sure) public void updateRange(int s, int e, int l, int r, long increment, int index){ //out of bounds if(l > e || r < s){ return; } //leaf node if(s == e){ tree[index] += increment; return; //behnchoda return tera baap krvayege? } //recursive case int mid = (s + e)/2; updateRange(s, mid, l, r, increment, 2 * index); updateRange(mid + 1, e, l, r, increment, 2 * index + 1); tree[index] = min(tree[2 * index], tree[2 * index + 1]); } } public static class segmentTreeLazy{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; public long[] lazy; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query-range -> O(logn) //lazy update-range -> O(logn) (imp) //simple iteration and stuff for sure public segmentTreeLazy(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; lazy = new long[1000000]; //pretty big for no inconvenience (no?) NONONONOONON! NO fucker NO! } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long queryLazy(int s, int e, int qs, int qe, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > qe || e < qs){ return Long.MAX_VALUE; } //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //partial overlap int mid = (s + e)/2; long left = queryLazy(s, mid, qs, qe, 2 * index); long right = queryLazy(mid + 1, e, qs, qe, 2 * index + 1); return Math.min(left, right); } //update range in O(logn) -- using lazy array public void updateRangeLazy(int s, int e, int l, int r, int inc, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > r || l > e){ return; } //another case if(l <= s && e <= r){ tree[index] += inc; //create a new lazy value for children node if(s != e){ lazy[2*index] += inc; lazy[2*index + 1] += inc; } return; } //recursive case int mid = (s + e)/2; updateRangeLazy(s, mid, l, r, inc, 2*index); updateRangeLazy(mid + 1, e, l, r, inc, 2*index + 1); //update the tree index tree[index] = Math.min(tree[2*index], tree[2*index + 1]); return; } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //primeExponentCounts public static int primeExponentsCount(int n) { if (n <= 1) return 0; int sqrt = (int) Math.sqrt(n); int remainingNumber = n; int result = 0; for (int i = 2; i <= sqrt; i++) { while (remainingNumber % i == 0) { result++; remainingNumber /= i; } } //in case of prime numbers this would happen if (remainingNumber > 1) { result++; } return result; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } //to sort the array with better method public 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); } //sort long public 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); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; public int hashCode; Pair(int a , int b){ this.a = a; this.b = b; this.hashCode = Objects.hash(a, b); } @Override public String toString(){ return a + " -> " + b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair that = (Pair) o; return a == that.a && b == that.b; } @Override public int hashCode() { return this.hashCode; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } public static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second a = (a * a) % mod; b = b >> 1; } return res; } //SOME EXTRA DOPE FUNCTIONS public static long mminvprime(long a, long b) { return expo(a, b - 2, b); } public static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } public static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } public static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } public static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } //O(n) every single time remember that public static long nCr(long N, long K , long mod){ long upper = 1L; long lower = 1L; long lowerr = 1L; for(long i = 1; i <= N; i++){ upper = mod_mul(upper, i, mod); } for(long i = 1; i <= K; i++){ lower = mod_mul(lower, i, mod); } for(long i = 1; i <= (N - K); i++){ lowerr = mod_mul(lowerr, i, mod); } // out.println(upper + " " + lower + " " + lowerr); long answer = mod_mul(lower, lowerr, mod); answer = mod_div(upper, answer, mod); return answer; } // long[] fact = new long[2 * n + 1]; // long[] ifact = new long[2 * n + 1]; // fact[0] = 1; // ifact[0] = 1; // for (long i = 1; i <= 2 * n; i++) // { // fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod); // ifact[(int)i] = mminvprime(fact[(int)i], mod); // } //ifact is basically inverse factorial in here!!!!!(imp) public static long combination(long n, long r, long m, long[] fact, long[] ifact) { long val1 = fact[(int)n]; long val2 = ifact[(int)(n - r)]; long val3 = ifact[(int)r]; return (((val1 * val2) % m) * val3) % m; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------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
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
6c959da72d0f525d50a7ea02820e5082
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = true; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { int n = fReader.nextInt(), m = fReader.nextInt(); DSU dsu = new DSU(n*m); char[][] a = new char[n][m]; for(int i=0;i<n;i++) { String s = fReader.nextString(); for(int j=0;j<m;j++) { a[i][j] = s.charAt(j); } } for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(i+1 < n && a[i][j] == a[i+1][j]) dsu.union(i*m+j, (i+1)*m+j); if(j+1 < m && a[i][j] == a[i][j+1]) dsu.union(i*m+j, i*m+j+1); } boolean ok = true; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(a[i][j] == '*' && dsu.getSize(i*m+j) != 3) ok = false; if(a[i][j] == '*' && i+2 < n && dsu.find((i+2)*m+j) == dsu.find(i*m+j)) ok = false; if(a[i][j] == '*' && j+2 < m && dsu.find(i*m+j+2) == dsu.find(i*m+j)) ok = false; } for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(i+1 < n && j+1 < m && a[i][j] == a[i+1][j+1]) dsu.union(i*m+j, (i+1)*m+j+1); if(i-1 > 0 && j+1 < m && a[i][j] == a[i-1][j+1]) dsu.union(i*m+j, (i-1)*m+j+1); } for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(a[i][j] == '*' && dsu.getSize(i*m+j) != 3) ok = false; } if(ok) o.println("YES"); else o.println("NO"); } catch (Exception e){e.printStackTrace();} } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(int[] a, int val){ int l = 0, r = a.length; while(l < r){ int mid = l + (r - l) / 2; if(a[mid] < val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } 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 / gcd(a,b)*b; } public static boolean isPrime(long x){ boolean ok = true; for(long i=2;i<=Math.sqrt(x);i++){ if(x % i == 0){ ok = false; break; } } return ok; } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static long qpow(long a, long n){ long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a; } n >>= 1; a = a * a; } return ret; } public static class DSU { int[] parent; long[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new long[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(p != parent[p]){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getCount(){ return n; } public long getSize(int x){ return size[find(x)]; } } public static class FenWick { long[] tree; int n; public FenWick(int n){ this.n = n; tree = new long[n+1]; } public void add(int x, int val){ while(x <= n){ tree[x] += val; x += lowBit(x); } } public int query(int x){ int ret = 0; while(x > 0){ ret += tree[x]; x -= lowBit(x); } return ret; } public int lowBit(int x) { return x&-x; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
9b81b756f126c70729e1b1516378233d
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer ST; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00"); final static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE, mod = (int) (1e9 + 7); final static long LMAX = Long.MAX_VALUE, LMIN = Long.MIN_VALUE; final static long INF = (long) 1e18, Neg_INF = (long) -1e18; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { n = readInt(); m = readInt(); arr = readMatrix(n, m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == '*') { if (i + 1 < n && j + 1 < m && arr[i + 1][j] == '*' && arr[i + 1][j + 1] == '*') { for (var d : d1) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i + 1][j] = arr[i + 1][j + 1] = '.'; } else if (i + 1 < n && j + 1 < m && arr[i][j + 1] == '*' && arr[i + 1][j + 1] == '*') { for (var d : d2) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i][j + 1] = arr[i + 1][j + 1] = '.'; } else if (i + 1 < n && j > 0 && arr[i + 1][j - 1] == '*' && arr[i + 1][j] == '*') { for (var d : d3) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i + 1][j - 1] = arr[i + 1][j] = '.'; } else if (i + 1 < n && j + 1 < m && arr[i][j + 1] == '*' && arr[i + 1][j] == '*') { for (var d : d4) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i][j + 1] = arr[i + 1][j] = '.'; } else { no(); return; } } } } yes(); } static int n, m; static char[][] arr; static int[][] d1 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 0, 2 }, { 1, -1 }, { 1, 2 }, { 2, -1 }, { 2, 0 }, { 2, 1 }, { 2, 2 } }; static int[][] d2 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { -1, 2 }, { 0, -1 }, { 0, 2 }, { 1, -1 }, { 1, 0 }, { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } }; static int[][] d3 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -2 }, { 0, -1 }, { 0, 1 }, { 1, -2 }, { 1, 1 }, { 2, -2 }, { 2, -1 }, { 2, 0 }, { 2, 1 } }; static int[][] d4 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { -1, 2 }, { 0, -1 }, { 0, 2 }, { 1, -1 }, { 1, 1 }, { 1, 2 }, { 2, -1 }, { 2, 0 }, { 2, 1 } }; private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java && java CodeForces // change Stack size -> java -Xss16M CodeForces.java // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } BR = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (ST == null || !ST.hasMoreTokens()) ST = new StringTokenizer(readLine()); return ST.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return BR.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray() throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(long a, long b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static long multiply(long a, long b) { return (((a % mod) * (b % mod)) % mod); } private static long divide(long a, long b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } boolean insert(String word) { Node curr = root; boolean ans = true; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; if (curr.isEnd) ans = false; } curr.isEnd = true; return ans; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== FENWICK TREE ================================ static class FT { int n; int[] arr; int[] tree; FT(int[] arr, int n) { this.arr = arr; this.n = n; this.tree = new int[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } FT(int n) { this.n = n; this.tree = new int[n + 1]; } // 1 based indexing void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } // 1 based indexing int query(int l, int r) { return getSum(r) - getSum(l - 1); } int getSum(int idx) { int ans = 0; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
2ab2e707ee14b081851ece5652cc009b
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class F1722 { static int[] dx; static int[] dy; static boolean[][] seen; static boolean[][] arr; static int[][] val; public static void main(String[] args) throws IOException{ StringBuffer ans = new StringBuffer(); StringTokenizer st; BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(; t > 0; t--){ dx = new int[]{-1,0,0,1}; dy = new int[]{0,1,-1,0}; st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); arr = new boolean[n][m]; val = new int[n][m]; for(int i = 0; i < n; i++){ st = new StringTokenizer(f.readLine()); String str = st.nextToken(); for(int x = 0; x < m; x++){ if(str.charAt(x) == '*') arr[i][x] = true; } } int curr = 1; seen = new boolean[n][m]; boolean broken = false; for(int i = 0; i < n; i++){ for(int x = 0; x < m; x++){ if(i != 0 && i < n-1 && arr[i][x] && arr[i-1][x] && arr[i+1][x]) broken = true; if(x != 0 && x < m-1 && arr[i][x] && arr[i][x-1] && arr[i][x+1]) broken = true; if(!seen[i][x] && arr[i][x]){ val[i][x] = curr; if(dfs(i,x,curr) != 3) broken = true; curr++; } } } //System.out.println(broken); dx = new int[]{0,0,1,1,1,-1,-1,-1}; dy = new int[]{1,-1,0,1,-1,-1,0,1}; for(int i = 0; i < n; i++){ for(int x = 0; x < m; x++){ if(!arr[i][x]) continue; for(int j = 0; j < 8; j++){ int ni = dx[j]+i; int nx = dy[j]+x; if(ni < 0 || nx < 0 || ni == n || nx == m) continue; if(val[i][x] < val[ni][nx]) broken = true; } } } //for(int i = 0; i < n; i++) System.out.println(Arrays.toString(val[i])); if(broken) ans.append("NO").append("\n"); else ans.append("YES").append("\n"); } System.out.println(ans); f.close(); } public static int dfs(int i, int x, int curr){ seen[i][x] = true; int hmany = 1; for(int p = 0; p < 4; p++){ int ji = i+dx[p]; int jy = x+dy[p]; if(ji >= 0 && jy >= 0 && ji < arr.length && jy < arr[0].length && !seen[ji][jy] && arr[ji][jy]){ val[ji][jy] = curr; hmany+=dfs(ji,jy,curr); } } return hmany; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
ba12fbe109dab52e00d0f92f6afd7be6
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
/* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' */ import java.util.*; import java.math.*; import java.io.*; import java.lang.Math.*; public class KickStart2020 { 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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } 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; } public static class Pair implements Comparable<Pair> { public int index; public long value; public Pair(int index, long value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { // multiplied to -1 as the author need descending sort order if(other.index < this.index) return 1; if(other.index > this.index) return -1; return 0; } @Override public String toString() { return this.index + " " + this.value; } } static int isPrime(long d) { if (d == 1) return -1; for (int i = 2; i <= (long) Math.sqrt(d); i++) { if (d % i == 0) return i; } return -1; } static boolean isPali(String n) { String s = n; int l = 0; int r = s.length() - 1; while(l < r) if(s.charAt(l++) != s.charAt(r--)) return false; return true; } static void decimalTob(long n, int k , int arr[], int i) { arr[i] += (n % k); n /= k; if(n > 0) { decimalTob(n, k, arr, i + 1); } } static long powermod(long x, long y, long mod) { if(y == 0) return 1; long value = powermod(x, y / 2, mod); if(y % 2 == 0) return (value * value) % mod; return (value * (value * x) % mod) % mod; } static long power(long x, long y) { if(y == 0) return 1; long value = power(x, y / 2); if(y % 2 == 0) return (value * value); return value * value * x; } static int bS(int l, int r, int find, Integer arr[]) { int ans = -1; while(l <= r) { int mid = (l + r) / 2; if(arr[mid] >= find) { ans = mid; r = mid - 1; } else l = mid + 1; } return ans; } static void build(int index, int l, int r, int seqtree[],int arr[]) { if(l == r) { seqtree[index] = arr[l]; return; } int mid = (l + r) / 2; build(2 * index + 1, l, mid, seqtree, arr); build(2 * index + 2, mid + 1, r, seqtree, arr); seqtree[index] = Math.max(seqtree[2 * index + 1], seqtree[2 * index + 2]); } static int query(int index, int low, int high, int l, int r, int seqtree[]) { if(high < l || low > r) return Integer.MIN_VALUE; if(l <= low && r >= high) { return seqtree[index]; } int mid = (low + high) / 2; int left = query(2 * index + 1, low , mid, l, r, seqtree); int right = query(2 * index + 2, mid + 1, high, l, r, seqtree); return Math.max(left, right); } static int dfs(int i, int j, char arr[][], boolean hachu[][]) { if(i < 0 || j < 0 || j >= arr[0].length || i >= arr.length) return 0; if(arr[i][j] == '.') return 0; else if(hachu[i][j]) return 0; hachu[i][j] = true; int x = 1; for(int k = -1; k <= 1; k++) { for(int l = -1; l <= 1; l++) { x += dfs(i + k, j + l, arr, hachu); } } return x; } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); outerloop: while(t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); char arr[][] = new char[n][m]; for(int i = 0; i < n; i++) { arr[i] = sc.next().toCharArray(); } boolean hachu[][] = new boolean[n][m]; int count = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(arr[i][j] == '*' && !hachu[i][j]) { int x = dfs(i, j, arr, hachu); if(x != 3) { out.println("NO"); continue outerloop; } count++; } } } int ans = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m ;j ++) { if(arr[i][j] == '*') { if(i - 1 >= 0 && j - 1 >= 0 && arr[i - 1][j] == '*' && arr[i][j - 1] == '*') { ans++; } else if(i - 1 >= 0 && j + 1 < m && arr[i - 1][j] == '*' && arr[i][j + 1] == '*') ans++; else if(i + 1 < n && j - 1 >= 0 && arr[i + 1][j] == '*' && arr[i][j - 1] == '*') ans++; else if(i + 1 < n && j + 1 < m && arr[i + 1][j] == '*' && arr[i][j + 1] == '*') ans++; } } } // out.println(ans + " " + count ); if(ans == count) out.println("YES"); else out.println("NO"); } out.close(); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
b3db59a20c1df2f4909573899843b01f
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int globalVariable = 123456789; static String author = "pl728 on codeforces"; public static void main(String[] args) { FastReader sc = new FastReader(); MathUtils mathUtils = new MathUtils(); ArrayUtils arrayUtils = new ArrayUtils(); int tc = sc.ni(); while (tc-- != 0) { int n = sc.ni(), m = sc.ni(); char[][] grid = new char[n][m]; int[][] id = new int[n][m]; // iterate through each cell // keep track of a set of x cords, y cords // if either > 2, return no // keep track of connected stars // if > 3, return no for (int i = 0; i < n; i++) { grid[i] = sc.ns().toCharArray(); } boolean ans = true; int curr = 1; for (int i = 0; i < n && ans; i++) { for (int j = 0; j < m; j++) { if(grid[i][j] == '*') { int numNeighbors = numNeighbors(grid, i, j); if(numNeighbors > 2) { ans = false; break; } if (numNeighbors == 2) { if(isLine(grid, i, j)) { ans = false; break; } if(!markL(grid, id, i, j, curr)) { ans = false; break; } curr++; } } } } for(int i = 0; i < n && ans; i++) { for(int j = 0; j < m; j++) { // check diagonal shaded cells are same number if (grid[i][j] == '*') { if(id[i][j] == 0) ans = false; if(i - 1 >= 0 && j - 1 >= 0 && grid[i - 1][j - 1] == '*' && id[i - 1][j - 1] != id[i][j]) ans = false; if(i - 1 >= 0 && j + 1 < m && grid[i - 1][j + 1] == '*' && id[i - 1][j + 1] != id[i][j]) ans = false; if(i + 1 < n && j - 1 >= 0 && grid[i + 1][j - 1] == '*' && id[i + 1][j - 1] != id[i][j]) ans = false; if(i + 1 < n && j + 1 < m && grid[i + 1][j + 1] == '*' && id[i + 1][j + 1] != id[i][j]) ans = false; } } } if(!ans) { System.out.println("NO"); } else { System.out.println("YES"); } } } static boolean markL(char[][] grid, int[][] id, int i, int j, int mark) { if(id[i][j] == 0) id[i][j] = mark; else return false; if(i - 1 >= 0 && grid[i-1][j] == '*') { if(id[i - 1][j] == 0) id[i - 1][j] = mark; else return false; } if(i + 1 < grid.length && grid[i+1][j] == '*') { if(id[i + 1][j] == 0) id[i + 1][j] = mark; else return false; } if(j - 1 >= 0 && grid[i][j-1] == '*') { if(id[i][j - 1] == 0) id[i][j - 1] = mark; else return false; } if(j + 1 < grid[0].length && grid[i][j + 1] == '*') { if(id[i][j + 1] == 0) id[i][j + 1] = mark; else return false; } return true; } static boolean isLine(char[][] grid, int i, int j) { boolean a = false, b = false, c = false, d = false; if(i - 1 >= 0 && grid[i-1][j] == '*') { a = true; } if(i + 1 < grid.length && grid[i+1][j] == '*') { b = true; } if(j - 1 >= 0 && grid[i][j-1] == '*') { c = true; } if(j + 1 < grid[0].length && grid[i][j + 1] == '*') { d = true; } return (a && b) || (c && d); } static int numNeighbors(char[][] grid, int i, int j) { int count = 0; if(i - 1 >= 0 && grid[i-1][j] == '*') { count++; } if(i + 1 < grid.length && grid[i+1][j] == '*') { count++; } if(j - 1 >= 0 && grid[i][j-1] == '*') { count++; } if(j + 1 < grid[0].length && grid[i][j + 1] == '*') { count++; } return count; } static class FastReader { /** * Uses BufferedReader and StringTokenizer for quick java I/O * get next int, long, double, string * get int, long, double, string arrays, both primitive and wrapped object when array contains all elements * on one line, and we know the array size (n) * next: gets next space separated item * nextLine: returns entire line as space */ BufferedReader br; StringTokenizer st; public FastReader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } // to parse something else: // T x = T.parseT(fastReader.next()); public int ni() { return Integer.parseInt(next()); } public String ns() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String[] readStringArrayLine(int n) { String line = ""; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line.split(" "); } public String[] readStringArrayLines(int n) { String[] result = new String[n]; for (int i = 0; i < n; i++) { result[i] = this.next(); } return result; } public int[] readIntArray(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = this.ni(); } return result; } public long[] readLongArray(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = this.nl(); } return result; } public Integer[] readIntArrayObject(int n) { Integer[] result = new Integer[n]; for (int i = 0; i < n; i++) { result[i] = this.ni(); } return result; } public long nl() { return Long.parseLong(next()); } public char[] readCharArray(int n) { return this.ns().toCharArray(); } } static class MathUtils { public MathUtils() { } public long gcdLong(long a, long b) { if (a % b == 0) return b; else return gcdLong(b, a % b); } public long lcmLong(long a, long b) { return a * b / gcdLong(a, b); } } static class ArrayUtils { public ArrayUtils() { } public static int[] reverse(int[] a) { int n = a.length; int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = a[i]; j = j - 1; } return b; } public int sumIntArrayInt(int[] a) { int ans = 0; for (int i = 0; i < a.length; i++) { ans += a[i]; } return ans; } public long sumLongArrayLong(int[] a) { long ans = 0; for (int i = 0; i < a.length; i++) { ans += a[i]; } return ans; } } public static int lowercaseToIndex(char c) { return (int) c - 97; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
5b42bc7ae37e19158e0d014c5f916601
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
/*LoudSilence*/ import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static FastScanner s = new FastScanner(); static FastWriter out = new FastWriter(); final static int mod = (int)1e9 + 7; final static int INT_MAX = Integer.MAX_VALUE; final static int INT_MIN = Integer.MIN_VALUE; final static long LONG_MAX = Long.MAX_VALUE; final static long LONG_MIN = Long.MIN_VALUE; final static double DOUBLE_MAX = Double.MAX_VALUE; final static double DOUBLE_MIN = Double.MIN_VALUE; final static float FLOAT_MAX = Float.MAX_VALUE; final static float FLOAT_MIN = Float.MIN_VALUE; /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static class FastScanner{BufferedReader br;StringTokenizer st; public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));} catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());} List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;} List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;} int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;} long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));} catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{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 debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void close() throws IOException{bw.close();}} public static void println(Object str) throws IOException{out.println(""+str);} public static void println(Object str, int nextLine) throws IOException{out.print(""+str);} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;} public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);} public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];} public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;} public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;} public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;} public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;} public static long modInv(long a, long b){ return expo(a, b-2)%b;} public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));} public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Pair class static class Pair implements Comparable<Pair>{ long f; long s; public Pair(long f, long s){ this.f = f; this.s = s; } public String toString(){ return "("+f+", "+s+")"; } @Override public int compareTo(Pair o) { if(f == o.f) return s-o.s < 0 ? -1 : 1; else return f-o.f < 0 ? -1 : 1; } } // public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X,Y>>{ // X first; // Y second; // public Pair(X first, Y second){ // this.first = first; // this.second = second; // } // public String toString(){ // return "( " + first+" , "+second+" )"; // } // @Override // public int compareTo(Pair<X, Y> o) { // int t = first.compareTo(o.first); // if(t == 0) return second.compareTo(o.second); // return t; // } // } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Code begins public static void solve() throws IOException { int n = s.nextInt(); int m = s.nextInt(); char arr[][] = new char[n][]; for(int i = 0; i < n; i++){ arr[i] = s.next().toCharArray(); } int total = 0; for(int i = 0; i < n-1; i++){ for(int j = 0; j < m-1; j++){ int count = (arr[i][j] == '*' ? 1 : 0) + (arr[i][j+1] == '*' ? 1 : 0) + (arr[i+1][j] == '*' ? 1 : 0) + (arr[i+1][j+1] == '*' ? 1 : 0); total += count == 3 ? 1 : 0; } } int check = 0; boolean visited[][] = new boolean[n][m]; for (int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(visited[i][j] == false && arr[i][j] == '*'){ int count = helper(i, j, arr, visited); if(count > 3){ println("NO"); return; } check++; } } } println(check == total ? "YES" : "NO"); } static int moves[][] = {{1,0},{0,1}, {-1,0}, {0,-1}, {1,1}, {-1,-1}, {1, -1}, {-1, 1}}; private static int helper(int i, int j, char[][] arr, boolean[][] visited) { visited[i][j] = true; int count = 1; for(int c = 0; c < moves.length; c++){ int newi = i+moves[c][0]; int newj = j+moves[c][1]; if((newi >= 0) && (newi < arr.length) && (newj >= 0) && (newj < arr[0].length) && !visited[newi][newj]){ if(arr[newi][newj] == '*'){ count += helper(newi, newj, arr, visited); } } } return count; } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static void main(String[] args) throws IOException { int test = s.nextInt(); for(int t = 1; t <= test; t++) { solve(); } out.close(); } } /*public static boolean allsame(int arr[]){ for(int i = 0; i < arr.length-1; i++){ if(arr[i] != arr[i+1]) return false; } return true; } public static List<List<Integer>> permutation(int arr[], int i){ if(i == 0){ List<List<Integer>> ans = new ArrayList<>(); List<Integer> li = new ArrayList<>(); li.add(arr[i]); ans.add(li); return ans; } int temp = arr[i]; List<List<Integer>> rec = permutation(arr, i-1); List<List<Integer>> ans = new ArrayList<>(); for(List<Integer> li : rec){ for(int j = 0; j <= li.size(); j++){ List<Integer> list = new ArrayList<>(); for(int ele: li){ list.add(ele); } list.add(j, temp); ans.add(list); } } return ans; }*/
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
4772f21475e2c2d586826c2355e78e6b
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; // @author : Dinosparton public class test { static class Pair{ long x; int y; Pair(long x,int y){ this.x = x; this.y = y; } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { 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 boolean triangle1(int x,int y,char mat[][]) { if((x>=0 && x<mat.length) && (x+1>=0 && x+1<mat.length) && (y>=0 && y<mat[0].length) && (y+1>=0 && y+1<mat[0].length)) { if(mat[x][y]=='*' && mat[x][y+1]=='*' && mat[x+1][y+1]=='*') { return true; } } return false; } static boolean triangle2(int x,int y,char mat[][]) { if((x>=0 && x<mat.length) && (x+1>=0 && x+1<mat.length) && (y>=0 && y<mat[0].length) && (y+1>=0 && y+1<mat[0].length)) { if(mat[x][y]=='*' && mat[x][y+1]=='*' && mat[x+1][y]=='*') { return true; } } return false; } static boolean triangle3(int x,int y,char mat[][]) { if((x>=0 && x<mat.length) && (x-1>=0 && x-1<mat.length) && (y>=0 && y<mat[0].length) && (y+1>=0 && y+1<mat[0].length)) { if(mat[x][y]=='*' && mat[x][y+1]=='*' && mat[x-1][y+1]=='*') { return true; } } return false; } static boolean triangle4(int x,int y,char mat[][]) { if((x>=0 && x<mat.length) && (x-1>=0 && x-1<mat.length) && (y>=0 && y<mat[0].length) && (y+1>=0 && y+1<mat[0].length)) { if(mat[x][y]=='*' && mat[x][y+1]=='*' && mat[x-1][y]=='*') { return true; } } return false; } static void triangle1(int x,int y,boolean c[][]) { c[x][y] = true; c[x][y+1] = true; c[x+1][y+1] = true; } static void triangle2(int x,int y,boolean c[][]) { c[x][y] = true; c[x][y+1] = true; c[x+1][y] = true; } static void triangle3(int x,int y,boolean c[][]) { c[x][y] = true; c[x][y+1] = true; c[x-1][y+1] = true; } static void triangle4(int x,int y,boolean c[][]) { c[x][y] = true; c[x][y+1] = true; c[x-1][y] = true; } static int dirx[] = {0,0,1,1,1,-1,-1,-1}; static int diry[] = {1,-1,0,1,-1,0,1,-1}; static boolean check(int x,int y,char mat[][],boolean c[][]) { for(int i=0;i<8;i++) { int xx = x + dirx[i]; int yy = y + diry[i]; if((xx>=0 && xx<mat.length) && (yy>=0 && yy<mat[0].length)) { if(mat[xx][yy]=='*' && c[xx][yy]==false) { return false; } } } return true; } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n = sc.nextInt(); int m = sc.nextInt(); char mat[][] = new char[n][m]; for(int i=0;i<n;i++) { String s = sc.next(); for(int j=0;j<m;j++) { mat[i][j] = s.charAt(j); } } boolean c[][] = new boolean[n][m]; boolean ok = true; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(c[i][j]) { if(!check(i,j,mat,c)) { ok = false; } } else { if(triangle1(i,j,mat)) { triangle1(i,j,c); if(!check(i,j,mat,c)) { ok = false; } } else if(triangle2(i,j,mat)) { triangle2(i,j,c); if(!check(i,j,mat,c)) { ok = false; } } else if(triangle3(i,j,mat)) { triangle3(i,j,c); if(!check(i,j,mat,c)) { ok = false; } } else if(triangle4(i,j,mat)) { triangle4(i,j,c); if(!check(i,j,mat,c)) { ok = false; } } } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mat[i][j]=='*' && c[i][j]==false) { ok = false; } } } if(ok) { res.append("YES"+"\n"); } else { res.append("NO"+"\n"); } } System.out.println(res); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
b37b4e1fa73ae19cd683d20317762b75
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.StringTokenizer; public class Main { static AReader scan = new AReader(); static int N = 60; static int[][] g = new int[N][N]; static int[] dx = {-1,0,1,0,-1,-1,1,1}; static int[] dy = {0,1,0,-1,-1,1,1,-1}; static boolean[][] st = new boolean[N][N]; static void solve() { int n = scan.nextInt(); int m = scan.nextInt(); for(int i = 1;i<=n;i++){ char[] cs = scan.next().toCharArray(); for(int j = 1;j<=m;j++){ g[i][j] = 0; if(cs[j-1] == '*') g[i][j] = 1; st[i][j] = false; } } boolean success = true; for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ if(!success) break; if(!st[i][j] && g[i][j] == 1){ Queue<Pair> queue = new LinkedList<>(); List<Pair> list = new ArrayList<>(); list.add(new Pair(i,j)); queue.offer(new Pair(i,j)); st[i][j] = true; while(!queue.isEmpty()){ Pair pair = queue.poll(); int x = pair.x; int y = pair.y; for(int k = 0;k<8;k++){ int a = x + dx[k]; int b = y + dy[k]; if(a <= 0 || a > n || b<=0 || b>m || g[a][b] == 0 || st[a][b]) continue; st[a][b] = true; queue.offer(new Pair(a,b)); list.add(new Pair(a,b)); } if(list.size() > 3) success = false; if(!success) break; } if(list.size() != 3) success = false; else{ // 等于3 int minx = 0x3f3f3f3f; int maxx = -0x3f3f3f3f; int miny = 0x3f3f3f3f; int maxy = -0x3f3f3f3f; for(Pair pair:list){ minx = Math.min(minx,pair.x); maxx = Math.max(maxx,pair.x); miny = Math.min(miny,pair.y); maxy = Math.max(maxy,pair.y); } if(maxx - minx + 1>=3 || maxy - miny + 1 >= 3) success = false; } } } if(!success) break; } if(success) System.out.println("YES"); else System.out.println("NO"); } public static void main(String[] args) { int T = scan.nextInt(); while (T-- > 0) { solve(); } } } class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } class Pair { int x,y; public Pair(int x, int y) { this.x = x; this.y = y; } } class Node{ int l,r; int val; int lazy; int cnt = 0,lnum,rnum; public Node(int l, int r) { this.l = l; this.r = r; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
55d20e7d84845ec3444ab1845e261ef6
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; public class Solution{ public 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 swap(int i, int j) { int temp = i; i = j; j = temp; } static class Pair{ int s; int end; public Pair(int a, int e) { end = e; s = a; } } 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(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } public static void main(String[] args) throws Exception { FastReader scn = new FastReader(); OutputStream outputStream = System.out; OutputWriter output = new OutputWriter(outputStream); int t = scn.nextInt(); long mod = 1000000007L; // String[] pal = {"0000", "0110","0220","0330","0440","0550","1001","1111","1221","1331","1441","1551","2002","2112","2222","2332"}; while(t>0) { // int n = scn.nextInt(); // long max =0, min= Integer.MAX_VALUE; // long[] a = new long[n]; // for(int i=0;i<n;i++) { // a[i]= scn.nextLong(); // max = Math.max(max,a[i]); // min = Math.min(min, a[i]); // } int n = scn.nextInt(); int m = scn.nextInt(); char[][] a = new char[n][m]; for(int i=0;i<n;i++) { String s = scn.next(); for(int j=0;j<m;j++) { a[i][j] = s.charAt(j); } } boolean b = true; boolean[][] mark = new boolean[n][m]; for(int i=0;i<n-1;i++) { boolean f = false; for(int j=0;j<m-1;j++) { if(checkL(a,i,j, mark)==1) { f = true; } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]=='*') { if(!mark[i][j]) { b=false; } } // output.print(mark[i][j]+" "); } //output.println(); } if(b==true) { output.println("YES"); }else { output.println("NO"); } t--; } output.close(); } public static int checkL(char[][] a, int r, int c, boolean[][] mark) { int cnt=0; for(int i=Math.max(r-1,0);i<=Math.min(r+2,a.length-1);i++) { for(int j= Math.max(c-1,0);j<=Math.min(c+2,a[0].length-1);j++) { if(a[i][j]=='*') { cnt++; } } } char ch11 = a[r][c]; char ch12 = a[r][c+1]; char ch21 = a[r+1][c]; char ch22 = a[r+1][c+1]; int n = a.length; int m = a[0].length; if(cnt==4) { if(ch22!='*' && r+2<n && c+2<m && a[r+2][c+2]=='*') { cnt--; }else if(ch21!='*' && r+2<n && c-1>=0 && a[r+2][c-1]=='*') { cnt--; }else if(ch11!='*' && r-1>=0 && c-1>=0 && a[r-1][c-1]=='*') { cnt--; }else if(ch12!='*' && r-1>=0 && c+2<m && a[r-1][c+2]=='*') { cnt--; } } if(cnt==0) { return 1; } if(cnt>3) { return 4; } if(cnt<3) { return 2; } if((ch11=='*' && ch12=='*' && ch22=='*' && ch21!='*')|| (ch11=='*' && ch21=='*' && ch22=='*' && ch12!='*') || (ch12=='*'&&ch22=='*'&&ch21=='*'&&ch11!='*')||(ch11=='*'&&ch12=='*'&&ch21=='*'&&ch22!='*')) { for(int i=r;i<r+2;i++) { for(int j=c;j<c+2;j++) { if(a[i][j]=='*') { mark[i][j] = true; } } } return 1; } return 1; } public static void addTime(int h, int m, int ah, int am) { int rm = m+am; int rh = (h+ah+(rm/60))%24; rm = rm%60; } public static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
3c4b1466866353126a86da82f390c3cd
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class ProblemC { static boolean ok = true; static int count = 0; public static void main(String[] args) throws Exception { Scanner in = new Scanner(); StringBuilder sb = new StringBuilder(); int test = in.readInt(); while(test-->0){ int n = in.readInt(); int m = in.readInt(); char c[][] = new char[n][m]; for(int i = 0;i<n;i++){ c[i] = in.readLine().toCharArray(); } ok = true; outer: for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ if(c[i][j] == '$' || c[i][j] == '.')continue; count = 0; visit(c,i,j); if(count != 3){ ok = false; break outer; } if(!ok1(c,i,j) && !ok2(c,i,j) && !ok3(c,i,j) && !ok4(c,i,j)){ ok = false; break outer; } } } if(ok)System.out.println("YES"); else System.out.println("NO"); } } public static boolean ok1(char c[][],int i,int j){ return (j+1<c[0].length && c[i][j+1] == '$' && c[i][j] == '$' && i+1<c.length && c[i+1][j+1] =='$'); } public static boolean ok2(char c[][],int i,int j){ return (j+1<c[0].length && c[i][j+1] == '$' && c[i][j] == '$' && i+1<c.length && c[i+1][j] =='$'); } public static boolean ok3(char c[][],int i,int j){ return (j-1>=0 && i+1<c.length && c[i+1][j-1] == '$' && c[i][j] == '$' && c[i+1][j] =='$'); } public static boolean ok4(char c[][],int i,int j){ return (j+1<c[0].length && i+1<c.length && c[i+1][j+1] == '$' && c[i][j] == '$' && c[i+1][j] == '$' ); } public static void visit(char c[][],int i,int j){ if(i>=c.length || i<0 || j>=c[0].length || j<0 || c[i][j] == '$' || c[i][j] =='.')return; if(c[i][j] == '*')count++; c[i][j] = '$'; visit(c,i+1,j); visit(c,i-1,j); visit(c,i,j+1); visit(c,i,j-1); visit(c,i-1,j-1); visit(c,i-1,j+1); visit(c,i+1,j-1); visit(c,i+1,j+1); } public static void swap(int a[],int i, int j){ int t = a[i]; a[i] = a[j]; a[j] = t; } public static boolean subsequence(String cur,String t){ int pos = 0; for(int i = 0;i<cur.length();i++){ if(pos<t.length() && cur.charAt(i) == t.charAt(pos)){ pos++; } } return pos == t.length(); } public static long gcd(long a, long b){ return b ==0 ?a:gcd(b,a%b); } static class Pair{ int x,y; public Pair(int x,int y){ this.x = x; this.y = y; } } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String read() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public double readDouble(){return Double.parseDouble(read());} public String readLine() throws Exception { return br.readLine(); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
d8fd69d53a0c4754a2c6f2acf2de7daa
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; public class Solution { // 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 scn = new FastReader(); int t=scn.nextInt(); while(t-->0){ int n=scn.nextInt(); int m=scn.nextInt(); char grid[][]=new char[n][m]; for(int i=0;i<n;i++){ String s=scn.next(); for(int j=0;j<m;j++){ grid[i][j]=s.charAt(j); } } boolean flag=true; for(int i=0;i<n && flag;i++){ for(int j=0;j<m && flag;j++){ if(grid[i][j]=='*'){ if(getSize(grid,i,j)!=3){ flag=false; } } } } if(!flag){ System.out.println("NO"); continue; } for(int i=0;i<n && flag;i++){ for(int j=0;j<m && flag;j++){ if(grid[i][j]=='-'){ size=0; if(!matchOrNot(grid,i,j,new StringBuilder()) || size!=3){ // System.out.println(size); flag=false; } } } } // for(int i=0;i<n;i++){ // for(int j=0;j<m;j++){ // System.out.print(grid[i][j]+" "); // } // System.out.println(); // } System.out.println(flag?"YES":"NO"); } } public static int getSize(char[][] grid,int i,int j){ grid[i][j]='-'; int sum=1; for(int ii=0;ii<8;ii++){ int ni=i+dirs[ii][0]; int nj=j+dirs[ii][1]; if(ni>=0 && nj>=0 && ni<grid.length && nj<grid[0].length && grid[ni][nj]=='*'){ sum+=getSize(grid,ni,nj); } } return sum; } static int[][] dirs=new int[][]{{0,1},{1,0},{0,-1},{-1,0},{-1,-1},{1,1},{1,-1},{-1,1}}; static char[] label=new char[]{'R','D','L','U','X','X','X','X'}; static int size; public static boolean matchOrNot(char[][] grid,int i,int j,StringBuilder sb){ if(sb.length()==2){ if(sb.charAt(0)==sb.charAt(1)){ return false; } } size++; grid[i][j]='.'; for(int ii=0;ii<4;ii++){ int ni=i+dirs[ii][0]; int nj=j+dirs[ii][1]; if(ni>=0 && nj>=0 && ni<grid.length && nj<grid[0].length && grid[ni][nj]=='-'){ sb.append(label[ii]); if(!matchOrNot(grid,ni,nj,sb)){ return false; } sb.deleteCharAt(sb.length()-1); } } return true; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
55f5279790ea431bf24744b3c3760ca9
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class ComdeFormces { static int dp[][]; static boolean mnans; static int x[]= {-1,0,0,1,1,1,-1,-1}; static int y[]= {0,1,-1,0,1,-1,1,-1}; static int ans[]; 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)); // OutputStream out = new BufferedOutputStream ( System.out ); int t=sc.nextInt(); int tc=1; while(t--!=0) { int n=sc.nextInt(); int m=sc.nextInt(); char a[][]=new char[n][m]; boolean vis[][]=new boolean[n][m]; for(int i=0;i<n;i++)a[i]=sc.next().toCharArray(); // for(int i=0;i<n;i++) { // for(int j=0;j<m;j++) { // System.out.print(a[i][j]+" "); // } // System.out.println(); // } boolean ans=true; for(int i=0;i<n && ans;i++) { for(int j=0;j<m && ans;j++) { if(a[i][j]=='.' || vis[i][j])continue; Queue<pair> qu=new LinkedList<>(); qu.add(new pair(i,j)); vis[i][j]=true; int cnt=0; ArrayList<pair> ar=new ArrayList<>(); while(!qu.isEmpty() && ans) { pair p=qu.poll(); ar.add(p); cnt++; if(cnt>3)ans=false; for(int k=0;k<4 && ans;k++) { if(p.a+x[k]<n && p.a+x[k]>=0 && p.b+y[k]<m && p.b+y[k]>=0 && a[p.a+x[k]][p.b+y[k]]=='*' && !vis[p.a+x[k]][p.b+y[k]]) { vis[p.a+x[k]][p.b+y[k]]=true; qu.add(new pair(p.a+x[k],p.b+y[k])); } } } if(cnt!=3)ans=false; if(ans){ if((ar.get(0).a==ar.get(1).a && ar.get(1).a==ar.get(2).a) || (ar.get(0).b==ar.get(1).b && ar.get(1).b==ar.get(2).b)) { ans=false; } else { for(int l=0;l<3 && ans;l++) { for(int k=4;k<8;k++) { pair p=ar.get(l); if(p.a+x[k]<n && p.a+x[k]>=0 && p.b+y[k]<m && p.b+y[k]>=0 && a[p.a+x[k]][p.b+y[k]]=='*' && !vis[p.a+x[k]][p.b+y[k]]) { ans=false; break; } } } } } } } log.write((ans?"YES":"NO")+"\n"); } log.flush(); } static int[] manacher_odd(String ss) { int n = ss.length(); ss = "$" + ss + "^"; char s[]=ss.toCharArray(); int p[]=new int[n+2]; int l = 1, r = 1; for(int i = 1; i <= n; i++) { p[i] = Math.max(0, Math.min(r - i, p[l + (r - i)])); while(s[i - p[i]] == s[i + p[i]]) { p[i]++; } if(i + p[i] > r) { l = i - p[i]; r = i + p[i]; } } return p; } static int mod=998244853; static int[] lps(int a[],String s) { int i=1; int j=0; a[0]=0; while(i<s.length()) { if(s.charAt(i)==s.charAt(j)) { a[i]=j+1; i++; j++; } else { if(j!=0) { j=a[j-1]; } else { a[i]=0; i++; } } } return a; } static int[] zed(char a[]) { int z[]=new int[a.length]; int l=0; int r=0; for(int i=0;i<a.length;i++) { if(i>r) { l=r=i; while(r<a.length && a[r]==a[r-l])r++; z[i]=r-l; r--; } else { int k1=i-l; if(z[k1]<r-i+1) { z[i]=z[k1]; } else { l=i; while(r<a.length && a[r]==a[r-l])r++; z[i]=r-l; r--; } } } return z; } public static class pair2{ int a,b,c,d; public pair2(int a,int b,int c,int d) { this.a=a; this.b=b; this.c=c; this.d=d; } } static boolean dfs(ArrayList<ArrayList<Integer>> ar,int src,int pr,HashSet<Integer> hs) { int cnt=0; boolean an=false; for(int k:ar.get(src)) { if(k==pr)continue; boolean p=dfs(ar,k,src,hs); an|=p; if(p)cnt++; } if(cnt>1)mnans=false; if(hs.contains(src))an=true; return an; } static int find(int el,int p[]) { if(p[el]<0)return el; return p[el]=find(p[el],p); } static boolean union(int a,int b,int p[]) { int p1=find(a,p); int p2=find(b,p); if(p1>=0 && p1==p2)return false; else { if(p[p1]<p[p2]) { p[p1]+=p[p2]; p[p2]=p1; } else { p[p2]+=p[p1]; p[p1]=p2; } return true; } } public static void build(int a[][],int b[]) { for(int i=0;i<b.length;i++) { a[i][0]=b[i]; } int jmp=2; while(jmp<=b.length) { for(int j=0;j<b.length;j++) { int ind=(int)(Math.log(jmp/2)/Math.log(2)); int ind2=(int)(Math.log(jmp)/Math.log(2)); if(j+jmp-1<b.length) { a[j][ind2]=Math.max(a[j][ind],a[j+(jmp/2)][ind]); } } jmp*=2; } // for(int i=0;i<a.length;i++) { // for(int j=0;j<33;j++) { // System.out.print(a[i][j]+" "); // } // System.out.println(); // } } public static void build2(int a[][],int b[]) { for(int i=0;i<b.length;i++) { a[i][0]=b[i]; } int jmp=2; while(jmp<=b.length) { for(int j=0;j<b.length;j++) { int ind=(int)(Math.log(jmp/2)/Math.log(2)); int ind2=(int)(Math.log(jmp)/Math.log(2)); if(j+jmp-1<b.length) { a[j][ind2]=Math.min(a[j][ind],a[j+(jmp/2)][ind]); } } jmp*=2; } // for(int i=0;i<a.length;i++) { // for(int j=0;j<33;j++) { // System.out.print(a[i][j]+" "); // } // System.out.println(); // } } public static int serst(int a[][],int i,int j) { int len=j-i+1; int hp=1; int tlen=len>>=1; // System.out.println(tlen); while(tlen!=0) { tlen>>=1; hp<<=1; } // System.out.println(hp); int ind=(int)(Math.log(hp)/Math.log(2)); int i2=j+1-hp; return Math.max(a[i][ind], a[i2][ind]); } public static int serst2(int a[][],int i,int j) { int len=j-i+1; int hp=1; int tlen=len>>=1; // System.out.println(tlen); while(tlen!=0) { tlen>>=1; hp<<=1; } // System.out.println(hp); int ind=(int)(Math.log(hp)/Math.log(2)); int i2=j+1-hp; return Math.min(a[i][ind], a[i2][ind]); } static void update(long f[],long upd,int ind) { int vl=ind; while(vl<f.length) { f[vl]+=upd; int tp=~vl; tp++; tp&=vl; vl+=tp; } } static long ser(long f[],int ind) { int vl=ind; long sm=0; while(vl!=0) { sm+=f[vl]; int tp=~vl; tp++; tp&=vl; vl-=tp; } return sm; } public static void radixSort(int a[]) { int n=a.length; int res[]=new int[n]; int p=1; for(int i=0;i<=8;i++) { int cnt[]=new int[10]; for(int j=0;j<n;j++) { a[j]=res[j]; cnt[(a[j]/p)%10]++; } for(int j=1;j<=9;j++) { cnt[j]+=cnt[j-1]; } for(int j=n-1;j>=0;j--) { res[cnt[(a[j]/p)%10]-1]=a[j]; cnt[(a[j]/p)%10]--; } p*=10; } } static int bits(long n) { int ans=0; while(n!=0) { if((n&1)==1)ans++; n>>=1; } return ans; } public static int kadane(int a[]) { int sum=0,mx=Integer.MIN_VALUE; for(int i=0;i<a.length;i++) { sum+=a[i]; mx=Math.max(mx, sum); if(sum<0) sum=0; } return mx; } public static int m=(int)(1e9+7); public static int mul(int a, int b) { return ((a%m)*(b%m))%m; } public static long mul(long a, long b) { return ((a%m)*(b%m))%m; } public static int add(int a, int b) { return ((a%mod)+(b%mod))%mod; } public static long add(long a, long b) { return ((a%m)+(b%m))%m; } //debug public static <E> void p(E[][] a,String s) { System.out.println(s); for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } public static void p(int[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static void p(long[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static <E> void p(E a,String s){ System.out.println(s+"="+a); } public static <E> void p(ArrayList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(LinkedList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(HashSet<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Stack<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Queue<E> a,String s){ System.out.println(s+"="+a); } //utils 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 ArrayList<Integer> prime(int n){ ArrayList<Integer> ar=new ArrayList<>(); int cnt=0; boolean pr=false; while(n%2==0) { ar.add(2); n/=2; } for(int i=3;i*i<=n;i+=2) { pr=false; while(n%i==0) { n/=i; ar.add(i); pr=true; } } if(n>2) ar.add(n); return ar; } static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static long factmod(long n,long mod) { if(n==0)return 0; long ans=1; long temp=1; while(temp<=n) { ans=((ans%mod)*((temp)%mod))%mod; temp++; } return ans%mod; } 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; int b; int 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.b-q.b; // } } static void mergesort(int[] a,int start,int end) { if(start>=end)return ; int mid=start+(end-start)/2; mergesort(a,start,mid); mergesort(a,mid+1,end); merge(a,start,mid,end); } 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 this.a-b.a; // // } // // public int compareToo(pair b) { // return this.b-b.b; // } @Override public String toString() { return "{"+this.a+" "+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; } public static int md=998244353; static long mpow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2)%md; if(pw%2==0)return mul(temp,temp); return mul(a,mul(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
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
2ea795335b4f404d7923bd76f546425e
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while(t-->0){ StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()); char[][] board = new char[m+2][n+2]; for(int i=0; i<n; i++){ String s = br.readLine(); for(int j=0; j<m; j++){ board[j+1][i+1]=s.charAt(j); } } String ans = "YES"; for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ if(board[j][i]=='*'){ int side = 0; int top = 0; int x = -1; int y = -1; if(board[j+1][i]=='*'){ side++; x = j+1; y = i; } if(board[j-1][i]=='*'){ side++; x = j-1; y = i; } if(board[j][i+1]=='*'){ top++; x = j; y = i+1; } if(board[j][i-1]=='*'){ top++; x = j; y = i-1; } if(top==2||side==2){ ans="NO"; break; } else if(top==0 && side==0){ ans="NO"; break; } int a = 0; if(board[j+1][i-1]=='*'){ a++; } if(board[j-1][i+1]=='*'){ a++; } if(board[j+1][i+1]=='*'){ a++; } if(board[j-1][i-1]=='*'){ a++; } if(a>1){ ans="NO"; break; } else if(top==1&&side==1){ if(board[j+1][i-1]=='*'){ ans="NO"; break; } if(board[j-1][i+1]=='*'){ ans="NO"; break; } if(board[j+1][i+1]=='*'){ ans="NO"; break; } if(board[j-1][i-1]=='*'){ ans="NO"; break; } } else{ int add = 0; if(board[x+1][y]=='*'){ add++; } if(board[x-1][y]=='*'){ add++; } if(board[x][y+1]=='*'){ add++; } if(board[x][y-1]=='*'){ add++; } if(add==1){ ans="NO"; break; } } } } } pw.println(ans); } pw.close(); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
e8d87baea375aaa00be7746c05a4caff
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.DataInputStream; import java.io.IOException; import java.util.*; public class Yash { public static boolean isShaded(int i, int j, char[][] grid) { int n = grid.length, m = grid[0].length; return i >= 0 && i < n && j >= 0 && j < m && grid[i][j] != '.'; } public static boolean isL(int i, int j, char[][] grid, boolean[][] vis) { boolean c1 = isShaded(i, j + 1, grid) && isShaded(i + 1, j + 1, grid) && !isShaded(i + 1, j, grid) && !isShaded(i - 1, j, grid) && !isShaded(i - 1, j - 1, grid) && !isShaded(i + 1, j - 1, grid) && !isShaded(i - 1, j + 1, grid) && !isShaded(i - 1, j + 2, grid) && !isShaded(i, j + 2, grid) && !isShaded(i + 1, j + 2, grid) && !isShaded(i + 2, j + 2, grid) && !isShaded(i + 2, j + 1, grid) && !isShaded(i + 2, j, grid) && !isShaded(i, j - 1, grid); boolean c2 = isShaded(i + 1, j, grid) && isShaded(i + 1, j + 1, grid) && !isShaded(i, j - 1, grid) && !isShaded(i - 1, j - 1, grid) && !isShaded(i - 1, j, grid) && !isShaded(i - 1, j + 1, grid) && !isShaded(i, j + 1, grid) && !isShaded(i, j + 2, grid) && !isShaded(i + 1, j + 2, grid) && !isShaded(i + 2, j + 2, grid) && !isShaded(i + 2, j + 1, grid) && !isShaded(i + 2, j, grid) && !isShaded(i + 2, j - 1, grid) && !isShaded(i + 1, j - 1, grid); boolean c3 = isShaded(i + 1, j, grid) && isShaded(i + 1, j - 1, grid) && !isShaded(i - 1, j, grid) && !isShaded(i - 1, j + 1, grid) && !isShaded(i, j + 1, grid) && !isShaded(i + 1, j + 1, grid) && !isShaded(i + 2, j + 1, grid) && !isShaded(i + 2, j, grid) && !isShaded(i + 2, j - 1, grid) && !isShaded(i + 2, j - 2, grid) && !isShaded(i + 1, j - 2, grid) && !isShaded(i, j - 2, grid) && !isShaded(i, j - 1, grid) && !isShaded(i - 1, j - 1, grid); boolean c4 = isShaded(i, j + 1, grid) && isShaded(i + 1, j, grid) && !isShaded(i - 1, j, grid) && !isShaded(i - 1, j + 1, grid) && !isShaded(i - 1, j + 2, grid) && !isShaded(i, j + 2, grid) && !isShaded(i + 1, j + 2, grid) && !isShaded(i + 1, j + 1, grid) && !isShaded(i + 2, j + 1, grid) && !isShaded(i + 2, j, grid) && !isShaded(i + 2, j - 1, grid) && !isShaded(i + 1, j - 1, grid) && !isShaded(i, j - 1, grid) && !isShaded(i - 1, j - 1, grid); if(c1) { vis[i][j + 1] = true; vis[i + 1][j + 1] = true; } if(c2) { vis[i + 1][j] = true; vis[i + 1][j + 1] = true; } if(c3) { vis[i + 1][j] = true; vis[i + 1][j - 1] = true; } if(c4) { vis[i][j + 1] = true; vis[i + 1][j] = true; } return c1 || c2 || c3 || c4; } 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(); char[][] grid = new char[n][m]; for(int i=0; i<n; i++) { String s = sc.next(); for(int j=0; j<m; j++) grid[i][j] = s.charAt(j); } String ans = "YES"; boolean[][] vis = new boolean[n][m]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(grid[i][j] == '*') { if(!vis[i][j] && !isL(i, j, grid, vis)) ans = "NO"; } } } System.out.println(ans); } } 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 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; } 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(); } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
bcf5208be3d9a83249008c421940d4d8
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { static class Pair { int x, y; Pair (int x, int y) { this.x= x; this.y= y; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int testcase = Integer.parseInt(br.readLine()); int[] dx = {-1,0,1,0,-1,-1,1,1} , dy = {0,1,0,-1,-1,1,1,-1}; C: while(testcase-->0){ StringTokenizer st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken()); char[][] board = new char[a][]; for (int i=0;i<a;i++) board[i] = br.readLine().toCharArray(); boolean[][] visited = new boolean[a][b]; Queue<Pair> qu = new LinkedList<>(); Pair[] t = new Pair[4]; for (int i=0;i<a;i++) { for (int j=0;j<b;j++) { if (board[i][j] == '*' && !visited[i][j]) { visited[i][j] = true; int cnt = 1; qu.offer(new Pair(i,j)); while(!qu.isEmpty()) { Pair n = qu.poll(); for (int k=0;k<8;k++) { int x = n.x + dx[k]; int y = n.y + dy[k]; if (x == -1 || y == -1 || x == a || y==b) continue; if (board[x][y] == '*' && !visited[x][y]) { qu.offer(new Pair(x,y)); visited[x][y] = true; if (++cnt > 3) { bw.write("NO\n"); continue C; } } } } if (cnt!=3) { bw.write("NO\n"); continue C; } } } } for (boolean[] v : visited) Arrays.fill(v, false); for (int i=0;i<a;i++) { for (int j=0;j<b;j++) { if (board[i][j] == '*' && !visited[i][j]) { visited[i][j] = true; int cnt = 1; t[cnt -1] = new Pair(i, j); qu.offer(new Pair(i,j)); while(!qu.isEmpty()) { Pair n = qu.poll(); for (int k=0;k<4;k++) { int x = n.x + dx[k]; int y = n.y + dy[k]; if (x == -1 || y == -1 || x == a || y==b) continue; if (board[x][y] == '*' && !visited[x][y]) { qu.offer(new Pair(x,y)); visited[x][y] = true; t[cnt++] = new Pair(x,y); if (cnt > 3) { bw.write("NO\n"); continue C; } } } } if (cnt != 3) { bw.write("NO\n"); continue C; } try { if ((t[0].x == t[1].x && t[0].x == t[2].x) || (t[0].y == t[1].y && t[0].y == t[2].y)) { bw.write("NO\n"); continue C; } } catch (NullPointerException ex) { bw.write("NO\n"); continue C; } } } } bw.write("YES\n"); } bw.flush(); } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
1939f5320fb0244dffca7deb564647fa
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class F { static IOHandler sc = new IOHandler(); public static void main(String[] args) { // TODO Auto-generated method stub int testCases = sc.nextInt(); for (int i = 1; i <= testCases; ++i) { solve(i); } } private static void solve(int t) { int m = sc.nextInt(); int n = sc.nextInt(); char [][] arr = new char [m][]; for (int i = 0; i < m; ++i) { arr[i] = sc.next().toCharArray(); } int totalAsterisk = 0; int twos = 0; int ones = 0; int count = 0; int r,c; int [][] dirs = { {0,1}, {1, 0}, {-1,0}, {0, -1}}; int [][] dirs2 = { {0,1}, {1, 0}, {-1,0}, {0, -1}, {1,1}, {-1, 1}, {1,-1}, {-1, -1}}; int [][] countArr = new int [m][n]; int [][] groups = new int [m][n]; int idx = 1; List<int []> twoArr = new ArrayList<>(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (arr[i][j] != '*') continue; ++totalAsterisk; count = 0; for (int [] dir : dirs) { r = dir[0] + i; c = dir[1] + j; if (r < 0 || c < 0 || r >= m || c >= n) continue; else if (arr[r][c] != '*') continue; ++count; } countArr[i][j] = count; //System.out.println(count); if (count == 2) { ++twos; twoArr.add(new int [] {i , j} ); groups[i][j] = idx++; } else if (count == 1) ++ones; } } //System.out.println(ones + " " + twos + " " + totalAsterisk); if (ones != 2 * twos && totalAsterisk != ones + twos) { System.out.println("No"); return; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (arr[i][j] != '*' || countArr[i][j] == 2) continue; ++totalAsterisk; count = 0; for (int [] dir : dirs) { r = dir[0] + i; c = dir[1] + j; if (r < 0 || c < 0 || r >= m || c >= n) continue; else if (arr[r][c] != '*') continue; if (countArr[r][c] == 2) { groups[i][j] = groups[r][c]; } } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (arr[i][j] != '*') continue; if (groups[i][j] == 0) { System.out.println("No"); return; } count = 0; for (int [] dir : dirs2) { r = dir[0] + i; c = dir[1] + j; if (r < 0 || c < 0 || r >= m || c >= n) continue; else if (groups[r][c] == 0) continue; if (groups[r][c] != groups[i][j]) { System.out.println("No"); return; }else { ++count; } } if (count != 2) { System.out.println("No"); return; } } } System.out.println("Yes"); } private static class IOHandler { BufferedReader br; StringTokenizer st; public IOHandler() { 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()); } int [] readArray(int n) { int [] res = new int [n]; for (int i = 0; i < n; ++i) res[i] = nextInt(); return res; } 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
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
3d7c73ce3015946428aae38aa5ec2e31
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class Solution { static int M = 998_244_353 ; static Random rng = new Random(); public static boolean testCase(int n, int m, char[][] grid) { int[][] count = new int[n][m]; int curr = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == '*') { if (i < n - 1 && j < m - 1 && grid[i][j + 1] == '*' && grid[i + 1][j + 1] == '*') { grid[i][j] = '#'; grid[i][j + 1] = '#'; grid[i + 1][j + 1] = '#'; count[i][j] = curr; count[i][j + 1] = curr; count[i + 1][j + 1] = curr; curr++; } else if (i < n - 1 && j < m - 1 && grid[i][j + 1] == '*' && grid[i + 1][j] == '*') { grid[i][j] = '#'; grid[i][j + 1] = '#'; grid[i + 1][j] = '#'; count[i][j] = curr; count[i][j + 1] = curr; count[i + 1][j] = curr; curr++; } else if (i < n - 1 && j < m - 1 && grid[i + 1][j] == '*' && grid[i + 1][j + 1] == '*') { grid[i][j] = '#'; grid[i + 1][j] = '#'; grid[i + 1][j + 1] = '#'; count[i][j] = curr; count[i + 1][j] = curr; count[i + 1][j + 1] = curr; curr++; } else if (i < n - 1 && j > 0 && grid[i + 1][j] == '*' && grid[i + 1][j - 1] == '*') { grid[i][j] = '#'; grid[i + 1][j] = '#'; grid[i + 1][j - 1] = '#'; count[i][j] = curr; count[i + 1][j] = curr; count[i + 1][j - 1] = curr; curr++; } else { return false; } } } //print(grid); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (count[i][j] != 0) { for (int i1 = Math.max(0, i - 1); i1 <= Math.min(n - 1, i + 1); i1++) { for (int j1 = Math.max(0, j - 1); j1 <= Math.min(m - 1, j + 1); j1++) { if (count[i1][j1] != 0 && count[i1][j1] != count[i][j]) { return false; } } } } } } return true; } private static void print(char[][] arr) { for (char[] a : arr) { System.out.println(new String(a)); } } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. for (int t0 = 1; t0 <= t; ++t0) { int n = in.nextInt(), m = in.nextInt(); char[][] grid = new char[n][m]; for (int i = 0; i < n; i++) { grid[i] = in.next().toCharArray(); } out.println(testCase(n, m, grid) ? "YES" : "NO"); } out.close(); } private 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(); } boolean hasNext() { return st.hasMoreTokens(); } char[] readCharArray(int n) { char[] arr = new char[n]; try { br.read(arr); br.readLine(); } catch (IOException e) { e.printStackTrace(); } return arr; } 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[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } private static void sort(int[] arr) { int temp, idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static void sort(long[] arr) { long temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr, Comparator<? super T> cmp) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr, cmp); } private static <T extends Comparable<? super T>> void sort(List<T> list) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list); } private static <T> void sort(List<T> list, Comparator<? super T> cmp) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list, cmp); } static class DSU { int[] parent, rank; public DSU(int n) { parent = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; } } public int find(int i) { if (parent[i] == i) { return i; } else { int res = find(parent[i]); parent[i] = res; return res; } } public boolean isSameSet(int i, int j) { return find(i) == find(j); } public void union(int i, int j) { int iParent = find(i), jParent = find(j); if (iParent != jParent) { if (rank[iParent] > rank[jParent]) { parent[jParent] = iParent; } else { parent[iParent] = jParent; if (rank[iParent] == rank[jParent]) { rank[jParent]++; } } } } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
1d13ce558103f493f4a4e45ad13d882f
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer ST; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00"); final static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE, mod = (int) (1e9 + 7); final static long LMAX = Long.MAX_VALUE, LMIN = Long.MIN_VALUE; final static long INF = (long) 1e18, Neg_INF = (long) -1e18; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { n = readInt(); m = readInt(); arr = readMatrix(n, m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == '*') { if (i + 1 < n && j + 1 < m && arr[i + 1][j] == '*' && arr[i + 1][j + 1] == '*') { for (var d : d1) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i + 1][j] = arr[i + 1][j + 1] = '.'; } else if (i + 1 < n && j + 1 < m && arr[i][j + 1] == '*' && arr[i + 1][j + 1] == '*') { for (var d : d2) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i][j + 1] = arr[i + 1][j + 1] = '.'; } else if (i + 1 < n && j > 0 && arr[i + 1][j - 1] == '*' && arr[i + 1][j] == '*') { for (var d : d3) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i + 1][j - 1] = arr[i + 1][j] = '.'; } else if (i + 1 < n && j + 1 < m && arr[i][j + 1] == '*' && arr[i + 1][j] == '*') { for (var d : d4) { int x = i + d[0], y = j + d[1]; if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '*') { no(); return; } } arr[i][j] = arr[i][j + 1] = arr[i + 1][j] = '.'; } else { no(); return; } } } } yes(); } static int n, m; static char[][] arr; static int[][] d1 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 0, 2 }, { 1, -1 }, { 1, 2 }, { 2, -1 }, { 2, 0 }, { 2, 1 }, { 2, 2 } }; static int[][] d2 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { -1, 2 }, { 0, -1 }, { 0, 2 }, { 1, -1 }, { 1, 0 }, { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } }; static int[][] d3 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -2 }, { 0, -1 }, { 0, 1 }, { 1, -2 }, { 1, 1 }, { 2, -2 }, { 2, -1 }, { 2, 0 }, { 2, 1 } }; static int[][] d4 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { -1, 2 }, { 0, -1 }, { 0, 2 }, { 1, -1 }, { 1, 1 }, { 1, 2 }, { 2, -1 }, { 2, 0 }, { 2, 1 } }; private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java && java CodeForces // change Stack size -> java -Xss16M CodeForces.java // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } BR = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (ST == null || !ST.hasMoreTokens()) ST = new StringTokenizer(readLine()); return ST.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return BR.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray() throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(long a, long b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static long multiply(long a, long b) { return (((a % mod) * (b % mod)) % mod); } private static long divide(long a, long b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } boolean insert(String word) { Node curr = root; boolean ans = true; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; if (curr.isEnd) ans = false; } curr.isEnd = true; return ans; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== FENWICK TREE ================================ static class FT { int n; int[] arr; int[] tree; FT(int[] arr, int n) { this.arr = arr; this.n = n; this.tree = new int[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } FT(int n) { this.n = n; this.tree = new int[n + 1]; } // 1 based indexing void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } // 1 based indexing int query(int l, int r) { return getSum(r) - getSum(l - 1); } int getSum(int idx) { int ans = 0; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
95c7c6feb28d3c1d0ee69fe559d3ffa6
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class F1722 { static int[] dx; static int[] dy; static boolean[][] seen; static boolean[][] arr; static int[][] val; public static void main(String[] args) throws IOException{ StringBuffer ans = new StringBuffer(); StringTokenizer st; BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(; t > 0; t--){ dx = new int[]{-1,0,0,1}; dy = new int[]{0,1,-1,0}; st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); arr = new boolean[n][m]; val = new int[n][m]; for(int i = 0; i < n; i++){ st = new StringTokenizer(f.readLine()); String str = st.nextToken(); for(int x = 0; x < m; x++){ if(str.charAt(x) == '*') arr[i][x] = true; } } int curr = 1; seen = new boolean[n][m]; boolean broken = false; for(int i = 0; i < n; i++){ for(int x = 0; x < m; x++){ if(i != 0 && i < n-1 && arr[i][x] && arr[i-1][x] && arr[i+1][x]) broken = true; if(x != 0 && x < m-1 && arr[i][x] && arr[i][x-1] && arr[i][x+1]) broken = true; if(!seen[i][x] && arr[i][x]){ val[i][x] = curr; if(dfs(i,x,curr) != 3) broken = true; curr++; } } } //System.out.println(broken); dx = new int[]{0,0,1,1,1,-1,-1,-1}; dy = new int[]{1,-1,0,1,-1,-1,0,1}; for(int i = 0; i < n; i++){ for(int x = 0; x < m; x++){ if(!arr[i][x]) continue; for(int j = 0; j < 8; j++){ int ni = dx[j]+i; int nx = dy[j]+x; if(ni < 0 || nx < 0 || ni == n || nx == m) continue; if(val[i][x] < val[ni][nx]) broken = true; } } } //for(int i = 0; i < n; i++) System.out.println(Arrays.toString(val[i])); if(broken) ans.append("NO").append("\n"); else ans.append("YES").append("\n"); } System.out.println(ans); f.close(); } public static int dfs(int i, int x, int curr){ seen[i][x] = true; int hmany = 1; for(int p = 0; p < 4; p++){ int ji = i+dx[p]; int jy = x+dy[p]; if(ji >= 0 && jy >= 0 && ji < arr.length && jy < arr[0].length && !seen[ji][jy] && arr[ji][jy]){ val[ji][jy] = curr; hmany+=dfs(ji,jy,curr); } } return hmany; } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
59b10c05752eed93a12f32f774266475
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { public static void main(String[]args) throws IOException { new Main().run(); } void run() throws IOException{ new solve().setIO(System.in, System.out).run(); } public class solve extends ioTask{ int t,n,i,j,len,h,m,k; char[][][]all= { { {' ','.','.','.'}, {'.','.','*','.'}, {'.','*','*','.'}, {'.','.','.','.'} }, { {'.','.','.',' '}, {'.','*','.','.'}, {'.','*','*','.'}, {'.','.','.','.'} }, { {'.','.','.','.'}, {'.','*','*','.'}, {'.','*','.','.'}, {'.','.','.',' '} }, { {'.','.','.','.'}, {'.','*','*','.'}, {'.','.','*','.'}, {' ','.','.','.'} }, }; char mp[][]; boolean judge(int a,int b,int k) { for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(all[k][i][j]!=' '&&all[k][i][j]!=mp[a+i][b+j]) { return false; } } } return true; } void fill(int a,int b) { for(int i=1;i<=2;i++) { for(int j=1;j<=2;j++) { mp[a+i][b+j]='.'; } } } void judge(int a,int b) { for(int k=0;k<4;k++) { if(judge(a,b,k)) { fill(a,b); return; } } } public void run() throws IOException { t=in.in(); while(t-->0){ n=in.in(); m=in.in(); mp=new char[n+2][]; mp[0]=new char[m+2]; Arrays.fill(mp[0], '.'); mp[n+1]=new char[m+2]; Arrays.fill(mp[n+1], '.'); for(int i=1;i<=n;i++){ mp[i]=("."+in.ins()+".").toCharArray(); } n-=2; m-=2; for(int i=0;i<=n;i++) { for(int j=0;j<=m;j++) { judge(i,j); } } n+=2; m+=2; boolean ans=true; ss:for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(mp[i][j]=='*') { ans=false; break ss; } } } out.println(ans?"YES":"NO"); } out.close(); } } class In{ private StringTokenizer in=new StringTokenizer(""); private InputStream is; private BufferedReader bf; public In(File file) throws IOException { is=new FileInputStream(file); init(); } public In(InputStream is) throws IOException { this.is=is; init(); } private void init() throws IOException { bf=new BufferedReader(new InputStreamReader(is)); } boolean hasNext() throws IOException { return in.hasMoreTokens()||bf.ready(); } String ins() throws IOException { while(!in.hasMoreTokens()) { in=new StringTokenizer(bf.readLine()); } return in.nextToken(); } int in() throws IOException { return Integer.parseInt(ins()); } long inl() throws IOException { return Long.parseLong(ins()); } double ind() throws IOException { return Double.parseDouble(ins()); } } class Out{ PrintWriter out; private OutputStream os; private void init() { out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os))); } public Out(File file) throws IOException { os=new FileOutputStream(file); init(); } public Out(OutputStream os) throws IOException { this.os=os; init(); } } class graph{ int[]to,nxt,head,x; int cnt; void init(int n) { cnt=1; for(int i=1;i<=n;i++) { head[i]=0; } } public graph(int n,int m) { to=new int[m+1]; nxt=new int[m+1]; head=new int[n+1]; x=new int[m+1]; cnt=1; } void add(int u,int v,int x) { to[cnt]=v; nxt[cnt]=head[u]; this.x[cnt]=x; head[u]=cnt++; } } abstract class ioTask{ In in; PrintWriter out; public ioTask setIO(File in,File out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } public ioTask setIO(File in,OutputStream out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } public ioTask setIO(InputStream in,OutputStream out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } public ioTask setIO(InputStream in,File out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } void run()throws IOException{ } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
0e3506ad17bc54446f201f6c285af045
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static final int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static final long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static final long mod = (long) 1e9 + 7; static void solve() { int caseNo = 1; for (int T = sc.nextInt(); T > 1; T--, caseNo++) { solveIt(caseNo); } solveIt(caseNo); } // Solution static int dirs[][] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } }; static int n, m; static void solveIt(int testCaseNo) { n = sc.nextInt(); m = sc.nextInt(); char a[][] = new char[n + 2][m + 2]; for (int i = 1; i <= n; i++) { char x[] = sc.readCharArray(m); for (int j = 1; j <= m; j++) { a[i][j] = x[j - 1]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == '*' && a[i + 1][j] == '*' && a[i + 1][j + 1] == '*') { // * // ** int c[] = { 0 }; dfs(i, j, a, c); if (c[0] > 3) { System.out.println("NO"); return; } } if (a[i][j] == '*' && a[i][j + 1] == '*' && a[i - 1][j + 1] == '*') { // * // ** int c[] = { 0 }; dfs(i, j, a, c); if (c[0] > 3) { System.out.println("NO"); return; } } if (a[i][j] == '*' && a[i][j + 1] == '*' && a[i + 1][j + 1] == '*') { int c[] = { 0 }; dfs(i, j, a, c); if (c[0] > 3) { System.out.println("NO"); return; } } if (a[i][j] == '*' && a[i][j + 1] == '*' && a[i + 1][j] == '*') { int c[] = { 0 }; dfs(i, j, a, c); if (c[0] > 3) { System.out.println("NO"); return; } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == '*') { System.out.println("NO"); return; } } } System.out.println("YES"); } static void dfs(int i, int j, char a[][], int c[]) { if (i == 0 || i > n || j == 0 || j > m || a[i][j] != '*') return; a[i][j] = '#'; c[0]++; for (int d[] : dirs) { int x = i + d[0]; int y = j + d[1]; dfs(x, y, a, c); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); sc.tr(G - S + "ms"); } static class sc { private static boolean endOfFile() { if (bufferLength == -1) return true; int lptr = ptrbuf; while (lptr < bufferLength) { if (!isThisTheSpaceCharacter(inputBufffferBigBoi[lptr++])) return false; } try { is.mark(1000); while (true) { int b = is.read(); if (b == -1) { is.reset(); return true; } else if (!isThisTheSpaceCharacter(b)) { is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inputBufffferBigBoi = new byte[1024]; static int bufferLength = 0, ptrbuf = 0; private static int justReadTheByte() { if (bufferLength == -1) throw new InputMismatchException(); if (ptrbuf >= bufferLength) { ptrbuf = 0; try { bufferLength = is.read(inputBufffferBigBoi); } catch (IOException e) { throw new InputMismatchException(); } if (bufferLength <= 0) return -1; } return inputBufffferBigBoi[ptrbuf++]; } private static boolean isThisTheSpaceCharacter(int c) { return !(c >= 33 && c <= 126); } private static int skipItBishhhhhhh() { int b; while ((b = justReadTheByte()) != -1 && isThisTheSpaceCharacter(b)); return b; } private static double nextDouble() { return Double.parseDouble(next()); } private static char nextChar() { return (char) skipItBishhhhhhh(); } private static String next() { int b = skipItBishhhhhhh(); StringBuilder sb = new StringBuilder(); while (!(isThisTheSpaceCharacter(b))) { sb.appendCodePoint(b); b = justReadTheByte(); } return sb.toString(); } private static char[] readCharArray(int n) { char[] buf = new char[n]; int b = skipItBishhhhhhh(), p = 0; while (p < n && !(isThisTheSpaceCharacter(b))) { buf[p++] = (char) b; b = justReadTheByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readCharMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = readCharArray(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } private static int nextInt() { int num = 0, b; boolean minus = false; while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if (b == '-') { minus = true; b = justReadTheByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = justReadTheByte(); } } private static long nextLong() { long num = 0; int b; boolean minus = false; while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if (b == '-') { minus = true; b = justReadTheByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = justReadTheByte(); } } private static void tr(Object... o) { if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o)); } } } // And I wish you could sing along, But this song is a joke, and the melody I // wrote, wrong
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
f72c9b28aa37d69d015875076bbe4d19
train_109.jsonl
1661871000
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.
256 megabytes
import java.io.*; import java.util.*; public class R817F { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); loop:while(t-->0){ int n = scan.nextInt(); int m = scan.nextInt(); char[][] board =new char[n][m]; for(int i = 0;i<n;i++){ board[i] = scan.next().toCharArray(); } int[][] nums = new int[n][m]; int curr = 1; for(int i = 0;i<n-1;i++){ for(int j = 0;j<m-1;j++){ int count = 0; count+=board[i][j] == '*'?1:0; count+=board[i][j+1] == '*'?1:0; count+=board[i+1][j] == '*'?1:0; count+=board[i+1][j+1] == '*'?1:0; if(count == 3){ if(board[i][j] == '*')nums[i][j] = curr; if(board[i][j+1] == '*')nums[i][j+1] = curr; if(board[i+1][j] == '*')nums[i+1][j] = curr; if(board[i+1][j+1] == '*')nums[i+1][j+1] = curr; curr++; } } } int[] dr = {1,1,1,-1,-1,-1,0,0}; int[] dc = {1,-1,0,1,-1,0,1,-1}; for(int i = 0;i<n;i++){ for(int j =0 ;j<m;j++){ if(board[i][j] == '*' && nums[i][j] == 0){ System.out.println("NO"); continue loop; } if(board[i][j] != '*')continue; for(int k = 0;k<8;k++){ int newi = i+dr[k]; int newj = j+dc[k]; if(newi>=0 && newj>=0 && newi<n && newj<m){ if(nums[newi][newj]!=0 && nums[newi][newj] != nums[i][j]){ System.out.println("NO"); continue loop; } } } } } System.out.println("YES"); } } static class JS { 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 JS() { in = new BufferedInputStream(System.in, BS); } public JS(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 + 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(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"]
1 second
["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"]
null
Java 11
standard input
[ "dfs and similar", "implementation" ]
6a06ad39dbdd97ca8654023009c89a42
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.
1,700
For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
standard output
PASSED
c6da341874c1c0493cf278d393b02079
train_109.jsonl
1629815700
Note that the memory limit in this problem is lower than in others.You have a vertical strip with $$$n$$$ cells, numbered consecutively from $$$1$$$ to $$$n$$$ from top to bottom.You also have a token that is initially placed in cell $$$n$$$. You will move the token up until it arrives at cell $$$1$$$.Let the token be in cell $$$x &gt; 1$$$ at some moment. One shift of the token can have either of the following kinds: Subtraction: you choose an integer $$$y$$$ between $$$1$$$ and $$$x-1$$$, inclusive, and move the token from cell $$$x$$$ to cell $$$x - y$$$. Floored division: you choose an integer $$$z$$$ between $$$2$$$ and $$$x$$$, inclusive, and move the token from cell $$$x$$$ to cell $$$\lfloor \frac{x}{z} \rfloor$$$ ($$$x$$$ divided by $$$z$$$ rounded down). Find the number of ways to move the token from cell $$$n$$$ to cell $$$1$$$ using one or more shifts, and print it modulo $$$m$$$. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding).
128 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B { static int mod; static int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } static void solve() throws Exception { int n = scanInt(); mod = scanInt(); int ans[] = new int[n]; ans[0] = 1; ans[1] = mod - 1; for (int i = 0; i < n - 1; i++) { int v = ans[i]; int c = i + 1; ans[c] = add(ans[c], v); for (int m = c + c, a = 2; m <= n; m += c, a++) { ans[m - 1] = add(ans[m - 1], v); if (m + a <= n && v != 0) { ans[m + a - 1] = add(ans[m + a - 1], mod - v); } } ans[i + 1] = add(ans[i + 1], ans[i]); } out.print(ans[n - 1]); } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
Java
["3 998244353", "5 998244353", "42 998244353", "787788 100000007"]
6 seconds
["5", "25", "793019428", "94810539"]
NoteIn the first test, there are three ways to move the token from cell $$$3$$$ to cell $$$1$$$ in one shift: using subtraction of $$$y = 2$$$, or using division by $$$z = 2$$$ or $$$z = 3$$$.There are also two ways to move the token from cell $$$3$$$ to cell $$$1$$$ via cell $$$2$$$: first subtract $$$y = 1$$$, and then either subtract $$$y = 1$$$ again or divide by $$$z = 2$$$.Therefore, there are five ways in total.
Java 11
standard input
[ "dp" ]
77443424be253352aaf2b6c89bdd4671
The only line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 4 \cdot 10^6$$$; $$$10^8 &lt; m &lt; 10^9$$$; $$$m$$$ is a prime number) — the length of the strip and the modulo.
1,900
Print the number of ways to move the token from cell $$$n$$$ to cell $$$1$$$, modulo $$$m$$$.
standard output
PASSED
5f6a67382f27c79befc3a5e29bf09c54
train_109.jsonl
1629815700
Alice and Borys are playing tennis.A tennis match consists of games. In each game, one of the players is serving and the other one is receiving.Players serve in turns: after a game where Alice is serving follows a game where Borys is serving, and vice versa.Each game ends with a victory of one of the players. If a game is won by the serving player, it's said that this player holds serve. If a game is won by the receiving player, it's said that this player breaks serve.It is known that Alice won $$$a$$$ games and Borys won $$$b$$$ games during the match. It is unknown who served first and who won which games.Find all values of $$$k$$$ such that exactly $$$k$$$ breaks could happen during the match between Alice and Borys in total.
512 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Math.min; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { static void solve() throws Exception { int tests = scanInt(); for (int test = 0; test < tests; test++) { int a = scanInt(), b = scanInt(), c = (a + b) / 2; if ((a + b) % 2 == 0) { int min = a + b - min(a, c) - min(b, c); int max = min(a, c) + min(b, c); out.println((max - min) / 2 + 1); for (int i = min; i <= max; i += 2) { out.print(i + " "); } out.println(); } else { int min = a + b - min(a, c + 1) - min(b, c + 1); int max = min(a, c + 1) + min(b, c + 1); out.println(max - min + 1); for (int i = min; i <= max; i++) { out.print(i + " "); } out.println(); } } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
Java
["3\n2 1\n1 1\n0 5"]
2 seconds
["4\n0 1 2 3\n2\n0 2\n2\n2 3"]
NoteIn the first test case, any number of breaks between $$$0$$$ and $$$3$$$ could happen during the match: Alice holds serve, Borys holds serve, Alice holds serve: $$$0$$$ breaks; Borys holds serve, Alice holds serve, Alice breaks serve: $$$1$$$ break; Borys breaks serve, Alice breaks serve, Alice holds serve: $$$2$$$ breaks; Alice breaks serve, Borys breaks serve, Alice breaks serve: $$$3$$$ breaks. In the second test case, the players could either both hold serves ($$$0$$$ breaks) or both break serves ($$$2$$$ breaks).In the third test case, either $$$2$$$ or $$$3$$$ breaks could happen: Borys holds serve, Borys breaks serve, Borys holds serve, Borys breaks serve, Borys holds serve: $$$2$$$ breaks; Borys breaks serve, Borys holds serve, Borys breaks serve, Borys holds serve, Borys breaks serve: $$$3$$$ breaks.
Java 11
standard input
[ "math" ]
04a37e9c68761f9a2588c0bbecad2147
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). Description of the test cases follows. Each of the next $$$t$$$ lines describes one test case and contains two integers $$$a$$$ and $$$b$$$ ($$$0 \le a, b \le 10^5$$$; $$$a + b &gt; 0$$$) — the number of games won by Alice and Borys, respectively. It is guaranteed that the sum of $$$a + b$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case print two lines. In the first line, print a single integer $$$m$$$ ($$$1 \le m \le a + b + 1$$$) — the number of values of $$$k$$$ such that exactly $$$k$$$ breaks could happen during the match. In the second line, print $$$m$$$ distinct integers $$$k_1, k_2, \ldots, k_m$$$ ($$$0 \le k_1 &lt; k_2 &lt; \ldots &lt; k_m \le a + b$$$) — the sought values of $$$k$$$ in increasing order.
standard output
PASSED
48871a5058f896351269e3e5c8507ab2
train_109.jsonl
1629815700
In a certain video game, the player controls a hero characterized by a single integer value: power.On the current level, the hero got into a system of $$$n$$$ caves numbered from $$$1$$$ to $$$n$$$, and $$$m$$$ tunnels between them. Each tunnel connects two distinct caves. Any two caves are connected with at most one tunnel. Any cave can be reached from any other cave by moving via tunnels.The hero starts the level in cave $$$1$$$, and every other cave contains a monster.The hero can move between caves via tunnels. If the hero leaves a cave and enters a tunnel, he must finish his movement and arrive at the opposite end of the tunnel.The hero can use each tunnel to move in both directions. However, the hero can not use the same tunnel twice in a row. Formally, if the hero has just moved from cave $$$i$$$ to cave $$$j$$$ via a tunnel, he can not head back to cave $$$i$$$ immediately after, but he can head to any other cave connected to cave $$$j$$$ with a tunnel.It is known that at least two tunnels come out of every cave, thus, the hero will never find himself in a dead end even considering the above requirement.To pass the level, the hero must beat the monsters in all the caves. When the hero enters a cave for the first time, he will have to fight the monster in it. The hero can beat the monster in cave $$$i$$$ if and only if the hero's power is strictly greater than $$$a_i$$$. In case of beating the monster, the hero's power increases by $$$b_i$$$. If the hero can't beat the monster he's fighting, the game ends and the player loses.After the hero beats the monster in cave $$$i$$$, all subsequent visits to cave $$$i$$$ won't have any consequences: the cave won't have any monsters, and the hero's power won't change either.Find the smallest possible power the hero must start the level with to be able to beat all the monsters and pass the level.
512 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Math.max; import static java.lang.System.arraycopy; import static java.lang.System.exit; import static java.util.Arrays.copyOf; import static java.util.Arrays.fill; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.util.StringTokenizer; public class E { static class IntList { int data[] = new int[3]; int size = 0; boolean isEmpty() { return size == 0; } int size() { return size; } int get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } return data[index]; } void clear() { size = 0; } void set(int index, int value) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } data[index] = value; } void expand() { if (size >= data.length) { data = copyOf(data, (data.length << 1) + 1); } } void insert(int index, int value) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(); } expand(); arraycopy(data, index, data, index + 1, size++ - index); data[index] = value; } int delete(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } int value = data[index]; arraycopy(data, index + 1, data, index, --size - index); return value; } void push(int value) { expand(); data[size++] = value; } int pop() { if (size == 0) { throw new NoSuchElementException(); } return data[--size]; } void unshift(int value) { expand(); arraycopy(data, 0, data, 1, size++); data[0] = value; } int shift() { if (size == 0) { throw new NoSuchElementException(); } int value = data[0]; arraycopy(data, 1, data, 0, --size); return value; } } static void solve() throws Exception { int tests = scanInt(); for (int test = 0; test < tests; test++) { int n = scanInt(), m = scanInt(), a[] = new int[n], b[] = new int[n], maxA = 0; for (int i = 1; i < n; i++) { a[i] = scanInt(); maxA = max(maxA, a[i]); } for (int i = 1; i < n; i++) { b[i] = scanInt(); } IntList edges[] = new IntList[n]; for (int i = 0; i < n; i++) { edges[i] = new IntList(); } for (int i = 0; i < m; i++) { int u = scanInt() - 1, v = scanInt() - 1; edges[u].push(v); edges[v].push(u); } int status[] = new int[n]; long strength[] = new long[n]; int stack[] = new int[n]; int l = 1, r = maxA; bs: while (l < r) { int mid = (l + r + 1) >> 1; long str = mid; status[0] = -2; fill(status, 1, n, -1); int left = n - 1; while (left > 0) { int stackSize = 0; int b1, b2; bb: { for (int cur = 0; cur < n; cur++) { if (status[cur] == -2) { IntList e = edges[cur]; for (int i = 0; i < e.size; i++) { int next = e.data[i]; if (a[next] >= str) { continue; } if (status[next] == -1) { status[next] = cur; strength[next] = str + b[next]; stack[stackSize++] = next; } else if (status[next] >= 0) { b1 = next; b2 = cur; break bb; } } } } while (stackSize > 0) { int cur = stack[--stackSize]; int prev = status[cur]; long cstr = strength[cur]; IntList e = edges[cur]; for (int i = 0; i < e.size; i++) { int next = e.data[i]; if (next == prev || a[next] >= cstr) { continue; } if (status[next] == -1) { status[next] = cur; strength[next] = cstr + b[next]; stack[stackSize++] = next; } else { b1 = next; b2 = cur; break bb; } } } l = mid; continue bs; } while (status[b1] >= 0) { str += b[b1]; int nb1 = status[b1]; status[b1] = -2; --left; b1 = nb1; } while (status[b2] >= 0) { str += b[b2]; int nb2 = status[b2]; status[b2] = -2; --left; b2 = nb2; } for (int i = 0; i < n; i++) { if (status[i] != -2) { status[i] = -1; } } } r = mid - 1; } out.println(l + 1); } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
Java
["3\n4 4\n11 22 13\n8 7 5\n1 2\n2 3\n3 4\n4 1\n4 4\n11 22 13\n5 7 8\n1 2\n2 3\n3 4\n4 1\n5 7\n10 40 20 30\n7 2 10 5\n1 2\n1 5\n2 3\n2 4\n2 5\n3 4\n4 5"]
5 seconds
["15\n15\n19"]
NoteIn the first test case, the hero can pass the level with initial power $$$15$$$ as follows: move from cave $$$1$$$ to cave $$$2$$$: since $$$15 &gt; 11$$$, the hero beats the monster, and his power increases to $$$15 + 8 = 23$$$; move from cave $$$2$$$ to cave $$$3$$$: since $$$23 &gt; 22$$$, the hero beats the monster, and his power increases to $$$23 + 7 = 30$$$; move from cave $$$3$$$ to cave $$$4$$$: since $$$30 &gt; 13$$$, the hero beats the monster, and his power increases to $$$30 + 5 = 35$$$. In the second test case, the situation is similar except that the power increases for beating monsters in caves $$$2$$$ and $$$4$$$ are exchanged. The hero can follow a different route, $$$1 \rightarrow 4 \rightarrow 3 \rightarrow 2$$$, and pass the level with initial power $$$15$$$.In the third test case, the hero can pass the level with initial power $$$19$$$ as follows: move from cave $$$1$$$ to cave $$$2$$$: since $$$19 &gt; 10$$$, the hero beats the monster, and his power increases to $$$19 + 7 = 26$$$; move from cave $$$2$$$ to cave $$$4$$$: since $$$26 &gt; 20$$$, the hero beats the monster, and his power increases to $$$26 + 10 = 36$$$; move from cave $$$4$$$ to cave $$$5$$$: since $$$36 &gt; 30$$$, the hero beats the monster, and his power increases to $$$36 + 5 = 41$$$; move from cave $$$5$$$ to cave $$$2$$$: there is no monster in this cave anymore, nothing happens; move from cave $$$2$$$ to cave $$$3$$$: since $$$41 &gt; 40$$$, the hero beats the monster, and his power increases to $$$41 + 2 = 43$$$.
Java 11
standard input
[ "graphs" ]
787ad7de581891696681fbc19cd7116a
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 1000$$$; $$$n \le m \le min(\frac{n(n-1)}{2}, 2000)$$$) — the number of caves and tunnels. The second line contains $$$n-1$$$ integers $$$a_2, a_3, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — values the hero's power are compared to while fighting monsters in caves $$$2, 3, \ldots, n$$$. The third line contains $$$n-1$$$ integers $$$b_2, b_3, \ldots, b_n$$$ ($$$1 \le b_i \le 10^9$$$) — increases applied to the hero's power for beating monsters in caves $$$2, 3, \ldots, n$$$. Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \ne v_i$$$) — the numbers of caves connected with a tunnel. No two caves are connected with more than one tunnel. Any cave can be reached from any other cave by moving via tunnels. At least two tunnels come out of every cave. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$2000$$$.
3,000
For each test case print a single integer — the smallest possible power the hero must start the level with to be able to beat all the monsters and pass the level.
standard output
PASSED
4cf22d36d1a61c222a3c41e96850b9e7
train_109.jsonl
1629815700
Consider the insertion sort algorithm used to sort an integer sequence $$$[a_1, a_2, \ldots, a_n]$$$ of length $$$n$$$ in non-decreasing order.For each $$$i$$$ in order from $$$2$$$ to $$$n$$$, do the following. If $$$a_i \ge a_{i-1}$$$, do nothing and move on to the next value of $$$i$$$. Otherwise, find the smallest $$$j$$$ such that $$$a_i &lt; a_j$$$, shift the elements on positions from $$$j$$$ to $$$i-1$$$ by one position to the right, and write down the initial value of $$$a_i$$$ to position $$$j$$$. In this case we'll say that we performed an insertion of an element from position $$$i$$$ to position $$$j$$$.It can be noticed that after processing any $$$i$$$, the prefix of the sequence $$$[a_1, a_2, \ldots, a_i]$$$ is sorted in non-decreasing order, therefore, the algorithm indeed sorts any sequence.For example, sorting $$$[4, 5, 3, 1, 3]$$$ proceeds as follows: $$$i = 2$$$: $$$a_2 \ge a_1$$$, do nothing; $$$i = 3$$$: $$$j = 1$$$, insert from position $$$3$$$ to position $$$1$$$: $$$[3, 4, 5, 1, 3]$$$; $$$i = 4$$$: $$$j = 1$$$, insert from position $$$4$$$ to position $$$1$$$: $$$[1, 3, 4, 5, 3]$$$; $$$i = 5$$$: $$$j = 3$$$, insert from position $$$5$$$ to position $$$3$$$: $$$[1, 3, 3, 4, 5]$$$. You are given an integer $$$n$$$ and a list of $$$m$$$ integer pairs $$$(x_i, y_i)$$$. We are interested in sequences such that if you sort them using the above algorithm, exactly $$$m$$$ insertions will be performed: first from position $$$x_1$$$ to position $$$y_1$$$, then from position $$$x_2$$$ to position $$$y_2$$$, ..., finally, from position $$$x_m$$$ to position $$$y_m$$$.How many sequences of length $$$n$$$ consisting of (not necessarily distinct) integers between $$$1$$$ and $$$n$$$, inclusive, satisfy the above condition? Print this number modulo $$$998\,244\,353$$$.
512 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.System.exit; import static java.util.Arrays.fill; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class D { static class LongMinAddTree { final int n; final long t[], m[]; LongMinAddTree(int n) { this.n = n; t = new long[2 * n - 1]; m = new long[n - 1]; } void init(long v[]) { for (int i = 0; i < n; i++) { t[n - 1 + i] = v[i]; } for (int i = n - 1; i != 0; i--) { t[i - 1] = min(t[(i << 1) - 1], t[i << 1]); } fill(m, 0); } void fix(int i) { t[i - 1] = m[i - 1] + min(t[(i << 1) - 1], t[i << 1]); } void push(int i) { for (int j = 31 - Integer.numberOfLeadingZeros(i); j != 0; --j) { int p = i >> j; long v = m[p - 1]; if (v != 0) { if (p << 1 < n) { m[(p << 1) - 1] += v; } t[(p << 1) - 1] += v; if ((p << 1) + 1 < n) { m[p << 1] += v; } t[p << 1] += v; m[p - 1] = 0; } } } long get(int i) { long v = t[(i += n) - 1]; for (i >>= 1; i != 0; i >>= 1) { v += m[i - 1]; } return v; } long getMin(int l, int r) { long vl = Long.MAX_VALUE, vr = Long.MAX_VALUE; for (l += n, r += n;; l >>>= 1, r >>>= 1) { if (vl != Long.MAX_VALUE && l != 1) { vl += m[l - 2]; } if (vr != Long.MAX_VALUE) { vr += m[r - 1]; } if (l == r && (l & 1) != 0) { break; } if ((l & 1) != 0) { vl = min(vl, t[l++ - 1]); } if ((r & 1) != 0) { vr = min(vr, t[--r - 1]); } } long v = min(vl, vr); for (l >>>= 1; l != 0; l >>>= 1) { v += m[l - 1]; } return v; } void set(int i, long v) { push(i += n); t[i - 1] = v; while ((i >>= 1) != 0) { fix(i); } } void add(int l, int r, long v) { if (l == r) { return; } l += n; r += n; for (int cl = l, cr = r; cl != cr; cl >>>= 1, cr >>>= 1) { if ((cl & 1) != 0) { if (cl < n) { m[cl - 1] += v; } t[cl++ - 1] += v; } if ((cr & 1) != 0) { if (--cr < n) { m[cr - 1] += v; } t[cr - 1] += v; } } for (int i = Integer.numberOfTrailingZeros(l) + 1; l >>> i != 0; ++i) { fix(l >>> i); } for (int i = Integer.numberOfTrailingZeros(r) + 1; r >>> i != 0; ++i) { fix(r >>> i); } } } static void sortBy(int a[], int n, int v[]) { if (n == 0) { return; } for (int i = 1; i < n; i++) { int j = i; int ca = a[i]; int cv = v[ca]; do { int nj = (j - 1) >> 1; int na = a[nj]; if (cv <= v[na]) { break; } a[j] = na; j = nj; } while (j != 0); a[j] = ca; } int ca = a[0]; for (int i = n - 1; i > 0; i--) { int j = 0; while ((j << 1) + 2 + Integer.MIN_VALUE < i + Integer.MIN_VALUE) { j <<= 1; j += (v[a[j + 2]] > v[a[j + 1]]) ? 2 : 1; } if ((j << 1) + 2 == i) { j = (j << 1) + 1; } int na = a[i]; a[i] = ca; ca = na; int cv = v[ca]; while (j != 0 && v[a[j]] < cv) { j = (j - 1) >> 1; } while (j != 0) { na = a[j]; a[j] = ca; ca = na; j = (j - 1) >> 1; } } a[0] = ca; } static final int MOD = 998244353; static int mul(int a, int b) { int res = (int) ((long) a * b % MOD); return res < 0 ? res + MOD : res; } static int pow(int a, int e) { if (e == 0) { return 1; } int r = a; for (int i = 30 - Integer.numberOfLeadingZeros(e); i >= 0; i--) { r = mul(r, r); if ((e & (1 << i)) != 0) { r = mul(r, a); } } return r; } static int inv(int a) { return pow(a, MOD - 2); } static void solve() throws Exception { int tests = scanInt(); int facts[] = new int[400010]; facts[0] = 1; for (int i = 1; i < facts.length; i++) { facts[i] = mul(facts[i - 1], i); } for (int test = 0; test < tests; test++) { int n = scanInt(), m = scanInt(), x[] = new int[m], y[] = new int[m]; LongMinAddTree tree = new LongMinAddTree(max(m, 1)); for (int i = 0; i < m; i++) { x[i] = scanInt() - 1; y[i] = scanInt() - 1; tree.set(i, ((long) y[i] << 32) | (m - i)); } for (int i = 0; i < m; i++) { long v = tree.getMin(0, m); int j = m - (int) v; y[j] = (int) (v >> 32); tree.add(0, j, 1L << 32); tree.set(j, Long.MAX_VALUE / 2); } int idx[] = new int[m]; for (int i = 0; i < m; i++) { idx[i] = i; } sortBy(idx, m, y); int invs = 0; for (int i = 0, p = 0; i < m; i++) { int cx = x[idx[i]], cy = y[idx[i]]; if (cy > (i == 0 ? 0 : y[idx[i - 1]] + 1)) { int px = cy - 1 - i + p; while (p < m && px >= x[p]) { ++p; ++px; } if (cx < px) { ++invs; } } if (cy < n - 1) { int nx; if (i < m - 1 && y[idx[i + 1]] == cy + 1) { nx = x[idx[i + 1]]; } else { nx = cy - i + p; while (p < m && nx >= x[p]) { ++p; ++nx; } } if (nx < cx) { ++invs; } } } out.println(mul(facts[2 * n - invs - 1], inv(mul(facts[n], facts[n - 1 - invs])))); } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
Java
["3\n3 0\n3 2\n2 1\n3 1\n5 3\n3 1\n4 1\n5 3"]
3 seconds
["10\n1\n21"]
NoteIn the first test case, the algorithm performs no insertions — therefore, the initial sequence is already sorted in non-decreasing order. There are $$$10$$$ such sequences: $$$[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]$$$.In the second test case, the only sequence satisfying the conditions is $$$[3, 2, 1]$$$.In the third test case, $$$[4, 5, 3, 1, 3]$$$ is one of the sought sequences.
Java 11
standard input
[ "combinatorics" ]
45da2b93570e7d26d0e4fd3a3fca20b7
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$0 \le m &lt; n$$$) — the length of the sequence and the number of insertions. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$2 \le x_1 &lt; x_2 &lt; \ldots &lt; x_m \le n$$$; $$$1 \le y_i &lt; x_i$$$). These lines describe the sequence of insertions in chronological order. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Note that there is no constraint on the sum of $$$n$$$ of the same kind.
2,600
For each test case, print the number of sequences of length $$$n$$$ consisting of integers from $$$1$$$ to $$$n$$$ such that sorting them with the described algorithm produces the given sequence of insertions, modulo $$$998\,244\,353$$$.
standard output
PASSED
2765d39d738dcd3b7706560a92d4ac7b
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.util.*; public class NotShading{ public static void main(String args[]){ Scanner s=new Scanner(System.in); int test=s.nextInt(); for(int k=0;k<test;k++){ int n=s.nextInt(); int m=s.nextInt(); int r=s.nextInt(); int c=s.nextInt(); boolean case1=false; boolean case2=false; boolean case3=false; for(int i=1;i<n+1;i++){ String str=s.next(); for(int j=1;j<m+1;j++){ char curr= str.charAt(j-1); if(i==r && j==c){ if(Character.compare(curr,'B')==0){ case1=true; break; } } else if(i==r || j==c){ if (Character.compare(curr,'B')==0){ case2=true; //break; } } else{ if (Character.compare(curr,'B')==0){ case3=true; } } } } if(case1==true){ System.out.println(0); } else if(case2==true){ System.out.println(1); } else if(case3==true){ System.out.println(2); } else{ System.out.println(-1); } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
30be744c3a7c3bcfa7b201a2470d8f4d
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.*; /* Name of the class has to be "Main" only if the class is public. */ public class S1 { public static void main(String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0){ int n= sc.nextInt(); int m= sc.nextInt(); int r= (sc.nextInt())-1; int c= (sc.nextInt())-1; boolean black=false; int ans=2; for(int i=0;i<n;i++){ char[] arr=sc.next().toCharArray(); for(int j=0;j<m;j++){ if(arr[j]!='W'){ black=true; if(r==i&&c==j){ ans=0; }else if(r==i||c==j){ ans=Math.min(1,ans); } } } } System.out.println(black?ans:-1); } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
ff955064a3b418f82d75055a5205f70a
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.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Locale; import java.util.StringTokenizer; public class Solution implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new Solution(), "", 256 * (1L << 20)).start(); } public void run() { try { long t1 = System.currentTimeMillis(); if (true) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } Locale.setDefault(Locale.US); solve(); in.close(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } // solution void solve() throws IOException { int testCount = readInt(); for (int i=0; i<testCount;i++) { StringBuilder ans = new StringBuilder(); int n = readInt(); int m = readInt(); int r = readInt(); int c = readInt(); char[][] arr = new char[n][m]; int bCounter = 0; for (int j=0; j<n; j++) { char[] characters = readString().toCharArray(); for (int k=0; k<m; k++) { if (characters[k] == 'B') ++bCounter; arr[j][k] = characters[k]; } } r -= 1; c -= 1; if (arr[r][c] == 'B') { out.println(0); continue; } if (bCounter == 0) { out.println(-1); continue; } boolean t = false; for (int p=0; p<m; p++) { if (arr[r][p] == 'B') { out.println(1); t = true; break; } } if (t) continue; t = false; for (int p=0; p<n; p++) { if (arr[p][c] == 'B') { out.println(1); t = true; break; } } if (t) continue; out.println(2); } in.close(); 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 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
f72a2d0d3be51bf88273beb8eaf8b98c
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Chill800 { static BufferedReader br; static StringTokenizer st; static PrintWriter pw; static String nextToken() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(nextToken()); } static long nextLong() { return Long.parseLong(nextToken()); } static double nextDouble() { return Double.parseDouble(nextToken()); } static String nextLine() { try { return br.readLine(); } catch (IOException e) { throw new IllegalArgumentException(); } } static char nextChar() { try { return (char) br.read(); } catch (IOException e) { throw new IllegalArgumentException(e); } } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); int t = 1; t = nextInt(); while (t-- > 0) { solve(); } pw.close(); } private static void solve() { int n = nextInt(); int m = nextInt(); int r = nextInt(); int c = nextInt(); boolean br = false; boolean bc = false; boolean haveB = false; boolean flag = false; for (int i = 0; i < n; i++) { String s = nextToken(); for (int j = 0; j < m; j++) { boolean t = s.charAt(j)=='B'; if (t) haveB = true; if (i+1==r && t) br = true; if (j+1==c && t) bc = true; if (i+1==r && j+1==c && t) flag = true; } } if (flag) pw.println(0); else if (!haveB) pw.println(-1); else if (!br && !bc) pw.println(2); else pw.println(1); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
c0440a45a77bc1e2e511bd4a0c768e60
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_Not_Shading{ static Scanner in=new Scanner(System.in); static int n,m,r,c,testCases; static char a[][]; static void solve(){ int white=0,black=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][c-1]=='B' || a[r-1][j]=='B' ){ black++; } if(a[i][j]=='W'){ ++white; } } } if(a[r-1][c-1]=='B'){ System.out.println(0); }else if(white==n*m){ System.out.println(-1); }else if(a[r-1][c-1]=='W' && black>0 ){ System.out.println(1); }else{ System.out.println(2); } } public static void main(String [] amit){ testCases=in.nextInt(); for(int t=0;t<testCases;t++){ n=in.nextInt(); m=in.nextInt(); r=in.nextInt(); c=in.nextInt(); a=new char[n][m]; for(int i=0;i<n;i++){ a[i]=in.next().trim().toCharArray(); } solve(); } } } /* 1 3 5 1 4 WBWWW BBBWB WWBBB */
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
e40eb0571bbb272f22d1f69a04188cdf
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 static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class MacroTemplates { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); char[][] arr = new char[n][m]; for (int i = 0; i < n; i++) { arr[i] = infile.readLine().trim().toCharArray(); } if (arr[r - 1][c - 1] == 'B') { sb.append("0\n"); } else{ int res = 3; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(arr[i][j] == 'B') { res = min(res, 2); if (i == r - 1 || j == c - 1) { res = 1; } } if(res == 3){ res = -1; } sb.append(res + "\n"); } } 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 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
3501452937e9c4ee2c05c690c39d8c37
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.util.*; import java.io.*; //import static com.sun.tools.javac.jvm.ByteCodes.swap; // ?)(? public class fastTemp { static class Node { int data; Node left, right; Node(int data) { this.data=data; left=right=null; } }; static FastScanner fs = null; static ArrayList<Integer> graph[] ; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); int m = fs.nextInt(); int r = fs.nextInt(); int c = fs.nextInt(); char ch[][] = new char[n][m]; for(int i=0;i<n;i++){ String s = fs.next(); ch[i] = s.toCharArray(); } boolean fr = false; boolean fc=false; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(ch[i][j]=='B') { fr = true; if (i == (r - 1) || j == (c - 1)) { fc = true; } } } } if(ch[r-1][c-1]=='B'){ System.out.println(0); } else if(fc){ System.out.println(1); }else if(!fc && fr){ System.out.println(2); }else if(!fc && !fr){ System.out.println(-1); } } // } out.close(); } // JAVA program to compute factorial // of big numbers // This function finds factorial of // large numbers and prints them static void factorial(int n) { int res[] = new int[500]; // Initialize result res[0] = 1; int res_size = 1; // Apply simple factorial formula // n! = 1 * 2 * 3 * 4...*n for (int x = 2; x <= n; x++) res_size = multiply(x, res, res_size); System.out.println("Factorial of given number is "); for (int i = res_size - 1; i >= 0; i--) System.out.print(res[i]); } // This function multiplies x with the number // represented by res[]. res_size is size of res[] or // number of digits in the number represented by res[]. // This function uses simple school mathematics for // multiplication. This function may value of res_size // and returns the new value of res_size static int multiply(int x, int res[], int res_size) { int carry = 0; // Initialize carry // One by one multiply n with individual // digits of res[] for (int i = 0; i < res_size; i++) { int prod = res[i] * x + carry; res[i] = prod % 10; // Store last digit of // 'prod' in res[] carry = prod/10; // Put rest in carry } // Put carry in res and increase result size while (carry!=0) { res[res_size] = carry % 10; carry = carry / 10; res_size++; } return res_size; } static int m = 998244353; static long fact[] = new long[200001]; public static void fact(){ fact[1] = 1; fact[0] = 1; for(int i=2;i<=200000;i++){ fact[i] = (fact[i-1]*i)%m; } } public static int help(int val){ System.out.println("+ "+val); System.out.flush(); int x = fs.nextInt(); return x; } static int lower(int array[], int key,int i,int k) { int lowerBound = i; // Traversing the array using length function while (lowerBound < k) { // If key is lesser than current value if (key > array[lowerBound]) lowerBound++; // This is either the first occurrence of key // or value just greater than key else return lowerBound; } return lowerBound; } static int upper(int array[], int key ,int i , int k) { int upperBound = k; // Traversing the array using length function while (upperBound>=i) { // If key is lesser than current value if (key > array[upperBound]) upperBound--; // This is either the first occurrence of key // or value just greater than key else return upperBound; } return upperBound; } 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 z = o.x + o.y; int l = x+y; return l-z; } } public static boolean contains(String s,String s1){ boolean found = false; for(int i=0;i<s.length();i++){ for(int j = i+1;j<s.length();j++){ String ss = s.substring(i,j); if(ss.equals(s1)){ found = true; } } } return found; } // Math.ceil(k/n) == (k+n-1)/n //----------------------graph-------------------------------------------------- /* int vtces = fs.nextInt(); ArrayList<Integer> graph[] = new ArrayList[vtces]; for (int v = 0; v < vtces; v++) { graph[v] = new ArrayList<>(); } int edge = fs.nextInt(); for (int i = 0; i < edge; i++) { int u = fs.nextInt(); int v = fs.nextInt(); graph[u].add(v); graph[v].add(u); } */ //----------------------graph-------------------------------------------------- public static class String1 implements Comparable<String1>{ String str; int id; String1(String str , int id){ this.str = str; this.id = id; } public int compareTo(String1 o){ int i=0; while(i<str.length() && str.charAt(i)==o.str.charAt(i)){ i++; } if(i<str.length()){ if(i%2==1){ return o.str.compareTo(str); // descending order }else{ return str.compareTo(o.str); // ascending order } } return str.compareTo(o.str); } } 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()); } } // ------------------------------------------swap---------------------------------------------------------------------- static void swap(int arr[],int i,int j) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } //-------------------------------------------seiveOfEratosthenes---------------------------------------------------- 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 = 0; 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) // System.out.print(i + " "); // } } //------------------------------------------- power------------------------------------------------------------------ public static long power(int a , int b) { if (b == 0) { return 1; } else if (b == 1) { return a; } else { long R = power(a, b / 2); if (b % 2 != 0) { return (((power(a, b / 2))) * a * ((power(a, b / 2)))); } else { return ((power(a, b / 2))) * ((power(a, b / 2))); } } } //--------------------------------------lower bound---------------------------------------------------------- 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; } //-------------------------------------------------------------------------------------------------------------- public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } //---------------------------------------EXTENDED EUCLID ALGO-------------------------------------------------------- /* public static class Pair{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y ; } } */ public static Pair Euclid(int a,int b){ if(b==0){ return new Pair(1,0); // answer of x and y } Pair dash = Euclid(b,a%b); return new Pair(dash.y , dash.x - (a/b)*dash.y); } //--------------------------------GCD------------------GCD-----------GCD-------------------------------------------- public static long gcd(long a,long b){ if(b==0){ return a; } return gcd(b,a%b); } public static void BFS(ArrayList<Integer>[] graph) { } } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
a93bb283bfcb2cf9a40d3479bf8669a7
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(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); r--; c--; boolean[][] grid = new boolean[n][m]; boolean hasBlack = false; for (int j = 0; j < n; j++) { String line = sc.next(); for (int k = 0; k < m; k++) { grid[j][k] = (line.charAt(k) == 'B'); hasBlack |= grid[j][k]; } } if (!hasBlack) System.out.println(-1); else if (grid[r][c]) System.out.println(0); else { boolean ok = false; for (int j = 0; j < n; j++) ok |= grid[j][c]; for (int k = 0; k < m; k++) ok |= grid[r][k]; if (ok) System.out.println(1); else System.out.println(2); } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
a7766748ea581808406b114676077be3
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.lang.*; import java.io.*; import java.util.*; public class NotShading { 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 rows = reader.nextInt(), cols = reader.nextInt(), r = reader.nextInt(), c = reader.nextInt(); String []strs = new String[rows]; boolean isBPres = false; for(int rw = 0; rw<rows; rw++){ strs[rw] = reader.nextLine(); if(strs[rw].indexOf('B') >= 0)isBPres = true; } //if now Black in matrix if(!isBPres){ System.out.println(-1); continue; } //already black at rth row and cth col if(strs[r-1].charAt(c-1) == 'B'){ System.out.println(0); continue; } //same row if(strs[r-1].indexOf('B') >= 0){ System.out.println(1); continue; } //same col boolean sameCol = false; for(int rw = 0; rw<rows; rw++){ if(strs[rw].charAt(c-1) == 'B'){ sameCol = true; break; } } if(sameCol){ System.out.println(1); continue; } //up row if(r-2>=0){ if(strs[r-2].indexOf('B') >= 0){ System.out.println(2); continue; } } //down row if(r<=rows){ if(strs[r].indexOf('B') >= 0){ System.out.println(2); continue; } } //if present anywhere else if(isBPres){ System.out.println(2); continue; } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
c5aff7718cd08ce99724d6439e09f2cb
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 in = new BufferedReader(new InputStreamReader(System.in)); int tc = Integer.parseInt(in.readLine()); for(int x=0; x<tc; x++) { String ip[] = new String[1]; ip = in.readLine().split(" "); int n = Integer.parseInt(ip[0]); int m = Integer.parseInt(ip[1]); int r = Integer.parseInt(ip[2]); int c = Integer.parseInt(ip[3]); r-=1; c-=1; boolean [][]matrix = new boolean[n][m]; boolean hasBlack = false; for (int j = 0; j < n; j++) { String line = in.readLine(); for (int k = 0; k < m; k++) { matrix[j][k] = (line.charAt(k) == 'B'); hasBlack |= matrix[j][k]; } } if (!hasBlack) { System.out.println(-1); } else if (matrix[r][c]) { System.out.println(0); } else { boolean okay = false; for (int j = 0; j < n; j++) { okay |= matrix[j][c]; } for (int k = 0; k < m; k++) { okay |= matrix[r][k]; } if (okay) { System.out.println(1); } else { System.out.println(2); } } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
726316a63eac2af2cb4b0082f7eed444
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 scanner=new Scanner(System.in); int x=scanner.nextInt(); int[] re=new int[x]; for (int i = 0; i < x; i++) { int status=2; int flag=0; int a=scanner.nextInt(); int b= scanner.nextInt(); int c= scanner.nextInt(); int d= scanner.nextInt(); char[][] z=new char[a][b]; for (int j = 0; j < a; j++) { String s= scanner.next(); for (int k = 0; k < b; k++) { z[j][k]=s.charAt(k); } } if(z[c-1][d-1]=='B'){ status=0; flag=1; }else { for (int j = 0; j < a; j++) { for (int k = 0; k < b; k++) { if(z[j][k]=='B'){ flag=1; if(j==c-1||k==d-1){ status=1; break; } } } } } if(flag==0){ status=-1; } re[i]=status; } for (int i = 0; i < re.length; i++) { System.out.println(re[i]); } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
a4390ff4bf2ef13d74f3c9808b7659d3
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Main { public static void main(String args[])throws IOException { // System.setIn(new FileInputStream("Case.txt")); BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(ob.readLine()); while(t --> 0) { /* StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(ob.readLine()); int []ar = new int[n]; */ StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); char [][]mat = new char[n][m]; boolean exist = false; for(int i = 0;i < n; i++) { String s = ob.readLine(); for(int j = 0; j < m; j++) { mat[i][j] = s.charAt(j); if(s.charAt(j) == 'B')exist = true; } } if(mat[r-1][c-1] == 'B') { System.out.println(0); }else if(!exist){ System.out.println(-1); }else { boolean found = false; for(int i = 0; i < n; i++){ if(mat[i][c-1] == 'B') { found = true; } } for(int i = 0; i < m; i++) { if(mat[r-1][i] == 'B') { found = true; } } if(found) { System.out.println(1); }else { System.out.println(2); } } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
a4704b088eac1e32f405bd0440ab27ca
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{ static Scanner sc; public static void solve(){ int n=sc.nextInt(); int m=sc.nextInt(); int r=sc.nextInt(); int c=sc.nextInt(); char a[][]=new char[n][m]; int b=0; for(int i=0;i<n;i++){ String s=sc.next(); for(int j=0;j<s.length();j++){ a[i][j]=s.charAt(j); if(a[i][j]=='B') b++; } } /*for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ System.out.print(a[i][j]+" "); } System.out.println(); }*/ if(a[r-1][c-1]=='B'){System.out.println(0); return;} if(b==0){System.out.println(-1); return;} for(int i=0;i<m;i++){ if(a[r-1][i]=='B') {System.out.println(1); return;} } for(int i=0;i<n;i++){ if(a[i][c-1]=='B') {System.out.println(1); return;} } System.out.println(2); } public static void main(String args[]){ sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) solve(); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
a2d94648e4ae8d26d9eba57b3b079db7
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Codechef { static class FR{ BufferedReader br; StringTokenizer st; public FR() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] intArray(int n){ int a[] = new int[n]; for(int i=0; i<n; i++){ a[i] = Integer.parseInt(next()); } return a; } long[] longArray(int n){ long a[] = new long[n]; for(int i=0; i<n; i++){ a[i] = Long.parseLong(next()); } return a; } String[] stringArray(int n){ String a[] = new String[n]; for(int i=0; i<n; i++){ a[i] = next(); } return a; } void printArry(int a[]){ for(int i=0; i<a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); } void printArry(long a[]){ for(int i=0; i<a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); } void printArry(String a[]){ for(int i=0; i<a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static int UB(long[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(long[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static int UB(int[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(int[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } 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[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long[][] ncr(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%mod; } } return C; } 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 m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { int tc = 1; tc = sc.nextInt(); while(tc-->0) { int n=sc.nextInt(), m=sc.nextInt(), r=sc.nextInt(), c=sc.nextInt(); char[][] arr = new char[n][m]; for(int i=0; i<n; i++){ String s = sc.next(); for(int j=0; j<m; j++){ arr[i][j] = s.charAt(j); } } solve(arr, n, m, r-1, c-1); } System.out.println(sb); } public static void solve(char[][] arr, int n, int m, int r, int c){ if(arr[r][c]=='B'){ sb.append("0" + '\n'); return; } if(blackPresent(arr, n, m, r, c)){ sb.append("1"+'\n'); return; } int i=r+1, j=c+1; while(i<n && j<m){ if(blackPresent(arr, n, m, i, j)){ sb.append("2"+'\n'); return; } i++; j++; } i=r-1; j=c-1; while(i>=0 && j>=0){ if(blackPresent(arr, n, m, i, j)){ sb.append("2"+'\n'); return; } i--; j--; } i=r-1; j=c+1; while(i>=0 && j<m){ if(blackPresent(arr, n, m, i, j)){ sb.append("2"+'\n'); return; } i--; j++; } i=r+1; j=c-1; while(i<n && j>=0){ if(blackPresent(arr, n, m, i, j)){ sb.append("2"+'\n'); return; } i++; j--; } sb.append("-1"+'\n'); } public static boolean blackPresent(char[][] arr, int n, int m, int r, int c){ for(int i=0; i<m; i++){ if(arr[r][i]=='B') return true; } for(int i=0; i<n; i++){ if(arr[i][c]=='B') return true; } return false; } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
e72fecc7abe280b2d185111349993099
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 scan = new Scanner(System.in); int t=scan.nextInt(); int map[][] = new int[55][55]; for(int i = 0;i<t;i++){ int flag=0; int n=scan.nextInt(); int m=scan.nextInt(); int r=scan.nextInt()-1; int c=scan.nextInt()-1; for(int j=0;j<n;j++){ String str = scan.next(); String[] strarray=str.split(""); for(int k = 0;k<m;k++){ if(strarray[k].equals("W")){ //0表示白色 map[j][k]=0; }else{ flag++; //1表示黑色 map[j][k]=1; } } } if(flag==0){ System.out.println("-1"); continue; }else if(map[r][c]==1){ System.out.println("0"); continue; }else{ int flagq=0; for(int j=0;j<m;j++){ if(map[r][j]==1){ System.out.println("1"); flagq=1; break; } } if(flagq==0) for(int j=0;j<n;j++){ if(map[j][c]==1){ System.out.println("1"); flagq=1; break; } } if(flagq==0) System.out.println("2"); } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
0cea30507c17baa3fbd7e73e74bca612
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_Not_Shading { 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+1]; int[] col = new int[m+1]; int flag = 0, flag1 = 0; for(int i =1;i<=n;i++){ String str = scn.next(); int j= 1; for(j=1;j<=m;j++){ if (str.charAt(j-1) == 'B'){ if(r == i && j==c){ flag1 = 1; } if(flag1 == 1){ break; } flag = 1; row[i] = 1; col[j] = 1; } } } if(flag1 == 1) { System.out.println(0); continue; } if(flag == 0){ System.out.println(-1); continue; } flag = 0; for(int i=1;i<=n;i++){ if(i == r && row[i]==1){ System.out.println(1); flag = 1; } if(flag == 1) break; } if(flag==1) continue; for(int i=1;i<=m;i++){ if(i == c && col[i]==1){ System.out.println(1); flag = 1; } if(flag == 1) break; } if(flag==1) continue; System.out.println(2); } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
93df01b7c49f63f6735628b3e51195de
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { in = new FastReader(); out = new PrintWriter(System.out); int t = ni(); while (t-- > 0) { int n = ni(); int m = ni(); int r = ni() - 1; int c = ni() - 1; char[][] board = new char[n][m]; for(int i = 0; i < n; i++){ String line = n(); board[i] = line.toCharArray(); } check(board, n, m, r, c); } } public static void check(char[][] board, int n, int m, int r, int c){ if(board[r][c] == 'B'){ System.out.println(0); return; } //to check if black exists boolean flag = false; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(board[i][j] == 'B'){ flag = true; } } } if(!flag) { System.out.println(-1); return; } for(int i = 0; i < n; i++){ if(board[i][c] == 'B'){ System.out.println(1); return; } } for(int i = 0; i < m; i++){ if(board[r][i] == 'B'){ System.out.println(1); return; } } System.out.println(2); } void pre() throws Exception{} void hold(boolean b)throws Exception{if (!b)throw new Exception("Hold right there, Sparky!");} static boolean multipleTC = true; private static FastReader in; private static PrintWriter out; // static void run() throws Exception{ // in = new FastReader(); // out = new PrintWriter(System.out); // int T = (multipleTC)?ni():1; // // pre(); // // for(int t = 1; t<= T; t++)solve(t); // out.flush(); // out.close(); // } // public static void main(String[] args) throws Exception{ // new TOTSCR().run(); // } static int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } static void p(Object o) { out.print(o); } static void pn(Object o) { out.println(o); } static void pni(Object o) { out.println(o); out.flush(); } static String n()throws Exception{return in.next();} static String nln()throws Exception{return in.nextLine();} static int ni()throws Exception{return Integer.parseInt(in.next());} static long nl()throws Exception{return Long.parseLong(in.next());} static double nd()throws Exception{return Double.parseDouble(in.next());} static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
028258cabd95f67148c449c8cd987feb
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.io.*; import java.util.*; public class Main { public static void main(String[] args)throws IOException { // System.out.println("Hello World"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int testCases=Integer.parseInt(br.readLine()); while(testCases-- > 0){ // write code here String[] s=br.readLine().split(" "); int n=Integer.parseInt(s[0]); int m=Integer.parseInt(s[1]); int r=Integer.parseInt(s[2]); int c=Integer.parseInt(s[3]); int[][] a=new int[n][m]; int b=0; for(int i=0;i<n;i++) { String k=br.readLine(); for(int j=0;j<k.length();j++) { if(k.charAt(j)=='W') a[i][j]=0; else { a[i][j]=1; b++; } } } // for(int i=0;i<n;i++) // { // for(int j=0;j<m;j++) // { // System.out.print(a[i][j]+" "); // } // System.out.println(""); // } r-=1; c-=1; if(b==0) { System.out.println("-1"); continue; } if(a[r][c]==1) { System.out.println("0"); continue; } int f=0; for(int i=0;i<m;i++) { if(a[r][i]==1) { f=1; break; } } if(f==1) { System.out.println("1"); continue; } f=0; for(int i=0;i<n;i++) { if(a[i][c]==1) { f=1; break; } } if(f==1) { System.out.println("1"); continue; } System.out.println("2"); } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
d6564b93d7aae971fdfb5f292c7d08ad
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) throws Exception { 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(); int rezultat = -1; int brojac = 0; for(int i=0;i<m;i++){ String str=sc.next(); //System.out.println(str); for(int j=0;j<n;j++){ //System.out.println(i+" "+j+" "+str.charAt(j)); if(str.charAt(j)=='B'){ brojac++; //System.out.println(i+" "+j); if(i==r-1 && j==c-1){ rezultat=0; } if(i==r-1 || j==c-1){ if(rezultat!=0) rezultat=1; } } } } if(brojac>0 && rezultat==-1){ rezultat=2; } System.out.println(rezultat); } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
b2e78c68233136ff7ae437b0f63a2b0c
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 sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(), m = sc.nextInt(), r = sc.nextInt(), c = sc.nextInt(); sc.nextLine(); String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLine(); } int is = -1; if (a[r-1].charAt(c - 1) == 'B') is = 0; else { if (a[r - 1].contains("B")) is = 1; else { for (int i = 0; i < n; i++) if (a[i].charAt(c - 1) == 'B') { is = 1; break; } if (is == -1) { for (int i = 0; i < n; i++) if (a[i].contains("B")) { is = 2; break; } } } } System.out.println(is); t--; } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
7e61ee96c36100ac51866928d4f34f19
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class test { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { StringTokenizer tokenizer = new StringTokenizer(br.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); int r = Integer.parseInt(tokenizer.nextToken()); int c = Integer.parseInt(tokenizer.nextToken()); boolean blackFound = false; int result = -1; for (int i = 1; i <= n; i++) { String row = br.readLine(); for (int j = 1; j <= m; j++) { if (row.charAt(j - 1) == 'B') { blackFound = true; if (result != 0 && (i == r || j == c)) { if (i == r && j == c) result = 0; else result = 1; } } } } if (result == -1 && blackFound) pw.println(2); else pw.println(result); } pw.flush(); pw.close(); br.close(); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
f35043cf31eb0812e53c8779824b709e
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * https://codeforces.com/problemset/problem/1627/A * @author ey * */ public class P1627A { public static void main(String[] args) throws IOException{ BufferedReader io = new BufferedReader(new InputStreamReader(System.in)); Integer cnt = Integer.valueOf(io.readLine()); List<Integer> rs = new ArrayList<Integer>(); while (cnt > 0) { cnt--; String[] nm = io.readLine().split(" "); Integer n = Integer.valueOf(nm[0]); Integer m = Integer.valueOf(nm[1]); Integer r = Integer.valueOf(nm[2]); Integer c = Integer.valueOf(nm[3]); Boolean black = false; Boolean selfBlack = false; Boolean sameRow = false; Boolean sameCow = false; for(int i = 0; i < n; i++) { String input = io.readLine(); if (input.contains("B")) { black = true; } if (i == (r - 1) && input.charAt(c - 1) == 'B') { selfBlack = true; } else if (i == (r - 1) && input.indexOf("B") >= 0) { sameRow = true; } else if (input.charAt(c - 1) == 'B') { sameCow = true; } } if (!black) { rs.add(-1); } else if (selfBlack) { rs.add(0); } else if (sameRow || sameCow) { rs.add(1); } else { rs.add(2); } } for (Integer integer : rs) { System.out.println(integer); } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
6560507858e868c04cde5b51fc29e758
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 Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); sc.nextLine(); //to clear the buffer char[][] arr = new char[n][m]; boolean hasAnyBlack = false; for (int i = 0; i < n; i++) { String input = sc.nextLine(); for (int j = 0; j < m; j++) { arr[i][j] = input.charAt(j); if (arr[i][j] == 'B' && !hasAnyBlack) hasAnyBlack = true; } } solve(n, m, r, c, arr, hasAnyBlack); } } } /** * method to solve current cp problem */ private static void solve(int n, int m, int r, int c, char[][] arr, boolean hasAnyBlack) { //Base Case if (arr[r - 1][c - 1] == 'B') { System.out.println("0"); return; } else if (arr[r - 1][c - 1] != 'B' && !hasAnyBlack) { System.out.println("-1"); return; } else { //same row ? boolean hasBlackCellInSameRow = false; for (int i = r - 1, j = 0; j < m; j++) { if (arr[i][j] == 'B') { hasBlackCellInSameRow = true; break; } } if (hasBlackCellInSameRow) { System.out.println("1"); return; } //same column boolean hasBlackCellInSameColumn = false; for (int i = 0, j = c - 1; i < n; i++) { if (arr[i][j] == 'B') { hasBlackCellInSameColumn = true; break; } } if (hasBlackCellInSameColumn) { System.out.println("1"); return; } System.out.println("2"); return; } } /** * method to check whether the provided string is palindrome or not * * @param str - the string to check if it is palindrome or not * @return - true if the provided string is palindrome else false */ private static boolean isPalindrome(String str) { if (str.length() % 2 == 0) { for (int i = 0, j = str.length() - 1; i < j && j > i; i++, j--) { if (str.charAt(i) != str.charAt(j)) return false; } } else { for (int i = 0, j = str.length() - 1; i <= j && j >= i; i++, j--) { if (str.charAt(i) != str.charAt(j)) return false; } } return true; } /** * method to check whether the provided number is palindrome or not * * @param n - the number to check if it is palindrome or not * @return - true if the provided number is palindrome else false */ private static boolean isPalindrome(int n) { int tempN = n, reverseNumber = 0; while (tempN > 0) { reverseNumber = reverseNumber * 10 + (tempN % 10); tempN /= 10; } if (reverseNumber == n) return true; return false; } /** * method to check whether the provided number is palindrome or not * * @param n - the number to check if it is palindrome or not * @return - true if the provided number is palindrome else false */ private static boolean isPalindrome(long n) { long tempN = n, reverseNumber = 0L; while (tempN > 0L) { reverseNumber = reverseNumber * 10L + (tempN % 10L); tempN /= 10L; } if (reverseNumber == n) return true; return false; } /** * method to get max and second max element returned into * the form of an array **/ private static int[] getMaxAndSecondMax(int a, int b, int c, int d) { int[] arr = new int[2]; if (a > b && a > c && a > d) { arr[0] = a; if (b > c && b > d) arr[1] = b; else if (c > b && c > d) arr[1] = c; else if (d > b && d > c) arr[1] = d; } else if (b > a && b > c && b > d) { arr[0] = b; if (a > c && a > d) arr[1] = a; else if (c > a && c > d) arr[1] = c; else if (d > a && d > c) arr[1] = d; } else if (c > a && c > b && c > d) { arr[0] = c; if (a > b && a > d) arr[1] = a; else if (b > a && b > d) arr[1] = b; else if (d > a && d > b) arr[1] = d; } else if (d > a && d > b && d > c) { arr[0] = d; if (a > b && a > c) arr[1] = a; else if (b > a && b > c) arr[1] = b; else if (c > a && c > b) arr[1] = c; } return arr; } /** * method to compute the total number of digits in a * given number */ private static int countDigits(int n) { int counter = 1; while (n > 9) { n /= 10; counter += 1; } return counter; } /** * method to compute the total number of digits in a * given number */ private static long countDigits(long n) { long counter = 1L; while (n > 9L) { n /= 10L; counter += 1L; } return counter; } /** * method to compute the sum of n natural numbers */ private static int getNDigitsSum(int n) { return (n * (n + 1)) / 2; } /** * method to check whether a number is prime or not */ private static boolean isPrime(int n) { //Base Cases if (n == 2 || n == 3) return true; if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } /** * method to compute factorial of a given number * * @param n * @return */ private static int factorial(int n) { int ans = 1; while (n > 1) { ans *= n; n -= 1; } return ans; } /** * method to compute factorial of a given number * * @param n * @return */ private static long factorial(long n) { long ans = 1L; while (n > 1L) { ans *= n; n -= 1L; } return ans; } /** * method to find gcd of two numbers * * @param a * @param b * @return */ private static int gcd(int a, int b) { while (b != 0) { int tempA = a; a = b; b = tempA % b; } return a; } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
78c1464c09690917aa12763d35a105fe
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int T = in.nextInt(); for (int tt = 0; tt < T; tt++) { int n = in.nextInt(); int m = in.nextInt(); int r = in.nextInt(); int c = in.nextInt(); char[][] ch = new char[n][m]; for (int i = 0; i < n; i++) { String s = in.next(); for (int j = 0; j < m; j++) { ch[i][j] = s.charAt(j); } } if (check(ch, n, m)) { if (ch[r - 1][c - 1] == 'B') pw.println(0); else if (ok(ch, r, c, n, m)) pw.println(1); else pw.println(2); } else pw.println(-1); } pw.close(); } static boolean ok(char[][] ch, int r, int c, int n, int m) { for (int i = 0; i < m; i++) { if (ch[r - 1][i] == 'B') return true; } for (int i = 0; i < n; i++) { if (ch[i][c - 1] == 'B') return true; } return false; } static boolean check(char[][] ch, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (ch[i][j] == 'B') return true; } } return false; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
9ee5e3010dde9c77b712e9e5e29661bb
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) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int T = in.nextInt(); for (int tt = 0; tt < T; tt++) { int n = in.nextInt(); int m = in.nextInt(); int r = in.nextInt(); int c = in.nextInt(); char[][] ch = new char[n][m]; for (int i = 0; i < n; i++) { String s = in.next(); for (int j = 0; j < m; j++) { ch[i][j] = s.charAt(j); } } debug(ch); if (check(ch, n, m)) { if (ch[r - 1][c - 1] == 'B') pw.println(0); else if (ok(ch, r, c, n, m)) pw.println(1); else pw.println(2); } else pw.println(-1); } pw.close(); } static boolean ok(char[][] ch, int r, int c, int n, int m) { for (int i = 0; i < m; i++) { if (ch[r - 1][i] == 'B') return true; } for (int i = 0; i < n; i++) { if (ch[i][c - 1] == 'B') return true; } return false; } static boolean check(char[][] ch, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (ch[i][j] == 'B') return true; } } return false; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
cee5c86975494b060b1ee2d811eaa924
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.lang.*; import java.util.*; import java.io.*; public class class5 { public static void main(String[] arg){ Scanner s=new Scanner(System.in); try { int t=s.nextInt(); while(t-->0){ int n=s.nextInt(); int m=s.nextInt(); int r=s.nextInt(); int c=s.nextInt(); int flag=0; char arr[][]=new char[n][]; for(int i=0;i<n;i++){ arr[i]=s.next().toCharArray(); for(int j=0;j<m && flag==0;j++){ if(arr[i][j]=='B'){ flag=1; } } } if(flag==0){ System.out.println("-1"); continue; } if(arr[r-1][c-1]=='B'){ System.out.println(0); continue; } flag=0; for(int i=0;i<n;i++){ if(arr[i][c-1]=='B'){ System.out.println(1); flag=1; break; } } if(flag==1){ continue; } for (int i = 0; i < m; i++) { if (arr[r-1][i] == 'B') { System.out.println(1); flag = 1; break; } } if(flag==1){ continue; } System.out.println(2); } } catch (Exception e) { } s.close(); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
f94f627f4101be8386d3374e2eb49ffe
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.*; public class A_Not_Shading { public static void main(String[] arg){ Scanner s=new Scanner(System.in); try { int t=s.nextInt(); while(t-->0){ int n=s.nextInt(); int m=s.nextInt(); int r=s.nextInt()-1; int c=s.nextInt()-1; char[][] arr=new char[n][]; for(int i=0;i<n;i++){ arr[i]=s.next().toCharArray(); } if(arr[r][c]=='B'){ System.out.println(0); continue; } int flag=0; for(int i=0;i<n;i++){ if(arr[i][c]=='B'){ System.out.println(1); flag=1; break; } } if(flag==1){ continue; } for(int i=0;i<m;i++){ if(arr[r][i]=='B'){ System.out.println(1); flag=1; break; } } if (flag == 1) { continue; } for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(arr[j][i]=='B'){ System.out.println(2); flag=1; break; } } if (flag == 1) { break; } } if (flag == 1) { continue; } System.out.println(-1); } } catch (Exception e) { } s.close(); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
a94dded7893e4c3b84b2eaeb44138b08
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.*; public final class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); String s[] = new String[n]; for(int i=0;i<n;i++) s[i] = sc.next(); System.out.println(solve(n,m,r,c,s)); } } public static int solve(int n, int m, int r, int c, String s[]) { if(s[r-1].charAt(c-1)=='B') return 0; if(s[r-1].indexOf('B') >= 0) return 1; int count = 0; for(int i=0;i<n;i++){ if(s[i].charAt(c-1)=='B') return 1; if(s[i].indexOf('B')>=0) count++; } if(count>0) return 2; return -1; } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
518e1e4f78f5b256eea34e4b1cc0e36f
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.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int Count = sc.nextInt(); for(int count=0;count<Count;count++){ int N = sc.nextInt(), M = sc.nextInt(); int[] point = {sc.nextInt()-1,sc.nextInt()-1}; String[][] grid = new String[N][M]; Boolean hasBlack = false; Boolean solvedInZero = false; Boolean solvedInOne = false; for(int n=0;n<N;n++){ String row = sc.next(); for(int m=0;m<M;m++){ grid[n][m] = String.valueOf(row.charAt(m)); if(grid[n][m].equals("B")){ if(n == point[0] && m == point[1]){ solvedInZero = true; }else if(n == point[0] ^ m == point[1]){ solvedInOne = true; } hasBlack = true; } } } if(solvedInZero){ System.out.println("0"); }else if(hasBlack && solvedInOne){ System.out.println("1"); }else if(hasBlack && !solvedInOne){ System.out.println("2"); }else if(!hasBlack){ System.out.println("-1"); } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
6a78f4c1997b3b581de0c31fc8c7e45e
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 cases= sc.nextInt(); for(int x=0; x< cases; x++) { int rows= sc.nextInt(); int cols= sc.nextInt(); int r_loc = sc.nextInt()-1; int c_loc= sc.nextInt()-1; int blacks=0; char grid[][] = new char[rows][cols]; int flag=0; for(int a= 0; a<rows; a++) { String row = sc.next(); for(int b=0; b< cols; b++) { grid[a][b]= row.charAt(b); if(row.charAt(b)=='B') blacks++; } } if( grid[r_loc][c_loc]=='B') { System.out.println(0); continue; } for(int i=0; i< cols; i++) { if(grid[r_loc][i]=='B') { System.out.println(1); flag=1; break; } } if(flag==1) continue; for(int j=0; j< rows; j++) { if(grid[j][c_loc]=='B') { System.out.println(1); flag=1; break; } } if(flag==1) continue; if(blacks==0) { System.out.println(-1); } else System.out.println(2); } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
b040044b31374e4baedf1a12b0414a41
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 scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { notShading(scanner); } } private static void notShading(Scanner scanner) { int n = scanner.nextInt(); int m = scanner.nextInt(); int r = scanner.nextInt() - 1; int c = scanner.nextInt() - 1; scanner.nextLine(); String[] row = new String[n]; for (int i = 0; i < n; i++) { row[i] = scanner.nextLine(); } if (row[r].charAt(c) == 'B') { System.out.println(0); return; } boolean black = false; boolean[] rf = new boolean[n]; boolean[] cf = new boolean[m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (row[i].charAt(j) == 'B') { black = true; rf[i] = true; cf[j] = true; } } } if (!black) { System.out.println(-1); return; } if (rf[r] || cf[c]) { System.out.println(1); return; } System.out.println(2); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
c6da9da5cd1d3d80501010e58ac3234a
train_109.jsonl
1642257300
There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; //@Manan Parmar public class Solution2 implements Runnable { public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); //code int t1=sc.nextInt(); while(t1--!=0) { int n=sc.nextInt(),m=sc.nextInt(),r=sc.nextInt()-1,c=sc.nextInt()-1; char a[][]=new char[n][m]; boolean b=false; int ans=2; for(int i=0;i<n;i++) { String s=sc.next(); for(int j=0;j<m;j++) { a[i][j]=s.charAt(j); if(a[i][j]=='B'&&ans!=1) { b=true; if(i==r||c==j) ans=1; } } } if(!b) out.println("-1"); else if(a[r][c]=='B') out.println("0"); else out.println(ans); } out.close(); } /*class pairComparator implements Comparator<Pair> { public int compare(Pair p1,Pair p2) { return p1.a-p2.a; } } class Pair{ int id; int a; public Pair(int id,int a) { this.id=id; this.a=a; } }*/ //======================================================================== int lcsLength(String s1,String s2) { int x=s1.length(),y=s2.length(); int dp[][]=new int[x+1][y+1]; for(int i=1;i<=x;i++) { for(int j=1;j<=y;j++) { if(s1.charAt(i-1)==s2.charAt(j-1)) { dp[i][j]=1+dp[i-1][j-1]; } else { dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); } } } return dp[x][y]; } String lcs(String s1,String s2) { int x=s1.length(),y=s2.length(); int dp[][]=new int[x+1][y+1]; for(int i=1;i<=x;i++) { for(int j=1;j<=y;j++) { if(s1.charAt(i-1)==s2.charAt(j-1)) { dp[i][j]=1+dp[i-1][j-1]; } else { dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); } } } int index = dp[x][y]; int temp = index; // Create a character array to store the lcs string char[] lcs = new char[index+1]; lcs[index] = '\u0000'; // Set the terminating character // Start from the right-most-bottom-most corner and // one by one store characters in lcs[] int i = x; int j = y; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (s1.charAt(i-1) == s2.charAt(j-1)) { // Put current character in result lcs[index-1] = s1.charAt(i-1); // reduce values of i, j and index i--; j--; index--; } // If not same, then find the larger of two and // go in the direction of larger value else if (dp[i-1][j] > dp[i][j-1]) i--; else j--; } // Print the lcs String ans=""; for(int k=0;k<=temp;k++) ans=ans+Character.toString(lcs[k]); return ans; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } boolean isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } int lcm(int number1, int number2) { if (number1 == 0 || number2 == 0) { return 0; } int absNumber1 = Math.abs(number1); int absNumber2 = Math.abs(number2); int absHigherNumber = Math.max(absNumber1, absNumber2); int absLowerNumber = Math.min(absNumber1, absNumber2); int lcm = absHigherNumber; while (lcm % absLowerNumber != 0) { lcm += absHigherNumber; } return lcm; } void quickSort(int a[],int l,int r) { if(l<r) { int pindex=partition(a,l,r); quickSort(a,l,pindex-1); quickSort(a,pindex+1,r); } } int partition(int a[],int l,int r) { int pivot = a[r],pindex=l; for(int i=l;i<r;i++) { if(a[i]<pivot) { int temp=a[i]; a[i]=a[pindex]; a[pindex]=temp; pindex++; } } int temp1=a[r]; a[r]=a[pindex]; a[pindex]=temp1; return pindex; } int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; // Check if x is present at mid if (arr[m] >= x) if(m!=0&&arr[m-1]<x) return m; else if(m==0) return m; // If x greater, ignore left half if (arr[m] < x) l = m + 1; // If x is smaller, ignore right half else r = m - 1; } // if we reach here, then element was // not present return -1; } boolean check(char a,char b) { if(a==b) return true; else return false; } long binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1,q=0; while (l<=r) { int m = l + (r - l) / 2; if (arr[l] >=x) { q=1; return l; } if (arr[m] < x) l=m+1; else r=m; } return -1; } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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 String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { 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 readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Solution2(),"Main",1<<27).start(); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
b8057b003dcabc05114287c06b3c65f9
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 using Reader Class import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static int binary_search(int arr[],int n,int se) { int low=0,high=n-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]>se) high=mid-1; else if(arr[mid]<se) low=mid+1; else return 1; } return 0; } public static void main(String[] args) throws IOException { Reader sc = new Reader(); 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]; int black=0; for(int i=0;i<n;i++) { String s=sc.readLine(); for(int j=0;j<m;j++) { arr[i][j]=s.charAt(j); if(arr[i][j]=='B') black++; } } if(black>0) { if(arr[r-1][c-1]=='B') { System.out.println("0"); continue; } int bbb=0; for(int i=0;i<n;i++) { if(arr[i][c-1]=='B') bbb=1; } for(int i=0;i<m;i++) { if(arr[r-1][i]=='B') bbb=1; } if(bbb==1) System.out.println("1"); else System.out.println("2"); } else { System.out.println("-1"); } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
d88dc4e7facf813497c678a39091881c
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 codeforces_464_div2; 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(); sc.nextLine(); char[][] arr = new char[n+1][m+1]; boolean contains = false; for(int i=1; i<=n; i++) { String str = sc.nextLine(); for(int j=0; j<str.length(); j++) { arr[i][j+1] = str.charAt(j); if(arr[i][j+1] == 'B') contains = true; } } if(contains == false) System.out.println(-1); else if(arr[r][c] == 'B') System.out.println(0); else if(containsRow(arr, r) || containsCol(arr, c)) System.out.println(1); else System.out.println(2); } } private static boolean containsRow(char[][] arr, int r) { for(int i=1; i<arr[0].length; i++) { if(arr[r][i] == 'B') return true; } return false; } private static boolean containsCol(char[][] arr, int c) { for(int i=1; i<arr.length; i++) { if(arr[i][c] == 'B') return true; } return false; } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
8c01e912e14b867fb0ef3b90f58f5a56
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 p1627; import java.util.Arrays; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int x = 0; x < t; x++) { int n = sc.nextInt(); int m = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); //System.out.println(n); String[] grid = new String[n]; for (int i = 0; i < n; i++) { grid[i] = sc.next(); } //System.out.println(Arrays.toString(grid)); System.out.println(solver(grid, n, m, r, c)); //System.out.printf("%d %d %d\n", a[0], a[1], a[6] - a[0] - a[1]); //System.out.println(Arrays.toString(a)); } } public static int solver(String[] grid, int n, int m, int r, int c) { if (grid[r - 1].charAt(c - 1) == 'B') { return 0; } for (int i = 0; i < n; i++) { if (grid[i].charAt(c - 1) == 'B') { return 1; } } for (int j = 0; j < m; j++) { if (grid[r - 1].charAt(j) == 'B') { return 1; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i].charAt(j) == 'B') { return 2; } } } return -1; } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
58516cd38125566562f4b5bd0f6ff016
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.*; import java.util.*; public class NotShading { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0) { int n = s.nextInt(); int m = s.nextInt(); int r = s.nextInt() - 1; int c = s.nextInt() - 1; String[][] mat = new String[n][m]; for(int i = 0 ; i < n ; i++) { String temp = s.next(); for(int j = 0 ; j < m ; j++) { mat[i][j] = temp.charAt(j) + ""; } } if(mat[r][c].equals("B")) System.out.println(0); else { boolean black = false; for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < m ; j++) { String st = mat[i][j]; if(st.equals("B")) { black = true; break; } } } if(black == false) System.out.println(-1); else { int ans = 2; for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < m ; j++) { String st = mat[i][j]; if(st.equals("B")) { if(i == r || j == c) { ans = 1; break; } } } } System.out.println(ans); } } } } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output
PASSED
c080ee2c88b4a513aea8ae38dd8c229c
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 in = new Scanner(System.in); int t = Integer.parseInt(in.nextLine()); StringBuilder ans = new StringBuilder(""); while(t-- > 0){ String[] s = in.nextLine().split(" "); int n = Integer.parseInt(s[0]),m = Integer.parseInt(s[1]),r = Integer.parseInt(s[2])-1,c = Integer.parseInt(s[3])-1; String[] mat = new String[n]; boolean flag = false; for(int i=0;i<n;i++){ mat[i] = in.nextLine(); flag |= mat[i].contains("B"); } if(!flag){ ans.append("-1\n"); continue; } if(mat[r].charAt(c) == 'B'){ ans.append("0\n"); continue; } int minDist = 2; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(mat[i].charAt(j)=='B'){ if(i==r || j==c){ minDist = 1; break; } } } } ans.append(minDist+"\n"); } System.out.print(ans); in.close(); } }
Java
["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"]
1 second
["1\n0\n-1\n2\n2\n0\n-1\n1\n1"]
NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
4ca13794471831953f2737ca9d4ba853
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \leq n, m \leq 50$$$; $$$1 \leq r \leq n$$$; $$$1 \leq c \leq m$$$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.
800
For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer — the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black.
standard output