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
|
b5b86d9173f8de26563f6c7fc746200b
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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};
static int y[]= {0,-1,1,0};
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 p=1<<(int)(1+Math.ceil(Math.log(n)/(double)Math.log(2)));
int tn=n;
int a[]=new int[n];
int ptr=1;
if(n==3) {
a[0]=1;
a[2]=2;
a[1]=3;
}
else {
if(n%2!=0) {
a[n-1]=0;
n--;
}
int xor1=0,xor2=0;
if(n%2==0){
for(int i=0;i<n;i+=2) {
a[i]=ptr;
a[i+1]=ptr;
ptr++;
}
if((n/2)%2!=0) {
int p2=p*2;
for(int i=0;i<n-2;i+=2) {
a[i]+=p;
}
a[n-2]+=p2;
a[n-4]+=p2;
}
else {
for(int i=0;i<n;i+=2) {
a[i]+=p;
}
}
}
for(int i=0;i<tn;i+=2) {
xor1^=a[i];
}
for(int i=1;i<tn;i+=2) {
xor2^=a[i];
}
}
for(int i=0;i<tn;i++)log.write(a[i]+" ");
log.write("\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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
3bf3ef04f58d42d416dc65ed3f787918
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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[][], visited[][];
static int[] dx = {-1,-1,-1,0,0,1,1,1};
static int[] dy = {-1,0,1,-1,1,-1,0,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{
int n = r.readInt();
if(n==3){ sb.append("2 1 3\n"); return;}
int[] arr = new int[n];
int odd = 0, even = 0;
for(int i=0;i<n-2;i++){
arr[i] = i;
if(i%2==1) odd^=i;
else even^=i;
}
if(odd==even){
arr[n-3]++;
if(n%2==0) odd = (odd^(n-3))^(n-2);
else even = (even^(n-3))^(n-2);
}
arr[n-2] = (n%2==1) ? (1<<20)+even : (1<<20)+odd;
arr[n-1] = (n%2==1) ? (1<<20)+odd : (1<<20)+even;
for(int x: arr)
sb.append(x).append(" ");
sb.append("\n");
}
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
676fb93d7bf07a76e07d0dda32ad8a53
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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 {
int n = readInt();
int rem = n % 4;
if (rem == 0) {
for (int i = 0; i < n; i++)
out.print((i + 4) + " ");
} else if (rem == 1) {
out.print("0 ");
for (int i = 0; i < n - 1; i++)
out.print((i + 4) + " ");
} else if (rem == 2) {
for (int i = 0; i < n - 6; i++)
out.print((i + 16) + " ");
out.print("4 1 2 12 3 8");
} else if (rem == 3) {
out.print("1 2 3 ");
for (int i = 0; i < n - 3; i++)
out.print((i + 4) + " ");
}
out.println();
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
2cb2836fe2c440a0138c3a6ac6ef900e
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static FastReader scanner = new FastReader();
public static void solve() {
int n = scanner.nextInt();
int[] arr = new int[n];
if (n == 3) {
System.out.println("2 1 3");
return;
}
if (n % 4 == 0) {
for(int i = 0; i < n; i++) {
arr[i] = i;
}
boolean needToSwap = false;
int startIndex = 0;
while (startIndex < n - 1) {
if (needToSwap) {
int t = arr[startIndex];
arr[startIndex] = arr[startIndex + 1];
arr[startIndex + 1] = t;
}
needToSwap = !needToSwap;
startIndex += 2;
}
}
else if (n % 4 == 1) {
for(int i = 1; i < n; i++) {
arr[i] = i + 1;
}
boolean needToSwap = false;
int startIndex = 0;
while (startIndex < n - 1) {
if (needToSwap) {
int t = arr[startIndex];
arr[startIndex] = arr[startIndex + 1];
arr[startIndex + 1] = t;
}
needToSwap = !needToSwap;
startIndex += 2;
}
}
else if (n % 4 == 2) {
for(int i = 1; i < n; i++) {
arr[i] = i + 1;
}
boolean needToSwap = false;
int startIndex = 0;
while (startIndex < n - 1) {
if (needToSwap) {
int t = arr[startIndex];
arr[startIndex] = arr[startIndex + 1];
arr[startIndex + 1] = t;
}
needToSwap = !needToSwap;
startIndex += 2;
}
arr[n - 2] = n * 2;
int even = 0;
int odd = 0;
for(int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
even = even ^ arr[i];
}
else {
odd = odd ^ arr[i];
}
}
arr[n - 1] = even ^ odd;
}
else {
for(int i = 0; i < n; i++) {
arr[i] = i;
}
boolean needToSwap = false;
int startIndex = 0;
while (startIndex < n - 1) {
if (needToSwap) {
int t = arr[startIndex];
arr[startIndex] = arr[startIndex + 1];
arr[startIndex + 1] = t;
}
needToSwap = !needToSwap;
startIndex += 2;
}
arr[n - 2] = n * 2;
int even = 0;
int odd = 0;
for(int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
even = even ^ arr[i];
}
else {
odd = odd ^ arr[i];
}
}
arr[n - 1] = even ^ odd;
}
for(int i: arr) {
System.out.print(i + " ");
}
System.out.println();
}
private static void validate(int[] a) {
int n = a.length;
int even = 0;
int odd = 0;
for(int i = 0; i < n; i++) {
if (i % 2 == 0) {
even = even ^ a[i];
}
else {
odd = odd ^ a[i];
}
}
if (even == odd) {
System.out.println("legit");
}
else {
System.out.println("deo");
}
}
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();
}
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
51f373d63b78a58fd276e67189b32e4f
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class SolutionG {
public static void main(String[] args) throws Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve(t);
}
out.close();
}
private static void solve(int t) {
int n = sc.nextInt();
long xor = 0;
for (int i = 1; i <= n - 3; i++) {
out.print(i + " ");
xor ^= i;
}
xor ^= (1L << 29);
xor ^= (1L << 30);
out.println((1L << 29) + " " + (1L << 30) + " " + xor);
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
506e2308a0169f4174bd2aaffdd0eb96
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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
{
//------------------------------------------------input class---------------------------------------//
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;
}
}
//-------------------------------------------segment tree-----------------------------------------//
static int[] segment;
static void constructSt(int n, int[] arr){
segment = new int[n*4+1];
formSt(arr, 1,0,n-1);
}
public static void formSt(int[] arr, int node, int s, int e){
if(s==e){
segment[node]= arr[s];
return;
}
formSt(arr, node*2,s,s+(e-s)/2);
formSt(arr, node*2+1,s+(e-s)/2+1,e);
segment[node]=Math.max(segment[node*2],segment[node*2+1]);
}
public static int findMax( int node, int s, int e,int l , int r){
if(l>e||s>r) return -1;
if(s==e) return segment[node];
if(l<=s&&r>=e) return segment[node];
int mid = s+(e-s)/2;
return Math.max(findMax(node*2,s,mid,l,r),findMax(node*2+1,mid+1,e,l,r));
}
//-----------------------------------------dsu-----------------------------------------//
static int[] parent,rank;
public static void dsu(int n){
parent = new int[n]; rank = new int[n];
for(int i =0;i<n;i++) parent[i]=i;
}
public static int find(int i){
if(i==parent[i] ) return i;
return parent[i]=find(parent[i]);
}
public static void merge(int i, int j){
if(rank[i]>=rank[j]){
rank[i]+=rank[j];
parent[j]=i;
}
else {
rank[j]+=rank[i];
parent[i]=j;
}
}
//-------------------------------------------topological sort--------------------------------------//
public static int[] topo(List<List<Integer>> a , int n , int[] in){
int[] ans = new int[n+1];
PriorityQueue<Integer> p = new PriorityQueue<>((x,y)->a.get(x).size()-a.get(y).size());
for(int i =1;i<=n;i++) if(in[i]==0) p.add(i);
int i =1;
while(p.size()>0){
int e = p.poll();
ans[i++]= e;
for(int temp : a.get(e)){
in[temp]--;
if(in[temp]==0)
p.add(temp);
}
}
return ans;
}
//-------------------------------------------max--------------------------------------------------//
public static int max (int a, int b){
return a>=b?a:b;
}
public static long max (long a, long b){
return a>=b?a:b;
}
public static int max (int a, int b, int c){
return Math.max(a,Math.max(b,c));
}
public static long max (long a, long b, long c){
return Math.max(a,Math.max(b,c));
}
//------------------------------------------min---------------------------------------------------//
public static int min (int a, int b){
return a<=b?a:b;
}
public static long min (long a, long b){
return a<=b?a:b;
}
public static int min (int a, int b, int c){
return Math.min(a,Math.min(b,c));
}
public static long min (long a, long b, long c){
return Math.min(a,Math.min(b,c));
}
//----------------------------------------- gcd-----------------------------------------//
public static int gcd(int a, int b){
if(a==0) return b;
if(b==0 ) return a;
if(a<b) return gcd(b%a,a);
return gcd(a%b,b);
}
public static long gcd(long a, long b){
if(a==0) return b;
if(b==0 ) return a;
if(a<b) return gcd(b%a,a);
return gcd(a%b,b);
}
//-------------------------------------lcm------------------------------------------------------//
public static int lcm(int a, int b){
return (a*b)/gcd(a,b);
}
public static long lcm(long a, long b){
return (a*b)/gcd(a,b);
}
//---------------------------------------------binary search--------------------------------------//
public static int binary(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}
public static int binary(long arr[], long x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}
//---------------------------------------helping methods---------------------------------------------//
//------------------------------------------main------------------------------------------------------//
public static void main (String[] args) throws java.lang.Exception
{
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader sc =new FastReader();
int t =sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int i =0;
if(n%4==1){
out.print("0 ");
n-=1;
i=2;
}
else if(n%4==2){
out.print("1 2 3 4 10 14 ");
n-=6;
i=16;
}
else if(n%4==3){
i=1;
}
while(n-->0) out.print(i++ + " ");
out.print("\n");
}
out.close();
// your code goes here"
}
}
class pair
{
long x;
long y;
public pair(long x , long y)
{
this.x= x;
this.y= y;
}
}
class node{
int x, y;
public node(int x , int y)
{
this.x= x;
this.y= y;
}
}
class solution{
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
660ab2a4f92782b5671170110da65217
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.util.Scanner;
public class _1722G {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int cases=sc.nextInt();
while(cases--!=0) {
//For every Testcase
int n=sc.nextInt();
int case1=0;
int case2=0;
for(int i=0;i<n-2;i++) {
case1^=i;
case2^=(i+1);
}
long last=(long) Math.pow(2, 30);
if(case1==0) {
for(int i=1;i<=n-2;i++) {
System.out.print(i+" ");
}
case2^=last;
System.out.print(last+" "+case2+"\n");
}else {
for(int i=0;i<n-2;i++) {
System.out.print(i+" ");
}
case1^=last;
System.out.println(last+" "+case1+"\n");
}
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
898e83ae9b7c30a919a41592fde1f4c0
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int g = Integer.parseInt(bf.readLine());
while (g-- > 0) {
int a=Integer.parseInt(bf.readLine());
int[] k=new int[a];
if(a%2==1) a--;
int b=a/2;
int i=0;
if(b%2==1 && a>=6){
i=6;
k[0]=1; k[2]=2; k[4]=3;
k[1]=13; k[3]=5; k[5]=8;
}
int x=32,p=0;
while (i<a) {
if(((x+p)&1)==0){
k[i]=x+p;
k[i+1]=x+p+1;
i+=2;
}
p++;
}
if(a==2){
k[0]=1; k[1]=2; k[2]=3;
}
for(int j=0; j<k.length; j++) System.out.print(k[j]+" ");
System.out.println();
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
21ec8add5c7aaffc87cf4a49a31d902b
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
//<———My cp————
//https://takeuforward.org/interview-experience/strivers-cp-sheet/?utm_source=youtube&utm_medium=striver&utm_campaign=yt_video
import java.util.*;
import java.io.*;
public class Solution{
static PrintWriter pw = new PrintWriter(System.out);
static FastReader fr = new FastReader(System.in);
private static long MOD = 1_000_000_007;
public static void main(String[] args) throws Exception{
FastReader fr = new FastReader(System.in);
int t = fr.nextInt();
while(t-->0){
int n = fr.nextInt();
if(n%4==1){
pw.print(0+" ");
n-=1;
}else if(n%4==2){
pw.print("4 1 2 12 3 8 ");
n-=6;
}else if(n%4==3){
pw.print("2 1 3 ");
n-=3;
}
int curr = 16;
while(n>0){
pw.print(curr+" "+(curr+2)+" "+(curr+1)+" "+(curr+3)+" ");
n-=4;
curr+=4;
}
pw.println("");
}
pw.close();
}
static boolean canPut(int n,int tempR,int tempC){
if(tempR<0||tempC<0||tempR>=n||tempC>=n){
return false;
}
return true;
}
static class Pair{
int height;
int width;
public Pair(int height,int width){
this.height = height;
this.width = width;
}
@Override
public String toString() {
return "height: "+height+" width: "+width;
}
}
public static long opNeeded(long c,long[] vals){
long tempResult = 0;
for(int j = 0;j<vals.length;j++){
tempResult=tempResult+Math.abs((long)(vals[j]-Math.pow(c,j)));
}
if(tempResult<0){
tempResult=Long.MAX_VALUE;
}
return tempResult;
}
static int isPerfectSquare(int vals){
int lastPow=1;
while(lastPow*lastPow<vals){
lastPow++;
}
if(lastPow*lastPow==vals){
return lastPow*lastPow;
}else{
return -1;
}
}
public static int[] sort(int[] vals){
ArrayList<Integer> values = new ArrayList<>();
for(int i = 0;i<vals.length;i++){
values.add(vals[i]);
}
Collections.sort(values);
for(int i =0;i<values.size();i++){
vals[i] = values.get(i);
}
return vals;
}
public static long[] sort(long[] vals){
ArrayList<Long> values = new ArrayList<>();
for(int i = 0;i<vals.length;i++){
values.add(vals[i]);
}
Collections.sort(values);
for(int i =0;i<values.size();i++){
vals[i] = values.get(i);
}
return vals;
}
public static void reverseArray(long[] vals){
int startIndex = 0;
int endIndex = vals.length-1;
while(startIndex<=endIndex){
long temp = vals[startIndex];
vals[startIndex] = vals[endIndex];
vals[endIndex] = temp;
startIndex++;
endIndex--;
}
}
public static void reverseArray(int[] vals){
int startIndex = 0;
int endIndex = vals.length-1;
while(startIndex<=endIndex){
int temp = vals[startIndex];
vals[startIndex] = vals[endIndex];
vals[endIndex] = temp;
startIndex++;
endIndex--;
}
}
static class FastReader{
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
public static int GCD(int numA, int numB){
if(numA==0){
return numB;
}else if(numB==0){
return numA;
}else{
if(numA>numB){
return GCD(numA%numB,numB);
}else{
return GCD(numA,numB%numA);
}
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
4583aba8b97f2d85ce83f7a1a895b9a4
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class New {
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int testCases = Integer.parseInt(br.readLine());
while(testCases-->0){
int n=Integer.parseInt(br.readLine());
int arr[]=new int[n];
if(n%2==0) {
for(int i=0,j=1;i<n;i+=2,j++) arr[i]=j;
for(int i=1,j=1;i<n;i+=2,j++) arr[i]=j;
}else {
for(int i=2,j=1;i<n;i+=2,j++) arr[i]=j;
for(int i=1,j=1;i<n;i+=2,j++) arr[i]=j;
}
if(((n+1)/2)%2==0) {
for(int i=0;i<n;i+=2) {
arr[i]|=(1<<30);
}
}else {
for(int i=0;i+2<n;i+=2) {
arr[i]|=(1<<30);
}
for(int i=2;i<n;i+=2) {
arr[i]|=(1<<29);
}
}
for(int i:arr) out.print(i+" ");
out.println();
}
out.close();
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
78ea39c675996231218e33321bfcd006
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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[] ans = new int[n];
int xor = 0;
int xor2 = 0;
for(int i = 0; i < n-2; i++){
ans[i] = i;
if(i % 2 == 0){
xor ^= i;
}else{
xor2 ^= i;
}
}
if(n % 2 == 0){
if(xor == xor2){
xor2 ^= ans[n-3];
ans[n-3]++;
xor2 ^= ans[n-3];
}
ans[n-2] = (1 << 30) + xor;
ans[n-1] = (1 << 30) + xor2;
}else{
if(xor == xor2){
xor ^= ans[n-3];
ans[n-3]++;
xor ^= ans[n-3];
}
ans[n-2] = (1 << 30) + xor2;
ans[n-1] = (1 << 30) + xor;
}
for(int v : ans){
sb.append(v).append(" ");
}
pw.println(sb.toString().trim());
sb.setLength(0);
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
ca55de37fd0f5f35fe35b41d8568b547
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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();
int pre1 = 0, pre2 = 0;
for(int i=0;i<n-2;i++) {
pre1 ^= i;
}
for(int i=1;i<n-1;i++) {
pre2 ^= i;
}
long last = (1l<<31)-1;
if(pre1 != 0) {
pre1 ^= last;
for(int i=0;i<n-2;i++) {
o.print(i + " ");
}
o.print(pre1 + " ");
o.println(last);
}
else {
pre2 ^= last;
for(int i=1;i<n-1;i++) {
o.print(i + " ");
}
o.print(pre2 + " ");
o.println(last);
}
} 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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
d7676f9556214529d33250cc3c1c78d0
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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 {
int n = readInt();
int rem = n % 4;
if (rem == 0) {
for (int i = 0; i < n; i++)
out.print((i + 4) + " ");
} else if (rem == 1) {
out.print("0 ");
for (int i = 0; i < n - 1; i++)
out.print((i + 4) + " ");
} else if (rem == 2) {
for (int i = 0; i < n - 6; i++)
out.print((i + 16) + " ");
out.print("4 1 2 12 3 8");
} else if (rem == 3) {
out.print("1 2 3 ");
for (int i = 0; i < n - 3; i++)
out.print((i + 4) + " ");
}
out.println();
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
6fde644e1745cf231e376ca109a99cfe
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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 = fastReader.nextInt();
while (tt-- > 0) {
int n = fastReader.nextInt();
if(n%4 == 0 || n%4 == 3){
if(n%2 == 0){
int ans[] = new int[n];
StringBuilder ans_st = new StringBuilder();
for(int i = 0; i<n;i++){
ans[i] = i;
}
for(int i = 0 ; i < n ; i++){
ans_st.append(ans[i]).append(" ");
}
out.println(ans_st);
}else{
int ans[] = new int[n];
StringBuilder ans_st = new StringBuilder();
int add = 1;
for(int i = 0; i<n;i+=2,add+=2){
ans[i] = add;
}
add = 2;
for(int i = 1; i<n;i+=2,add+=2){
ans[i] = add;
}
for(int i = 0 ; i < n ; i++){
ans_st.append(ans[i]).append(" ");
}
out.println(ans_st);
}
}else{
if(n%2 ==1){
int pow = n-1;
int ans[] = new int[n];
ans[0] = 0;
StringBuilder ans_st = new StringBuilder();
for(int i = 2; i<n;i+=2){
ans[i] = i;
}
ans[1] = n;
for(int i = 3; i<n;i+=2){
ans[i] = i;
}
for(int i = 0 ; i < n ; i++){
ans_st.append(ans[i]).append(" ");
}
out.println(ans_st);
}else{
int pow = 0;
int bit= 0;
while(pow <= n){
pow = (1<<bit);
bit++;
}
int ans[] = new int[n];
StringBuilder ans_st = new StringBuilder();
ans[0] = pow|1;
ans[3] = pow|3;
for(int i = 0;i<n;i++){
if(i == 0 || i== 3) continue;
ans[i] = i;
}
for(int i = 0 ; i < n ; i++){
ans_st.append(ans[i]).append(" ");
}
out.println(ans_st);
}
}
}
out.close();
}
// 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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
637e917c171a8445a73ad08e00bc05d9
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class g {
//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 a[]=new int[n];
int xval=0;
for(int i=0;i<n-2;i++){
a[i]=i;
xval^=a[i];
}
if(xval==0){
a[0]=n-2;
xval^=a[0];
}
a[n-2]=(int)power(2,30);
xval^=a[n-2];
a[n-1]=xval;
for(int i=0;i<n;i++)
sb.append(a[i]+" ");
sb.append("\n");
}
output.write(sb+"");
output.close();
}
catch(Exception e)
{
return;
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
a2f0ebb44b3323ff50bdf94634a15917
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.util.*;
public class Codeforces {
static final int MOD = (int)1e9 + 7;
// Function to return (x^y) % MOD
// in O(log(y))
static long power(long x,
long y)
{
long res = 1;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % MOD;
x = (x * x) % MOD;
y /= 2;
}
return res;
}
static int lbound(int arr[], int key)
{
int l=0,h=arr.length-1;
while(l<h)
{
int mid=l+(h-l+1)/2;
if(arr[mid]<=key)
l=mid;
else if(key<arr[mid])
h=mid-1;
System.out.println(l+" "+h);
}
if(arr[l]<key)
l= l+1;
return l;
}
static int ubound(int arr[], int key)
{
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int ans[]= new int[n];
int p=0;
if(n%4==1)
{
ans[0]=0;
p=1;
}
else if(n%4==2)
{
ans[0]=4;ans[1]=1;ans[2]=2;ans[3]=12;ans[4]=3;ans[5]=8;
p=6;
}
else if(n%4==3)
{
ans[0]=1;
ans[1]=3;
ans[2]=2;
p=3;
}
int q=16;
for(int i=p;i<n;i++)
{
ans[i]=q;
q++;
}
for(int i=0;i<n;i++)
{
System.out.print(ans[i] + " ");
}
System.out.println();
}
sc.close();
}
}
class Pair{
int x; int y;
Pair(int x, int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return x+" "+y;
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
3617333d9bbedb34a551147afc8f217e
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static AReader scan = new AReader();
static void solve() {
int n = scan.nextInt();
int p = n % 4;
if(p == 0){
for(int i = 1,j = 100;i<=n;i+=4,j+=4){
System.out.print(j+" " + (j+1) + " " + (j + 2) + " " + (j + 3)+" ");
}
System.out.println();
}else if(p == 1){
for(int i = 1,j = 100;i<=n - 5;i+=4,j+=4){
System.out.print(j+" " + (j+1) + " " + (j + 2) + " " + (j + 3)+" ");
}
System.out.println("2 0 4 5 3");
}else if(p == 2){
for(int i = 1,j = 100;i<=n - 6;i+=4,j+=4){
System.out.print(j+" " + (j+1) + " " + (j + 2) + " " + (j + 3)+" ");
}
System.out.println("4 1 2 12 3 8");
}else if(p == 3){
for(int i = 1,j = 100;i<=n - 3;i+=4,j+=4){
System.out.print(j+" " + (j+1) + " " + (j + 2) + " " + (j + 3)+" ");
}
System.out.println("2 1 3");
}
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
77458f249d8de88a8e7831f821ffbda4
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
label:while(tes-->0)
{
int n=sc.nextInt();
int i;
if(n%4==0)
{
for(i=0;i<n;i++)
System.out.print(i+" "); System.out.println(); continue;
}
if(n%4==1)
{
System.out.print("0 ");
for(i=1;i<n;i++)
System.out.print((i+1)+" "); System.out.println(); continue;
}
if(n%4==3)
{
System.out.print("2 1 3 ");
for(i=0;i<n-3;i++)
System.out.print((i+4)+" "); System.out.println(); continue;
}
System.out.print("2 3 1 4 12 8 ");
for(i=0;i<n-6;i++)
System.out.print((i+14)+" "); System.out.println(); continue;
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
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;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
0c8fe7da2da3cc52893378b8a78bd889
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.text.DecimalFormat;
import java.io.*;
import java.util.*;
public class Main
{
static class Pair
{
int a,ind;
public Pair(int a,int ind)
{
this.a=a;
this.ind=ind;
}
}
static final int INF = 1 << 30;
static final long INFL = 1L << 60;
static final long NINF = INFL * -1;
static final long mod = (long) 1e9 + 7;
static final long mod2 = 998244353;
static DecimalFormat df = new DecimalFormat("0.00000000000000");
public static final double PI = 3.141592653589793d, eps = 1e-9;
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
//int t=in.ni();
solver.solve(1, in, out);
out.close();
}
static class TaskC
{
public static void solve(int testNumber, InputReader in, OutputWriter out)
{
int t=in.ni();
while(t-->0)
{
int n=in.ni();
if(n%4==0)
{
for(int i=2;i<n+2;++i)
{
out.print(i+" ");
}
out.printLine();
}
else if(n%4==1)
{
out.print(0+" ");
for(int i=2;i<n+1;++i)
{
out.print(i+" ");
}
out.printLine();
}
else if(n%4==2)
{
out.print(2+" "+1+" "+3+" "+4+" "+12+" "+8+" ");
long tp=14;
int i=0;
n-=6;
while(i<n)
{
out.print(tp+" ");
++tp;
++i;
}
out.printLine();
}
else
{
out.print(2+" "+1+" "+3+" ");
n-=3;
long tp=4;
int i=0;
while(i<n)
{
out.print(tp+" ");
++tp;
++i;
}
out.printLine();
}
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int ni() {
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;
}
private char nc() { return (char)skip(); }
public int[] na(int arraySize) {
int[] array = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = ni();
}
return array;
}
private int skip() {
int b;
while ((b = read()) != -1 && isSpaceChar(b)) ;
return b;
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = read();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
int[][] nim(int h, int w) {
int[][] a = new int[h][w];
for (int i = 0; i < h; i++) {
a[i] = na(w);
}
return a;
}
long[][] nlm(int h, int w) {
long[][] a = new long[h][w];
for (int i = 0; i < h; i++) {
a[i] = nla(w);
}
return a;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isNewLine(int c) {
return c == '\n';
}
public String nextLine() {
int c = read();
StringBuilder result = new StringBuilder();
do {
result.appendCodePoint(c);
c = read();
} while (!isNewLine(c));
return result.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
long result = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
result *= 10;
result += c & 15;
c = read();
} while (!isSpaceChar(c));
return result * sign;
}
public long[] nla(int arraySize) {
long array[] = new long[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextLong();
}
return array;
}
public double nextDouble() {
double ret = 0, div = 1;
byte c = (byte) read();
while (c <= ' ') {
c = (byte) read();
}
boolean neg = (c == '-');
if (neg) {
c = (byte) read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = (byte) read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = (byte) read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
public static class CP
{
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; (long) i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
public static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long pow(long a, long n, long mod) {
// a %= mod;
long ret = 1;
int x = 63 - Long.numberOfLeadingZeros(n);
for (; x >= 0; x--) {
ret = ret * ret % mod;
if (n << 63 - x < 0)
ret = ret * a % mod;
}
return ret;
}
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
CP.swap(nums, mark, idx);
CP.reverse(nums, mark + 1, nums.length - 1);
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
public static boolean isComposite(long n) {
if (n < 2)
return true;
if (n == 2 || n == 3)
return false;
if (n % 2 == 0 || n % 3 == 0)
return true;
for (long i = 6L; i * i <= n; i += 6)
if (n % (i - 1) == 0 || n % (i + 1) == 0)
return true;
return false;
}
static int ifnotPrime(int[] prime, int x) {
return (prime[x / 64] & (1 << ((x >> 1) & 31)));
}
static long log2(long n)
{
return (long)(Math.log10(n)/Math.log10(2L));
}
static void makeComposite(int[] prime, int x) {
prime[x / 64] |= (1 << ((x >> 1) & 31));
}
public static String swap(String a, int i, int j) {
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
static void reverse(long arr[]){
int l = 0, r = arr.length-1;
while(l<r){
long temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
l++;
r--;
}
}
static void reverse(int arr[]){
int l = 0, r = arr.length-1;
while(l<r){
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
l++;
r--;
}
}
public static int[] sieveEratosthenes(int n) {
if (n <= 32) {
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
for (int i = 0; i < primes.length; i++) {
if (n < primes[i]) {
return Arrays.copyOf(primes, i);
}
}
return primes;
}
int u = n + 32;
double lu = Math.log(u);
int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)];
ret[0] = 2;
int pos = 1;
int[] isnp = new int[(n + 1) / 32 / 2 + 1];
int sup = (n + 1) / 32 / 2 + 1;
int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
for (int tp : tprimes) {
ret[pos++] = tp;
int[] ptn = new int[tp];
for (int i = (tp - 3) / 2; i < tp << 5; i += tp)
ptn[i >> 5] |= 1 << (i & 31);
for (int j = 0; j < sup; j += tp) {
for (int i = 0; i < tp && i + j < sup; i++) {
isnp[j + i] |= ptn[i];
}
}
}
// 3,5,7
// 2x+3=n
int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4,
13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14};
int h = n / 2;
for (int i = 0; i < sup; i++) {
for (int j = ~isnp[i]; j != 0; j &= j - 1) {
int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27];
int p = 2 * pp + 3;
if (p > n)
break;
ret[pos++] = p;
if ((long) p * p > n)
continue;
for (int q = (p * p - 3) / 2; q <= h; q += p)
isnp[q >> 5] |= 1 << q;
}
}
return Arrays.copyOf(ret, pos);
}
static long digitSum(long s) {
long brute = 0;
while (s > 0) {
brute+=s%10;
s /= 10;
}
return brute;
}
public static int[] primefacts(int n, int[] primes) {
int[] ret = new int[15];
int rp = 0;
for (int p : primes) {
if (p * p > n) break;
int i;
for (i = 0; n % p == 0; n /= p, i++) ;
if (i > 0) ret[rp++] = p;
}
if (n != 1) ret[rp++] = n;
return Arrays.copyOf(ret, rp);
}
public static long[] sort(long arr[]) {
List<Long> list = new ArrayList<>();
for (long n : arr) {
list.add(n);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static long[] revsort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long n : arr) {
list.add(n);
}
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static int[] revsort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int n : arr) {
list.add(n);
}
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static ArrayList<Integer> reverse(
ArrayList<Integer> data,
int left, int right)
{
while (left < right)
{
int temp = data.get(left);
data.set(left++,
data.get(right));
data.set(right--, temp);
}
return data;
}
static ArrayList<Integer> sieve(long size)
{
ArrayList<Integer> pr = new ArrayList<Integer>();
boolean prime[] = new boolean[(int) size];
for (int i = 2; i < prime.length; i++) prime[i] = true;
for (int i = 2; i * i < prime.length; i++) {
if (prime[i]) {
for (int j = i * i; j < prime.length; j += i) {
prime[j] = false;
}
}
}
for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i);
return pr;
}
static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) {
ArrayList<Integer> al = new ArrayList<>();
if (l == 1) ++l;
int max = r - l + 1;
int arr[] = new int[max];
for (int p : primes) {
if (p * p <= r) {
int i = (l / p) * p;
if (i < l) i += p;
for (; i <= r; i += p) {
if (i != p) {
arr[i - l] = 1;
}
}
}
}
for (int i = 0; i < max; ++i) {
if (arr[i] == 0) {
al.add(l + i);
}
}
return al;
}
static boolean isfPrime(long n, int iteration) {
if (n == 0 || n == 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
Random rand = new Random();
for (int i = 0; i < iteration; i++) {
long r = Math.abs(rand.nextLong());
long a = r % (n - 1) + 1;
if (modPow(a, n - 1, n) != 1)
return false;
}
return true;
}
static long modPow(long a, long b, long c) {
long res = 1;
for (int i = 0; i < b; i++) {
res *= a;
res %= c;
}
return res % c;
}
private static long binPower(long a, long l, long mod) {
long res = 0;
while (l > 0) {
if ((l & 1) == 1) {
res = mulmod(res, a, mod);
l >>= 1;
}
a = mulmod(a, a, mod);
}
return res;
}
private static long mulmod(long a, long b, long c) {
long x = 0, y = a % c;
while (b > 0) {
if (b % 2 == 1) {
x = (x + y) % c;
}
y = (y * 2L) % c;
b /= 2;
}
return x % c;
}
static long binary_Expo(long a, long b) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res *= a;
--b;
}
a *= a;
b /= 2;
}
return res;
}
static void swap(int a, int b) {
int tp = b;
b = a;
a = tp;
}
static long Modular_Expo(long a, long b, long mod) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res = (res * a) % mod;
--b;
}
a = (a * a) % mod;
b /= 2;
}
return res % mod;
}
static int i_gcd(int a, int b) {
while (true) {
if (b == 0)
return a;
int c = a;
a = b;
b = c % b;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int gcd(int a,int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long ceil_div(long a, long b) {
return (a + b - 1) / b;
}
static int getIthBitFromInt(int bits, int i) {
return (bits >> (i - 1)) & 1;
}
static long getIthBitFromLong(long bits, int i) {
return (bits >> (i - 1)) & 1;
}
static boolean isPerfectSquare(long a)
{
long sq=(long)(Math.floor(Math.sqrt(a))*Math.floor(Math.sqrt(a)));
return sq==a;
}
private static TreeMap<Long, Long> primeFactorize(long n) {
TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder());
long cnt = 0;
long total = 1;
for (long i = 2; (long) i * i <= n; ++i) {
if (n % i == 0) {
cnt = 0;
while (n % i == 0) {
++cnt;
n /= i;
}
pf.put(cnt, i);
//total*=(cnt+1);
}
}
if (n > 1) {
pf.put(1L, n);
//total*=2;
}
return pf;
}
//less than or equal
private static int lower_bound(ArrayList<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;
hi = mid - 1;
} else {
lo = 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(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;
}
private static int[] LIS(long arr[], int n) {
List<Long> list = new ArrayList<>();
int[] cnt=new int[n];
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]);
cnt[i]=list.size();
}
return cnt;
}
private static int find1(List<Long> list, long 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;
}
private static long nCr(long n, long r,long mod) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans = (ans%mod*i%mod)%mod;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans%mod;
}
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;
}
static void initStringHash(String s,long[] dp,long[] inv,long p)
{
long pow=1;
inv[0]=1;
int n=s.length();
dp[0]=((s.charAt(0)-'a')+1);
for(int i=1;i<n;++i)
{
pow=(pow*p)%mod;
dp[i]=(dp[i-1]+((s.charAt(i)-'a')+1)*pow)%mod;
inv[i]=CP.modinv(pow,mod)%mod;
}
}
static long getStringHash(long[] dp,long[] inv,int l,int r)
{
long ans=dp[r];
if(l-1>=0)
{
ans-=dp[l-1];
}
ans=(ans*inv[l])%mod;
return ans;
}
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();
}
static boolean isSquarefactor(int x, int factor, int target) {
int s = (int) Math.round(Math.sqrt(x));
return factor * s * s == target;
}
static boolean isSquare(long x) {
long s = (long) Math.round(Math.sqrt(x));
return x * x == s;
}
static int bs(ArrayList<Integer> al, int val) {
int l = 0, h = al.size() - 1, mid = 0, ans = -1;
while (l <= h) {
mid = (l + h) >> 1;
if (al.get(mid) == val) {
return mid;
} else if (al.get(mid) > val) {
h = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
static void sort(int a[]) // heap sort
{
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int i = 0; i < a.length; i++)
q.add(a[i]);
for (int i = 0; i < a.length; i++)
a[i] = q.poll();
}
static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
fast_swap(in, idx, i);
}
}
public static int[] radixSort2(int[] a) {
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for (int v : a) {
c0[(v & 0xff) + 1]++;
c1[(v >>> 8 & 0xff) + 1]++;
c2[(v >>> 16 & 0xff) + 1]++;
c3[(v >>> 24 ^ 0x80) + 1]++;
}
for (int i = 0; i < 0xff; i++) {
c0[i + 1] += c0[i];
c1[i + 1] += c1[i];
c2[i + 1] += c2[i];
c3[i + 1] += c3[i];
}
int[] t = new int[n];
for (int v : a) t[c0[v & 0xff]++] = v;
for (int v : t) a[c1[v >>> 8 & 0xff]++] = v;
for (int v : a) t[c2[v >>> 16 & 0xff]++] = v;
for (int v : t) a[c3[v >>> 24 ^ 0x80]++] = v;
return a;
}
static int[] computeLps(String pat) {
int len = 0, i = 1, m = pat.length();
int lps[] = new int[m];
lps[0] = 0;
while (i < m) {
if (pat.charAt(i) == pat.charAt(len)) {
++len;
lps[i] = len;
++i;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = len;
++i;
}
}
}
return lps;
}
static ArrayList<Integer> kmp(String s, String pat) {
ArrayList<Integer> al = new ArrayList<>();
int n = s.length(), m = pat.length();
int lps[] = computeLps(pat);
int i = 0, j = 0;
while (i < n) {
if (s.charAt(i) == pat.charAt(j)) {
i++;
j++;
if (j == m) {
al.add(i - j);
j = lps[j - 1];
}
} else {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return al;
}
static ArrayList<Long> primeFact(long n) {
ArrayList<Long> al = new ArrayList<>();
al.add(1L);
while (n % 2 == 0) {
if (!al.contains(2L)) {
al.add(2L);
}
n /= 2L;
}
for (long i = 3; (long) i * i <= n; i += 2) {
while ((n % i == 0)) {
if (!al.contains((long) i)) {
al.add((long) i);
}
n /= i;
}
}
if (n > 2) {
if (!al.contains(n)) {
al.add(n);
}
}
return al;
}
public static long totFact(long n)
{
long cnt = 0, tot = 1;
while (n % 2 == 0) {
n /= 2;
++cnt;
}
tot *= (cnt + 1);
for (int i = 3; i <= Math.sqrt(n); i += 2) {
cnt = 0;
while (n % i == 0) {
n /= i;
++cnt;
}
tot *= (cnt + 1);
}
if (n > 2) {
tot *= 2;
}
return tot;
}
static int[] z_function(String s)
{
int n = s.length(), z[] = new int[n];
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = Math.min(z[i - l], r - i + 1);
while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i]))
++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
static void fast_swap(int[] a, int idx1, int idx2) {
if (a[idx1] == a[idx2])
return;
a[idx1] ^= a[idx2];
a[idx2] ^= a[idx1];
a[idx1] ^= a[idx2];
}
public static void fast_sort(long[] array) {
ArrayList<Long> copy = new ArrayList<>();
for (long i : array)
copy.add(i);
Collections.sort(copy);
for (int i = 0; i < array.length; i++)
array[i] = copy.get(i);
}
static int factorsCount(int n) {
boolean hash[] = new boolean[n + 1];
Arrays.fill(hash, true);
for (int p = 2; p * p < n; p++)
if (hash[p] == true)
for (int i = p * 2; i < n; i += p)
hash[i] = false;
int total = 1;
for (int p = 2; p <= n; p++) {
if (hash[p]) {
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
static long binomialCoeff(long n, long k) {
long res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i) {
res = (res * (n - i));
res /= (i + 1);
}
return res;
}
static long nck(long fact[], long inv[], long n, long k) {
if (k > n) return 0;
long res = fact[(int) n]%mod;
res = (int) ((res%mod* inv[(int) k]%mod))%mod;
res = (int) ((res%mod*inv[(int) (n - k)]%mod))%mod;
return res % mod;
}
public static long fact(long x) {
long fact = 1;
for (int i = 2; i <= x; ++i) {
fact = fact * i;
}
return fact;
}
public static ArrayList<Long> getFact(long x) {
ArrayList<Long> facts = new ArrayList<>();
facts.add(1L);
for (long i = 2; i * i <= x; ++i) {
if (x % i == 0) {
facts.add(i);
if (i != x / i) {
facts.add(x / i);
}
}
}
return facts;
}
static void matrix_ex(long n, long[][] A, long[][] I) {
while (n > 0) {
if (n % 2 == 0) {
Multiply(A, A);
n /= 2;
} else {
Multiply(I, A);
n--;
}
}
}
static void Multiply(long[][] A, long[][] B) {
int n = A.length, m = A[0].length, p = B[0].length;
long[][] C = new long[n][p];
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < m; k++) {
C[i][j] += ((A[i][k] % mod) * (B[k][j] % mod)) % mod;
C[i][j] = C[i][j] % mod;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
A[i][j] = C[i][j];
}
}
}
public static HashMap<Integer,Integer> sortIntMapDesc(HashMap<Integer,Integer> map) {
List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) ->(int) (o2.getValue() - o1.getValue()));
HashMap<Integer,Integer> temp = new LinkedHashMap<>();
for (Map.Entry<Integer,Integer> i : list) {
temp.put(i.getKey(),i.getValue());
}
return temp;
}
public static HashMap<Long, Integer> sortMapDesc(HashMap<Long,Integer> map) {
List<Map.Entry<Long, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue());
HashMap<Long, Integer> temp = new LinkedHashMap<>();
for (Map.Entry<Long, Integer> i : list) {
temp.put(i.getKey(), i.getValue());
}
return temp;
}
public static HashMap<Character, Integer> sortMapDescch(HashMap<Character, Integer> map) {
List<Map.Entry<Character, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue());
HashMap<Character, Integer> temp = new LinkedHashMap<>();
for (Map.Entry<Character, Integer> i : list) {
temp.put(i.getKey(), i.getValue());
}
return temp;
}
public static HashMap<Long,Long> sortMapAsclng(HashMap<Long,Long> map) {
List<Map.Entry<Long,Long>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> (int)(o1.getValue() - o2.getValue()));
HashMap<Long,Long> temp = new LinkedHashMap<>();
for (Map.Entry<Long,Long> i : list) {
temp.put(i.getKey(), i.getValue());
}
return temp;
}
public static HashMap<Integer, Integer> sortMapAsc(HashMap<Integer, Integer> map) {
List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> o1.getValue() - o2.getValue());
HashMap<Integer, Integer> temp = new LinkedHashMap<>();
for (Map.Entry<Integer, Integer> i : list) {
temp.put(i.getKey(), i.getValue());
}
return temp;
}
public static long lcm(long l, long l2) {
long val = gcd(l, l2);
return (l * l2) / val;
}
public static boolean isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
if (m > n) {
return false;
}
int i = 0, j = 0, skip = 0;
while (i < n && j < m) {
if (s.charAt(i) == t.charAt(j)) {
--skip;
++j;
}
++skip;
++i;
}
return j==m;
}
public static long[][] combination(int l, int r) {
long[][] pascal = new long[l + 1][r + 1];
pascal[0][0] = 1;
for (int i = 1; i <= l; ++i) {
pascal[i][0] = 1;
for (int j = 1; j <= r; ++j) {
pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
}
}
return pascal;
}
public static long gcd(long... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]);
return ret;
}
public static long min(long... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]);
return ret;
}
public static long max(long a, long b) {
return a > b ? a : b;
}
public static long max(long... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]);
return ret;
}
public static long sum(long... array) {
long ret = 0;
for (long i : array) ret += i;
return ret;
}
public static long[] facts(int n,long m)
{
long[] fact=new long[n];
fact[0]=1;
fact[1]=1;
for(int i=2;i<n;i++)
{
fact[i]=(long)(i*fact[i-1])%m;
}
return fact;
}
public static long[] inv(long[] fact,int n,long mod)
{
long[] inv=new long[n];
inv[n-1]= CP.Modular_Expo(fact[n-1],mod-2,mod)%mod;
for(int i=n-2;i>=0;--i)
{
inv[i]=(inv[i+1]*(i+1))%mod;
}
return inv;
}
public static int modinv(long x, long mod) {
return (int) (CP.Modular_Expo(x, mod - 2, mod) % mod);
}
}
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(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void printLine(long[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
public void printLine(int i) {
writer.println(i);
}
public void printLine(char c) {
writer.println(c);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void py()
{
print("YES");
writer.println();
}
public void pn()
{
print("NO");
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
void flush() {
writer.flush();
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
5755e35bfd2796c24fd14599244fc291
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class G_Even_Odd_XOR {
public static void s() {
int n = sc.nextInt();
int[] arr = new int[n];
int curr = 1;
int xor = 0;
for(int i=0; i<n; i+=2) {
arr[i] = curr++;
xor^=arr[i];
}
int nxor = 0;
for(int i=1; i<n; i+=2) {
arr[i] = curr++;
nxor^=arr[i];
}
if((n&1) == 0) {
nxor^=arr[n-1];
arr[n-1] = (xor^nxor);
} else {
nxor^=arr[n-2];
arr[n-2] = (xor^nxor);
}
arr[n-1]|=(1<<28);
arr[n-2]|=(1<<28);
p.writes(arr);
p.writeln();
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
4bcd6776d536ba2be89f2383181531cb
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.*;
import java.util.ArrayList;
public class Main {
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());
String bb = "4 1 2 12 3 8 ";
int bbb = 1<<21;
int bbbb = 1<<22;
bbb++;
bbbb++;
C: while(testcase-->0){
int N = Integer.parseInt(br.readLine());
ArrayList<Integer> n1 = new ArrayList<>() ,n2 = new ArrayList<>();
if (N==3) {
bw.write("2 1 3\n");
continue C;
}
if (N%2==1) {
bw.write("0 ");
N--;
}
if ((N/2) %2 == 1) {
bw.write(bb);
N -= 6;
}
if (N==0) {
bw.write('\n');
continue C;
}
N /= 2;
for (int i=0;i<N;i++) {
bw.write(bbb+i+" ");
bw.write(bbbb+i+" ");
}
bw.write('\n');
}
bw.flush();
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
41e96bb06cc3bc620d150f56d4c6a86c
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.util.*;
public class Codeforces {
static final int MOD = (int)1e9 + 7;
// Function to return (x^y) % MOD
// in O(log(y))
static long power(long x,
long y)
{
long res = 1;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % MOD;
x = (x * x) % MOD;
y /= 2;
}
return res;
}
static int lbound(int arr[], int key)
{
int l=0,h=arr.length-1;
while(l<h)
{
int mid=l+(h-l+1)/2;
if(arr[mid]<=key)
l=mid;
else if(key<arr[mid])
h=mid-1;
System.out.println(l+" "+h);
}
if(arr[l]<key)
l= l+1;
return l;
}
static int ubound(int arr[], int key)
{
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int ans[]= new int[n];
StringBuilder sb= new StringBuilder("");
int p=0;
if(n%4==1)
{
ans[0]=0;
p=1;
}
else if(n%4==2)
{
ans[0]=4;ans[1]=1;ans[2]=2;ans[3]=12;ans[4]=3;ans[5]=8;
p=6;
}
else if(n%4==3)
{
ans[0]=1;
ans[1]=3;
ans[2]=2;
p=3;
}
int q=16;
for(int i=p;i<n;i++)
{
ans[i]=q;
q++;
}
for(int i=0;i<n;i++)
{
sb.append(ans[i]+" ");
}
System.out.println(sb);
}
sc.close();
}
}
class Pair{
int x; int y;
Pair(int x, int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return x+" "+y;
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
987abdf06f01b56e5a9e2ded75558271
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class G {
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 n = sc.nextInt();
int [] arr = new int [n];
for (int i = 2; i < n; ++i) {
arr[i] = 2*i;
}
arr[2] = 1;
int change = 1;
int modified = 0;
int xorEven = 0;
int xorOdd = 0;
Set<Integer> visited = new HashSet<>();
for (int i = 2; i < n; i += 2) {
xorEven ^= arr[i];
}
for (int i = 3; i < n; i += 2) {
xorOdd ^= arr[i];
}
arr[1] = (int) Math.pow(2, 20);
xorOdd ^= arr[1];
arr[0] = xorOdd ^ xorEven;
xorEven ^= arr[0];
StringBuilder sb = new StringBuilder();
for (int num : arr) {
sb.append(num);
sb.append(' ');
}
sb.setLength(sb.length() - 1);
//System.out.println(xorOdd + " " + xorEven);
System.out.println(sb);
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
9026ec70f545ba51a76f0eff9650da7f
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class R817G {
public static void main(String[] args) {
JS scan = new JS();
int t = scan.nextInt();
while (t-- > 0) {
int n = scan.nextInt();
int xor1 = 0;
int xor2 = 0;
int[] ans = new int[n];
int start = 1;
for (int i = 0; i < n - 2; i++) {
if (i % 2 == 0) {
if ((xor1 ^ start) == xor2) {
start++;
}
xor1 ^= start;
ans[i] = start;
} else {
if ((xor2 ^ start) == xor1) {
start++;
}
xor2 ^= start;
ans[i] = start;
}
start++;
}
if (n % 2 == 1) {
xor2 ^= (1 << 30) - 1;
} else {
xor1 ^= (1 << 30) - 1;
}
int need = xor1 ^ xor2;
xor1 ^= need;
ans[n - 2] = (1 << 30) - 1;
ans[n - 1] = need;
for (int i = 0; i < n; i++) {
System.out.print(ans[i] + " ");
}
System.out.println();
//System.out.println();
//System.out.println(xor1 + " " + xor2);
}
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
b730b7195afbddc83f8082d14207f879
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class CF_817 {
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[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -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();
while (t-- > 0) {
solve();
}
fw.out.close();
}
private static void solve() {
int n = fs.nextInt();
int[] ans = new int[n];
int num = (1 << 30);
int xor = 0;
for (int i = 0; i < n; i += 2) {
ans[i] = num;
xor ^= num;
num--;
}
int temp = 0;
num = n + 1;
for (int i = 1; ; i += 2) {
if (i + 2 >= n) {
int curr = xor ^ temp;
ans[i] = curr;
break;
}
ans[i] = num;
temp ^= num;
num++;
}
for (int xx: ans) {
fw.out.print(xx + " ");
}
fw.out.println();
}
private static void dekho(int i, int n, List<Integer> list, int mask) {
if (i >= n) {
int xor1 = 0, xor2 = 0;
for (int ii = 0; ii < n; ii++) {
if ((ii & 1) == 0) xor1 ^= list.get(ii);
else xor2 ^= list.get(ii);
}
if (xor1 == xor2) fw.out.println(list);
return;
}
for (int ii = 0; ii <= 20; ii++) {
if (((mask >> ii) & 1) == 1) continue;
list.add(ii);
dekho(i + 1, n, list, mask | (1 << ii));
list.remove(list.size() - 1);
}
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
63ddd73822930be9c63b7b30e4340059
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
// Even-Odd XOR
//
// Codeforces
// Solution by Anmol Sharma
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static long mod = 1000000007;
public static long mod2 = 998244353;
public static void main(String[] args) throws java.lang.Exception {
Reader sc = new Reader();
FastNum in = new FastNum(System.in);
Writer out = new Writer(System.out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tests = in.nextInt();
for (int test = 1; test <= tests; test++) {
int n = in.nextInt();
int[] a = new int[n];
if (n % 4 == 0) {
for (int i = 0; i < n; i++) {
a[i] = i;
}
} else if (n % 4 == 3) {
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
} else if (n % 4 == 1) {
int curr = 0;
for (int i = 0; i < n; i++) {
if (curr == 1) {
curr++;
}
a[i] = curr;
curr++;
}
} else {
for (int i = 0; i < n - 2; i++) {
a[i] = i + 1;
}
int t = n - 2;
a[n - 2] = t << 1;
a[n - 1] = (t << 1) ^ t;
}
out.printArray(a);
}
out.flush();
}
static long modSum(long p, long q) {
if (q > 0) {
return (p + q) % mod;
} else {
return (p + q + mod) % mod;
}
}
static long invMod(long p, long q, long m) {
long expo = m - 2;
while (expo != 0) {
if ((expo & 1) == 1) {
p = (p * q) % m;
}
q = (q * q) % m;
expo >>= 1;
}
return p;
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long power(long a, long b) {
if (b == 0)
return 1;
long answer = power(a, b / 2) % mod;
answer = (answer * answer) % mod;
if (b % 2 != 0)
answer = (answer * a) % mod;
return answer;
}
public static void swap(int x, int y) {
int t = x;
x = y;
y = t;
}
public static long min(long a, long b) {
if (a < b) return a;
return b;
}
public static long divide(long a, long b) {
return (a % mod * (power(b, mod - 2) % mod)) % mod;
}
public static long nCr(long n, long r) {
long answer = 1;
long k = min(r, n - r);
for (long i = 0; i < k; i++) {
answer = (answer % mod * (n - i) % mod) % mod;
answer = divide(answer, i + 1);
}
return answer % mod;
}
public static boolean plaindrome(String str) {
StringBuilder sb = new StringBuilder();
sb.append(str);
return (str.equals((sb.reverse()).toString()));
}
public static class Pair {
int a;
int b;
Pair(int s, int e) {
a = s;
b = e;
}
static void sort(Pair[] a) {
Arrays.sort(a, (o1, o2) -> {
if (o1.a == o2.a) {
return o1.b - o2.b;
} else {
return o1.a - o2.a;
}
});
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
}
static class Assert {
static void check(boolean e) {
if (!e) {
throw new AssertionError();
}
}
}
static class FastNum implements AutoCloseable {
InputStream is;
byte buffer[] = new byte[1 << 16];
int size = 0;
int pos = 0;
FastNum(InputStream is) {
this.is = is;
}
int nextChar() {
if (pos >= size) {
try {
size = is.read(buffer);
} catch (IOException e) {
throw new IOError(e);
}
pos = 0;
if (size == -1) {
return -1;
}
}
Assert.check(pos < size);
int c = buffer[pos] & 0xFF;
pos++;
return c;
}
int nextInt() {
int c = nextChar();
while (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
c = nextChar();
}
if (c == '-') {
c = nextChar();
Assert.check('0' <= c && c <= '9');
int n = -(c - '0');
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(n > Integer.MIN_VALUE / 10
|| n == Integer.MIN_VALUE / 10 && d <= -(Integer.MIN_VALUE % 10));
n = n * 10 - d;
}
return n;
} else {
Assert.check('0' <= c && c <= '9');
int n = c - '0';
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(
n < Integer.MAX_VALUE / 10 || n == Integer.MAX_VALUE / 10 && d <= Integer.MAX_VALUE % 10);
n = n * 10 + d;
}
return n;
}
}
char nextCh() {
int c = nextChar();
while (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
c = nextChar();
}
return (char) c;
}
long nextLong() {
int c = nextChar();
while (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
c = nextChar();
}
if (c == '-') {
c = nextChar();
Assert.check('0' <= c && c <= '9');
long n = -(c - '0');
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(n > Long.MIN_VALUE / 10
|| n == Long.MIN_VALUE / 10 && d <= -(Long.MIN_VALUE % 10));
n = n * 10 - d;
}
return n;
} else {
Assert.check('0' <= c && c <= '9');
long n = c - '0';
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(
n < Long.MAX_VALUE / 10 || n == Long.MAX_VALUE / 10 && d <= Long.MAX_VALUE % 10);
n = n * 10 + d;
}
return n;
}
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
char[] nextCharArray(int n) {
char[] arr = new char[n];
for (int i = 0; i < n; i++) {
arr[i] = nextCh();
}
return arr;
}
@Override
public void close() {
}
}
static class Writer extends PrintWriter {
public Writer(java.io.Writer out) {
super(out);
}
public Writer(java.io.Writer out, boolean autoFlush) {
super(out, autoFlush);
}
public Writer(OutputStream out) {
super(out);
}
public Writer(OutputStream out, boolean autoFlush) {
super(out, autoFlush);
}
public void printArray(int[] arr) {
for (int j : arr) {
print(j);
print(' ');
}
println();
}
public void printArray(long[] arr) {
for (long j : arr) {
print(j);
print(' ');
}
println();
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
4b606470a2529efc36bbb17a5fe34173
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
256 megabytes
|
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
// graph, dfs,bfs, get connected components,iscycle, isbipartite, dfs on trees
public class Solution{
static class Graph{
public static class Vertex{
HashMap<Integer,Long> nb= new HashMap<>(); // for neighbours of each vertex
}
public static HashMap<Integer,Vertex> vt; // for vertices(all)
public Graph(){
vt= new HashMap<>();
}
public static int numVer(){
return vt.size();
}
public static boolean contVer(int ver){
return vt.containsKey(ver);
}
public static void addVer(int ver){
Vertex v= new Vertex();
vt.put(ver,v);
}
public static void addEdge(int ver1, int ver2, long weight){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
Vertex v1= vt.get(ver1);
Vertex v2= vt.get(ver2);
v1.nb.put(ver2,(long)weight); // if previously there is an edge, then this replaces that edge
v2.nb.put(ver1,(long)weight);
}
public static void delEdge(int ver1, int ver2){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
vt.get(ver1).nb.remove(ver2);
vt.get(ver2).nb.remove(ver1);
}
public static void delVer(int ver){
if(!vt.containsKey(ver)){
return;
}
Vertex v1= vt.get(ver);
ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int s= arr.get(i);
vt.get(s).nb.remove(ver);
}
vt.remove(ver);
}
static boolean done[];
static int parent[];
static ArrayList<Integer>vals= new ArrayList<>();
public static boolean isCycle(int i){
Stack<Integer>stk= new Stack<>();
stk.push(i);
while(!stk.isEmpty()){
int x= stk.pop();
vals.add(x);
// System.out.print("current="+x+" stackinit="+stk);
if(!done[x]){
done[x]=true;
}
else if(done[x] ){
return true;
}
ArrayList<Integer>ar= new ArrayList<>(vt.get(x).nb.keySet());
for (int j = 0; j <ar.size() ; j++) {
if(parent[x]!=ar.get(j)){
parent[ar.get(j)]=x;
stk.push(ar.get(j));
}
}
// System.out.println(" stackfin="+stk);
}
return false;
}
static int[]level;
static int[] curr;
static int[] fin;
public static void dfs(int src){
done[src]=true;
level[src]=0;
Queue<Integer>q= new LinkedList<>();
q.add(src);
while(!q.isEmpty()){
int x= q.poll();
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
level[v]=level[x]+1;
done[v]=true;
q.offer(v);
}
}
}
}
static int oc[];
static int ec[];
public static void dfs1(int src){
Queue<Integer>q= new LinkedList<>();
q.add(src);
done[src]= true;
// int count=0;
while(!q.isEmpty()){
int x= q.poll();
// System.out.println("x="+x);
int even= ec[x];
int odd= oc[x];
if(level[x]%2==0){
int val= (curr[x]+even)%2;
if(val!=fin[x]){
// System.out.println("bc");
even++;
vals.add(x);
}
}
else{
int val= (curr[x]+odd)%2;
if(val!=fin[x]){
// System.out.println("bc");
odd++;
vals.add(x);
}
}
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
// System.out.println(arr);
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
done[v]=true;
oc[v]=odd;
ec[v]=even;
q.add(v);
}
}
}
}
static long popu[];
static long happy[];
static long count[]; // total people crossing that pos
static long sum[]; // total sum of happy people including that.
HashMap<Integer, ArrayList<Integer>>map= new HashMap<>();
public static void bfs(int x,ArrayList<Long>curr){
}
public static void bfs1(int x){
done[x]=true;
// long total= popu[x];
long smile= 0;
ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <nbrs.size() ; i++) {
int r= nbrs.get(i);
if(!done[r]){
bfs1(r);
// total+=count[r];
smile+=happy[r];
}
}
// count[x]=total;
sum[x]=smile;
}
}
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
static class Pair implements Comparable<Pair> {
int val;
int pos;
public Pair(int val, int pos) {
this.pos = pos;
this.val = val;
}
@Override
public String toString() {
return val + " " + pos;
}
@Override
public int compareTo(Pair o) {
if(this.pos >o.pos){
return 1;
}
else if(this.pos==o.pos){
return 0;
}
else{
return -1;
}
}}
static class Pair1 implements Comparable<Pair1> {
int val;
int pos;
public Pair1(int val, int pos) {
this.pos = pos;
this.val = val;
}
@Override
public String toString() {
return val + " " + pos;
}
@Override
public int compareTo(Pair1 o) {
if(this.val >o.val){
return 1;
}
else if(this.val==o.val){
return 0;
}
else{
return -1;
}
}
}
static ArrayList<Pair> findSubArrays(long[] arr, int n)
{
// create an empty map
HashMap<Long,ArrayList<Integer>> map = new HashMap<>();
// create an empty vector of pairs to store
// subarray starting and ending index
ArrayList<Pair> out = new ArrayList<>();
// Maintains sum of elements so far
long sum = 0;
for (int i = 0; i < n; i++)
{
// add current element to sum
sum += arr[i];
// if sum is 0, we found a subarray starting
// from index 0 and ending at index i
if (sum == 0)
out.add(new Pair(0, i));
ArrayList<Integer> al = new ArrayList<>();
// If sum already exists in the map there exists
// at-least one subarray ending at index i with
// 0 sum
if (map.containsKey(sum))
{
// map[sum] stores starting index of all subarrays
al = map.get(sum);
for (int it = 0; it < al.size(); it++)
{
out.add(new Pair(al.get(it) + 1, i));
}
}
al.add(i);
map.put(sum, al);
}
return out;
}
// After writing solution, quick scan for:
// array out of bounds
// special cases e.g. n=1?
//
// Big numbers arithmetic bugs:
// int overflow
// sorting, or taking max, or negative after MOD
static long mod= (long)(1e8);
static long dp[][][][];
static BufferedWriter out;
public static void main(String[] args) throws IOException {
Reader.init(System.in);
out = new BufferedWriter(new OutputStreamWriter(System.out));
int t= Reader.nextInt();
for (int tt = 0; tt <t ; tt++) {
int n= Reader.nextInt();
findArray(n,0);
out.append("\n");
}
out.flush();
out.close();
}
static void findArray(int N, int K) throws IOException {
// Base Cases
if (N == 1)
{
out.append(K + " ");
return;
}
if (N == 2)
{
out.append(0 + " " + K);
return;
}
// Assign values to P and Q
int P = N - 2;
int Q = N - 1;
// Stores Bitwise XOR of the
// first (N - 3) elements
int VAL = 0;
// Print the first N - 3 elements
for(int i = 1; i <= (N - 3); i++)
{
out.append(i + " ");
// Calculate Bitwise XOR of
// first (N - 3) elements
VAL ^= i;
}
if (VAL == K)
{ // checking whether P ^ Q is greater than P or not.
while( (P ^ Q) <= P)
{ Q++;
}
out.append(P + " " +
Q + " " + (P ^ Q));
}
else
{ // checking whether P ^ K ^ VAL is greater than N-2 or not.
while( (P ^ K ^ VAL) <= N-2)
{ P++;
}
out.append(0 + " " +
P + " " +
(P ^ K ^ VAL));
}
}
public static String convert(String s,int n){
if(s.length()==n){
return s;
}
else{
int x= s.length();
int v=n-x;
String str="";
StringBuilder s2= new StringBuilder();
for (int i = 0; i <v ; i++) {
s2.append('0');
}
StringBuilder s1= new StringBuilder(s);
s2.append(s1);
// str+=s;
String q= s2.toString();
return q;
}
}
public static int lower(ArrayList<Integer>arr,int key){
int low = 0;
int high = arr.size()-1;
while(low < high){
int mid = low + (high - low)/2;
if(arr.get(mid) >= key){
high = mid;
}
else{
low = mid+1;
}
}
return low;
}
public static int upper(ArrayList<Integer>arr,int key){
int low = 0;
int high = arr.size()-1;
while(low < high){
int mid = low + (high - low+1)/2;
if(arr.get(mid) <= key){
low = mid;
}
else{
high = mid-1;
}
}
return low;
}
static long modExp(long a, long b, long mod) {
//System.out.println("a is " + a + " and b is " + b);
if (a==1) return 1;
long ans = 1;
while (b!=0) {
if (b%2==1) {
ans = (ans*a)%mod;
}
a = (a*a)%mod;
b/=2;
}
return ans;
}
public static long modmul(long a, long b, long mod) {
return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod;
}
static long sum(long n){
// System.out.println("lol="+ (n*(n-1))/2);
return (n*(n+1))/2;
}
public static ArrayList<Integer> Sieve(int n) {
boolean arr[]= new boolean [n+1];
Arrays.fill(arr,true);
arr[0]=false;
arr[1]=false;
for (int i = 2; i*i <=n ; i++) {
if(arr[i]){
for (int j = 2; j <=n/i ; j++) {
int u= i*j;
arr[u]=false;
}}
}
ArrayList<Integer> ans= new ArrayList<>();
for (int i = 0; i <n+1 ; i++) {
if(arr[i]){
ans.add(i);
}
}
return ans;
}
static long power( long x, long y, long p)
{
long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ceil_div(long a, long b){
return (a+b-1)/b;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a*b)/gcd(a, b);
}}
|
Java
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
361049874cec200fc9742e28c77d5cc5
|
train_109.jsonl
|
1661871000
|
Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
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;
public void run() throws IOException {
t=in.in();
while(t-->0){
n=in.in();
if(n==3) {
out.println("2 1 3");
continue;
}
if(n%2!=0) {
n--;
out.print("0 ");
}
int p=1;
for(int tmp=n>>1;tmp>0;tmp>>=1) {
p<<=1;
}
if(n/2%2==0) {
k=1;
for(i=0;i<n;i+=2) {
out.print((k|p)+" ");
out.print(k+++" ");
// a[i]=k|p;
// a[i+1]=k++;
}
}else {
k=1;
out.print((k|(p<<1))+" ");
out.print(k+++" ");
out.print((k|(p<<1)|p)+" ");
out.print(k+++" ");
// a[0]=k|(p<<1);
// a[1]=k++;
// a[2]=k|(p<<1)|p;
// a[3]=k++;
for(i=4;i<n;i+=2) {
out.print((k|p)+" ");
out.print(k+++" ");
// a[i]=k|p;
// a[i+1]=k++;
}
}
out.println();
}
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
|
["7\n8\n3\n4\n5\n6\n7\n9"]
|
1 second
|
["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"]
|
NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.
|
Java 11
|
standard input
|
[
"bitmasks",
"constructive algorithms",
"greedy"
] |
52bd5dc6b92b2af5aebd387553ef4076
|
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
| 1,500
|
For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.
|
standard output
| |
PASSED
|
e6268a6922c33f6291ddfa5c5638e016
|
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 static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
public class F {
private static int m, n;
private static int[][] vis;
public static void process() throws IOException {
n = sc.nextInt();
m = sc.nextInt();
char[][] arr = new char[n][m];
for(int i = 0; i<n; i++)arr[i] = sc.next().toCharArray();
vis = new int[n][m];
for(int i = 0; i<n; i++) {
for(int j = 0; j+1<m; j++) {
char f = arr[i][j], s = arr[i][j+1];
if(f == '*' && s == '*') {
if(vis[i][j] == 1 || vis[i][j+1] == 1) {
System.out.println("NO");
return;
}
vis[i][j] = 1;
vis[i][j+1] = 1;
}
}
}
for(int i = 0; i<n; i++) {
for(int j = 0; j+1<m; j++) {
if(vis[i][j] != 1)continue;
ArrayList<Pair> lis = new ArrayList<F.Pair>();
int nextx = -1, nexty = -1;
if(i-1>=0 && arr[i-1][j] == '*') {
if(vis[i-1][j] != 0) {
System.out.println("NO");
return;
}
lis.add(new Pair(i-1, j));
nextx = i-1; nexty = j+1;
}
if(i-1>=0 && arr[i-1][j+1] == '*') {
if(vis[i-1][j+1] != 0) {
System.out.println("NO");
return;
}
lis.add(new Pair(i-1, j+1));
nextx = i-1; nexty = j;
}
if(i+1<n && arr[i+1][j] == '*') {
if(vis[i+1][j] != 0) {
System.out.println("NO");
return;
}
lis.add(new Pair(i+1, j));
nextx = i+1; nexty = j+1;
}
if(i+1<n && arr[i+1][j+1] == '*') {
if(vis[i+1][j+1] != 0) {
System.out.println("NO");
return;
}
lis.add(new Pair(i+1, j+1));
nextx = i+1; nexty = j;
}
if(lis.size() != 1) {
System.out.println("NO");
return;
}
lis.add(new Pair(i, j));
lis.add(new Pair(i, j+1));
for(int k = 0; k<lis.size(); k++) {
Pair e = lis.get(k);
if(check(e, lis, arr)) {
System.out.println("NO");
return;
}
vis[e.x][e.y] = 2;
}
}
}
for(int i = 0; i<n; i++) {
for(int j = 0; j<m; j++) {
if(arr[i][j] == '*' && vis[i][j] != 2) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
}
private static boolean check(Pair e, ArrayList<Pair> lis, char[][] arr) {
int x = e.x, y = e.y;
ArrayList<Pair> my = new ArrayList<F.Pair>();
if(x-1>=0) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x-1 && mm.y == y) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x-1, y));
}
if(y-1>=0) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x && mm.y == y-1) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x, y-1));
}
if(x+1<n) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x+1 && mm.y == y) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x+1, y));
}
if(y+1<m) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x && mm.y == y+1) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x, y+1));
}
if(x+1<n && y+1<m) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x+1 && mm.y == y+1) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x+1, y+1));
}
if(x-1>=0 && y-1>=0) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x-1 && mm.y == y-1) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x-1, y-1));
}
if(x-1>=0 && y+1<m) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x-1 && mm.y == y+1) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x-1, y+1));
}
if(x+1<n && y-1>=0) {
int cc = 0;
for(Pair mm: lis) {
if(mm.x == x+1 && mm.y == y-1) {
cc = 1;
}
}
if(cc == 0)my.add(new Pair(x+1, y-1));
}
for(Pair o : my) {
if(arr[o.x][o.y] == '*')return true;
}
return false;
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readArrayDouble(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
}
|
Java
|
["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 17
|
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
|
2180faa941ab0b97247992719ddcc6fb
|
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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.ToIntFunction;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Codeforces {
static final Set<Integer> VALID = Set.of(
0b000_010_011,
0b000_010_110,
0b000_110_100,
0b100_110_000,
0b110_010_000,
0b011_010_000,
0b001_011_000,
0b000_011_001,
0b000_011_010,
0b000_110_010,
0b010_110_000,
0b010_011_000
);
static void solve() {
int t = i32();
while (t-- > 0) {
if (isValid()) {
print("YES");
} else {
print("NO");
}
}
}
private static boolean isValid() {
int n = i32(), m = i32();
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++) {
mat[i] = str().toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == '*') {
int pat = 0;
for (var dr = -1; dr <= 1; dr++) {
for (var dc = -1; dc <= 1; dc++) {
int p = (1 + dr) * 3 + (1 + dc);
int r = i + dr, c = j + dc;
if ((0 <= r && r < n) && (0 <= c && c < m) && mat[r][c] == '*') {
pat |= (1 << p);
}
}
}
if (!VALID.contains(pat) || bfs(mat, i, j) != 3) return false;
}
}
}
return true;
}
private static int bfs(char[][] mat, int i, int j) {
int n = mat.length, m = mat[0].length;
if ((0 <= i && i < n) && (0 <= j && j < m) && mat[i][j] == '*') {
int result = 1;
mat[i][j] = '.';
for (var dr = -1; dr <= 1; dr++) {
for (var dc = -1; dc <= 1; dc++) {
if (Math.abs(dr) + Math.abs(dc) != 0 /* all directions */) {
result += bfs(mat, i + dr, j + dc);
}
}
}
return result;
} else {
return 0;
}
}
//####################################################################################################################
private static final Scanner _IN_ = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
private static final PrintStream _OUT_ = new PrintStream(new BufferedOutputStream(System.out));
public static void main(String[] args) {
try (_IN_; _OUT_) { solve(); }
catch (Exception e) { print(e.getMessage()); throw e; }
}
// ARRAY #############################################################################################################
private static <T> T[] array(int n, Class<T> type, IntFunction<T> fun) {
@SuppressWarnings("unchecked") T[] array = (T[]) Array.newInstance(type, n);
for (int i = 0; i < n; i++) array[i] = fun.apply(i); return array;
}
private static int[] array(int n, int d) { int[] array = new int[n]; Arrays.fill(array, d); return array; }
private static int fst(int[] arr) { return arr[0]; }
private static int last(int[] arr) { return arr[arr.length - 1]; }
private static void swap(int[] arr, int i, int j) { int x = arr[i]; arr[i] = arr[j]; arr[j] = x; }
private static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) swap(arr, (int) (Math.random() * arr.length), i); }
private static Stream<pair> pairs(int[] as) {
int n = as.length;
if (n < 2) return Stream.empty();
return Stream.iterate(new pair(0, 1), it -> it.fst < n - 1,
it -> it.snd + 1 == n ? new pair(it.fst + 1, it.fst + 2) : new pair(it.fst, it.snd + 1));
}
private static Stream<pair> cartesian(int m, int n) {
return Stream.iterate(new pair(0, 0), it -> it.fst < m,
it -> it.snd + 1 == n ? new pair(it.fst + 1, 0) : new pair(it.fst, it.snd + 1));
}
// DS ################################################################################################################
private static class pair {
final int fst, snd;
pair(int fst, int snd) { this.fst = fst; this.snd = snd; }
public String toString() { return fst + " " + snd; }
boolean neq(pair p) { return fst != p.fst || snd != p.snd; }
}
private static class triple {
final int fst, snd, thd;
triple(int fst, int snd, int thd) { this.fst = fst; this.snd = snd; this.thd = thd; }
public String toString() { return fst + " " + snd + " " + thd; }
}
// GEOMETRY ##########################################################################################################
private static double dist(int x1, int y1, int x2, int y2) { return Math.sqrt(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2)); }
// INPUT #############################################################################################################
private static int i32() { return _IN_.nextInt(); }
private static int[] i32(int n) { return IntStream.generate(Codeforces::i32).limit(n).toArray(); }
private static String str() { return _IN_.next(); }
private static String[] str(int n) { return Stream.generate(Codeforces::str).limit(n).toArray(String[]::new); }
// ITERATION #########################################################################################################
private static IntStream iter(int[] arr) { return Arrays.stream(arr); }
private static IntStream iter(int l, int r) { return IntStream.range(l, r); }
private static IntStream iter(int l, IntUnaryOperator next) { return IntStream.iterate(l, next); }
private static class Window<T> {
T[] arr; int i, sz;
Window(T[] arr, int sz) { this.arr = arr; this.sz = sz; }
T get(int j) { return arr[i + j]; }
T fst() { return arr[i]; }
T snd() { return arr[i + 1]; }
boolean hasNext() { return i + sz <= arr.length; }
public Window<T> next() { i++; return this; }
}
private static class IntWindow {
int[] arr; int i, sz;
IntWindow(int[] arr, int sz) { this.arr = arr; this.sz = sz; }
int get(int j) { return arr[i + j]; }
int fst() { return arr[i]; }
int snd() { return arr[i + 1]; }
boolean hasNext() { return i + sz <= arr.length; }
public IntWindow next() { i++; return this; }
}
private static <T> Stream<Window<T>> windows(T[] arr, int sz) {
if (sz > arr.length) return Stream.empty();
return Stream.iterate(new Window<>(arr, sz), Window::hasNext, Window::next);
}
private static Stream<IntWindow> windows(int[] arr, int sz) {
if (sz > arr.length) return Stream.empty();
return Stream.iterate(new IntWindow(arr, sz), IntWindow::hasNext, IntWindow::next);
}
// NUMBER ############################################################################################################
private static final int INF = Integer.MAX_VALUE;
private static int sum(int[] xs) { return iter(xs).sum(); }
private static int sum(int[] xs, int l) { return Arrays.stream(xs).skip(l).sum(); }
private static int min(int a, int b) { return Math.min(a, b); }
private static int abs(int x) { return Math.abs(x); }
// OUTPUT ############################################################################################################
private static void print(Object o) { _OUT_.println(o); }
private static void print(int[] xs) { for (int x : xs) _OUT_.print(x + " "); _OUT_.println(); }
private static void print(long[] xs) { for (long x : xs) _OUT_.print(x + " "); _OUT_.println(); }
// SORT ##############################################################################################################
private static <T> void sort(T[] arr, ToIntFunction<T> comp) { Arrays.sort(arr, Comparator.comparingInt(comp)); }
private static void sort(int[] arr) { shuffle(arr); Arrays.sort(arr); }
// STRING ############################################################################################################
private static class Str {
final String s; final int l, r;
Str(String s, int l, int r) { this.s = s; this.l = l; this.r = r; }
int len() { return r - l; }
boolean eq(Str that) {
if (that.len() != this.len()) return false;
for (int i = 0, len = len(); i < len; i++) if (s.charAt(l + i) != that.s.charAt(that.l + i)) return false;
return true;
}
}
private static String reverse(String s) { return new StringBuilder(s).reverse().toString(); }
}
|
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 17
|
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
|
f965ec41d58b4ef33e112b26dc4ea664
|
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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
// static int[] prime = new int[100001];
final static long mod = 1000000007;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
// sieve();
long t = in.nextLong();
while(t-- > 0){
int n = in.nextInt(), m = in.nextInt();
char[][] a = new char[n][];
for(int i = 0; i < n; i++)
a[i] = in.next().toCharArray();
boolean ans = true;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i][j] == '.'){
continue;
}
if(!check(a, i, j)){
ans = false;
break;
}
}
if(!ans)
break;
}
out.println(ans ? "YES" : "NO");
}
out.flush();
}
static boolean check(char[][] a, int i, int j){
int diag = 0, hv = 0;
if(i > 0){
hv += (a[i-1][j] == '*' ? 1 : 0);
if(j > 0){
diag += (a[i-1][j-1] == '*' ? 1 : 0);
}
if(j < a[0].length - 1){
diag += (a[i-1][j+1] == '*' ? 1 : 0);
}
}
if(i < a.length-1){
hv += (a[i+1][j] == '*' ? 1 : 0);
if(j > 0){
diag += (a[i+1][j-1] == '*' ? 1 : 0);
}
if(j < a[0].length - 1){
diag += (a[i+1][j+1] == '*' ? 1 : 0);
}
}
if(j > 0){
hv += (a[i][j-1] == '*' ? 1 : 0);
}
if(j < a[i].length-1){
hv += (a[i][j+1] == '*' ? 1 : 0);
}
if(diag + hv != 2){
return false;
}
if(hv == 0)
return false;
if(hv == 2){
return checkHV(a, i, j);
}
return checkDiag(a, i, j);
}
private static boolean checkDiag(char[][] a, int i, int j) {
int n = a.length, m = a[0].length;
if(i < n-1 && j < m-1){
if(a[i+1][j+1] == '*' && a[i+1][j] == '*'){
return true;
}else if(a[i+1][j+1] == '*' && a[i][j+1] == '*')
return true;
}
if(i < n-1 && j > 0){
if(a[i+1][j-1] == '*' && a[i+1][j] == '*'){
return true;
}else if(a[i+1][j-1] == '*' && a[i][j-1] == '*'){
return true;
}
}
if(i > 0 && j < m-1){
if(a[i-1][j+1] == '*' && a[i-1][j] == '*'){
return true;
}else if(a[i-1][j+1] == '*' && a[i][j+1] == '*')
return true;
}
if(i > 0 && j > 0){
if(a[i-1][j-1] == '*' && a[i-1][j] == '*'){
return true;
}else if(a[i-1][j-1] == '*' && a[i][j-1] == '*')
return true;
}
return false;
}
private static boolean checkHV(char[][] a, int i, int j) {
if(i > 0 && j > 0){
if(a[i-1][j] == '*' && a[i][j-1] == '*'){
return true;
}
}
if(i < a.length-1 && j < a[i].length-1){
if(a[i+1][j] == '*' && a[i][j+1] == '*')
return true;
}
if(i < a.length-1 && j > 0){
if(a[i+1][j] == '*' && a[i][j-1] == '*')
return true;
}
if(i > 0 && j < a[i].length-1){
if(a[i-1][j] == '*' && a[i][j+1] == '*')
return true;
}
return false;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static Integer[] intInput(int n, InputReader in) {
Integer[] a = new Integer[n];
for (int i = 0; i < a.length; i++)
a[i] = in.nextInt();
return a;
}
static Long[] longInput(int n, InputReader in) {
Long[] a = new Long[n];
for (int i = 0; i < a.length; i++)
a[i] = in.nextLong();
return a;
}
static String[] strInput(int n, InputReader in) {
String[] a = new String[n];
for (int i = 0; i < a.length; i++)
a[i] = in.next();
return a;
}
// static void sieve() {
// for (int i = 2; i * i < prime.length; i++) {
// if (prime[i])
// continue;
// for (int j = i * i; j < prime.length; j += i) {
// prime[j] = true;
// }
// }
// }
}
class Segment {
int[] a;
final int n;
Segment(int n){
this.n = n;
a = new int[4*n];
Arrays.fill(a, Integer.MIN_VALUE);
}
void set(int index, int val){
set(0, n-1, 0, index, val);
}
private void set(int s, int e, int ind, int index, int val) {
if(s == e){
a[ind] = val;
return;
}
int mid = (s+e)/2;
if(mid <= index){
set(s, mid, 2*ind+1, index, val);
}else{
set(mid+1, e, 2*ind+2, index, val);
}
a[ind] = Math.max(a[2*ind+1], a[2*ind+2]);
}
int get(int l, int h){
return get(0, n-1, l, h, 0);
}
private int get(int s, int e, int l, int h, int ind) {
if(s >= l && e <= h){
return a[ind];
}
if(s > h || e < l){
return Integer.MIN_VALUE;
}
int mid = (s+e)/2;
return Math.max(get(s, mid, l, h, 2*ind+1), get(mid+1, e, l, h, 2*ind+2));
}
}
class Data{
int i, ind;
public Data(int i, int ind) {
this.i = i;
this.ind = ind;
}
}
// class compareVal implements Comparator<Data> {
// @Override
// public int compare(Data o1, Data o2) {
// return (o1.val - o2.val);
// }
// }
// class compareInd implements Comparator<Data> {
// @Override
// public int compare(Data o1, Data o2) {
// return o1.ind - o2.ind == 0 ? o1.val - o2.val : o1.ind - o2.ind;
// }
// }
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = reader.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 17
|
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
|
611efc4ecf4054e3424b09e5a3bb7be9
|
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.Scanner;
public class problemF {
public static int n, m;
public static char[][] a;
public static int[][] dd;
public static boolean isValid(int i, int j) {
if (i >= 1 && i <= n && j >= 1 && j <= m)
return true;
return false;
}
public static int[] dx = { -1, 1, 0, 0 };
public static int[] dy = { 0, 0, -1, 1 };
public static int[] numb;
public static boolean isOk = true;
public static boolean isPlus(int i, int j) {
int dem = 0;
for (int k = 0; k < 4; k++) {
if (isValid(i + dx[k], j + dy[k]) && a[i + dx[k]][j + dy[k]] == '*') {
if (isValid(i - dx[k], j - dy[k]) && a[i - dx[k]][j - dy[k]] == '*')
return false;
dem++;
}
}
if (dem == 2)
return true;
return false;
}
public static boolean checkingSequence(int i, int j) {
for (int k = 0; k < 4; k++) {
if (isValid(i + dx[k], j + dy[k]) && a[i + dx[k]][j + dy[k]] == '*') {
if (isPlus(i + dx[k], j + dy[k])) {
return true;
}
}
}
return false;
}
public static void dfs(int i, int j, int dem, int count) {
if (dem > 3) {
isOk = false;
return;
}
if (dem == 3) {
if (!checkingSequence(i, j)) {
isOk = false;
return;
}
}
dd[i][j] = count;
numb[count]++;
for (int k = 0; k < 4; k++) {
if (isValid(i + dx[k], j + dy[k]) && dd[i + dx[k]][j + dy[k]] == 0 && a[i + dx[k]][j + dy[k]] == '*') {
dfs(i + dx[k], j + dy[k], dem + 1, count);
}
}
}
public static int[] bx = { -1, -1, 1, 1 };
public static int[] by = { -1, 1, -1, 1 };
public static boolean checkBishop(int i, int j) {
for (int k = 0; k < 4; k++) {
if (isValid(i + bx[k], j + by[k]) && a[i + bx[k]][j + by[k]] == '*' && dd[i + bx[k]][j + by[k]] != dd[i][j])
return false;
}
return true;
}
public static void initialize() {
a = new char[n + 5][m + 5];
dd = new int[n + 5][m + 5];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dd[i][j] = 0;
}
}
numb = new int[n*m];
for (int i = 0; i < n*m; i++) {
numb[i] = 0;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
initialize();
for (int i = 1; i <= n; i++) {
String newLine = sc.next();
char[] s = newLine.toCharArray();
for (int j = 1; j <= m; j++) {
a[i][j] = s[j - 1];
}
}
isOk = true;
int count = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '*' && dd[i][j] == 0) {
count++;
dfs(i, j, 1, count);
if (!isOk) {
break;
}
}
}
if (!isOk)
break;
}
if (isOk) {
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (a[i][j] == '*' && !checkBishop(i, j)) {
isOk = false;
}
if (!isOk) {
break;
}
}
if (!isOk) {
break;
}
}
}
if (isOk) {
if (numb.length == 0)
isOk = false;
else
for (int i = 1; i <= count; i++) {
if (numb[i] != 3) {
isOk = false;
break;
}
}
}
if (isOk)
System.out.println("YES");
else
System.out.println("NO");
}
sc.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 17
|
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
|
a6ea42e6c3f6d78c087c60c4996399fa
|
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.*;
public class Test1 {
static int[] dir = new int[] { 0, 1, 0, -1, 0 };
static int[] diag = new int[] { 1, 1, -1, -1, 1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int row = sc.nextInt(), col = sc.nextInt();
char[][] grid = new char[row][];
boolean[][] vis = new boolean[row][col];
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
boolean ok = true;
for(int i=0;i<row;i++) grid[i] = sc.next().toCharArray();
// System.out.println(grid.length+" "+grid[0].length);
for(int i=0;i<row && ok;i++) {
for(int j=0;j<col && ok;j++) {
if(grid[i][j] == '*' && vis[i][j] == false) {
List<int[]> ds = new ArrayList<>();
dfs(i, j, row, col, grid, vis, ds);
if(ds.size() != 3) {
ok = false; break;
}
set1.clear();
set2.clear();
for(int[] temp : ds) {
set1.add(temp[0]);
set2.add(temp[1]);
}
if(set1.size() != 2 || set2.size() != 2) {
ok = false; break;
}
}
}
}
String ans = ok? "yes" : "no";
System.out.println(ans);
}
// ####################
}
// ####################
static void dfs(int r, int c, int row, int col, char[][] grid,
boolean[][] vis, List<int[]> ds) {
if(r < 0 || r >= row || c < 0 || c >= col || grid[r][c] != '*' || vis[r][c]) return;
vis[r][c] = true;
ds.add(new int[] {r, c});
for(int i=0;i<4;i++) {
int r1 = r + dir[i], c1 = c + dir[i+1];
dfs(r1, c1, row, col, grid, vis, ds);
int r2 = r + diag[i], c2 = c + diag[i+1];
dfs(r2, c2, row, col, grid, vis, ds);
}
}
}
|
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 17
|
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
|
4f4e86d3400bda865e7de191b5bd638f
|
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.*;
public class Test1 {
static int[] dir = new int[] { 0, 1, 0, -1, 0 };
static int[] diag = new int[] { 1, 1, -1, -1, 1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int row = sc.nextInt(), col = sc.nextInt();
char[][] grid = new char[row][];
boolean[][] vis = new boolean[row][col];
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
boolean ok = true;
for(int i=0;i<row;i++) grid[i] = sc.next().toCharArray();
// System.out.println(grid.length+" "+grid[0].length);
for(int i=0;i<row && ok;i++) {
for(int j=0;j<col && ok;j++) {
if(grid[i][j] == '*' && vis[i][j] == false) {
List<int[]> ds = new ArrayList<>();
dfs(i, j, row, col, grid, vis, ds);
if(ds.size() != 3) {
ok = false; break;
}
set1.clear();
set2.clear();
for(int[] temp : ds) {
set1.add(temp[0]);
set2.add(temp[1]);
}
if(set1.size() != 2 || set2.size() != 2) {
ok = false; break;
}
}
}
}
String ans = ok? "yes" : "no";
System.out.println(ans);
}
// ####################
}
// ####################
static void dfs(int r, int c, int row, int col, char[][] grid,
boolean[][] vis, List<int[]> ds) {
if(r < 0 || r >= row || c < 0 || c >= col || grid[r][c] != '*' || vis[r][c]) return;
vis[r][c] = true;
ds.add(new int[] {r, c});
for(int i=0;i<4;i++) {
int r1 = r + dir[i], c1 = c + dir[i+1];
dfs(r1, c1, row, col, grid, vis, ds);
int r2 = r + diag[i], c2 = c + diag[i+1];
dfs(r2, c2, row, col, grid, vis, ds);
}
}
}
|
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 17
|
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
|
566b3dd9cd40856223cf4086de8712f3
|
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 CF1722F extends PrintWriter {
CF1722F() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1722F o = new CF1722F(); o.main(); o.flush();
}
int n, m;
byte[][] cc;
int count1(int i, int j) {
return i >= 0 && i < n && j >= 0 && j < m && cc[i][j] == '*' ? 1 : 0;
}
int count9(int i, int j) {
int c = 0;
for (int di = -1; di <= 1; di++)
for (int dj = -1; dj <= 1; dj++)
c += count1(i + di, j + dj);
return c;
}
int count4(int i, int j) {
return count1(i - 1, j) + count1(i + 1, j) + count1(i, j - 1) + count1(i, j + 1);
}
boolean straight(int i, int j) {
return count1(i - 1, j) == 1 && count1(i + 1, j) == 1
|| count1(i, j - 1) == 1 && count1(i, j + 1) == 1;
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
cc = new byte[n][];
for (int i = 0; i < n; i++)
cc[i] = sc.next().getBytes();
boolean yes = true;
int cnt1 = 0, cnt2 = 0;
out:
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (cc[i][j] == '*') {
if (count9(i, j) != 3) {
yes = false;
break out;
}
int k = count4(i, j);
if (k != 1 && k != 2 || k == 2 && straight(i, j)) {
yes = false;
break out;
}
if (k == 1)
cnt1++;
else
cnt2++;
}
if (cnt1 != cnt2 * 2)
yes = false;
println(yes ? "YES" : "NO");
}
}
}
|
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 17
|
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
|
51c62d8e5b9a7029dea6c4717d41a0b7
|
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.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class CF1722F {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int m = scanner.nextInt();
char[][] chars = new char[n][m];
for (int j = 0; j < n; j++) {
chars[j] = scanner.next().toCharArray();
}
System.out.println(solve(n, m, chars));
}
}
private static final int[][] DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
private static int M;
private static int N;
private static boolean[][] visited;
private static String solve(int n, int m, char[][] chars) {
M = n;
N = m;
visited = new boolean[M][N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (chars[i][j] == '*' && !visited[i][j]) {
if (!check(chars, i, j)) {
return "NO";
}
}
}
}
return "YES";
}
private static boolean check(char[][] grid, int i, int j) {
// BFS
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{i, j});
visited[i][j] = true;
int area = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int k = 0; k < size; k++) {
int[] cur = queue.remove();
// 特判
// *
// *** & *
// *
if (Math.abs(cur[0] - i) >= 2 || Math.abs(cur[1] - j) >= 2) {
return false;
}
if (cntLive(grid, cur[0], cur[1]) != 3) {
return false;
}
area++;
if (area > 3) {
return false;
}
for (int[] dir : DIRECTIONS) {
int nextM = cur[0] + dir[0];
int nextN = cur[1] + dir[1];
if (nextM >= 0 && nextM < M && nextN >= 0 && nextN < N
&& grid[nextM][nextN] == '*' && !visited[nextM][nextN]) {
visited[nextM][nextN] = true;
queue.add(new int[]{nextM, nextN});
}
}
}
}
return area == 3;
}
private static int cntLive(char[][] grid, int i, int j) {
int cnt = 0;
for (int row = Math.max(0, i - 1); row <= Math.min(M - 1, i + 1); row++) {
for (int col = Math.max(0, j - 1); col <= Math.min(N - 1, j + 1); col++) {
if (grid[row][col] == '*') {
cnt++;
}
}
}
return cnt;
}
}
|
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 17
|
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
|
d80174830e82c601b1789b031931b1f4
|
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.BufferedInputStream;
import java.util.*;
public class F {
static final int[][] dirs = {
{-1, 0}, {-1, -1}, {-1, 1},
{0, 1}, {0, -1},
{1, 0}, {1, -1}, {1, 1}
};
static boolean handle(char[][] chess, boolean[][] used, int y, int x) {
int r = chess.length, c = chess[0].length;
Deque<int[]> deq = new LinkedList<>();
int cnt = 0;
deq.offer(new int[] {y, x});
used[y][x] = true;
List<int[]> arr = new ArrayList<>();
arr.add(new int[] {y, x});
while (!deq.isEmpty()) {
int[] cur = deq.poll();
for (int i = 0; i < dirs.length; i++) {
int tx = cur[1] + dirs[i][1];
int ty = cur[0] + dirs[i][0];
if (tx >= 0 && tx < c && ty >= 0 && ty < r) {
if (chess[ty][tx] == '*' && !used[ty][tx]) {
used[ty][tx] = true;
deq.offer(new int[]{ty, tx});
arr.add(new int[]{ty, tx});
}
}
}
}
if (arr.size() != 3) return false;
Set<Integer> sy = new TreeSet<>();
Set<Integer> sx = new TreeSet<>();
for (int[] v: arr) {
sy.add(v[0]);
sx.add(v[1]);
}
return sy.size() == 2 && sx.size() == 2;
}
static boolean solve(char[][] chess) {
int r = chess.length;
int c = chess[0].length;
boolean[][] used = new boolean[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
char ch = chess[i][j];
if (ch == '*') {
if (!used[i][j]) {
if (!handle(chess, used, i, j)) {
return false;
}
}
}
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int kase = sc.nextInt();
while (kase-- > 0) {
int h = sc.nextInt();
int w = sc.nextInt();
char[][] chess = new char[h][];
for (int i = 0; i < h; i++) {
chess[i] = sc.next().toCharArray();
}
System.out.println(solve(chess) ? "YES" : "NO");
}
}
}
|
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 17
|
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
|
ea8df0b24731d641c06121a24a1ba664
|
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: Atuer
*/
public class Main
{
// ==== Solve Code ====//
static int INF = 2000000010;
public static void csh()
{
}
public static void main(String[] args) throws IOException
{
// csh();
int t = in.nextInt();
while (t-- > 0)
{
solve();
out.flush();
}
out.close();
}
static int n, m;
static int[][] a = new int[55][55];
public static void solve()
{
n = in.nextInt();
m = in.nextInt();
for (int i = 0; i < n; i++)
{
char[] s = in.next().toCharArray();
for (int j = 0; j < m; j++)
a[i][j] = s[j] == '*' ? 1 : 0;
}
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < m - 1; j++)
if (a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1] == 3)
{
int tp = 0;
for (int ii = Math.max(0, i - 1); ii < Math.min(n, i + 3); ii++)
for (int jj = Math.max(0, j - 1); jj < Math.min(m, j + 3); jj++)
tp += a[ii][jj];
if (tp == 3 || (tp == 4 && check(i, j)))
filla(i, j);
}
boolean flag = true;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] == 1)
flag = false;
out.println(flag ? "YES" : "NO");
}
private static boolean check(int i, int j)
{
if (i - 1 >= 0 & j - 1 >= 0 && a[i - 1][j - 1] == 1 && a[i][j] == 0)
return true;
else if (i - 1 >= 0 && j + 2 < m && a[i - 1][j + 2] == 1 && a[i][j + 1] == 0)
return true;
else if (i + 2 < n && j - 1 >= 0 && a[i + 2][j - 1] == 1 && a[i + 1][j] == 0)
return true;
else if (i + 2 < n && j + 2 < m && a[i + 2][j + 2] == 1 && a[i + 1][j + 1] == 0)
return true;
return false;
}
private static void filla(int i, int j)
{
a[i][j] = 0;
a[i + 1][j] = 0;
a[i][j + 1] = 0;
a[i + 1][j + 1] = 0;
}
public static class Node
{
int x, y, k;
public Node(int x, int y, int k)
{
this.x = x;
this.y = y;
this.k = k;
}
}
// ==== Solve Code ====//
// ==== Template ==== //
public static long cnm(int a, int b)
{
long sum = 1;
int i = a, j = 1;
while (j <= b)
{
sum = sum * i / j;
i--;
j++;
}
return sum;
}
public static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
public static int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
public static void gbSort(int[] a, int l, int r)
{
if (l < r)
{
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r)
if (a[i] <= a[j])
t[idx++] = a[i++];
else
t[idx++] = a[j++];
while (i <= m)
t[idx++] = a[i++];
while (j <= r)
t[idx++] = a[j++];
for (int z = 0; z < t.length; z++)
a[l + z] = t[z];
}
}
// ==== Template ==== //
// ==== IO ==== //
static InputStream inputStream = System.in;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(System.out);
static class InputReader
{
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream)
{
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
boolean hasNext()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e)
{
return false;
// TODO: handle exception
}
}
return true;
}
public String nextLine()
{
String str = null;
try
{
str = reader.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public Double nextDouble()
{
return Double.parseDouble(next());
}
public BigInteger nextBigInteger()
{
return new BigInteger(next());
}
public BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}
// ==== IO ==== //
}
|
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 17
|
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
|
5bcf11acb7df0edbbe537be682ca69fb
|
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.ArrayList;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
public class Solution{
static int[] dx = {-1, 0, 1};
static int[] dy = {-1, 0, 1};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while(t-- > 0){
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] grid = new int[n][m];
int[][] id = new int[n][m];
for(int i = 0 ; i < n ; i++){
String s = scanner.next();
for(int j = 0 ; j < m ; j++){
grid[i][j] = s.charAt(j) == '*' ? 1 : 0;
}
}
String ans = solve(grid, id);
System.out.println(ans);
}
}
private static String solve(int[][] grid, int[][] id) {
int n = grid.length;
int m = grid[0].length;
int curid = 1;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
if(grid[i][j] == 1){
ArrayList<Pair> hr = new ArrayList<>();
ArrayList<Pair> ve = new ArrayList<>();
if(i > 0 && grid[i - 1][j] == 1)
hr.add(new Pair(i - 1, j));
if(i < n - 1 && grid[i + 1][j] == 1)
hr.add(new Pair(i + 1, j));
if(j < m - 1 && grid[i][j + 1] == 1)
ve.add(new Pair(i, j + 1));
if(j > 0 && grid[i][j - 1] == 1)
ve.add(new Pair(i, j - 1));
if(hr.size() == 1 && ve.size() == 1){
if(id[i][j] == 0) id[i][j] = curid;
else return "NO";
if(id[hr.get(0).i][hr.get(0).j] == 0) id[hr.get(0).i][hr.get(0).j] = curid;
else return "NO";
if(id[ve.get(0).i][ve.get(0).j] == 0) id[ve.get(0).i][ve.get(0).j] = curid;
else return "NO";
curid++;
} else if (hr.size() > 1 || ve.size() > 1) return "NO";
}
}
}
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
if(grid[i][j] == 1){
if(id[i][j] == 0) return "NO";
else {
for(int x = 0 ; x < 3 ; x++){
for(int y = 0 ; y < 3 ; y++){
if(i + dx[x] >= 0 && i + dx[x] < n){
if(j + dy[y] >= 0 && j + dy[y] < m){
if(id[i + dx[x]][j + dy[y]] != id[i][j] && id[i + dx[x]][j + dy[y]] != 0)
return "NO";
}
}
}
}
}
}
}
}
return "YES";
}
}
class Pair{
int i;
int j;
Pair(int i, int j){
this.i = i;
this.j = j;
}
}
|
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 17
|
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
|
e2e52b300c5306a1fcc1d3ea4257b68d
|
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 kg.my_algorithms.Codeforces;
import java.io.*;
import java.util.*;
public class Solution {
private static final int[][] directions = {{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1},{0,-1},{0,1}};
private static final List<List<Point>> LShapedPoints = List.of(List.of(new Point(1,0),new Point(1,1)),List.of(new Point(1,-1),new Point(1,0)),
List.of(new Point(0,1),new Point(1,1)),List.of(new Point(0,1),new Point(1,0)));
public static void main(String[] args) throws IOException {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader fr = new FastReader();
StringBuilder sb = new StringBuilder();
int testCases = fr.nextInt();
for(int test=1;test<=testCases;test++){
int rows= fr.nextInt();
int cols = fr.nextInt();
char[][] matrix = new char[rows][cols];
for(int r=0;r<rows;r++) matrix[r] = fr.next().toCharArray();
if(solve(matrix)) sb.append("YES\n");
else sb.append("NO\n");
}
output.write(sb.toString());
output.flush();
}
private static boolean isValidPoint(int r, int c, int rows, int cols){
return r>=0 && c>=0 && r<rows && c<cols;
}
private static List<Point> getNeighboringStarts(int r, int c, char[][] matrix){
List<Point> points = new ArrayList<>();
for(int[] direction: directions){
if(isValidPoint(r+direction[0],c+direction[1],matrix.length,matrix[0].length) && matrix[r+direction[0]][c+direction[1]]=='*')
points.add(new Point(r+direction[0],c+direction[1]));
}
return points;
}
private static boolean solve(char[][] matrix){
for(int r=0;r<matrix.length;r++){
for(int c=0;c<matrix[0].length;c++){
if(matrix[r][c] == '*'){
List<Point> neighboringStars = getNeighboringStarts(r,c,matrix);
if(neighboringStars.size()!=2) return false;
matrix[r][c] = 'L';
for(Point point: neighboringStars) matrix[point.row][point.col] = 'L';
for(Point point: neighboringStars) if(getNeighboringStarts(point.row,point.col,matrix).size()>0) return false;
if(!isLShaped(new Point(r,c),neighboringStars.get(0),neighboringStars.get(1),matrix)) return false;
}
}
}
return true;
}
private static boolean isLShaped(Point p1, Point p2, Point p3, char[][] matrix){
Point d1 = new Point(p2.row-p1.row,p2.col-p1.col);
Point d2 = new Point(p3.row-p1.row,p3.col-p1.col);
for(List<Point> LShaped: LShapedPoints){
if((d1.equals(LShaped.get(0)) || d2.equals(LShaped.get(0))) && (d1.equals(LShaped.get(1)) || d2.equals(LShaped.get(1)))) return true;
}
return false;
}
}
class Point{
int row;
int col;
public Point(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point point = (Point) o;
return row == point.row && col == point.col;
}
}
//Fast Input
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 {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
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 17
|
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
|
2951f0b044a6e62a0b9416bf5fcf0cf8
|
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.*;
public class Main {
public static boolean [][] vis;
public static char [][] g;
public static int [] dx = new int[] {-1,-1,-1,0,0,1,1,1};
public static int [] dy = new int[] {-1,0,1,1,-1,-1,0,1};
public static int n,m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0){
n = sc.nextInt();
m = sc.nextInt();
g = new char[n][m];
vis = new boolean[n][m];
for (int i = 0; i < n; i++) {
g[i] = sc.next().toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == '.') vis[i][j] = true;
}
}
boolean flag = true;
to:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j]) continue ;
vis[i][j] = true;
List<int[]> list = new ArrayList<>();
list.add(new int[] {i,j});
dfs(i,j,list);
if (list.size() != 3){
flag = false;
break to;
}
int maxX = -1, minX = 100, maxY = -1, minY =100;
for (int k = 0; k < 3; k++) {
maxX = Math.max(maxX, list.get(k)[0]);
minX = Math.min(minX, list.get(k)[0]);
maxY = Math.max(maxY, list.get(k)[1]);
minY = Math.min(minY, list.get(k)[1]);
}
if(maxY-minY!=1 || maxX-minX!=1){
flag = false;
break to;
}
}
}
if (flag) System.out.println("Yes");
else System.out.println("No");
}
}
private static void dfs(int x, int y, List<int[]> list) {
for (int i = 0; i < 8; i++) {
int fx = x + dx[i];
int fy = y + dy[i];
if (fx >= n || fx < 0 || fy >= m || fy < 0) continue;
if(vis[fx][fy]) continue;
vis[fx][fy] = true;
if (g[fx][fy] == '*'){
list.add(new int[] {fx,fy});
dfs(fx,fy,list);
}
}
}
}
|
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 17
|
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
|
d67c229810f23a24c9f0b81ce98ed939
|
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 {
final static boolean multipleTests = true;
Input in;
PrintWriter out;
public F() {
in = new Input(System.in);
out = new PrintWriter(System.out);
}
public static void main(String[] args) {
F solution = new F();
int t = 1;
if (multipleTests) t = solution.in.nextInt();
for (; t > 0; t--) {
solution.solve();
}
solution.out.close();
}
final static int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
final static int[][] dirExtra = {{1, 1}, {1, 0}, {1, -1}, {0, 1}, {0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
final static char FULL = '*';
int n, m;
char[][] a;
boolean ans;
boolean[][] visited, forbidden;
Deque<int[]> queue;
int dfs(int x, int y) {
visited[x][y] = true;
int count = 1;
queue.add(new int[]{x, y});
if (forbidden[x][y]) ans = false;
for (int[] d: dir) {
int xn = x + d[0];
int yn = y + d[1];
if (!(0<=xn && xn<n && 0<=yn && yn<m)) continue;
if (visited[xn][yn]) continue;
if (a[xn][yn] != FULL) continue;
count += dfs(xn, yn);
}
return count;
}
void bfs() {
while (!queue.isEmpty()) {
int[] curr = queue.poll();
for (int[] d: dirExtra) {
int xn = curr[0] + d[0];
int yn = curr[1] + d[1];
if (!(0<=xn && xn<n && 0<=yn && yn<m)) continue;
if (visited[xn][yn]) continue;
forbidden[xn][yn] = true;
}
}
}
boolean line(int x, int y) {
boolean hasLine = false;
for (int[] d: dir) {
int xn = x + d[0];
int yn = y + d[1];
if (!(0<=xn && xn<n && 0<=yn && yn<m)) continue;
if (a[xn][yn] != FULL) continue;
xn += d[0];
yn += d[1];
if (!(0<=xn && xn<n && 0<=yn && yn<m)) continue;
if (a[xn][yn] != FULL) continue;
hasLine = true;
}
return hasLine;
}
void solve() {
n = in.nextInt();
m = in.nextInt();
a = new char[n][];
ans = true;
visited = new boolean[n][m];
forbidden = new boolean[n][m];
queue = new ArrayDeque<>();
for (int i=0; i<n; i++) {
a[i] = in.nextString().toCharArray();
}
for (int i=0; i<n && ans; i++) {
for (int j=0; j<m && ans; j++) {
if (a[i][j] == FULL && !visited[i][j]) {
if (dfs(i, j) != 3) ans = false;
if (line(i, j)) ans = false;
bfs();
}
}
}
if (ans) out.println("YES");
else out.println("NO");
}
static class Input {
BufferedReader br;
StringTokenizer st;
public Input(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
String nextString() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextString());
}
long nextLong() {
return Long.parseLong(nextString());
}
double nextDouble() {
return Double.parseDouble(nextString());
}
int[] nextIntArray(int size) {
int[] ans = new int[size];
for (int i = 0; i < size; i++) {
ans[i] = nextInt();
}
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 17
|
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
|
bdc44e8409d39eaf21b7462587a3be51
|
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.awt.Rectangle;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
/**
*
* @author eslam
*/
public class Solution {
// Beginning of the solution
static Kattio input = new Kattio();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
static ArrayList<ArrayList<Integer>> powerSet = new ArrayList<>();
static ArrayList<LinkedList<Integer>> allprem = new ArrayList<>();
static ArrayList<LinkedList<String>> allprems = new ArrayList<>();
static ArrayList<Long> luc = new ArrayList<>();
static long mod = (long) (Math.pow(10, 9) + 7);
static int grid[][] = {{0, 0, 1, -1, 1, 1, -1, -1}, {1, -1, 0, 0, 1, -1, 1, -1}};
static int dp[];
static double cmp = 0.000000001;
public static void main(String[] args) throws IOException {
// Kattio input = new Kattio("cbarn");
// BufferedWriter log = new BufferedWriter(new FileWriter("cbarn.txt"));
int test = input.nextInt();
loop:
for (int o = 1; o <= test; o++) {
int n = input.nextInt();
int m = input.nextInt();
char ch[][] = new char[n][m];
for (int i = 0; i < n; i++) {
ch[i] = input.next().toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
boolean ca = false;
if (ch[i][j] == '*') {
int an[] = new int[12];
if (i + 1 < n && j + 1 < m) {
int b = 0;
if (ch[i + 1][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[0] = b;
}
if (i - 1 > -1 && j + 1 < m) {
int b = 0;
if (ch[i - 1][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[1] = b;
}
if (i + 1 < n && j - 1 > -1) {
int b = 0;
if (ch[i + 1][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[2] = b;
}
if (i - 1 > -1 && j - 1 > -1) {
int b = 0;
if (ch[i - 1][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[3] = b;
}
if (i + 1 < n && j - 1 > -1) {
int b = 0;
if (ch[i + 1][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i + 1][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[4] = b;
}
if (i + 1 < n && j + 1 < m) {
int b = 0;
if (ch[i + 1][j] == '*') {
int cnt = 0;
boolean c = true;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i + 1][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[5] = b;
}
if (i - 1 > -1 && j - 1 > -1) {
int b = 0;
if (ch[i - 1][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i - 1][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[6] = b;
}
if (i - 1 > -1 && j + 1 < m) {
int b = 0;
if (ch[i - 1][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i - 1][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k <8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[7] = b;
}
if (i + 1 < n && j - 1 > -1) {
int b = 0;
if (ch[i][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i + 1][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[8] = b;
}
if (i + 1 < n && j + 1 < m) {
int b = 0;
if (ch[i][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i + 1][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + 1 + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[9] = b;
}
if (i - 1 > -1 && j - 1 > -1) {
int b = 0;
if (ch[i][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i - 1][j - 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j - 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[10] = b;
}
if (i - 1 > -1 && j + 1 < m) {
int b = 0;
if (ch[i][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i - 1][j + 1] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i - 1 + grid[0][k];
int ny = j + 1 + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
if (ch[i][j] == '*') {
boolean c = true;
int cnt = 0;
for (int k = 0; k < 8; k++) {
int nx = i + grid[0][k];
int ny = j + grid[1][k];
if (isValid(nx, ny, n, m)) {
if (ch[nx][ny] == '*') {
cnt++;
}
}
}
c = cnt == 2;
if (c) {
b++;
}
}
an[11] = b;
}
for (int k = 0; k < 12; k++) {
if (an[k] == 3) {
ca = true;
break;
}
}
if (!ca) {
log.write("NO\n");
continue loop;
}
}
}
}
log.write("YES\n");
}
log.flush();
}
static class rec {
long x1;
long x2;
long y1;
long y2;
public rec(long x1, long y1, long x2, long y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public long getArea() {
return (x2 - x1) * (y2 - y1);
}
}
public static int bs(int b[], int va) {
int max = b.length - 1;
int min = 0;
int ans = -1;
while (max >= min) {
int mid = (max + min) / 2;
if (b[mid] >= va) {
ans = b[mid];
max = mid - 1;
} else {
min = mid + 1;
}
}
return ans;
}
static int sumOfRange(int x1, int y1, int x2, int y2, int e, int a[][][]) {
return (a[e][x2][y2] - a[e][x1 - 1][y2] - a[e][x2][y1 - 1]) + a[e][x1 - 1][y1 - 1];
}
public static int[][] bfs(int i, int j, String w[]) {
Queue<pair> q = new ArrayDeque<>();
q.add(new pair(i, j));
int dis[][] = new int[w.length][w[0].length()];
for (int k = 0; k < w.length; k++) {
Arrays.fill(dis[k], -1);
}
dis[i][j] = 0;
while (!q.isEmpty()) {
pair p = q.poll();
int cost = dis[p.x][p.y];
for (int k = 0; k < 4; k++) {
int nx = p.x + grid[0][k];
int ny = p.y + grid[1][k];
if (isValid(nx, ny, w.length, w[0].length())) {
if (dis[nx][ny] == -1 && w[nx].charAt(ny) == '.') {
q.add(new pair(nx, ny));
dis[nx][ny] = cost + 1;
}
}
}
}
return dis;
}
public static void dfs(int node, ArrayList<Integer> a[], boolean vi[], boolean closed[]) {
vi[node] = true;
for (Integer ch : a[node]) {
if (!vi[ch] && !closed[ch]) {
dfs(ch, a, vi, closed);
}
}
}
public static void graphRepresintion(ArrayList<Integer>[] a, int q) throws IOException {
for (int i = 0; i < a.length; i++) {
a[i] = new ArrayList<>();
}
while (q-- > 0) {
int x = input.nextInt();
int y = input.nextInt();
a[x].add(y);
a[y].add(x);
}
}
public static boolean isValid(int i, int j, int n, int m) {
return (i > -1 && i < n) && (j > -1 && j < m);
}
// present in the left and right indices
public static int[] swap(int data[], int left, int right) {
// Swap the data
int temp = data[left];
data[left] = data[right];
data[right] = temp;
// Return the updated array
return data;
}
// Function to reverse the sub-array
// starting from left to the right
// both inclusive
public static int[] reverse(int data[], int left, int right) {
// Reverse the sub-array
while (left < right) {
int temp = data[left];
data[left++] = data[right];
data[right--] = temp;
}
// Return the updated array
return data;
}
// Function to find the next permutation
// of the given integer array
public static boolean findNextPermutation(int data[]) {
// If the given dataset is empty
// or contains only one element
// next_permutation is not possible
if (data.length <= 1) {
return false;
}
int last = data.length - 2;
// find the longest non-increasing suffix
// and find the pivot
while (last >= 0) {
if (data[last] < data[last + 1]) {
break;
}
last--;
}
// If there is no increasing pair
// there is no higher order permutation
if (last < 0) {
return false;
}
int nextGreater = data.length - 1;
// Find the rightmost successor to the pivot
for (int i = data.length - 1; i > last; i--) {
if (data[i] > data[last]) {
nextGreater = i;
break;
}
}
// Swap the successor and the pivot
data = swap(data, nextGreater, last);
// Reverse the suffix
data = reverse(data, last + 1, data.length - 1);
// Return true as the next_permutation is done
return true;
}
public static pair[] dijkstra(int node, ArrayList<pair> a[]) {
PriorityQueue<tri> q = new PriorityQueue<>(new Comparator<tri>() {
@Override
public int compare(tri o1, tri o2) {
if (o1.y > o2.y) {
return 1;
} else if (o1.y < o2.y) {
return -1;
} else {
return 0;
}
}
});
q.add(new tri(node, 0, -1));
pair distance[] = new pair[a.length];
while (!q.isEmpty()) {
tri p = q.poll();
int cost = p.y;
if (distance[p.x] != null) {
continue;
}
distance[p.x] = new pair(p.z, cost);
ArrayList<pair> nodes = a[p.x];
for (pair node1 : nodes) {
if (distance[node1.x] == null) {
tri pa = new tri(node1.x, cost + node1.y, p.x);
q.add(pa);
}
}
}
return distance;
}
public static String revs(String w) {
String ans = "";
for (int i = w.length() - 1; i > -1; i--) {
ans += w.charAt(i);
}
return ans;
}
public static boolean isPalindrome(String w) {
for (int i = 0; i < w.length() / 2; i++) {
if (w.charAt(i) != w.charAt(w.length() - i - 1)) {
return false;
}
}
return true;
}
public static void getPowerSet(Queue<Integer> a) {
int n = a.poll();
if (!a.isEmpty()) {
getPowerSet(a);
}
int s = powerSet.size();
for (int i = 0; i < s; i++) {
ArrayList<Integer> ne = new ArrayList<>();
ne.add(n);
for (int j = 0; j < powerSet.get(i).size(); j++) {
ne.add(powerSet.get(i).get(j));
}
powerSet.add(ne);
}
ArrayList<Integer> p = new ArrayList<>();
p.add(n);
powerSet.add(p);
}
public static int getlo(int va) {
int v = 1;
while (v <= va) {
if ((va&v) != 0) {
return v;
}
v <<= 1;
}
return 0;
}
static long fast_pow(long a, long p, long mod) {
long res = 1;
while (p > 0) {
if (p % 2 == 0) {
a = (a * a) % mod;
p /= 2;
} else {
res = (res * a) % mod;
p--;
}
}
return res;
}
public static int countPrimeInRange(int n, boolean isPrime[]) {
int cnt = 0;
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * 2; j <= n; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
cnt++;
}
}
return cnt;
}
public static void create(long num) {
luc.add(num);
if (num > power(10, 9)) {
return;
}
create(num * 10 + 4);
create(num * 10 + 7);
}
public static long ceil(long a, long b) {
return (a + b - 1) / b;
}
public static long round(long a, long b) {
if (a < 0) {
return (a - b / 2) / b;
}
return (a + b / 2) / b;
}
public static void allPremutationsst(LinkedList<String> l, boolean visited[], ArrayList<String> st) {
if (l.size() == st.size()) {
allprems.add(l);
}
for (int i = 0; i < st.size(); i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<String> nl = new LinkedList<>();
for (String x : l) {
nl.add(x);
}
nl.add(st.get(i));
allPremutationsst(nl, visited, st);
visited[i] = false;
}
}
}
public static void allPremutations(LinkedList<Integer> l, boolean visited[], int a[]) {
if (l.size() == a.length) {
allprem.add(l);
}
for (int i = 0; i < a.length; i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<Integer> nl = new LinkedList<>();
for (Integer x : l) {
nl.add(x);
}
nl.add(a[i]);
allPremutations(nl, visited, a);
visited[i] = false;
}
}
}
public static int binarySearch(long[] a, long value) {
int l = 0;
int r = a.length - 1;
while (l <= r) {
int m = (l + r) / 2;
if (a[m] == value) {
return m;
} else if (a[m] > value) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}
public static void reverse(int l, int r, char ch[]) {
for (int i = 0; i < r / 2; i++) {
char c = ch[i];
ch[i] = ch[r - i - 1];
ch[r - i - 1] = c;
}
}
public static int logK(long v, long k) {
int ans = 0;
while (v > 1) {
ans++;
v /= k;
}
return ans;
}
public static long power(long a, long n) {
if (n == 1) {
return a;
}
long pow = power(a, n / 2);
pow *= pow;
if (n % 2 != 0) {
pow *= a;
}
return pow;
}
public static long get(long max, long x) {
if (x == 1) {
return max;
}
int cnt = 0;
while (max > 0) {
cnt++;
max /= x;
}
return cnt;
}
public static int numOF0(long v) {
long x = 1;
int cnt = 0;
while (x <= v) {
if ((x & v) == 0) {
cnt++;
}
x <<= 1;
}
return cnt;
}
public static int log2(double n) {
int cnt = 0;
while (n > 1) {
n /= 2;
cnt++;
}
return cnt;
}
public static int[] bfs(int node, ArrayList<Integer> a[]) {
Queue<Integer> q = new LinkedList<>();
q.add(node);
int distances[] = new int[a.length];
Arrays.fill(distances, -1);
distances[node] = 0;
while (!q.isEmpty()) {
int parent = q.poll();
ArrayList<Integer> nodes = a[parent];
int cost = distances[parent];
for (Integer node1 : nodes) {
if (distances[node1] == -1) {
q.add(node1);
distances[node1] = cost + 1;
}
}
}
return distances;
}
public static int get(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static ArrayList<Integer> primeFactors(int n) {
ArrayList<Integer> a = new ArrayList<>();
while (n % 2 == 0) {
a.add(2);
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
a.add(i);
n /= i;
}
if (n < i) {
break;
}
}
if (n > 2) {
a.add(n);
}
return a;
}
public static ArrayList<Integer> printPrimeFactoriztion(int n) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = 1; i < Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
if (isPrime(i)) {
a.add(i);
n /= i;
i = 0;
} else if (isPrime(n / i)) {
a.add(n / i);
n = i;
i = 0;
}
}
}
return a;
}
// end of solution
public static BigInteger f(long n) {
if (n <= 1) {
return BigInteger.ONE;
}
long t = n - 1;
BigInteger b = new BigInteger(t + "");
BigInteger ans = new BigInteger(n + "");
while (t > 1) {
ans = ans.multiply(b);
b = b.subtract(BigInteger.ONE);
t--;
}
return ans;
}
public static long factorial(long n) {
if (n <= 1) {
return 1;
}
long t = n - 1;
while (t > 1) {
n = mod((mod(n, mod) * mod(t, mod)), mod);
t--;
}
return n;
}
public static long rev(long n) {
long t = n;
long ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans;
}
public static boolean isPalindrome(int n) {
int t = n;
int ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans == n;
}
static class tri {
int x, y, z;
public tri(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return x + " " + y + " " + z;
}
}
static boolean isPrime(long num) {
if (num == 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
if (num == 3) {
return true;
}
for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void prefixSum(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = a[i] + a[i - 1];
}
}
public static void suffixSum(long[] a) {
for (int i = a.length - 2; i > -1; i--) {
a[i] = a[i] + a[i + 1];
}
}
static long mod(long a, long b) {
long r = a % b;
return r < 0 ? r + b : r;
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] >= a[i + 1]) {
return false;
}
}
return true;
}
public static void print(int[] a) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt();
}
}
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 + " " + y;
}
}
static class pai {
long x;
int y;
public pai(long x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return x / GCD(x, y) * y;
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
public static void simplifyTheFraction(long a, long b) {
long GCD = GCD(a, b);
System.out.println(a / GCD + " " + b / GCD);
}
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(problemName + ".out");
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
String nextLine() {
String str = "";
try {
str = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
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());
}
}
}
|
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 17
|
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
|
e046eb01ce967e25a1b3363fdd4c5be0
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
List<Location> locs = findPoints(S, r, c);
if (!locs.isEmpty() && !isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>();
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
while (!q.isEmpty()) {
Location loc = q.poll();
if (loc.R < 0 || loc.R >= S.length || loc.C < 0 || loc.C >= S[loc.R].length || S[loc.R][loc.C] != '*') {
continue;
}
seen.add(loc);
S[loc.R][loc.C] = '#';
for (int d = 0; d < DR.length; ++d) {
q.offer(new Location(loc.R + DR[d], loc.C + DC[d]));
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
364174c827163076fbffa076008e689f
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
List<Location> locs = findPoints(S, r, c);
if (!locs.isEmpty() && !isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> BIG_LIST = new ArrayList<>(10000);
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = BIG_LIST;
seen.clear();
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
while (!q.isEmpty()) {
Location loc = q.poll();
if (loc.R < 0 || loc.R >= S.length || loc.C < 0 || loc.C >= S[loc.R].length || S[loc.R][loc.C] != '*') {
continue;
}
seen.add(loc);
S[loc.R][loc.C] = '#';
for (int d = 0; d < DR.length; ++d) {
q.offer(new Location(loc.R + DR[d], loc.C + DC[d]));
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
e4e5a59e12ba536224b7d03fb22958ee
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
List<Location> locs = findPoints(S, r, c);
if (!locs.isEmpty() && !isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>(4);
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
while (!q.isEmpty()) {
Location loc = q.poll();
if (loc.R < 0 || loc.R >= S.length || loc.C < 0 || loc.C >= S[loc.R].length || S[loc.R][loc.C] != '*') {
continue;
}
seen.add(loc);
S[loc.R][loc.C] = '#';
for (int d = 0; d < DR.length; ++d) {
q.offer(new Location(loc.R + DR[d], loc.C + DC[d]));
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
a5a36a1015f47481a3ce1409c2ff2c8a
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
List<Location> locs = findPoints(S, r, c);
if (!locs.isEmpty() && !isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>();
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
while (!q.isEmpty()) {
Location loc = q.poll();
if (loc.R < 0 || loc.R >= S.length || loc.C < 0 || loc.C >= S[loc.R].length || S[loc.R][loc.C] != '*') {
continue;
}
seen.add(loc);
S[loc.R][loc.C] = '#';
for (int d = 0; d < DR.length; ++d) {
q.offer(new Location(loc.R + DR[d], loc.C + DC[d]));
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
8ef424d490266c570cf4f3130e7c773f
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
if (S[r][c] != '*') {
continue;
}
List<Location> locs = findPoints(S, r, c);
if (!isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>();
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
while (!q.isEmpty()) {
Location loc = q.poll();
if (loc.R < 0 || loc.R >= S.length || loc.C < 0 || loc.C >= S[loc.R].length || S[loc.R][loc.C] != '*') {
continue;
}
seen.add(loc);
S[loc.R][loc.C] = '#';
for (int d = 0; d < DR.length; ++d) {
q.offer(new Location(loc.R + DR[d], loc.C + DC[d]));
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
697ffd4561be490845c77b7105ad4354
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
if (S[r][c] != '*') {
continue;
}
List<Location> locs = findPoints(S, r, c);
if (!isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new LinkedList<>();
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
while (!q.isEmpty()) {
Location loc = q.poll();
if (loc.R < 0 || loc.R >= S.length || loc.C < 0 || loc.C >= S[loc.R].length || S[loc.R][loc.C] != '*') {
continue;
}
seen.add(loc);
S[loc.R][loc.C] = '#';
for (int d = 0; d < DR.length; ++d) {
q.offer(new Location(loc.R + DR[d], loc.C + DC[d]));
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
e160ac030befad469a850219a4393a35
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
if (S[r][c] != '*') {
continue;
}
List<Location> locs = findPoints(S, r, c);
if (!isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>(4);
ArrayDeque<Location> q = new ArrayDeque<>(4);
q.offer(new Location(r0, c0));
S[r0][c0] = '#';
while (!q.isEmpty()) {
Location loc = q.poll();
seen.add(loc);
for (int d = 0; d < DR.length; ++d) {
int nr = loc.R + DR[d];
int nc = loc.C + DC[d];
if (nr >= 0 && nr < S.length && nc >= 0 && nc < S[nr].length && S[nr][nc] == '*') {
q.offer(new Location(nr, nc));
S[nr][nc] = '#';
}
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
70ab61cf5e239b45cf34f3c99b43fbf7
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
if (S[r][c] != '*') {
continue;
}
List<Location> locs = findPoints(S, r, c);
if (!isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>(4);
ArrayDeque<Location> q = new ArrayDeque<>();
q.offer(new Location(r0, c0));
S[r0][c0] = '#';
while (!q.isEmpty()) {
Location loc = q.poll();
seen.add(loc);
for (int d = 0; d < DR.length; ++d) {
int nr = loc.R + DR[d];
int nc = loc.C + DC[d];
if (nr >= 0 && nr < S.length && nc >= 0 && nc < S[nr].length && S[nr][nc] == '*') {
q.offer(new Location(nr, nc));
S[nr][nc] = '#';
}
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
b77de7ff505948a782bf43bf55be40d9
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
if (S[r][c] != '*') {
continue;
}
List<Location> locs = findPoints(S, r, c);
if (!isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>(4);
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
S[r0][c0] = '#';
while (!q.isEmpty()) {
Location loc = q.poll();
seen.add(loc);
for (int d = 0; d < DR.length; ++d) {
int nr = loc.R + DR[d];
int nc = loc.C + DC[d];
if (nr >= 0 && nr < S.length && nc >= 0 && nc < S[nr].length && S[nr][nc] == '*') {
q.offer(new Location(nr, nc));
S[nr][nc] = '#';
}
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
a0198d6a6b1419f5a83ecfc4d4021957
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
if (S[r][c] != '*') {
continue;
}
List<Location> locs = findPoints(S, r, c);
if (!isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new ArrayList<>();
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
S[r0][c0] = '#';
while (!q.isEmpty()) {
Location loc = q.poll();
seen.add(loc);
for (int d = 0; d < DR.length; ++d) {
int nr = loc.R + DR[d];
int nc = loc.C + DC[d];
if (nr >= 0 && nr < S.length && nc >= 0 && nc < S[nr].length && S[nr][nc] == '*') {
q.offer(new Location(nr, nc));
S[nr][nc] = '#';
}
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
d84f293ac695322f96cb241c8b373c53
|
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 {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int M = io.nextInt();
char[][] S = new char[N][];
for (int i = 0; i < N; ++i) {
S[i] = io.nextLine().toCharArray();
}
for (int r = 0; r + 1 < N; ++r) {
for (int c = 0; c + 1 < M; ++c) {
if (S[r][c] == S[r + 1][c + 1] && S[r + 1][c] == S[r][c + 1] && S[r][c] != S[r][c + 1]) {
io.println("NO");
return;
}
}
}
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
if (S[r][c] != '*') {
continue;
}
List<Location> locs = findPoints(S, r, c);
if (!isLShape(locs)) {
io.println("NO");
return;
}
}
}
io.println("YES");
}
private static boolean isLShape(List<Location> locs) {
if (locs.size() != 3) {
return false;
}
int rMin = Integer.MAX_VALUE;
int cMin = Integer.MAX_VALUE;
for (Location loc : locs) {
rMin = Math.min(rMin, loc.R);
cMin = Math.min(cMin, loc.C);
}
for (Location loc : locs) {
if (loc.R - rMin > 1 || loc.C - cMin > 1) {
return false;
}
}
return true;
}
private static final int[] DR = {-1, 0, 1, 0};
private static final int[] DC = {0, 1, 0, -1};
private static List<Location> findPoints(char[][] S, int r0, int c0) {
List<Location> seen = new LinkedList<>();
LinkedList<Location> q = new LinkedList<>();
q.offer(new Location(r0, c0));
S[r0][c0] = '#';
while (!q.isEmpty()) {
Location loc = q.poll();
seen.add(loc);
for (int d = 0; d < DR.length; ++d) {
int nr = loc.R + DR[d];
int nc = loc.C + DC[d];
if (nr >= 0 && nr < S.length && nc >= 0 && nc < S[nr].length && S[nr][nc] == '*') {
q.offer(new Location(nr, nc));
S[nr][nc] = '#';
}
}
}
return seen;
}
private static class Location {
public int R, C;
public Location(int R, int C) {
this.R = R;
this.C = C;
}
@Override
public String toString() {
return String.format("(%d, %d)", R, C);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.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 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
|
aaf55301c62a33246c6de116707cea03
|
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.*;
public class Main {
private static boolean check(int[] a, int[] b, int[] c) {
if (a[0] - 1 == b[0] && a[1] == b[1] && a[0] == c[0] && a[1] - 1 == c[1]) {
return true;
}
if (a[0] - 1 == c[0] && a[1] == c[1] && a[0] == b[0] && a[1] - 1 == b[1]) {
return true;
}
if (a[0] - 1 == b[0] && a[1] == b[1] && a[0] == c[0] && a[1] + 1 == c[1]) {
return true;
}
if (a[0] - 1 == c[0] && a[1] == c[1] && a[0] == b[0] && a[1] + 1 == b[1]) {
return true;
}
if (a[0] == b[0] && a[1] - 1 == b[1] && a[1] == c[1] && a[0] + 1 == c[0]) {
return true;
}
if (a[0] == c[0] && a[1] - 1 == c[1] && a[1] == b[1] && a[0] + 1 == b[0]) {
return true;
}
if (a[0] == b[0] && a[1] + 1 == b[1] && a[1] == c[1] && a[0] + 1 == c[0]) {
return true;
}
if (a[0] == c[0] && a[1] + 1 == c[1] && a[1] == b[1] && a[0] + 1 == b[0]) {
return true;
}
return false;
}
private static void soutln(Object o) {
System.out.println(o);
}
private static void sout(Object o) {
System.out.print(o);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int[] dx = new int[]{1, -1, 0, 0, 1, -1, 1, -1};
int[] dy = new int[]{0, 0, 1, -1, -1, -1, 1, 1};
while (T-- > 0) {
int row = sc.nextInt();
int col = sc.nextInt();
char[][] g = new char[row][col];
for (int i = 0; i < row; i++) {
g[i] = sc.next().toCharArray();
}
int tag = 0;
int[][] vis = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (g[i][j] == '*' && vis[i][j] == 0) {
int count = 0;
Deque<int[]> que = new LinkedList<>();
vis[i][j] = 1;
que.add(new int[]{i, j});
List<int[]> list = new ArrayList<>();
while (!que.isEmpty()) {
int[] p = que.poll();
count++;
int x = p[0];
int y = p[1];
list.add(new int[]{x, y});
for (int u = 0; u < 8; u++) {
int nx = x + dx[u];
int ny = y + dy[u];
if (nx >= 0 && nx < row && ny >= 0 && ny < col &&
g[nx][ny] == '*' && vis[nx][ny] == 0) {
vis[nx][ny] = 1;
que.add(new int[]{nx, ny});
}
}
}
if (count != 3) {
tag = 1;
break;
}
int[] a = list.get(0);
int[] b = list.get(1);
int[] c = list.get(2);
if (!check(a, b, c) &&
!check(a, c, b) &&
!check(b, a, c) &&
!check(b, c, a) &&
!check(c, a, b) &&
!check(c, b, a)) {
tag = 1;
break;
}
}
}
if (tag == 1) {
break;
}
}
if (tag == 0) {
soutln("yes");
} else {
soutln("no");
}
}
}
}
|
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
|
f37b98d786204e1a7f70e6a1eb380aef
|
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 TaskF {
static int[][] fillX = {
{1, 1},
{1, 1},
{0, 1},
{1, 0}
};
static int[][] fillY = {
{0, 1},
{-1, 0},
{1, 1},
{0, 1}
};
static int[][] checkX = {
{-1, -1, 0, 0, 1, 2, 2, 2, 2, 1, 0, -1},
{-1, -1, 0, 1, 2, 2, 2, 2, 1, 0, 0, -1},
{-1, -1, -1, 0, 1, 2, 2, 2, 1, 1, 0, -1},
{-1, -1, -1, 0, 1, 1, 2, 2, 2, 1, 0, -1}
};
static int[][] checkY = {
{0, 1, 1, 2, 2, 2, 1, 0, -1, -1, -1, -1},
{0, 1, 1, 1, 1, 0, -1, -2, -2, -2, -1, -1},
{0, 1, 2, 2, 2, 2, 1, 0, 0, -1, -1, -1},
{0, 1, 2, 2, 2, 1, 1, 0, -1, -1, -1, -1}
};
public static void main(String[] args) {
FastReader reader = new FastReader();
int tt = reader.nextInt();
// int tt = 1;
mainLoop: for (; tt > 0; tt--) {
int n = reader.nextInt();
int m = reader.nextInt();
int[][] arr = new int[n][m];
boolean[][] checked = new boolean[n][m];
for (int i = 0; i < n; i++) {
String line = reader.nextLine();
for (int j = 0; j < line.length(); j++) {
if (line.charAt(j) == '*') {
arr[i][j] = 1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] == 1 && !checked[i][j]) {
int type = getTypeOfL(i, j, arr, checked);
if (type == -1 || !emptyArea(i, j, type, arr)) {
System.out.println("NO");
continue mainLoop;
}
}
}
}
System.out.println("YES");
}
}
static int getTypeOfL(int x, int y, int[][] arr, boolean[][] checked) {
loopT: for (int t = 0; t < fillX.length; t++) {
for (int d = 0; d < fillX[0].length; d++) {
int nx = x + fillX[t][d];
int ny = y + fillY[t][d];
if (nx < 0 || nx >= arr.length || ny < 0 || ny >= arr[0].length || arr[nx][ny] != 1) {
continue loopT;
}
}
for (int d = 0; d < fillX[0].length; d++) {
int nx = x + fillX[t][d];
int ny = y + fillY[t][d];
checked[nx][ny] = true;
}
return t;
}
return -1;
}
static boolean emptyArea(int x, int y, int type, int[][] arr) {
for (int d = 0; d < checkX[0].length; d++) {
int nx = x + checkX[type][d];
int ny = y + checkY[type][d];
if (nx >= 0 && nx < arr.length && ny >= 0 && ny < arr[0].length && arr[nx][ny] == 1) {
return false;
}
}
return true;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
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
|
86bffe1c31cdb5060824cc85dfd79e1b
|
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.*;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
StringTokenizer st1 = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st1.nextToken());
int m = Integer.parseInt(st1.nextToken());
char[][] shaded = new char[n][m];
for (int i=0; i<n; i++)
shaded[i] = in.readLine().toCharArray();
if (isAllShadedCellsValid(n, m, shaded)) out.println("YES");
else out.println("NO");
}
in.close();
out.close();
}
private static boolean isAllShadedCellsValid(int n, int m, char[][] shaded) {
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
// If current cell is shaded, then, check if it is part of a valid L shape or not
if (isGivenCellShaded(shaded, n, m, i, j) && !isCurrentShadedCellPartOfAValidLShape(shaded, n, m, i, j))
return false;
return true;
}
private static boolean isCurrentShadedCellPartOfAValidLShape(char[][] shaded, int n, int m, int i, int j) {
int left = 0, right = 0, top = 0, bottom = 0, leftTop = 0, rightTop = 0, leftBottom = 0, rightBottom = 0;
if ( isGivenCellShaded(shaded, n, m, i , j-1) ) left++;
if ( isGivenCellShaded(shaded, n, m, i , j+1) ) right++;
if ( isGivenCellShaded(shaded, n, m, i-1, j ) ) top++;
if ( isGivenCellShaded(shaded, n, m, i-1, j-1) ) leftTop++;
if ( isGivenCellShaded(shaded, n, m, i-1, j+1) ) rightTop++;
if ( isGivenCellShaded(shaded, n, m, i+1, j ) ) bottom++;
if ( isGivenCellShaded(shaded, n, m, i+1, j-1) ) leftBottom++;
if ( isGivenCellShaded(shaded, n, m, i+1, j+1) ) rightBottom++;
// If neighbours count is not equal to 2, then, invalid
if (left + right + top + bottom + leftTop + rightTop + leftBottom + rightBottom != 2)
return false;
// If more than 1 diagonal neighbour is shaded, then, invalid
if (leftTop + rightTop + leftBottom + rightBottom > 1)
return false;
// Current cell is the centre of the 'L' shape
if (left == 1 && top == 1) return true;
if (left == 1 && bottom == 1) return true;
if (top == 1 && right == 1) return true;
if (right == 1 && bottom == 1) return true;
// Current cell is one of the edges of the 'L' shape
if (left == 1 && leftTop == 1) return true;
if (leftTop == 1 && top == 1) return true;
if (top == 1 && rightTop == 1) return true;
if (rightTop == 1 && right == 1) return true;
if (right == 1 && rightBottom == 1) return true;
if (rightBottom == 1 && bottom == 1) return true;
if (bottom == 1 && leftBottom == 1) return true;
if (leftBottom == 1 && left == 1) return true;
return false;
}
private static boolean isGivenCellShaded(char[][] shaded, int n, int m, int i, int j) {
return !(i < 0 || j < 0 || i == n || j == m) && shaded[i][j] == '*';
}
}
|
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
|
243c6c3af615717957c4291c06171caf
|
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.*;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
StringTokenizer st1 = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st1.nextToken());
int m = Integer.parseInt(st1.nextToken());
char[][] shaded = new char[n][m];
for (int i=0; i<n; i++)
shaded[i] = in.readLine().toCharArray();
if (isInvalid(n, m, shaded)) out.println("NO");
else out.println("YES");
}
in.close();
out.close();
}
private static boolean isInvalid(int n, int m, char[][] shaded) {
boolean invalid = false;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (isGivenCellShaded(shaded, n, m, i, j) && get8NeighboursShadedCount(shaded, n, m, i, j) == -1) {
invalid = true;
break;
}
}
if (invalid)
break;
}
return invalid;
}
private static int get8NeighboursShadedCount(char[][] shaded, int n, int m, int i, int j) {
int left = 0, right = 0, up = 0, down = 0, leftTop = 0, rightTop = 0, leftBottom = 0, rightBottom = 0;
if (isGivenCellShaded(shaded, n, m, i, j-1)) left++;
if (isGivenCellShaded(shaded, n, m, i, j+1)) right++;
if (isGivenCellShaded(shaded, n, m, i-1, j)) up++;
if (isGivenCellShaded(shaded, n, m, i-1, j-1)) leftTop++;
if (isGivenCellShaded(shaded, n, m, i-1, j+1)) rightTop++;
if (isGivenCellShaded(shaded, n, m, i+1, j)) down++;
if (isGivenCellShaded(shaded, n, m, i+1, j-1)) leftBottom++;
if (isGivenCellShaded(shaded, n, m, i+1, j+1)) rightBottom++;
// If neighbours count is not equal 2, then, invalid
if (left+right+up+down+leftTop+rightTop+leftBottom+rightBottom != 2)
return -1;
// If more than 1 diagonal neighbour is shaded, then, invalid
if (leftTop + rightTop + leftBottom + rightBottom > 1)
return -1;
/*
1 2 .
* * .
3 4 .
*/
if (left == 1) {
if (leftTop + up + down + leftBottom != 1)
return -1;
}
/*
1 * 2
3 * 4
. . .
*/
if (up == 1) {
if (leftTop + rightTop + left + right != 1)
return -1;
}
/*
. 1 2
. * *
. 3 4
*/
if (right == 1) {
if (up + rightTop + down + rightBottom != 1)
return -1;
}
/*
. . .
1 * 2
3 * 4
*/
if (down == 1) {
if (left + right + leftBottom + rightBottom != 1)
return -1;
}
return 1;
}
private static boolean isGivenCellShaded(char[][] shaded, int n, int m, int i, int j) {
return !(i < 0 || j < 0 || i == n || j == m) && shaded[i][j] == '*';
}
}
|
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
|
46ea91584f8deb0624184f33d5107c79
|
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.text.*;
import java.math.*;
import java.util.regex.*;
public class HarHarShambhu{
static Scanner in=new Scanner();
static long systemTime;
static long mod = 1000000007;
//static ArrayList<ArrayList<Integer>> adj;
static int seive[]=new int[1000001];
static long C[][];
public static void main(String[] args) throws Exception{
int z=in.readInt();
for(int test=1;test<=z;test++) {
//setTime();
solve();
//printTime();
//printMemory();
}
}
static void solve() {
int n=in.readInt();
int m=in.readInt();
char c[][]=new char[n][m];
for(int i=0;i<n;i++) {
c[i]=in.readString().toCharArray();
}
int vis[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(vis[i][j]==0&&c[i][j]=='*') {
ArrayList<Integer[]> al=new ArrayList<>();
dfs(i,j,c,vis,n,m,al);
int cnt=al.size();
// for(Integer p[]:al) {
// print(p);
// }
if(cnt!=3) {
print("NO");
return;
}
Collections.sort(al,(u,v)->(u[0]-v[0]==0?u[1]-v[1]:u[0]-v[0]));
int f=1;
int f2=0;
for(int ii=1;ii<3;ii++) {
if(al.get(ii)[0]-al.get(ii-1)[0]==0) {
}
else {
f2=1;
}
}
if(f2==0) {
print("NO");
return;
}
for(int ii=1;ii<3;ii++) {
if(al.get(ii)[1]-al.get(ii-1)[1]==0) {
}
else {
f2=1;
}
}
if(f2==0) {
print("NO");
return;
}
if(f==1) {
if(al.get(0)[0]-al.get(1)[0]==0) {
if(al.get(0)[1]-al.get(1)[1]==-1) {
if(al.get(1)[1]-al.get(2)[1]==0) {
if(al.get(2)[0]-al.get(1)[0]==1) {
f=0;
}
}
}
}
if(al.get(0)[1]-al.get(1)[1]==0) {
if(al.get(0)[0]-al.get(1)[0]==-1) {
if(al.get(1)[0]-al.get(2)[0]==0) {
if(al.get(2)[1]-al.get(1)[1]==1) {
f=0;
}
}
}
}
if(al.get(0)[0]-al.get(1)[0]==0) {
if(al.get(0)[1]-al.get(1)[1]==-1) {
if(al.get(0)[1]-al.get(2)[1]==0) {
if(al.get(2)[0]-al.get(0)[0]==1) {
f=0;
}
}
}
}
}
if(f==1) {
if(al.get(0)[0]-al.get(2)[0]==0) {
if(al.get(2)[1]-al.get(0)[1]==1) {
if(al.get(2)[0]-al.get(1)[0]==0) {
if(al.get(2)[1]-al.get(1)[1]==1) {
f=0;
}
}
}
}
if(al.get(0)[1]-al.get(2)[1]==0) {
if(al.get(2)[0]-al.get(0)[0]==1) {
if(al.get(2)[1]-al.get(1)[1]==1) {
if(al.get(2)[0]-al.get(1)[0]==0) {
f=0;
}
}
}
}
}
if(f==1) {
print("NO");
return;
}
}
}
}
print("YES");
}
static int dfs(int i,int j,char c[][],int vis[][],int n,int m,ArrayList<Integer[]> al) {
vis[i][j]=1;
int cnt=0;
al.add(new Integer[] {i,j});
if(isvalid(i+1,j,c,vis,n,m)) {
cnt=1+dfs(i+1,j,c,vis,n,m,al);
}
if(isvalid(i,j+1,c,vis,n,m)) {
cnt=1+dfs(i,j+1,c,vis,n,m,al);
}
if(isvalid(i-1,j,c,vis,n,m)) {
cnt=1+dfs(i-1,j,c,vis,n,m,al);
}
if(isvalid(i,j-1,c,vis,n,m)) {
cnt=1+dfs(i,j-1,c,vis,n,m,al);
}
if(isvalid(i+1,j+1,c,vis,n,m)) {
cnt=1+dfs(i+1,j+1,c,vis,n,m,al);
}
if(isvalid(i+1,j-1,c,vis,n,m)) {
cnt=1+dfs(i+1,j-1,c,vis,n,m,al);
}
if(isvalid(i-1,j+1,c,vis,n,m)) {
cnt=1+dfs(i-1,j+1,c,vis,n,m,al);
}
if(isvalid(i-1,j-1,c,vis,n,m)) {
cnt=1+dfs(i-1,j-1,c,vis,n,m,al);
}
return cnt;
}
static boolean isvalid(int i,int j,char c[][],int vis[][],int n,int m) {
if(i<0||i>=n||j<0||j>=m) {
return false;
}
if(vis[i][j]==1||c[i][j]!='*') {
return false;
}
return true;
}
static long pow(long n, long m) {
if(m==0)
return 1;
else if(m==1)
return n;
else {
long r=pow(n,m/2);
if(m%2==0)
return (r*r);
else
return (r*r*n);
}
}
static long maxsumsub(ArrayList<Long> al) {
long max=0;
long sum=0;
for(int i=0;i<al.size();i++) {
sum+=al.get(i);
if(sum<0) {
sum=0;
}
max=Math.max(max,sum);
}
return max;
}
static long abs(long a) {
return Math.abs(a);
}
static void ncr(int n, int k){
C= new long[n + 1][k + 1];
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
}
static boolean isPalin(String s) {
int i=0,j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
static int knapsack(int W, int wt[],int val[], int n){
int []dp = new int[W + 1];
for (int i = 1; i < n + 1; i++) {
for (int w = W; w >= 0; w--) {
if (wt[i - 1] <= w) {
dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]);
}
}
}
return dp[W];
}
static void seive() {
Arrays.fill(seive, 1);
seive[0]=0;
seive[1]=0;
for(int i=2;i*i<1000001;i++) {
if(seive[i]==1) {
for(int j=i*i;j<1000001;j+=i) {
if(seive[j]==1) {
seive[j]=0;
}
}
}
}
}
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[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long[] nla1(int n){
long[] arr= new long[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static Integer[] nIa(int n){
Integer[] arr= new Integer[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static Long[] nLa(int n){
Long[] arr= new Long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static void no() {
System.out.println("NO");
}
static void yes() {
System.out.println("YES");
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void setTime() {
systemTime = System.currentTimeMillis();
}
static void printTime() {
System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
}
static void printMemory() {
System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
}
|
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
|
c43738ec1ba75289c5da9cbb664ae3a4
|
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 static java.lang.Math.*;
public class MainF {
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
String string = reader.readLine();
if (string == null) {
return false;
}
tokenizer = new StringTokenizer(string);
return tokenizer.hasMoreTokens();
} catch (IOException e) {
return false;
}
}
}
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static void ini() {
p1 = new int[3][2];
p2 = new int[3][2];
}
static String yes = "YES";
static String no = "NO";
static int ipInf = Integer.MAX_VALUE-5;
static int inInf = Integer.MIN_VALUE+5;
static long lpInf = Long.MAX_VALUE - 5;
static long lnInf = Long.MIN_VALUE + 5;
public static void main(String[] args) {
int t = in.nextInt();
ini();
while (t -- > 0) {
solve();
}
out.close();
}
static void solve() {
int n = in.nextInt();
int m = in.nextInt();
char[][] map = new char[n][m];
for (int i = 0; i < n; i++) {
String s = in.next();
map[i] = s.toCharArray();
}
int[][] d = new int[][]{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int k1, k2, x1, y1, x2, y2;
ArrayList<L> ls = new ArrayList<>(100);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == '*') {
// check the four direction:
for (int k = 0; k < 4; k++) {
k1 = k;
k2 = k + 1;
k2 %= 4;
x1 = i + d[k1][0];
y1 = j + d[k1][1];
x2 = i + d[k2][0];
y2 = j + d[k2][1];
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m &&
x2 >= 0 && x2 < n && y2 >= 0 && y2 < m) {
if (map[x1][y1] == '*' && map[x2][y2] == '*') {
ls.add(new L(i, j, k));
map[i][j] = '.';
map[x1][y1] = '.';
map[x2][y2] = '.';
break;
}
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == '*') {
out.println(no);
return;
}
}
}
int nn = ls.size();
for (int i = 0; i < nn; i++) {
for (int j = i+1; j < nn; j++) {
if (!check(ls.get(i), ls.get(j))) {
out.println(no);
return;
}
}
}
out.println(yes);
}
static int[][] p1, p2;
static boolean check(L l1, L l2) {
p1[0][0] = l1.x;
p1[0][1] = l1.y;
p2[0][0] = l2.x;
p2[0][1] = l2.y;
switch (l1.direction) {
case 0:
p1[1][0] = l1.x - 1;
p1[1][1] = l1.y;
p1[2][0] = l1.x;
p1[2][1] = l1.y + 1;
break;
case 1:
p1[1][0] = l1.x + 1;
p1[1][1] = l1.y;
p1[2][0] = l1.x;
p1[2][1] = l1.y + 1;
break;
case 2:
p1[1][0] = l1.x + 1;
p1[1][1] = l1.y;
p1[2][0] = l1.x;
p1[2][1] = l1.y - 1;
break;
case 3:
p1[1][0] = l1.x - 1;
p1[1][1] = l1.y;
p1[2][0] = l1.x;
p1[2][1] = l1.y - 1;
break;
}
switch (l2.direction) {
case 0:
p2[1][0] = l2.x - 1;
p2[1][1] = l2.y;
p2[2][0] = l2.x;
p2[2][1] = l2.y + 1;
break;
case 1:
p2[1][0] = l2.x + 1;
p2[1][1] = l2.y;
p2[2][0] = l2.x;
p2[2][1] = l2.y + 1;
break;
case 2:
p2[1][0] = l2.x + 1;
p2[1][1] = l2.y;
p2[2][0] = l2.x;
p2[2][1] = l2.y - 1;
break;
case 3:
p2[1][0] = l2.x - 1;
p2[1][1] = l2.y;
p2[2][0] = l2.x;
p2[2][1] = l2.y - 1;
break;
}
int x1, y1, x2, y2;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
x1 = p1[i][0];
y1 = p1[i][1];
x2 = p2[j][0];
y2 = p2[j][1];
int dist = abs(x1 - x2) + abs(y1 - y2);
if (dist <= 1) return false;
if (dist == 2 && abs(x1 - x2) == 1) return false;
}
}
return true;
}
static class L {
int x, y, direction;
public L (int _1, int _2, int _3) {
x = _1;
y = _2;
direction = _3; // 0, 1, 2, 3: 分别对应四个象限。
}
}
static void printArr (int[] arr) {
int n = arr.length;
if (n == 0) return;
for (int i = 0; i < n-1; i++) {
out.print(arr[i] + " ");
}
out.println(arr[n-1]);
}
static void printArr (long[] arr) {
int n = arr.length;
if (n == 0) return;
for (int i = 0; i < n-1; i++) {
out.print(arr[i] + " ");
}
out.println(arr[n-1]);
}
static long _gcd (long a, long b) {
long temp;
while (true) {
temp = a % b;
if (temp == 0) return b;
a = b;
b = 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 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
|
87d396dcd98a2824e51b66fd9761f659
|
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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Main {
static int[]dx= {0,0,-1,1,1,-1,1,-1};
static int[]dy= {1,-1,0,0,1,-1,-1,1};
static int[][]weizhi;
static char[][]arr;
static String asn="Yes";
static List<List<Integer>>llList;
static int d;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
List<String>list=new ArrayList<>();
for (int i = 0; i < t; i++) {
int n=sc.nextInt();
int m=sc.nextInt();
arr=new char[n][m];
for (int j = 0; j < arr.length; j++) {
arr[j]=sc.next().toCharArray();
}
asn="Yes";
weizhi=new int[n][m];
for (int j = 0; j < arr.length; j++) {
for (int k = 0; k < arr[0].length; k++) {
if (arr[j][k]=='*'&&weizhi[j][k]==0) {
d=0;
llList=new ArrayList<>();
dfs(j, k);
if (d!=3) {
asn="No";
}else {
if ((llList.get(0).get(0)==llList.get(1).get(0)&&llList.get(2).get(0)==llList.get(1).get(0))||(llList.get(0).get(1)==llList.get(1).get(1)&&llList.get(2).get(1)==llList.get(1).get(1))) {
asn="No";
}
if (kgh(llList.get(0), llList.get(1), llList.get(2))||kgh(llList.get(1), llList.get(0), llList.get(2))||kgh(llList.get(2), llList.get(1), llList.get(0))) {
}else {
asn="No";
}
}
}
}
}
list.add(asn);
}
for (String string : list) {
System.out.println(string);
}
}
static boolean kgh(List<Integer>a,List<Integer>b,List<Integer>c) {
return (a.get(0)==b.get(0)||a.get(1)==b.get(1))&&(a.get(0)==c.get(0)||a.get(1)==c.get(1));
}
static void dfs(int x1,int y1) {
weizhi[x1][y1]=1;
d++;
List<Integer>a=new ArrayList<>();
a.add(x1);a.add(y1);
llList.add(a);
for (int i = 0; i < dx.length; i++) {
int x=x1+dx[i];int y=y1+dy[i];
if (x>=0&&x<arr.length&&y>=0&&y<arr[0].length&&weizhi[x][y]==0&&arr[x][y]=='*') {
dfs(x, y);
}
}
}
}
|
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
|
994ec076521255e3a679b52de5f548e8
|
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.*;
import java.util.Map.Entry;
public class Main {
// static int [] arr;
// static boolean prime[] = new boolean[1000];
// static int l;
// static String s; static StringBuilder sb;
// static HashSet<L> hs;
// static HashSet<Long> hs = new HashSet<Long>();
// static int ans;
// static boolean checked [];
//static final int mod = 1000000007;
// static int[][] dp;
// static int[] w ,v;
static int n,m;
// static int arr[][];
// static long ans;
static Scanner sc;
static PrintWriter out;
//static int n, m, w, t;
// static char [] a , b;
//static StringBuilder sb;
// static int ans;
static long[] sum;
static long dp[][];
static long ans;
static char[] []arr;
static ArrayList<Integer> v;
public static void main(String[] args) throws IOException, InterruptedException {
sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
arr = new char[n][m];
ArrayList<Integer> is = new ArrayList<Integer>();
ArrayList<Integer> js = new ArrayList<Integer>();
for(int i = 0 ; i < n ; i++) {
arr[i] = sc.next().toCharArray();
}
boolean ok = true;
char idx='1';
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(arr[i][j] == '*') {
ok = ok && solve(i,j);
idx++;
}
}
}
out.println(ok? "YES" : "NO");
}
out.close();
}
public static boolean solve(int x , int y ) {
int c = 0;
int [] a = {-1,0,1};
int xsum = 0;
int ysum = 0;
for(int i : a) {
for(int j : a) {
if(i==0 && j == 0)
continue;
if(x+i<0 || x+i >=n || y+j <0 || y + j >=m)
continue;
if(arr[x+i][y+j] == '*') {
c++;
xsum +=i;
ysum+=j;
}
}
}
if(c!=2)
return false;
if(xsum == 0 || ysum == 0 )
return false;
return true;
}
public static int bs(int target) {
int l = 0;
int r = v.size()-1;
while(l<=r) {
int m = (l+r)/2;
if(v.get(m) < target)
l = m+1;
else
r = m-1;
}
return l;
}
public static void swap(int i , int j ,int [] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// public static int trace(int idx,int cost){
// if(cost> t || idx == m)
// return 0;
// int take = v[idx] + solve(idx+1,cost + 3*w*d[idx]);
// //int leave = solve(idx+1,t);
// if(dp[idx][cost] == take){
// sb.append(d[idx]+" "+v[idx]+"\n");
// return 1 + trace(idx+1,cost+3*w*d[idx]);
// }
//
// return trace(idx+1,cost);
// }
private static void reverse(long[] arr) {
// TODO Auto-generated method stub
}
// recursive implementation
static long arrayLCM(int[] arr, int idx) {
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.length - 1) {
return arr[idx];
}
long a = arr[idx];
long b = arrayLCM(arr, idx + 1);
return (a * b / gcd(a, b)); //
}
static int longestSubarrWthSumDivByK(int arr[], int n, int k) {
// unordered map 'um' implemented as
// hash table
HashMap<Integer, Integer> um = new HashMap<Integer, Integer>();
// 'mod_arr[i]' stores (sum[0..i] % k)
int mod_arr[] = new int[n];
int max_len = 0;
long curr_sum = 0;
// traverse arr[] and build up the
// array 'mod_arr[]'
for (int i = 0; i < n; i++) {
curr_sum += arr[i];
// as the sum can be negative,
// taking modulo twice
mod_arr[i] = (int) ((curr_sum % k) + k) % k;
// if true then sum(0..i) is
// divisible by k
if (mod_arr[i] == 0)
// update 'max'
max_len = i + 1;
// if value 'mod_arr[i]' not present in 'um'
// then store it in 'um' with index of its
// first occurrence
else if (um.containsKey(mod_arr[i]) == false)
um.put(mod_arr[i], i);
else
// if true, then update 'max'
if (max_len < (i - um.get(mod_arr[i])))
max_len = i - um.get(mod_arr[i]);
}
// return the required length of longest subarray
// with sum divisible by 'k'
return max_len;
}
static int longestSubArrayOfSumK(int[] arr, int n, int k) {
// HashMap to store (sum, index) tuples
HashMap<Integer, Integer> map = new HashMap<>();
int sum = 0, maxLen = 0;
// traverse the given array
for (int i = 0; i < n; i++) {
// accumulate sum
sum += arr[i];
// when subarray starts from index '0'
if (sum == k)
maxLen = i + 1;
// make an entry for 'sum' if it is
// not present in 'map'
if (!map.containsKey(sum)) {
map.put(sum, i);
}
// check if 'sum-k' is present in 'map'
// or not
if (map.containsKey(sum - k)) {
// update maxLength
if (maxLen < (i - map.get(sum - k)))
maxLen = i - map.get(sum - k);
}
}
return maxLen;
}
static boolean isPrime(long n) {
if (n == 2 || n == 3)
return true;
if (n <= 1 || n % 2 == 0 || n % 3 == 0)
return false;
// To check through all numbers of the form 6k ± 1
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
static long smallestDivisor(long n) {
// if divisible by 2
if (n % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (long i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
static long nCr(int n, int r) {
return fact(n) / (fact(r) * fact(n - r));
}
static long fact(int n) {
long res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
if (arr[i] > arr[i + 1])
return false;
return true;
}
// static void findsubsequences(String s, String ans){
// if (s.length() == 0) {
// if(ans!="")
// if(ans.length()!=l)
// al.add(Long.parseLong(ans));
// return;
// }
//
// // We add adding 1st character in string
// findsubsequences(s.substring(1), ans + s.charAt(0));
//
// // Not adding first character of the string
// // because the concept of subsequence either
// // character will present or not
// findsubsequences(s.substring(1), ans);
//}
static void sieve(int n, boolean[] prime, List<Integer> al) {
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
al.add(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 + " ");
// }
public static void reverse(Object[] arr) {
int i = 0;
int j = arr.length - 1;
while (i < j) {
Object temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
}
class Pair implements Comparable<Pair> {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return this.a - o.a;
}
public String toString() {
return "( " + this.a + " , " + this.b + " )\n";
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public int[][] nextInt2DArr(int l, int w) throws IOException {
int[][] arr = new int[l][w];
for (int i = 0; i < l; i++)
for (int j = 0; j < w; j++)
arr[i][j] = Integer.parseInt(next());
return arr;
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int length) throws IOException {
int[] arr = new int[length];
for (int i = 0; i < length; i++)
arr[i] = Integer.parseInt(next());
return arr;
}
public long[] nextLongArr(int length) throws IOException {
long[] arr = new long[length];
for (int i = 0; i < length; i++)
arr[i] = Long.parseLong(next());
return arr;
}
public boolean ready() throws IOException {
return br.ready();
}
}
|
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
|
ee68ee27d11b5de4dda597acd605cd11
|
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.*;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
static char a[][];
static boolean h[][];
static int m,n;
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
while (t-->0){
n=nextInt();m=nextInt();
a=new char[n][m];
for(int i=0;i<n;i++){
a[i]=next().toCharArray();
}
boolean aa=true;
h=new boolean[n][m];
for(int i=0;i<n-1;i++){
if(!h[i][0]&&a[i][0]=='*'){
if(mm(i,0)==0){
// pw.println(i+" "+0);
aa=false;break;
}
}
for(int j=1;j<m-1;j++){
if(!h[i][j]&&a[i][j]=='*'){
if(mm(i,j)==0&&mm1(i,j)==0){
// pw.println(i+" "+j);
aa=false;break;
}
}
}
if(!aa)break;
if(!h[i][m-1]&&a[i][m-1]=='*'){
if(mm1(i,m-1)==0){
// pw.println(i+" "+(m-1));
aa=false;break;
}
}
}
for(int i=0;i<m;i++){
if(a[n-1][i]=='*'&&!h[n-1][i])aa=false;
}
if(aa)pw.println("YES");
else pw.println("NO");
}
pw.close();
}
static int mm(int i,int j){
int num=0;
for(int q=0;q<2;q++){
for(int w=0;w<2;w++){
if(a[q+i][w+j]=='*')num++;
}
}
if(num!=3)return 0;
if(a[i][j+1]=='.'){
int sum=0;
for(int q=-1;q<3;q++){
for(int w=-1;w<3;w++){
if(q==-1&&w==2)continue;
if(q+i<0||q+i>=n||w+j<0||w+j>=m)continue;
if(a[q+i][w+j]=='*')sum++;
}
}
if(sum!=3)return 0;
else {
h[i][j]=true;
h[i+1][j]=true;
h[i+1][j+1]=true;
return 1;
}
}
else if(a[i+1][j+1]=='.'){
int sum=0;
for(int q=-1;q<3;q++){
for(int w=-1;w<3;w++){
if(q==2&&w==2)continue;
if(q+i<0||q+i>=n||w+j<0||w+j>=m)continue;
if(a[q+i][w+j]=='*')sum++;
}
}
if(sum!=3)return 0;
else {
h[i][j]=true;
h[i+1][j]=true;
h[i][j+1]=true;
return 1;
}
}
else {
int sum=0;
for(int q=-1;q<3;q++){
for(int w=-1;w<3;w++){
if(q==2&&w==-1)continue;
if(q+i<0||q+i>=n||w+j<0||w+j>=m)continue;
if(a[q+i][w+j]=='*')sum++;
}
}
if(sum!=3)return 0;
else {
h[i][j]=true;
h[i][j+1]=true;
h[i+1][j+1]=true;
return 1;
}
}
}
static int mm1(int i,int j){
int sum=0;
if(!(a[i+1][j-1]=='*'&&a[i+1][j]=='*'))return 0;
for(int q=-1;q<3;q++){
for(int w=-2;w<2;w++){
if(q+i<0||q+i>=n||w+j<0||w+j>=m)continue;
if(q==-1&&w==-2)continue;
if(a[q+i][w+j]=='*')sum++;
}
}
if(sum!=3)return 0;
else {
h[i][j]=true;
h[i+1][j-1]=true;
h[i+1][j]=true;
return 1;
}
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
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().charAt(0);
}
}
|
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
|
0684c4aa34ee099e4c7e6a9b12b142b6
|
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: Atuer
*/
public class Main
{
// ==== Solve Code ====//
static int INF = 2000000010;
public static void csh()
{
}
public static void main(String[] args) throws IOException
{
// csh();
int t = in.nextInt();
while (t-- > 0)
{
solve();
out.flush();
}
out.close();
}
static int n, m;
static int[][] a = new int[55][55];
public static void solve()
{
n = in.nextInt();
m = in.nextInt();
for (int i = 0; i < n; i++)
{
char[] s = in.next().toCharArray();
for (int j = 0; j < m; j++)
a[i][j] = s[j] == '*' ? 1 : 0;
}
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < m - 1; j++)
if (a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1] == 3)
{
int tp = 0;
for (int ii = Math.max(0, i - 1); ii < Math.min(n, i + 3); ii++)
for (int jj = Math.max(0, j - 1); jj < Math.min(m, j + 3); jj++)
tp += a[ii][jj];
if (tp == 3 || (tp == 4 && check(i, j)))
filla(i, j);
}
boolean flag = true;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] == 1)
flag = false;
out.println(flag ? "YES" : "NO");
}
private static boolean check(int i, int j)
{
if (i - 1 >= 0 & j - 1 >= 0 && a[i - 1][j - 1] == 1 && a[i][j] == 0)
return true;
else if (i - 1 >= 0 && j + 2 < m && a[i - 1][j + 2] == 1 && a[i][j + 1] == 0)
return true;
else if (i + 2 < n && j - 1 >= 0 && a[i + 2][j - 1] == 1 && a[i + 1][j] == 0)
return true;
else if (i + 2 < n && j + 2 < m && a[i + 2][j + 2] == 1 && a[i + 1][j + 1] == 0)
return true;
return false;
}
private static void filla(int i, int j)
{
a[i][j] = 0;
a[i + 1][j] = 0;
a[i][j + 1] = 0;
a[i + 1][j + 1] = 0;
}
public static class Node
{
int x, y, k;
public Node(int x, int y, int k)
{
this.x = x;
this.y = y;
this.k = k;
}
}
// ==== Solve Code ====//
// ==== Template ==== //
public static long cnm(int a, int b)
{
long sum = 1;
int i = a, j = 1;
while (j <= b)
{
sum = sum * i / j;
i--;
j++;
}
return sum;
}
public static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
public static int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
public static void gbSort(int[] a, int l, int r)
{
if (l < r)
{
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r)
if (a[i] <= a[j])
t[idx++] = a[i++];
else
t[idx++] = a[j++];
while (i <= m)
t[idx++] = a[i++];
while (j <= r)
t[idx++] = a[j++];
for (int z = 0; z < t.length; z++)
a[l + z] = t[z];
}
}
// ==== Template ==== //
// ==== IO ==== //
static InputStream inputStream = System.in;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(System.out);
static class InputReader
{
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream)
{
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
boolean hasNext()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e)
{
return false;
// TODO: handle exception
}
}
return true;
}
public String nextLine()
{
String str = null;
try
{
str = reader.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public Double nextDouble()
{
return Double.parseDouble(next());
}
public BigInteger nextBigInteger()
{
return new BigInteger(next());
}
public BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}
// ==== IO ==== //
}
|
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
|
151a2704ef3b3f4a4f5469ca10aa76f8
|
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: Atuer
*/
public class Main
{
// ==== Solve Code ====//
static int INF = 2000000010;
public static void csh()
{
}
public static void main(String[] args) throws IOException
{
// csh();
int t = in.nextInt();
while (t-- > 0)
{
solve();
out.flush();
}
out.close();
}
static int[][] a = new int[55][55];
public static void solve()
{
int n = in.nextInt();
int m = in.nextInt();
for (int i = 0; i < n; i++)
{
char[] s = in.next().toCharArray();
for (int j = 0; j < m; j++)
a[i][j] = s[j] == '*' ? 1 : 0;
}
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < m - 1; j++)
if (a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1] == 3)
{
// out.println(i+" "+j);
int tp = 0;
for (int ii = Math.max(0, i - 1); ii < Math.min(n, i + 3); ii++)
for (int jj = Math.max(0, j - 1); jj < Math.min(m, j + 3); jj++)
tp += a[ii][jj];
if (tp == 3)
{
filla(i, j);
} else if (tp == 4)
{
if (i - 1 >= 0 & j - 1 >= 0 && a[i - 1][j - 1] == 1 && a[i][j] == 0)
filla(i, j);
else if (i - 1 >= 0 && j + 2 < m && a[i - 1][j + 2] == 1 && a[i][j + 1] == 0)
filla(i, j);
else if (i + 2 < n && j - 1 >= 0 && a[i + 2][j - 1] == 1 && a[i + 1][j] == 0)
filla(i, j);
else if (i + 2 < n && j + 2 < m && a[i + 2][j + 2] == 1 && a[i + 1][j + 1] == 0)
filla(i, j);
}
}
boolean flag = true;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] ==1)
flag = false;
// for (int i =0;i<n;i++)
// out.println(Arrays.toString(a[i]).substring(0,50));
out.println(flag?"YES":"NO");
}
private static void filla(int i, int j)
{
a[i][j] = 0;
a[i + 1][j] = 0;
a[i][j + 1] = 0;
a[i + 1][j + 1] = 0;
}
public static class Node
{
int x, y, k;
public Node(int x, int y, int k)
{
this.x = x;
this.y = y;
this.k = k;
}
}
// ==== Solve Code ====//
// ==== Template ==== //
public static long cnm(int a, int b)
{
long sum = 1;
int i = a, j = 1;
while (j <= b)
{
sum = sum * i / j;
i--;
j++;
}
return sum;
}
public static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
public static int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
public static void gbSort(int[] a, int l, int r)
{
if (l < r)
{
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r)
if (a[i] <= a[j])
t[idx++] = a[i++];
else
t[idx++] = a[j++];
while (i <= m)
t[idx++] = a[i++];
while (j <= r)
t[idx++] = a[j++];
for (int z = 0; z < t.length; z++)
a[l + z] = t[z];
}
}
// ==== Template ==== //
// ==== IO ==== //
static InputStream inputStream = System.in;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(System.out);
static class InputReader
{
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream)
{
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
boolean hasNext()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e)
{
return false;
// TODO: handle exception
}
}
return true;
}
public String nextLine()
{
String str = null;
try
{
str = reader.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public Double nextDouble()
{
return Double.parseDouble(next());
}
public BigInteger nextBigInteger()
{
return new BigInteger(next());
}
public BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}
// ==== IO ==== //
}
|
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
|
0f299bb70db9c2c8cee1c48a28fd5a80
|
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.Scanner;
public class Lshapes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tests = sc.nextInt();
while(tests-->0) {
boolean valid = true;
int rows = sc.nextInt();
int columns = sc.nextInt();
//create and fill the array
char[][] array = new char[rows][columns];
for(int i = 0 ; i < rows ; i++) {
array[i] = sc.next().toCharArray();
}
//iterate the array
for(int i = 0; i < rows ; i++) {
for(int j = 0; j < columns ; j++) {
if(array[i][j] == '*') { //a shaded cell
//the 4 valid L shapes
//check if i+1 j+1 or j-1 are not out of bound
//then check if these cells are shaded
if( i+1 < rows && j+1 < columns && array[i+1][j+1] == '*' && array[i+1][j] == '*' ) { // #1
//delete it
array[i][j] = '.';
array[i+1][j+1] = '.';
array[i+1][j] = '.';
//may form a valid L shapes if there is not any * around after deletion
if(stillThere(array, i, j) || stillThere(array, i+1, j) || stillThere(array, i+1, j+1)) {
valid = false;
}
} else if( i+1 < rows && j+1 < columns && array[i+1][j+1] == '*' && array[i][j+1] == '*' ) { // #2
array[i][j] = '.';
array[i+1][j+1] = '.';
array[i][j+1] = '.';
if(stillThere(array, i, j) || stillThere(array, i, j+1) || stillThere(array, i+1, j+1)) {
valid = false;
}
} else if( i+1 < rows && j+1 < columns && array[i][j+1] == '*' && array[i+1][j] == '*' ) { // #3
array[i][j] = '.';
array[i][j+1] = '.';
array[i+1][j] = '.';
if(stillThere(array, i, j) || stillThere(array, i+1, j) || stillThere(array, i, j+1)) {
valid = false;
}
} else if( i+1 < rows && j-1 >= 0 && array[i+1][j-1] == '*' && array[i+1][j] == '*' ) { // #4
array[i][j] = '.';
array[i+1][j-1] = '.';
array[i+1][j] = '.';
if(stillThere(array, i, j) || stillThere(array, i+1, j) || stillThere(array, i+1, j-1)) {
valid = false;
}
} else {
//not a part of a L shape
valid = false;
}
}
}
}
System.out.println(valid ? "YES" : "NO");
}
}
public static void solve() {
}
public static boolean stillThere(char[][] array , int x , int y) {
//this method check if there is other *s in a 3*3 square centered by this index
//meaning it was sharing a corner or edge
// i = { x-1 , x , x+1 }
// j = { y-1 , y , y+1 }
// if in bounds of course
//no of rows = length of the whole array
//no of columns = length of the first row (or any row)
for(int i = x-1 ; i <= x+1 ; i++) {
for(int j = y-1 ; j <= y+1 ; j++) {
if(i>=0 && j>=0 && i<array.length && j<array[0].length && array[i][j]=='*')
return true;
}
}
return false;
}
}
|
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
|
04ff8f982c3fb3bc74af5b465746563b
|
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.Arrays;
import java.util.Scanner;
public class F1722 {
public static void main(String[] args) {
char[][][] valid = new char[][][] {
{
{'.', '.', '.', '?'},
{'.', '*', '.', '.'},
{'.', '*', '*', '.'},
{'.', '.', '.', '.'},
},
{
{'?', '.', '.', '.'},
{'.', '.', '*', '.'},
{'.', '*', '*', '.'},
{'.', '.', '.', '.'}
},
{
{'.', '.', '.', '.'},
{'.', '*', '*', '.'},
{'.', '.', '*', '.'},
{'?', '.', '.', '.'}
},
{
{'.', '.', '.', '.'},
{'.', '*', '*', '.'},
{'.', '*', '.', '.'},
{'.', '.', '.', '?'}
}
};
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int R = in.nextInt()+2;
int C = in.nextInt()+2;
char[][] A = new char[R][C];
for (int r=0; r<R; r++) {
Arrays.fill(A[r], '.');
}
for (int r=1; r<R-1; r++) {
char[] row = in.next().toCharArray();
System.arraycopy(row, 0, A[r], 1, C-2);
}
for (int r=0; r<=R-4; r++) {
for (int c=0; c<=C-4; c++) {
for (char[][] piece : valid) {
boolean found = true;
for (int rr=0; rr<4; rr++) {
for (int cc=0; cc<4; cc++) {
if (piece[rr][cc] != '?' && piece[rr][cc] != A[r+rr][c+cc]) {
found = false;
}
}
}
if (found) {
for (int rr=0; rr<4; rr++) {
for (int cc=0; cc<4; cc++) {
if (piece[rr][cc] == '*') {
A[r+rr][c+cc] = '.';
}
}
}
}
}
}
}
boolean empty = true;
for (int r=0; r<R; r++) {
for (int c=0; c<C; c++) {
if (A[r][c] != '.') {
empty = false;
}
}
}
System.out.println(empty ? "YES" : "NO");
}
}
}
|
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
|
847068302d445e4ff7218e7e39dcf0fe
|
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.math.BigInteger;
import java.util.*;
public class Solution {
static MyScanner str = new MyScanner();
public static void main(String[] args) throws IOException {
int T = i();
while (T-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = i(), m = i();
char a[][] = new char[n][m];
for (int i = 0; i < n; i++) {
a[i] = str.next().toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '*') {
if (i + 1 < n && j + 1 < m && a[i][j + 1] == '*' && a[i + 1][j + 1] == '*') {
a[i][j] = '.';
a[i][j + 1] = '.';
a[i + 1][j + 1] = '.';
if (check(a, i, j) || check(a, i, j + 1) || check(a, i + 1, j + 1)) {
System.out.println("NO");
return;
}
} else if (i + 1 < n && j + 1 < m && a[i][j + 1] == '*' && a[i + 1][j] == '*') {
a[i][j] = '.';
a[i][j + 1] = '.';
a[i + 1][j] = '.';
if (check(a, i, j) || check(a, i, j + 1) || check(a, i + 1, j)) {
System.out.println("NO");
return;
}
} else if (i + 1 < n && j + 1 < m && a[i + 1][j] == '*' && a[i + 1][j + 1] == '*') {
a[i][j] = '.';
a[i + 1][j] = '.';
a[i + 1][j + 1] = '.';
if (check(a, i, j) || check(a, i + 1, j) || check(a, i + 1, j + 1)) {
System.out.println("NO");
return;
}
} else if (i + 1 < n && j - 1 >= 0 && a[i + 1][j] == '*' && a[i + 1][j - 1] == '*') {
a[i][j] = '.';
a[i + 1][j] = '.';
a[i + 1][j - 1] = '.';
if (check(a, i, j) || check(a, i + 1, j) || check(a, i + 1, j - 1)) {
System.out.println("NO");
return;
}
} else {
System.out.println("NO");
return;
}
}
}
}
System.out.println("YES");
}
public static boolean check(char[][] a, int i, int j) {
for (int k = i - 1; k <= i + 1; k++) {
for (int l = j - 1; l <= j + 1; l++) {
if (k >= 0 && k < a.length && l >= 0 && l < a[0].length && a[k][l] == '*') return true;
}
}
return false;
}
public static boolean Ul(char[][] a, int n, int m, int i, int j) {
boolean res = true;
if (j > 0) res &= (a[i][j - 1] != '*');
if (j + 2 < m) res &= (a[i][j + 2] != '*');
if (i > 0) {
res &= (a[i - 1][j] != '*');
if (j > 0) res &= (a[i - 1][j - 1] != '*');
if (j + 1 < m) res &= (a[i - 1][j + 1] != '*');
if (j + 2 < m) res &= (a[i - 1][j + 2] != '*');
}
if(i + 1 < n) {
if(j > 0) res &= (a[i + 1][j - 1] != '*');
if(j + 1 < m) res &= (a[i + 1][j + 1] != '*');
if(j + 2 < m) res &= (a[i + 1][j + 2] != '*');
}
if(i + 2 < n) {
res &= (a[i + 2][j] != '*');
if(j > 0) res &= (a[i + 2][j - 1] != '*');
if(j + 1 < m) res &= (a[i + 2][j + 1] != '*');
}
return res;
}
public static boolean Ur(char[][] a, int n, int m, int i, int j) {
boolean res = true;
if(j - 1 > 0) res &= (a[i][j - 2] != '*');
if(j + 1 < m) res &= (a[i][j + 1] != '*');
if(i > 0) {
res &= (a[i - 1][j] != '*');
if(j - 1 > 0) res &= (a[i - 1][j - 2] != '*');
if(j > 0) res &= (a[i - 1][j - 1] != '*');
if(j + 1 < m) res &= (a[i - 1][j + 1] != '*');
}
if(i + 1 < n) {
if(j - 1 > 0) res &= (a[i + 1][j - 2] != '*');
if(j > 0) res &= (a[i + 1][j - 1] != '*');
if(j + 1 > 0) res &= (a[i + 1][j + 1] != '*');
}
if(i + 2 < n) {
res &= (a[i + 2][j] != '*');
if(j > 0) res &= (a[i + 2][j - 1] != '*');
if(j + 1 < m) res &= (a[i + 2][j + 1] != '*');
}
return res;
}
public static boolean Ll(char[][] a, int n, int m, int i, int j) {
boolean res = true;
if(j > 0) res &= (a[i][j - 1] != '*');
if(j + 2 < m) res &= (a[i][j + 2] != '*');
if(i + 1 < n) {
res &= (a[i + 1][j] != '*');
if(j > 0) res &= (a[i + 1][j - 1] != '*');
if(j + 1 < m) res &= (a[i + 1][j + 1] != '*');
if(j + 2 < m) res &= (a[i + 1][j + 2] != '*');
}
if(i > 0) {
if(j > 0) res &= (a[i - 1][j - 1] != '*');
if(j + 1 < m) res &= (a[i - 1][j + 1] != '*');
if(j + 2 < m) res &= (a[i - 1][j + 2] != '*');
}
if(i - 1 > 0) {
res &= (a[i - 2][j] != '*');
if(j > 0) res &= (a[i - 2][j - 1] != '*');
if(j + 1 < m) res &= (a[i - 2][j + 1] != '*');
}
return res;
}
public static boolean Lr(char[][] a, int n, int m, int i, int j) {
boolean res = true;
if(j - 1 > 0) res &= (a[i][j - 2] != '*');
if(j + 1 < m) res &= (a[i][j + 1] != '*');
if(i + 1 < n) {
res &= (a[i + 1][j] != '*');
if(j - 1 > 0) res &= (a[i + 1][j - 2] != '*');
if(j > 0) res &= (a[i + 1][j - 1] != '*');
if(j + 1 < m) res &= (a[i + 1][j + 1] != '*');
}
if(i > 0) {
if(j > 0) res &= (a[i - 1][j - 1] != '*');
if(j - 1 > 0) res &= (a[i - 1][j - 2] != '*');
if(j + 1 < m) res &= (a[i - 1][j + 1] != '*');
}
if(i - 1 > 0) {
res &= (a[i - 2][j] != '*');
if(j > 0) res &= (a[i - 2][j - 1] != '*');
if(j + 1 < m) res &= (a[i - 2][j + 1] != '*');
}
return res;
}
public static int i() throws IOException {
return str.nextInt();
}
public static long l() throws IOException {
return str.nextLong();
}
public static double d() throws IOException {
return str.nextDouble();
}
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
|
520137207b882f2531947a80c270628c
|
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 p5
{
BufferedReader br;
StringTokenizer st;
BufferedWriter bw;
public static void main(String[] args)throws Exception
{
new p5().run();
}
void run()throws IOException
{
br = new BufferedReader(new InputStreamReader(System.in));
bw=new BufferedWriter(new OutputStreamWriter(System.out));
solve();
}
void solve() throws IOException
{
int t=ni();
while(t-->0)
{
int n=ni();
int m=ni();
char c[][]=nmc(n);
if(sol(c, n, m))
System.out.println("YES");
else
System.out.println("NO");
}
}
/////////////////////////////////////// FOR INPUT ///////////////////////////////////////
private boolean sol(char[][] c, int n, int m)
{
for(int i=-1;++i<n-1;)
{
for(int j=-1;++j<m-1;)
{
int y=0;
if(c[i][j]=='*')
y++;
if(c[i][j+1]=='*')
y++;
if(c[i+1][j]=='*')
y++;
if(c[i+1][j+1]=='*')
y++;
if(y==4)
return false;
if(y!=3)
continue;
int x=count(c, i, j);
if(x>1)
return false;
else if(x==1)
{
if(c[i][j]=='*' && c[i+1][j]=='*' && c[i+1][j+1]=='*')
{
if(!(i-1>=0 && j+2<m && c[i-1][j+2]=='*'))
return false;
}
if(c[i][j]=='*' && c[i+1][j]=='*' && c[i][j+1]=='*')
{
if(!(i+2<n && j+2<m && c[i+2][j+2]=='*'))
return false;
}
if(c[i][j]=='*' && c[i][j+1]=='*' && c[i+1][j+1]=='*')
{
if(!(i+2<n && j-1>=0 && c[i+2][j-1]=='*'))
return false;
}
if(c[i][j+1]=='*' && c[i+1][j]=='*' && c[i+1][j+1]=='*')
{
if(!(i-1>=0 && j-1>=0 && c[i-1][j-1]=='*'))
return false;
}
}
c[i][j]='.';c[i][j+1]='.';c[i+1][j]='.';c[i+1][j+1]='.';
}
}
for(int i=-1;++i<n;)
{
for(int j=-1;++j<m;)
{
if(c[i][j]=='*')
return false;
}
}
return true;
}
private int count(char[][] c, int i, int j)
{
int n=c.length;
int m=c[0].length;
int ans=0;
if(i-1>=0 && c[i-1][j]=='*')
ans++;
if(i-1>=0 && j-1>=0 && c[i-1][j-1]=='*')
ans++;
if(i-1>=0 && j+1<m && c[i-1][j+1]=='*')
ans++;
if(i-1>=0 && j+2<m && c[i-1][j+2]=='*')
ans++;
if(j+2<m && c[i][j+2]=='*')
ans++;
if(j-1>=0 && c[i][j-1]=='*')
ans++;
if(j+2<m && c[i+1][j+2]=='*')
ans++;
if(j-1>=0 && c[i+1][j-1]=='*')
ans++;
if(i+2<n && c[i+2][j]=='*')
ans++;
if(i+2<n && j-1>=0 && c[i+2][j-1]=='*')
ans++;
if(i+2<n && j+1<m && c[i+2][j+1]=='*')
ans++;
if(i+2<n && j+2<m && c[i+2][j+2]=='*')
ans++;
return ans;
}
int[] nai(int n) { int a[]=new int[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
Integer[] naI(int n) { Integer a[]=new Integer[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
long[] nal(int n) { long a[]=new long[n]; for(int i=-1;++i<n;)a[i]=nl(); return a;}
char[] nac() {char c[]=nextLine().toCharArray(); return c;}
char [][] nmc(int n) {char c[][]=new char[n][]; for(int i=-1;++i<n;)c[i]=nac(); return c;}
int[][] nmi(int r, int c) {int a[][]=new int[r][c]; for(int i=-1;++i<r;)a[i]=nai(c); return a;}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {st = new StringTokenizer(br.readLine());}
catch (IOException e) {e.printStackTrace();}
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
byte nb() { return Byte.parseByte(next()); }
short ns() { return Short.parseShort(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { 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
|
691c9e0a9e725020b8e3c66839b0a326
|
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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class F {
static char[][]c;
public static void main(String[]args) throws IOException {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int t=sc.nextInt();
a:while(t-->0) {
int n=sc.nextInt(),m=sc.nextInt();
c=new char[n][m];
for(int i=0;i<n;i++) {
c[i]=sc.next().toCharArray();
}
for(int i=0;i<n-1;i++) {
for(int j=0;j<m-1;j++) {
int v=hasL(i, j);
if(v==4){out.println("NO");continue a;}
if(v!=-1) {
if(hasSur(i,j,v)) {
out.println("NO");continue a;
}
clear(i,j);
}
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(c[i][j]=='*') {out.println("NO");continue a;}
}
}
out.println("YES");
}
out.close();
}
private static void clear(int i, int j) {
for(int a=0;a<2;a++) {
for(int b=0;b<2;b++) {
c[i+a][j+b]='.';
}
}
}
private static boolean hasSur(int i, int j, int v) {
//handle v according to l shape
int[]dx=new int[] {-1,-1,-1,-1,0,0,1,1,2,2,2,2};
int[]dy=new int[] {-1,0,1,2,-1,2,-1,2,-1,0,1,2};
int skip=v==0?0:v==1?3:v==2?8:11;
for(int a=0;a<12;a++) {if(a==skip)continue;
int nx=i+dx[a],ny=j+dy[a];
if(valid(nx,ny)&&c[nx][ny]=='*')return true;
}
return false;
}
private static boolean valid(int nx, int ny) {
return nx>=0&&nx<c.length&&ny>=0&&ny<c[0].length;
}
private static int hasL(int i, int j) {
int cnt=0;
for(int a=0;a<2;a++) {
for(int b=0;b<2;b++) {
if(c[i+a][j+b]=='*')cnt++;
}
}
if(cnt<3)return -1;
if(cnt==4)return 4;
if(c[i][j]=='.')return 0;
if(c[i][j+1]=='.')return 1;
if(c[i+1][j]=='.')return 2;
return 3;
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public boolean hasNext() {return st.hasMoreTokens();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
|
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
|
74fa25182dc492d889a35986257979c4
|
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
|
/*
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
Motto: Do what i Love <=> Love what i do
If you don't use your brain 100%, it deteriorates gradually
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Coder {
static StringBuffer str=new StringBuffer();
static BufferedReader bf;
static PrintWriter pw;
static int n,m;
static char mat[][];
static class Pair {
int u, v;
Pair(int u, int v){
this.u=u;
this.v=v;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
return u == pair.u && v == pair.v;
}
@Override
public int hashCode() {
return Objects.hash(u, v);
}
}
static String solve(int te) throws Exception{
int islands=0;
boolean vis[][]=new boolean[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis[i][j] || mat[i][j]=='.') continue;
// System.out.println(i+" - "+j+" - "+mat[i][j]);
Queue<Pair> q=new LinkedList<>();
q.add(new Pair(i, j));
vis[i][j]=true;
int cnt=1;
while(!q.isEmpty()){
Pair p=q.remove();
// System.out.println(p.u+" - "+p.v+" - "+mat[p.u][p.v]);
for(int dx=-1;dx<=1;dx++){
for(int dy=-1;dy<=1;dy++){
if(p.u+dx>=0 && p.u+dx<n && p.v+dy>=0 && p.v+dy<m && mat[p.u+dx][p.v+dy]=='*' && !vis[p.u+dx][p.v+dy]){
q.add(new Pair(p.u+dx, p.v+dy));
vis[p.u+dx][p.v+dy]=true;
cnt++;
}
}
}
}
if(cnt!=3){
// System.out.println(te+" - "+cnt+" - "+i+" - "+j+" - "+mat[i][j+1]);
return "NO\n";
}else islands++;
}
}
// System.out.println(te+" - "+islands);
// check l shape
// Hint: Using Elbow
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]=='.') continue;
if(i-1>=0 && j+1<m && mat[i-1][j]=='*' && mat[i][j+1]=='*') islands--;
if(i+1<n && j+1<m && mat[i+1][j]=='*' && mat[i][j+1]=='*') islands--;
if(j-1>=0 && i+1<n && mat[i][j-1]=='*' && mat[i+1][j]=='*') islands--;
if(i-1>=0 && j-1>=0 && mat[i-1][j]=='*' && mat[i][j-1]=='*') islands--;
}
}
// System.out.println(te+" -> "+islands);
return islands==0?"YES\n":"NO\n";
}
public static void main(String[] args) throws java.lang.Exception {
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
int q1 = Integer.parseInt(bf.readLine().trim());
for(int te=1;te<=q1;te++) {
String s[]=bf.readLine().trim().split("\\s+");
n=Integer.parseInt(s[0]);
m=Integer.parseInt(s[1]);
mat=new char[n][];
for(int i=0;i<n;i++) mat[i]=bf.readLine().trim().toCharArray();
str.append(solve(te));
}
pw.print(str);
pw.flush();
// System.out.println(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
|
89613c2e43046b2550e8aedf9c4b9f93
|
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.*;
public class f {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
int m = fs.nextInt();
char[][] board = new char[n][m];
for (int i = 0; i < n; ++i) {
board[i] = fs.next().toCharArray();
}
check(out, n, m, board);
}
out.close();
}
private static void check(PrintWriter out, int n, int m, char[][] board) {
boolean[][] visited = new boolean[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!visited[i][j] && board[i][j]=='*') {
if (!bfs(i, j, board, visited)) {
out.println("NO");
return;
}
}
}
}
out.println("YES");
}
static int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}};
private static boolean bfs(int i, int j, char[][] board, boolean[][] visited) {
ArrayDeque<int[]> queue = new ArrayDeque<>();
queue.offer(new int[]{i, j});
visited[i][j] = true;
int size = 1;
Set<Integer> setX = new HashSet<>();
Set<Integer> setY = new HashSet<>();
setX.add(j);
setY.add(i);
while (!queue.isEmpty()) {
int[] curr = queue.poll();
for (int[] dir : dirs) {
int y = dir[0] + curr[0];
int x = dir[1] + curr[1];
if (y >= 0 && y < board.length && x >= 0 && x < board[0].length && board[y][x] == '*' && !visited[y][x]) {
visited[y][x] = true;
queue.offer(new int[]{y, x});
setX.add(x);
setY.add(y);
size++;
}
}
}
return size == 3 && setX.size() == 2 && setY.size() == 2;
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["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
|
b0417dd79432c4c71fe027db13cb7d2f
|
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 FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static int dx[]={0,0,-1,1,-1,-1,1,1},dy[]={-1,1,0,0,-1,1,-1,1};
static final double pi=3.1415926536;
static long mod=1000000007;
// static long mod=998244353;
static int MAX=Integer.MAX_VALUE;
static int MIN=Integer.MIN_VALUE;
static long MAXL=Long.MAX_VALUE;
static long MINL=Long.MIN_VALUE;
static ArrayList<pair> graph[];
static long fact[];
static long seg[];
static int dp[][];
// static long dp[][];
public static void main (String[] args) throws java.lang.Exception
{
// code goes here
int t=I();
outer:while(t-->0)
{
int n=I(),m=I();
char c[][]=new char[n][m];
for(int i=0;i<n;i++){
String s=S();
for(int j=0;j<m;j++){
c[i][j]=s.charAt(j);
}
}
boolean v[][]=new boolean[n][m];
int q=0;
yyy:for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(c[i][j]=='*' && !v[i][j]){
int p=fun(c,v,n,m,i,j);
// out.println(p);
if(p!=3){
q=1;
break yyy;
}
}
}
}
// out.println(q);
for(int i=0;i<n;i++){
Arrays.fill(v[i],false);
}
char c1[][]={{'*','.'},{'*','*'}};
char c2[][]={{'.','*'},{'*','*'}};
char c3[][]={{'*','*'},{'*','.'}};
char c4[][]={{'*','*'},{'.','*'}};
int qq=0;
if(q==0){
xxx:for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!v[i][j] && c[i][j]=='*'){
int temp=0;
if(check(i,j,c1,c)){
// out.println("->1");
temp++;
v[i][j]=true;
v[i+1][j]=true;
v[i+1][j+1]=true;
continue;
}if(check(i,j-1,c2,c)){
// out.println("->2");
temp++;
v[i][j]=true;
v[i+1][j]=true;
v[i+1][j-1]=true;
continue;
}
if(check(i,j,c3,c)){
// out.println("->3");
temp++;
v[i][j]=true;
v[i][j+1]=true;
v[i+1][j]=true;
continue;
}
if(check(i,j,c4,c)){
// out.println("->4");
temp++;
v[i][j]=true;
v[i][j+1]=true;
v[i+1][j+1]=true;
continue;
}
if(temp==0){
qq=1;
break xxx;
}
}
// printArray(v);
}
}
if(qq==1){
out.println("NO");
}else{
out.println("YES");
}
}else{
out.println("NO");
}
}
out.close();
}
public static boolean check(int i,int j,char ch[][],char c[][])
{
if(i<0 || i>=c.length || j<0 || j>=c[0].length)return false;
// if(j<0 || j>=c[0].length)return false;
if(c[i][j]!=ch[0][0])return false;
if(i+1>=c.length || c[i+1][j]!=ch[1][0])return false;
if(j+1>=c[0].length || c[i][j+1]!=ch[0][1])return false;
if(i+1>=c.length || j+1>=c[0].length || c[i+1][j+1]!=ch[1][1])return false;
return true;
}
public static int fun(char c[][],boolean v[][],int n,int m,int i,int j)
{
v[i][j]=true;
int ans=1;
for(int k=0;k<8;k++){
int nx=i+dx[k];
int ny=j+dy[k];
if(nx>=0 && nx<n && ny>=0 && ny<m && !v[nx][ny] && c[nx][ny]=='*'){
ans+=fun(c,v,n,m,nx,ny);
}
}
return ans;
}
public static class pair
{
long a;
long b;
public pair(long aa,long bb)
{
a=aa;
b=bb;
}
}
public static class myComp implements Comparator<pair>
{
public int compare(pair p1,pair p2)
{
if(p1.a-p1.b==p2.a-p2.b)return 0;
if(p1.a-p1.b>p2.a-p2.b)return -1;
return 1;
}
//sort in ascending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return -1;
// else
// return 1;
// }
// sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.b==p2.b)
// return 0;
// else if(p1.b<p2.b)
// return 1;
// else
// return -1;
// }
}
// public static void setGraph(int n,int m)throws IOException
// {
// graph=new ArrayList[n+1];
// for(int i=0;i<=n;i++){
// graph[i]=new ArrayList<>();
// }
// for(int i=0;i<m;i++){
// int u=I(),v=I();
// graph[u].add(v);
// graph[v].add(u);
// }
// }
//LOWER_BOUND and UPPER_BOUND functions
//It returns answer according to zero based indexing.
public static int lower_bound(pair[] arr,int X,int start, int end) //start=0,end=n-1
{
if(start>end)return -1;
// if(arr.get(end)<X)return end;
if(arr[end].a<X)return end;
// if(arr.get(start)>X)return -1;
if(arr[start].a>X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
if(arr[mid].a==X){
// if(arr.get(mid)==X){ //Returns last index of lower bound value.
if(mid<end && arr[mid+1].a==X){
// if(mid<end && arr.get(mid+1)==X){
left=mid+1;
}else{
return mid;
}
}
// if(arr.get(mid)==X){ //Returns first index of lower bound value.
// if(arr[mid]==X){
// // if(mid>start && arr.get(mid-1)==X){
// if(mid>start && arr[mid-1]==X){
// right=mid-1;
// }else{
// return mid;
// }
// }
else if(arr[mid].a>X){
// else if(arr.get(mid)>X){
if(mid>start && arr[mid-1].a<X){
// if(mid>start && arr.get(mid-1)<X){
return mid-1;
}else{
right=mid-1;
}
}else{
if(mid<end && arr[mid+1].a>X){
// if(mid<end && arr.get(mid+1)>X){
return mid;
}else{
left=mid+1;
}
}
}
return left;
}
//It returns answer according to zero based indexing.
public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1
{
if(arr[0]>=X)return start;
if(arr[arr.length-1]<X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
if(arr[mid]==X){ //returns first index of upper bound value.
if(mid>start && arr[mid-1]==X){
right=mid-1;
}else{
return mid;
}
}
// if(arr[mid]==X){ //returns last index of upper bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
else if(arr[mid]>X){
if(mid>start && arr[mid-1]<X){
return mid;
}else{
right=mid-1;
}
}else{
if(mid<end && arr[mid+1]>X){
return mid+1;
}else{
left=mid+1;
}
}
}
return left;
}
//END
//Segment Tree Code
public static void buildTree(long a[],int si,int ss,int se)
{
if(ss==se){
seg[si]=a[ss];
return;
}
int mid=(ss+se)/2;
buildTree(a,2*si+1,ss,mid);
buildTree(a,2*si+2,mid+1,se);
seg[si]=max(seg[2*si+1],seg[2*si+2]);
}
// public static void update(int si,int ss,int se,int pos,int val)
// {
// if(ss==se){
// // seg[si]=val;
// return;
// }
// int mid=(ss+se)/2;
// if(pos<=mid){
// update(2*si+1,ss,mid,pos,val);
// }else{
// update(2*si+2,mid+1,se,pos,val);
// }
// // seg[si]=min(seg[2*si+1],seg[2*si+2]);
// if(seg[2*si+1].a < seg[2*si+2].a){
// seg[si].a=seg[2*si+1].a;
// seg[si].b=seg[2*si+1].b;
// }else{
// seg[si].a=seg[2*si+2].a;
// seg[si].b=seg[2*si+2].b;
// }
// }
public static long query1(int si,int ss,int se,int qs,int qe)
{
if(qs>se || qe<ss)return 0;
if(ss>=qs && se<=qe)return seg[si];
int mid=(ss+se)/2;
long p1=query1(2*si+1,ss,mid,qs,qe);
long p2=query1(2*si+2,mid+1,se,qs,qe);
return max(p1,p2);
}
public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b)
{
int i=0,j=0;
while(i<a.size() && j<b.size()){
if(a.get(i)<=b.get(j)){
f.add(a.get(i));
i++;
}else{
f.add(b.get(j));
j++;
}
}
while(i<a.size()){
f.add(a.get(i));
i++;
}
while(j<b.size()){
f.add(b.get(j));
j++;
}
}
//Segment Tree Code end
//Prefix Function of KMP Algorithm
public static int[] KMP(char c[],int n)
{
int pi[]=new int[n];
for(int i=1;i<n;i++){
int j=pi[i-1];
while(j>0 && c[i]!=c[j]){
j=pi[j-1];
}
if(c[i]==c[j])j++;
pi[i]=j;
}
return pi;
}
public static long kadane(long a[],int n) //largest sum subarray
{
long max_sum=Long.MIN_VALUE,max_end=0;
for(int i=0;i<n;i++){
max_end+=a[i];
if(max_sum<max_end){max_sum=max_end;}
if(max_end<0){max_end=0;}
}
return max_sum;
}
public static ArrayList<Long> primeFact(long x)
{
ArrayList<Long> arr=new ArrayList<>();
if(x%2==0){
arr.add(2L);
while(x%2==0){
x/=2;
}
}
for(long i=3;i*i<=x;i+=2){
if(x%i==0){
arr.add(i);
while(x%i==0){
x/=i;
}
}
}
if(x>0){
arr.add(x);
}
return arr;
}
public static long nPr(int n,int r)
{
long ans=divide(fact(n),fact(n-r),mod);
return ans;
}
public static long nCr(int n,int r)
{
long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod);
return ans;
}
public static boolean isSorted(int a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static boolean isSorted(long a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static int computeXOR(int n) //compute XOR of all numbers between 1 to n.
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
public static int np2(int x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
public static int hp2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static long hp2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static ArrayList<Integer> primeSieve(int n)
{
ArrayList<Integer> arr=new ArrayList<>();
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
public static class FenwickTree
{
int farr[];
int n;
public FenwickTree(int c)
{
n=c+1;
farr=new int[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x,int p)
{
for(;x<n;x+=x&(-x))
{
farr[x]+=p;
}
}
public int get(int x)
{
int ans=0;
for(;x>0;x-=x&(-x))
{
ans=ans+farr[x];
}
return ans;
}
}
//Disjoint Set Union
//NOTE: call find function for all the index in the par array at last,
//in order to set parent of every index properly.
public static class DSU
{
int par[],rank[];
public DSU(int c)
{
par=new int[c+1];
rank=new int[c+1];
for(int i=0;i<=c;i++)
{
par[i]=i;
rank[i]=0;
}
}
public int find(int a)
{
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public void union(int a,int b)
{
int a_rep=find(a),b_rep=find(b);
if(a_rep==b_rep)
return;
if(rank[a_rep]<rank[b_rep])
par[a_rep]=b_rep;
else if(rank[a_rep]>rank[b_rep])
par[b_rep]=a_rep;
else
{
par[b_rep]=a_rep;
rank[a_rep]++;
}
}
}
public static boolean isVowel(char c)
{
if(c=='a' || c=='e' || c=='i' || c=='u' || c=='o')return true;
return false;
}
public static boolean isInteger(double N)
{
int X = (int)N;
double temp2 = N - X;
if (temp2 > 0)
{
return false;
}
return true;
}
public static boolean isPalindrome(String s)
{
int n=s.length();
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long fact(long n)
{
long fact=1;
for(long i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long fact(int n)
{
long fact=1;
for(int i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static boolean isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean isPrime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void printArray(long a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(char a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]);
}
out.println();
}
public static void printArray(String a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(boolean a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(pair a[])
{
for(pair p:a){
out.println(p.a+"->"+p.b);
}
}
public static void printArray(int a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(String a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(boolean a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(long a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(char a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(ArrayList<?> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapI(HashMap<?,?> hm){
for(Map.Entry<?,?> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMap(HashMap<Long,ArrayList<Integer>> hm){
for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){
out.print(e.getKey()+"->");
ArrayList<Integer> arr=e.getValue();
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}out.println();
}
}
public static void printGraph(ArrayList<Integer> graph[])
{
int n=graph.length;
for(int i=0;i<n;i++){
out.print(i+"->");
for(int j:graph[i]){
out.print(j+" ");
}out.println();
}
}
//Modular Arithmetic
public static long add(long a,long b)
{
a+=b;
if(a>=mod)a-=mod;
return a;
}
public static long sub(long a,long b)
{
a-=b;
if(a<0)a+=mod;
return a;
}
public static long mul(long a,long b)
{
return ((a%mod)*(b%mod))%mod;
}
public static long divide(long a,long b,long m)
{
a=mul(a,modInverse(b,m));
return a;
}
public static long modInverse(long a,long m)
{
int x=0,y=0;
own p=new own(x,y);
long g=gcdExt(a,m,p);
if(g!=1){
out.println("inverse does not exists");
return -1;
}else{
long res=((p.a%m)+m)%m;
return res;
}
}
public static long gcdExt(long a,long b,own p)
{
if(b==0){
p.a=1;
p.b=0;
return a;
}
int x1=0,y1=0;
own p1=new own(x1,y1);
long gcd=gcdExt(b,a%b,p1);
p.b=p1.a - (a/b) * p1.b;
p.a=p1.b;
return gcd;
}
public static long pwr(long m,long n)
{
long res=1;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m);
}
n=n>>1;
m=(m*m);
}
return res;
}
public static long modpwr(long m,long n)
{
long res=1;
m=m%mod;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m)%mod;
}
n=n>>1;
m=(m*m)%mod;
}
return res;
}
public static class own
{
long a;
long b;
public own(long val,long index)
{
a=val;
b=index;
}
}
//Modular Airthmetic
public static void sort(int[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(char[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
char tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(long[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
//max & min
public static int max(int a,int b){return Math.max(a,b);}
public static int min(int a,int b){return Math.min(a,b);}
public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));}
public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));}
public static long max(long a,long b){return Math.max(a,b);}
public static long min(long a,long b){return Math.min(a,b);}
public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));}
public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));}
public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
//end
public static int[] I(int n)throws IOException{int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;}
public static long[] IL(int n)throws IOException{long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;}
public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static int I()throws IOException{return sc.I();}
public static long L()throws IOException{return sc.L();}
public static String S()throws IOException{return sc.S();}
public static double D()throws IOException{return sc.D();}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int I(){ return Integer.parseInt(next());}
long L(){ return Long.parseLong(next());}
double D(){return Double.parseDouble(next());}
String S(){
String str = "";
try {
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
|
Java
|
["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
|
64462e0eec4a718fb096128444fc7e52
|
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.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
public class R817F {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st = null;
private static int nextInt() {
return Integer.parseInt(st.nextToken());
}
// private static long nextLong() {
// return Long.parseLong(st.nextToken());
// }
//
private static String nextString() {
return st.nextToken();
}
private static void nextLine() throws Exception {
st = new StringTokenizer(br.readLine());
}
static class Rectangle {
int h;
int w;
public Rectangle(int h, int w) {
this.h = h;
this.w = w;
}
}
public static void main(String[] args) throws Exception {
nextLine();
int t = nextInt();
while (t-- > 0) {
nextLine();
int n = nextInt();
int m = nextInt();
char[][] matrix = new char[n][];
for (int i = 0; i < n; i++) {
nextLine();
matrix[i] = nextString().toCharArray();
}
solve(matrix);
}
}
private static void solve(char[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
char c = matrix[i][j];
if (c == '*') {
int[] points = getPoints(matrix, i, j);
if (points == null) {
System.out.println("NO");
return;
}
fillPoints(matrix, points, 'C');
if (!isValid(matrix, points)) {
System.out.println("NO");
return;
}
}
}
}
System.out.println("YES");
return;
}
private static int[] getPoints(char[][] matrix, int i, int j) {
int[][] shapes = new int[6][];
shapes[0] = new int[] {i, j+1, i-1, j+1, i, j};
shapes[1] = new int[] {i, j+1, i+1, j+1, i, j};
shapes[2] = new int[] {i-1, j, i, j+1, i, j};
shapes[3] = new int[] {i+1, j, i, j+1, i, j};
shapes[4] = new int[] {i+1, j, i+1, j-1, i, j};
shapes[5] = new int[] {i+1, j, i+1, j+1, i, j};
for (int[] shape : shapes) {
if (isValidShape(matrix, shape)) {
return shape;
}
}
return null;
}
private static boolean isValidShape(char[][] matrix, int[] shape) {
if (isValidRow(matrix, shape[0]) && isValidCol(matrix, shape[1]) &&
isValidRow(matrix, shape[2]) && isValidCol(matrix, shape[3]) &&
matrix[shape[0]][shape[1]] == '*' && matrix[shape[2]][shape[3]] == '*')
return true;
return false;
}
private static boolean isValidRow(char[][] matrix, int i) {
return i >= 0 && i < matrix.length;
}
private static boolean isValidCol(char[][] matrix, int i) {
return i >= 0 && i < matrix[0].length;
}
private static void fillPoints(char[][] matrix, int[] points, char fill) {
for (int i = 0; i < 6; i+=2) {
matrix[points[i]][points[i+1]] = fill;
}
}
private static boolean isValid(char[][] matrix, int[] points) {
for (int i = 0; i < 6; i+=2) {
boolean result = isValidL(matrix, points[i], points[i+1]);
if (!result)
return false;
}
return true;
}
private static boolean isValidL(char[][] matrix, int row, int col) {
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (isValidRow(matrix, i) && isValidCol(matrix, j) && matrix[i][j] == '*') {
return false;
}
}
}
return true;
}
}
/*
1
5 4
.*..
**..
....
..**
..*.
1
2 2
**
*.
*/
|
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
|
d59f0a7dbb9a7dfbcd5d22fe86bffaa0
|
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 Solution2 admin = new Solution2();
public static void main(String[] args) {
admin.start();
}
}
class Solution2 {
//---------------------------------INPUT READER-----------------------------------------//
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine());} catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long n) {int[]ret=new int[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
long[] nal(long n) {long[]ret=new long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
Integer[] nA(long n) {Integer[]ret=new Integer[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
Long[] nAl(long n) {Long[]ret=new Long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
//--------------------------------------PRINTER------------------------------------------//
PrintWriter w;
void p(int i) {w.println(i);} void p(long l) {w.println(l);}
void p(double d) {w.println(d);} void p(String s) { w.println(s);}
void pr(int i) {w.print(i);} void pr(long l) {w.print(l);}
void pr(double d) {w.print(d);} void pr(String s) { w.print(s);}
void pl() {w.println();}
//--------------------------------------VARIABLES-----------------------------------------//
long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
long mod = 1000000007;
{
w = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
try {if(new File(System.getProperty("user.dir")).getName().equals("LOCAL")) {
w = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt")));
br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));}
} catch (Exception ignore) { }
}
//----------------------START---------------------//
void start() {
int t = ni(); while(t-- > 0)
solve();
w.close();
}
int n, m;
char[][] mat;
int[] dx_ver = {-1, 0, 1}, dy_ver = {0, 0, 0};
int[] dx_hor = {0, 0, 0}, dy_hor = {-1, 0, 1};
// int[] dx_box = {0, 0, 1, 1}, dy_box = {0, 1, 0, 1};
int[] dx_neighbor = {-1, -1, 0, 1, 1, 1, 0, -1}, dy_neighbor = {0, 1, 1, 1, 0, -1, -1, -1};
boolean isValid (int x, int y) {
if(x >= 0 && x < n && y >= 0 && y < m) return true;
else return false;
}
boolean check(int x, int y) {
int neighbors = 0;
int nx = -1, ny = -1;
for(int i = 0; i < 8; i++) {
int xx = x + dx_neighbor[i], yy = y + dy_neighbor[i];
if(isValid(xx, yy) && mat[xx][yy] == '*') {
if(neighbors == 2) return false;
else if(neighbors == 0) {
nx = xx; ny = yy;
neighbors++;
} else {
if(Math.abs(nx-xx) > 1 || Math.abs(ny-yy) > 1) return false;
neighbors++;
}
}
}
return neighbors == 2;
}
void solve() {
n = ni();
m = ni();
mat = new char[n][];
for(int i = 0; i < n; i++) mat[i] = ns().toCharArray();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(mat[i][j] == '*') if(!check(i, j)) {p("NO"); return;}
}
}
p("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 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
|
d1a5240a5f9b45148f16f151044dd196
|
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 codeforce.div4.r817;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1722/problem/F" target="_top">https://codeforces.com/contest/1722/problem/F</a>
* @since 30/08/22 9:04 PM
*/
public class F {
static FastScanner sc = new FastScanner(System.in);
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static int[] dx8 = {-1, -1, 0, 1, 1, 1, 0, -1};
static int[] dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
public static void main(String[] args) {
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
int n = sc.nextInt();
int m = sc.nextInt();
char[][] grid = new char[n][m];
for (int i = 0; i < n; i++) {
grid[i] = sc.next().toCharArray();
}
boolean valid = true;
int[][] idGrid = new int[n][m];
outer:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '*') {
for (int d = 0; d < 4; d++) {
int dir1 = d;
int dir2 = (d + 1) % 4;
int tx1 = i + dx[dir1];
int ty1 = j + dy[dir1];
int tx2 = i + dx[dir2];
int ty2 = j + dy[dir2];
if (valid(n, m, tx1, ty1) && valid(n, m, tx2, ty2) && grid[tx1][ty1] == '*' && grid[tx2][ty2] == '*') {
//found an L shape
int[][] points = new int[3][2];
points[0][0] = i;
points[0][1] = j;
points[1][0] = tx1;
points[1][1] = ty1;
points[2][0] = tx2;
points[2][1] = ty2;
//
Set<Integer> pointsId = new HashSet<>();
for (int[] p : points) {
pointsId.add(id(p[0], p[1], n));
idGrid[p[0]][p[1]]++;
}
//these 3 are valid points
for (int[] p : points) {
for (int d8 = 0; d8 < dx8.length; d8++) {
//we iterate over all 8 neighbors of p
int tx = p[0] + dx8[d8];
int ty = p[1] + dy8[d8];
if (valid(n, m, tx, ty) && !pointsId.contains(id(tx, ty, n)) && grid[tx][ty] == '*')
valid = false;
}
}
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '*' && idGrid[i][j] != 1)
valid = false;
}
}
System.out.println(valid ? "YES" : "NO");
}
}
}
private static int id(int i, int j, int r) {
return i * r + j;
}
private static boolean valid(int n, int m, int i, int j) {
return i >= 0 && j >= 0 && i < n && j < m;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
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 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
|
3417f9af3e3ccaf26c9793f334db5761
|
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.util.Map.Entry;
import javax.swing.text.StyledEditorKit.BoldAction;
import java.io.*;
import java.lang.*;
public class main1{
FastScanner in;
PrintWriter out;
public static void main(String[] arg) {
new main1().run();
}
///////////////////////////////////////////////////////////////////
///////////////// /MAIN PROGRAM STARTS HERE///////////////////////
//////////////////////////////////////////////////////////////////
public static int check(int i,int j,char[][] x)
{
int flag1=(x[i][j]=='*')?1:0;
int flag2=(x[i][j+1]=='*')?1:0;
int flag3=(x[i+1][j]=='*')?1:0;
int flag4=(x[i+1][j+1]=='*')?1:0;
return flag1+flag2+flag3+flag4;
}
public static boolean check2(int i,int j,int ci,int cj,int n,int m)
{
if(ci+i>=0 && ci+i<n && cj+j>=0 && cj+j<m)
{
return true;
}
return false;
}
public void solve() throws IOException {
int tc=in.nextInt();
while(tc-->0)
{
int n=in.nextInt();
int m=in.nextInt();
char[][] ch=new char[n][m];
int[][] visited=new int[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);
visited[i][j]=-1;
}
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<m;j++)
// {
// out.print(ch[i][j]+" ");
// }
// out.println();
// }
int cnt=0;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<m-1;j++)
{
if(check(i,j,ch)==3)
{
visited[i][j]=visited[i][j+1]=visited[i+1][j]=visited[i+1][j+1]=cnt++;
}
}
}
//go for corner case
boolean flag=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(ch[i][j]=='*')
{
if(visited[i][j]==-1)
{
flag=true;
}
}
else if(ch[i][j]=='.')
{
visited[i][j]=-1;
}
}
}
//check whether over lap or not
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int xi=-1;xi<=1;xi++)
{
for(int xj=-1;xj<=1;xj++)
{
if(i+xi>=0 && i+xi<n && j+xj>=0 && j+xj<m && visited[i][j]!=-1 && visited[i+xi][j+xj]!=-1 && visited[i][j]!=visited[i+xi][j+xj])
{
flag=true;
break;
}
}
}
}
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<m;j++)
// {
// out.print(visited[i][j]+" ");
// }
// out.println();
// }
if(flag)
{
out.println("NO");
}
else
{
out.println("YES");
}
}
}
///////////////////////////////////////////////////////////////
//////////////////////MAIN PROGRAM ENDS HERE///////////////////
///////////////////////////////////////////////////////////////
public void run() {
try {
in = new FastScanner(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader f) {
br = new BufferedReader(f);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["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
|
344fc1805226d9d52b15ce87cba7e28a
|
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
|
// OM NAMAH SHIVAY
import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class Vaibhav {
static long bit[];
static boolean prime[];
static class Pair implements Comparable<Pair> {
long x;
int y;
Pair(long x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o){
return (int)(this.x-o.x);
}
}
static long power(long x, long y, long p) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1l;
x = x % p;
while (y > 0) {
if (y % 2 == 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
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 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());
}
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;
}
long nextLong() {
return Long.parseLong(next());
}
}
static void sieveOfEratosthenes(int n) {
prime = new boolean[n + 1];
for (int i = 0; i <= n; i++) prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
static int binomialCoeff(int n, int r)
{
if (r > n)
return 0;
long m = 1000000007;
long inv[] = new long[r + 1];
inv[0] = 1;
if(r+1>=2)
inv[1] = 1;
for (int i = 2; i <= r; i++) {
inv[i] = m - (m / i) * inv[(int) (m % i)] % m;
}
int ans = 1;
for (int i = 2; i <= r; i++) {
ans = (int) (((ans % m) * (inv[i] % m)) % m);
}
for (int i = n; i >= (n - r + 1); i--) {
ans = (int) (((ans % m) * (i % m)) % m);
}
return ans;
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static BigInteger bi(String str) {
return new BigInteger(str);
}
// Author - vaibhav_1710
static FastScanner fs = null;
static long mod=998244353;
static int a[][];
static ArrayList<Integer> al[];
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t =fs.nextInt();
outer:
while (t-- > 0 ){
int n = fs.nextInt();
int m = fs.nextInt();
char c[][] = new char[n][m];
for(int i=0;i<n;i++){
String s = fs.next();
c[i] = s.toCharArray();
}
a = new int[n][m];
int count=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(c[i][j]=='*' && a[i][j]==0){
if(j>0 && c[i][j-1]=='*' && a[i][j-1]==0 && i<n-1 && c[i+1][j]=='*' && a[i+1][j]==0){ // left bottom
a[i][j] = count;
a[i+1][j] = count;
a[i][j-1] = count;
count++;
}
else if(j<(m-1) && c[i][j+1]=='*' && a[i][j+1]==0 && i<(n-1) && c[i+1][j]=='*' && a[i+1][j]==0){ // bottom right
a[i][j] = count;
a[i+1][j] = count;
a[i][j+1] = count;
count++;
}
else if(j>0 && c[i][j-1]=='*' && a[i][j-1]==0 && i>0 && c[i-1][j]=='*' && a[i-1][j]==0){ // up && left
a[i][j] = count;
a[i-1][j] = count;
a[i][j-1] = count;
count++;
}
else if(j<(m-1) && c[i][j+1]=='*' && a[i][j+1]==0 && i>0 && c[i-1][j]=='*' && a[i-1][j]==0){ // right && up
a[i][j] = count;
a[i][j+1] = count;
a[i-1][j] = count;
count++;
}
}else{
}
}
}
boolean f= true;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(c[i][j]=='*'){
if(a[i][j]==0) f = false;
if(check(i,j)){
f = false;
}
}
}
}
if(f){
out.println("YES");
}else{
out.println("NO");
}
}
out.close();
}
public static boolean check(int i,int j){
int count=0;
if(i>0 && a[i-1][j]>0 && a[i-1][j]!=a[i][j]){
return true;
}
if(i<(a.length-1) && a[i+1][j]>0 && a[i+1][j]!=a[i][j]){
return true;
}
if(i>0 && j>0 && a[i-1][j-1]>0 && a[i-1][j-1]!=a[i][j]){
return true;
}
if(j>0 && a[i][j-1]>0 && a[i][j-1]!=a[i][j]){
return true;
}
if(j<(a[0].length-1) && a[i][j+1]>0 && a[i][j+1]!=a[i][j]){
return true;
}
if(i>0 && j<(a[0].length-1) && a[i-1][j+1]>0 && a[i-1][j+1]!=a[i][j]){
return true;
}if(i<(a.length-1) && j>0 && a[i+1][j-1]>0 && a[i+1][j-1]!=a[i][j]){
return true;
}if(i<(a.length-1) && j<(a[0].length-1) && a[i+1][j+1]>0 && a[i+1][j+1]!=a[i][j]){
return true;
}
return false;
}
}
|
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
|
d9ba6ad7d9ee13eda5d6afe33965368c
|
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 codeforces1722F {
static int count;
static char [][] arr;
static boolean [][] visited;
public static void main(String[] args) throws Exception {
FastScanner in = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int numCases = in.nextInt();
while (numCases-->0)
{
int r = in.nextInt();
int c = in.nextInt();
arr = new char[r][c];
for (int i = 0; i<r;i++)
{
arr[i] = in.nextString().toCharArray();
}
visited= new boolean[r][c];
boolean ok = true;
for (int a = 0; a<r;a++)
{
for (int b = 0; b<c;b++)
{
if (!visited[a][b] && arr[a][b]=='*')
{
count = 0;
flood(a,b,r,c);
//pw.println(count);
if (count!=3) ok = false;
}
}
}
int [][] array = new int[r][c];
int max = Integer.MAX_VALUE;
for (int a = 0; a<r;a++)
{
for (int b = 0; b<c;b++)
{
if (arr[a][b]=='*') array[a][b] = max;
}
}
int i = 1;
for (int a = 0; a<r;a++)
{
for (int b = 0; b<c;b++)
{
if (arr[a][b]=='*')
{
if (a>0 && b>0 && arr[a-1][b]=='*' && arr[a][b-1]=='*' && array[a-1][b]==max && array[a][b-1]==max && array[a][b]==max)
{
array[a][b]=i;
array[a-1][b]=i;
array[a][b-1]= i;
i++;
continue;
}
if (a<r-1 && b<c-1 && arr[a+1][b]=='*' && arr[a][b+1]=='*' && array[a+1][b]==max && array[a][b+1]==max && array[a][b]==max)
{
array[a][b]=i;
array[a+1][b]=i;
array[a][b+1]= i;
i++;
continue;
}
if (a<r-1 && b>0 && arr[a+1][b]=='*' && arr[a][b-1]=='*' && array[a+1][b]==max && array[a][b-1]==max && array[a][b]==max)
{
array[a][b]=i;
array[a+1][b]=i;
array[a][b-1]= i;
i++;
continue;
}
if (a>0 && b<c-1 && arr[a-1][b]=='*' && arr[a][b+1]=='*' && array[a-1][b]==max && array[a][b+1]==max && array[a][b]==max)
{
array[a][b]=i;
array[a-1][b]=i;
array[a][b+1]= i;
i++;
}
}
}
}
for (int a = 0; a<r;a++)
{
for (int b = 0; b<c;b++)
{
if (array[a][b] == max) {
ok = false;
break;
}
}
}
if (ok) pw.println("YES");
else pw.println("NO");
}
pw.close();
}
//static boolean checkL(int )
static void flood(int r,int c, int R, int C)
{
if (r<R && r>=0 && c<C && c>=0 && arr[r][c]=='*' && !visited[r][c])
{
count++;
visited[r][c] = true;
flood(r-1,c,R,C);
flood(r+1,c,R,C);
flood(r,c-1,R,C);
flood(r,c+1,R,C);
flood(r+1,c+1,R,C);
flood(r-1,c+1,R,C);
flood(r+1,c-1,R,C);
flood(r-1,c-1,R,C);
}
}
static class L
{
int center;
int type;
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
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++];
}
}
}
|
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
|
227d1281bc144a586132341a49f222ac
|
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
|
// Mahakal
// Remainder: Agar ni ban raha to demotivate ni hona. Yehi chance hai sikhne ka.
import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static PrintWriter out = new PrintWriter(System.out);
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
static FastReader sc;
public static void main(String[] args) {
sc = new FastReader();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
char[][] ch = new char[n][m];
for(int i=0;i<n;i++) {
ch[i] = sc.next().toCharArray();
}
boolean visited[][] = new boolean[n][m];
int isL = 0;
int cnt = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(!visited[i][j] && ch[i][j]=='*') {
isL = func(ch,i,j,visited);
cnt++;
if(isL>3) break;
}
}
if(isL>3) break;
}
if(isL>3) {
println("NO");
continue;
}
int elbow = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(ch[i][j]!='*') continue;
if(i-1>=0 && j+1<m && ch[i-1][j]=='*' && ch[i][j+1]=='*') {
elbow++;
}else if(i+1<n && j+1<m && ch[i+1][j]=='*' && ch[i][j+1]=='*') elbow++;
else if(j-1>=0 && i+1<n && ch[i][j-1]=='*' && ch[i+1][j]=='*') elbow++;
else if(i-1>=0 && j-1>=0 && ch[i-1][j]=='*' && ch[i][j-1]=='*') elbow++;
}
}
// println(cnt+" "+elbow);
if(cnt!=elbow) println("NO");
else println("YES");
}
out.flush();
out.close();
}
public static int dir4[][] = {{1,0},{0,1},{-1,0},{0,-1}};
public static int dir8[][] = {{1,0},{0,1},{-1,0},{0,-1},{-1,-1},{-1,1},{1,1},{1,-1}};
public static int func(char ch[][],int x,int y,boolean visited[][]) {
if(x<0 || y<0 || x>=ch.length || y>=ch[0].length || visited[x][y] || ch[x][y]=='.') return 0;
visited[x][y] = true;
int ans = 0;
for(int ele[]:dir8) {
int dx = ele[0]+x;
int dy = ele[1]+y;
ans+=func(ch,dx,dy,visited);
}
return ans+1;
}
public static int lowerbound(int l,int r,int a[],int val) {
int ans = l-1;
while(l<=r) {
int mid = l+(r-l)/2;
if(a[mid]<val) {
ans = mid;
l = mid+1;
}else r =mid-1;
}
return ans;
}
// 2
// 1 2 2 2 3 4 4 5 5 5
public static int dis(int x,int y) {
return Math.abs(x-y);
}
public static class pair{
int v1;
int v2;
pair(int v1,int v2){
this.v1 = v1;
this.v2 = v2;
}
}
public static class graph{
int v;
int maxbit;
ArrayList<int[]> edgelist = new ArrayList<>();
ArrayList<int[]> adj[];
public static int[][] table;
graph(int v){
this.v = v;
adj = new ArrayList[v];
for(int i=0;i<v;i++) adj[i] = new ArrayList<>();
maxbit = 19;
}
public void addD(int f,int t){
int a[] = {f,t};
adj[f].add(a);
edgelist.add(new int[] {f,t});
}
public void addUD(int f,int t) {
addD(f,t);
addD(t,f);
}
public void addD(int f,int t,int w){
int a[] = {f,t,w};
adj[f].add(a);
edgelist.add(new int[] {f,t,w});
}
public void addUD(int f,int t,int w) {
addD(f,t,w);
addD(t,f,w);
}
public void printEdgelist() {
for(int ele[]:edgelist) {
println(ele);
}
}
public void parentArray(int par[],int src,int parent) {
for(int ele[]:adj[src]) {
int t = ele[1];
if(t==parent) continue;
par[t] = src;
parentArray(par,t,src);
}
}
public boolean IsBipartite(int i,boolean visited[]) {
int color[] = new int[v];
Arrays.fill(color, -1);
Queue<int[]> queue = new ArrayDeque<>();
queue.add(new int[] {i,1,-1});
while(queue.size()>0) {
int top[] = queue.poll();
int src = top[0];
int pc = top[1];
int parent = top[2];
visited[src] = true;
if(color[src]!=-1) {
if(color[src]==pc) return false;
}else color[src] = 1-pc;
for(int ele[]:adj[src]) {
int t = ele[1];
if(t==parent) continue;
else {
if(!visited[t]) queue.add(new int[] {t,color[src],src});
}
}
}
return true;
}
public void printGraph() {
for(int i=0;i<v;i++) {
out.print(i+" - ");
for(int ele[]:adj[i]) {
out.print(ele[1]+" ");
}
out.println();
}
}
public void fillLevel(int src,int d,int level[],int parent) {
level[src] = d;
for(int ele[]:this.adj[src]) {
int t = ele[1];
if(t==parent) continue;
fillLevel(t,d+1,level,src);
}
}
public void LcaTable(int parent[]) {
table = new int[maxbit+1][v];
table[0] = parent;
for(int i=1;i<=maxbit;i++) {
for(int j=0;j<v;j++) {
table[i][j] = table[i-1][table[i-1][j]];
}
}
}
public int getLCA(int u,int v,int level[],int parent[]) {
if(level[u]>level[v]) {
int temp = u;
u = v;
v = temp;
}
int k = level[v]-level[u];
for(int i=0;i<maxbit;i++) {
int bit = 1<<i;
if((bit&k)>0) {
v = table[i][v];
}
}
if(v==u) return u;
for(int i=maxbit;i>=0;i--) {
int p1 = table[i][u];
int p2 = table[i][v];
if(p1==p2) continue;
else {
u = p1;
v = p2;
}
}
return table[0][u];
}
}
//============= For loop bhi ni likha jaa raha======================
public static int[] Intfor(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = sc.nextInt();
return a;
}
public static long[] Longfor(int n) {
long a[] = new long[n];
for(int i=0;i<n;i++) a[i] = sc.nextLong();
return a;
}
public static double[] Doublefor(int n) {
double a[] = new double[n];
for(int i=0;i<n;i++) a[i] = sc.nextDouble();
return a;
}
public static String[] Stringfor(int n) {
String a[] = new String[n];
for(int i=0;i<n;i++) a[i] = sc.next();
return a;
}
// =========================MergeSort Krdo=====================================
public static int[] mergeSort(int arr[],int l,int r) {
if(l>r) return new int[] {};
if(l==r) return new int[] {arr[l]};
int mid = l+(r-l)/2;
int larr[] = mergeSort(arr,l,mid);
int rarr[] = mergeSort(arr,mid+1,r);
int newarr[] = merge(larr,rarr);
return newarr;
}
public static int[] merge(int a[],int b[]) {
int m[] = new int[a.length+b.length];
int i=0;
int j=0;
int ind = 0;
while(i<a.length && j<b.length) {
if(a[i]<b[j]) {
m[ind] = a[i];
i++;ind++;
}else {
m[ind++] = b[j++];
}
}
while(i<a.length) m[ind++] = a[i++];
while(j<b.length) m[ind++] = b[j++];
return m;
}
// =================== Fast Print krne ke lie itna mehnat==================
public static void print(String str) {
out.print(str);
}
public static void println() {
out.println();
}
public static void print(int a) {
out.print(a);
}
public static void print(char c) {
out.print(c);
}
public static void print(char c[]) {
out.print(Arrays.toString(c));
}
public static void print(int a[]) {
out.println(Arrays.toString(a));
}
public static void print(String a[]) {
out.print(Arrays.toString(a));
}
public static void print(float a) {
out.print(a);
}
public static void print(double a) {
out.print(a);
}
public static void print(long a[]) {
out.print(Arrays.toString(a));
}
public static void println(String str) {
out.println(str);
}
public static void println(boolean ap[][]) {
for(boolean ele[]:ap) out.println(Arrays.toString(ele));
}
public static void println(int a) {
out.println(a);
}
public static void println(char c) {
out.println(c);
}
public static void println(char c[]) {
out.println(Arrays.toString(c));
}
public static void println(int a[]) {
out.println(Arrays.toString(a));
}
public static void println(String a[]) {
out.println(Arrays.toString(a));
}
public static void println(float a) {
out.println(a);
}
public static void println(double a) {
out.println(a);
}
public static void println(long a[]) {
out.println(Arrays.toString(a));
}
public static void print(int a[][]) {
for(int ele[]:a) {
out.println(Arrays.toString(ele));
}
out.println();
}
public static void print(char a[][]) {
for(char ele[]:a) {
out.println(Arrays.toString(ele));
}
}
public static void print(long a[][]) {
for(long ele[]:a) {
out.println(Arrays.toString(ele));
}
}
public static void print(String a[][]) {
for(String ele[]:a) {
out.println(Arrays.toString(ele));
}
}
public static void println(long a) {
out.println(a);
}
public static void println(Long a) {
out.println(a);
}
public static void println(Integer a) {
out.println(a);
}
public static void println(Long a[]) {
out.println(Arrays.toString(a));
}
public static void println(Integer a[]) {
out.println(Arrays.toString(a));
}
public static void println(boolean a[]) {
out.println(Arrays.toString(a));
}
public static void forEach(int a[]) {
for(int ele:a) print(ele+" ");
println();
}
public static void forEach(long a[]) {
for(long ele:a) print(ele+" ");
println();
}
public static void forEach(String a[]) {
for(String ele:a) print(ele+" ");
println();
}
public static void forEach(char a[]) {
for(char ele:a) print(ele+" ");
println();
}
public static void forEach(double a[]) {
for(double ele:a) print(ele+" ");
println();
}
// ==================================================
}
|
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
|
a2428dec8ca1eb341c7ac91803ca423b
|
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 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int t = Integer.parseInt(br.readLine());
while(t-->0){
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
char board[][] = new char[n][m];
for(int i = 0; i<n; i++){
board[i] = br.readLine().toCharArray();
}
boolean visited[][] = new boolean[n][m];
boolean flag = true;
loop:
for(int i = 0; i<n; i++ ){
for(int j = 0; j<m; j++){
if(!visited[i][j] && board[i][j] == '*'){
if(isWrong(board, visited, i, j)){
flag = false;
break loop;
}
}
}
}
bw.write(flag ? "YES\n" : "NO\n");
}
bw.flush();
}
static int caseX[][] = {{1,1},{1,1},{0,1},{1,0}};
static int caseY[][] = {{0,1},{-1,0},{1,1},{0,1}};
static boolean isWrong(char[][] board, boolean visited[][], int x, int y){
int cnt = bfs(board, visited, x, y);
if(cnt != 3) return true; // 틀렸습니다!
for(int i = 0; i<4; i++){
int count = 0;
for(int j = 0; j<2; j++) {
int nx = x + caseX[i][j];
int ny = y + caseY[i][j];
if(nx < 0 || ny < 0 || nx >= board.length || ny >= board[0].length) continue;
if(board[nx][ny] == '*') count++;
}
if(count == 2) return false;
}
return true;
}
static int dx[] = {0,1,0,-1,1,1,-1,-1};
static int dy[] = {1,0,-1,0,1,-1,1,-1};
static int bfs(char[][] board, boolean visited[][], int x, int y){
int ret = 1;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{x,y});
visited[x][y] = true;
while(!q.isEmpty()){
int[] now = q.poll();
for(int i = 0; i < 8; i++){
int nx = now[0] + dx[i];
int ny = now[1] + dy[i];
if(nx < 0 || ny < 0 || nx >= board.length || ny >= board[0].length) continue;
if(visited[nx][ny] || board[nx][ny] != '*') continue;
visited[nx][ny] = true;
ret++;
q.add(new int[]{nx, ny});
}
}
return ret;
}
}
|
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
|
df4dd677659bb888c88d4d90cc0dbafb
|
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 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();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int[][]ddd=new int[][]{{1,-1},{-1,1},{1,1},{-1,-1},
{0,1},{0,-1},{-1,0},{1,0}};
static char[][]str;
public static boolean process(){
int n=str.length,m=str[0].length;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(str[i][j]=='*'){int cnt=0;
for(int[]d:ddd){
int x=i+d[0],y=j+d[1];
if(x>=0&&x<n&&y>=0&&y<m&&str[x][y]=='*')cnt++;
}
if(cnt>2)return false;
}
}
}
return true;
}
public static boolean process(int i,int j){
int[][]direc=new int[][]{
{i-1,j,i,j+1},{i-1,j,i,j-1},{i+1,j,i,j-1},{i+1,j,i,j+1}
};
for(int[]d:direc){
int a1=d[0],b1=d[1],a2=d[2],b2=d[3];
if(a1>=0&&b1>=0&&a1<str.length&&b1<str[0].length&&
a2>=0&&b2>=0&&a2<str.length&&b2<str[0].length&&
str[a1][b1]=='*'&&str[a2][b2]=='*'){
str[i][j]='a';
str[a1][b1]='a';
str[a2][b2]='a';
break;
}
}
return true;
}
public static void main(String[] args) {
MyScanner s=new MyScanner();
int num=s.nextInt();
for(int k=0;k<num;k++){
int n=s.nextInt(),m=s.nextInt();str=new char[n][m];
for(int i=0;i<n;i++){
String st=s.next();
for(int j=0;j<m;j++){
str[i][j]=st.charAt(j);
}
}
if(!process()){
System.out.println("NO");
continue;
}
boolean f1=false;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(str[i][j]=='*'){
boolean flag=process(i,j);
if(!flag){
System.out.println("NO");
f1=true;
break;
}
}
}
if(f1)break;
}
if(f1)continue;
boolean f=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++){
if(str[i][j]=='*'){
f=true;
break;
}
}
if(f)break;
}
if(f) 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 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
|
ca1101757e23a7912d6e88aee848c427
|
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.math.*;
public class CF1
{
static FastReader sc=new FastReader();
// static long dp[][];
// static boolean v[][][];
// static int mod=998244353;;
// static int mod=1000000007;
static long oset[];
static int oset_p;
static long mod=1000000007;
// static int max;
// static int bit[];
//static long fact[];
//static HashMap<Long,Long> mp;
//static StringBuffer sb=new StringBuffer("");
//static HashMap<Integer,Integer> map;
//static List<Integer>list;
//static int m;
//static StringBuffer sb=new StringBuffer();
// static PriorityQueue<Integer>heap;
//static int dp[];
// static boolean cycle;
static PrintWriter out=new PrintWriter(System.out);
// static int msg[];
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
// Queue<int[]>q=new LinkedList<>();
outer :while (ttt-- > 0)
{
int n=i();
int m=i();
char grid[][]=inputG(n,m);
boolean visited[][]=new boolean[n+1][m+1];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(!visited[i][j]&&grid[i][j]=='*')
{
int d=dfs(grid,n,m,i,j,visited);
if(d!=3)
{
// pln(d+" "+i+" "+j);
pln("NO");
continue outer;
}
}
}
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<m;j++)
{
if(grid[i][j]=='*')
{
if(j+1<m&&grid[i+1][j]=='*'&&grid[i][j+1]=='*')
{
grid[i+1][j]='.';
grid[i][j+1]='.';
grid[i][j]='.';
}
else
if(j+1<m&&grid[i][j+1]=='*'&&grid[i+1][j+1]=='*')
{
grid[i][j]='.';
grid[i][j+1]='.';
grid[i+1][j+1]='.';
}
else
if(j+1<m&&grid[i+1][j]=='*'&&grid[i+1][j+1]=='*')
{
grid[i+1][j]='.';
grid[i+1][j+1]='.';
grid[i][j]='.';
}
else
if(j-1>=0&&grid[i+1][j]=='*'&&grid[i+1][j-1]=='*')
{
//pln(i+" "+j);
grid[i+1][j]='.';
grid[i+1][j-1]='.';
grid[i][j]='.';
}
else
{
//pln(i+" "+j);
pln("NO");
continue outer;
}
}
}
}
for(int i=0;i<m;i++)
if(grid[n-1][i]=='*')
{
//1pln((n-1)+" "+i);
pln("NO");
continue outer;
}
for(int i=0;i<n-1;i++)
if(grid[i][m-1]=='*')
{
//pln(i+" "+(m-1));
pln("NO");
continue outer;
}
out.println("YES");
}
out.close();
}
static int dfs(char grid[][],int n,int m,int row,int col,boolean visited[][])
{
if(row>=n||col>=m||row<0||col<0||grid[row][col]=='.'||visited[row][col])
return 0;
visited[row][col]=true;
int move[][]= {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
int c=1;
for(int i=0;i<8;i++)
{
c+=dfs(grid,n,m,row+move[i][0],col+move[i][1],visited);
}
return c;
}
static int findPar(int x,int parent[])
{
if(parent[x]==x)
return x;
return parent[x]=findPar(parent[x],parent);
}
static void union(int u,int v,int parent[],int rank[])
{
int x=findPar(u,parent);
int y=findPar(v,parent);
if(x==y)
return;
if(rank[x]>rank[y])
{
parent[y]=x;
}
else
if(rank[y]>rank[x])
parent[x]=y;
else
{
parent[y]=x;
rank[x]++;
}
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
// int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static ArrayList<Long> gLL()
{
return new ArrayList<>();
}
static ArrayList<Integer> gL()
{
return new ArrayList<>();
}
static StringBuffer gsb()
{
return new StringBuffer();
}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return A[a]=find(A, A[a]);
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return find(A, A[a]);
//}
//FENWICK TREE
static class BIT{
int bit[];
BIT(int n){
bit=new int[n+1];
}
int lowbit(int i){
return i&(-i);
}
int query(int i){
int res=0;
while(i>0){
res+=bit[i];
i-=lowbit(i);
}
return res;
}
void update(int i,int val){
while(i<bit.length){
bit[i]+=val;
i+=lowbit(i);
}
}
}
//END
static long summation(long A[],int si,int ei)
{
long ans=0;
for(int i=si;i<=ei;i++)
ans+=A[i];
return ans;
}
static void add(long v,Map<Long,Long>mp) {
if(!mp.containsKey(v)) {
mp.put(v, (long)1);
}
else {
mp.put(v, mp.get(v)+(long)1);
}
}
static void remove(long v,Map<Long,Long>mp) {
if(mp.containsKey(v)) {
mp.put(v, mp.get(v)-(long)1);
if(mp.get(v)==0)
mp.remove(v);
}
}
public static int upper(List<Long>A,long k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A.get(mid)<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int upper(long A[],long k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(List<Long>A,long k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A.get(mid)<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
public static int lower(long A[],long k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static void pln(String s)
{
out.println(s);
}
static void p(String s)
{
out.print(s);
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static char[][] inputG(int n,int m)
{
char str[][]=new char[n][m];
for(int i=0;i<n;i++)
{
String s=s();
for(int j=0;j<m;j++)
str[i][j]=s.charAt(j);
}
return str;
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static long[] inputL1(int n)
{
long arr[]=new long[n+1];
for(int i=1;i<=n;i++)
arr[i]=l();
return arr;
}
static int[] input1(int n)
{
int arr[]=new int[n+1];
for(int i=1;i<=n;i++)
arr[i]=i();
return arr;
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static long[][] inputL(int n,int m){
long A[][]=new long[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=l();
}
}
return A;
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
if(n==0)
return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static char[] cs()
{
String s=s();
char ch[]=new char[s.length()];
for(int i=0;i<s.length();i++)
ch[i]=s.charAt(i);
return ch;
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long 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;
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void dln(String s)
{
out.println(s);
}
static void d(String s)
{
out.print(s);
}
static void print(int A[]) {
for(int i : A) {
out.print(i+" ");
}
out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Long> hash(int A[]){
HashMap<Integer,Long> map=new HashMap<Integer, Long>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+(long)1);
}
else {
map.put(i, (long)1);
}
}
return map;
}
static HashMap<Long,Long> hash(long A[]){
HashMap<Long,Long> map=new HashMap<Long, Long>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+(long)1);
}
else {
map.put(i, (long)1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
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
|
a645e0e914e9838df023075e60bbe160
|
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 CodeForces {
/*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/
public static void solve(int tCase) throws IOException {
int n = sc.nextInt();
int m =sc.nextInt();
char[][] grid = new char[n][m];
for(int i=0;i<n;i++)grid[i] = sc.next().toCharArray();
int[][] arr = new int[n][m];
int x = 1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j] == '*' && arr[i][j] == 0){
if(i+1<n && j+1< m && grid[i+1][j] == '*' && grid[i+1][j+1] == '*'){
arr[i][j] = arr[i+1][j] = arr[i+1][j+1] = x++;
}else if(i+1<n && j>0 &&grid[i+1][j] == '*' && grid[i+1][j-1] == '*'){
arr[i][j] = arr[i+1][j] = arr[i+1][j-1] = x++;
}
else if(i+1<n && j+1< m && grid[i][j+1] == '*' && grid[i+1][j+1] == '*'){
arr[i][j] = arr[i][j+1] = arr[i+1][j+1] = x++;
}else if(i+1<n && j+1< m && grid[i][j+1] == '*' && grid[i+1][j] == '*'){
arr[i][j] = arr[i][j+1] = arr[i+1][j] = x++;
}else{
out.println("NO");return;
}
}
}
}
int[] dx = new int[]{1,1,1,-1,-1,-1,0,0};
int[] dy = new int[]{0,1,-1,0,1,-1,-1,1};
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j]==0)continue;
for(int k=0;k<8;k++){
int ii = i + dx[k];
int jj = j + dy[k];
if(ii>=0 && jj>=0 && ii<n && jj<m && (arr[ii][jj] !=0 && arr[ii][jj] !=arr[i][j])){
out.println("NO");return;
}
}
}
}
out.println("YES");
}
public static void main(String[] args) throws IOException {
openIO();
int testCase = 1;
testCase = sc.nextInt();
for (int i = 1; i <= testCase; i++) solve(i);
closeIO();
}
/*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/
/*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/
public static int mod = (int) 1e9 + 9;
// public static int mod = 998244353;
public static int inf_int = (int)1e9;
public static long inf_long = (long)2e15;
public static void _sort(int[] arr, boolean isAscending) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
public static void _sort(long[] arr, boolean isAscending) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (long ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
// time : O(1), space : O(1)
public static int _digitCount(long num,int base){
// this will give the # of digits needed for a number num in format : base
return (int)(1 + Math.log(num)/Math.log(base));
}
// time for pre-computation of factorial and inverse-factorial table : O(nlog(mod))
public static long[] factorial , inverseFact;
public static void _ncr_precompute(int n){
factorial = new long[n+1];
inverseFact = new long[n+1];
factorial[0] = inverseFact[0] = 1;
for (int i = 1; i <=n; i++) {
factorial[i] = (factorial[i - 1] * i) % mod;
inverseFact[i] = _modExpo(factorial[i], mod - 2);
}
}
// time of factorial calculation after pre-computation is O(1)
public static int _ncr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod);
}
public static int _npr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[n - r] % mod);
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a>0){
long x = a;
a = b % a;
b = x;
}
return b;
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// binary exponentiation time O(logn)
public static long _modExpo(long x, long n) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans *= x;
ans %= mod;
n--;
} else {
x *= x;
x %= mod;
n >>= 1;
}
}
return ans;
}
// function to find a/b under modulo mod. time : O(logn)
public static long _modInv(long a,long b){
return (a * _modExpo(b,mod-2)) % mod;
}
//sieve or first divisor time : O(mx * log ( log (mx) ) )
public static int[] _seive(int mx){
int[] firstDivisor = new int[mx+1];
for(int i=0;i<=mx;i++)firstDivisor[i] = i;
for(int i=2;i*i<=mx;i++)
if(firstDivisor[i] == i)
for(int j = i*i;j<=mx;j+=i)
if(firstDivisor[j]==j)firstDivisor[j] = i;
return firstDivisor;
}
static class Pair<K, V>{
K ff;
V ss;
public Pair(K ff, V ss) {
this.ff = ff;
this.ss = ss;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return ff.equals(pair.ff) && ss.equals(pair.ss);
}
@Override
public int hashCode() {
return Objects.hash(ff, ss);
}
@Override
public String toString(){
return ff.toString()+" "+ss.toString();
}
}
/*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/
/*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/
static FastestReader sc;
static PrintWriter out;
private static void openIO() throws IOException {
sc = new FastestReader();
out = new PrintWriter(System.out);
}
public static void closeIO() throws IOException {
out.flush();
out.close();
sc.close();
}
private static final class FastestReader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public FastestReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastestReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() throws IOException {
int b;
//noinspection StatementWithEmptyBody
while ((b = read()) != -1 && isSpaceChar(b)) {}
return b;
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
final boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return neg?-ret:ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
final boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return neg?-ret:ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') c = read();
final boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
return neg?-ret:ret;
}
public String nextLine() throws IOException {
final byte[] buf = new byte[(1<<10)]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
/*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/
}
/** Some points to keep in mind :
* 1. don't use Arrays.sort(primitive data type array)
* 2. try to make the parameters of a recursive function as less as possible,
* more use static variables.
* 3. If n = 5000, then O(n^2 logn) need atleast 4 sec to work
* 4. dp[2][n] works faster than dp[n][2]
* 5. if split wrt 1 char use '\\' before char: .split("\\.");
*
**/
|
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
|
78c360365aea79ca05f92bcfca49d7ad
|
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.ArrayList;
import java.util.List;
import java.util.Scanner;
public class F {
static boolean[][] vis;
static char[][] arr;
static int n;
static int m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
arr = new char[n][m];
for (int i = 0; i < n; i++) {
arr[i] = sc.next().toCharArray();
}
vis = new boolean[n][m];
boolean flag = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(arr[i][j] =='.'){
vis[i][j] = true;
}
}
}
ll:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(vis[i][j]){
continue;
}
vis[i][j] = true;
if(arr[i][j]=='*'){
List<int[]> list = new ArrayList<>();
list.add(new int[]{i,j});
dfs(i, j , list);
if(list.size()!=3){
flag = false;
break ll;
}
int maxX = -1, minX = 100, maxY = -1, minY =100;
for (int k = 0; k < 3; k++) {
maxX = Math.max(maxX, list.get(k)[0]);
minX = Math.min(minX, list.get(k)[0]);
maxY = Math.max(maxY, list.get(k)[1]);
minY = Math.min(minY, list.get(k)[1]);
}
if(maxY-minY!=1 || maxX-minX!=1){
flag = false;
break ll;
}
}
}
}
if(flag){
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}
static int[][] dirs = new int[][]{{-1,-1},{-1,0},{-1,1},{0,1},{0,-1},{1,-1},{1,0},{1,1}};
private static void dfs(int i , int j,List<int[]> list ){
for(int[] dir: dirs){
int x = dir[0], y= dir[1];
if(i+x<0 || i+x>=n || j+y<0 || j+y>=m){
continue;
}
if(vis[i+x][j+y]){
continue;
}
vis[i+x][j+y] = true;
if(arr[i+x][j+y]=='*'){
list.add(new int[]{i+x,j+y});
dfs(i+x, j+y, list);
}
}
}
}
|
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
|
0e6943e6af9fba7241f076107cd57d87
|
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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class F {
static final long mod = (long) 1e9 + 7l;
static List<int[]> [] adj;
static int [] dx = {-1, 0, 1}, dy = {-1, 0, 1};
private static void solve(int t){
int n = fs.nextInt();
int m = fs.nextInt();
char[][] chr = new char[n][m];
for (int i = 0; i < n; i++) {
chr[i]=fs.next().toCharArray();
}
int seen[][] = new int[n][m];
int num = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(seen[i][j]==0 && chr[i][j]=='*' && !check(i,j, chr, seen, num++)){
out.println("NO");
return;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(chr[i][j]=='*' && seen[i][j]==0){
out.println("NO");
return;
}
if(chr[i][j]=='*' && neighbours(i,j, seen)){
out.println("NO");
return;
}
}
}
out.println("YES");
return;
}
private static boolean neighbours(int i, int j, int[][] seen) {
int n = seen.length;
int m = seen[0].length;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
if (0 <= i + dx[x] && i + dx[x] < n && 0 <= j + dy[y] && j + dy[y] < m){
if (seen[i + dx[x]][j + dy[y]] != seen[i][j] && seen[i + dx[x]][j + dy[y]] != 0) {
return true;
}
}
}
}
return false;
}
private static boolean check(int r, int c, char[][] chr, int[][] seen, int num) {
int n = chr.length;
int m = chr[0].length;
// rd |- , dr _|, dl |_, rrd -|
int cnt = 0;
seen[r][c]=num;
if(r+1<n && c-1 >=0 && chr[r+1][c] =='*' && chr[r+1][c-1]=='*'){
cnt++;
seen[r+1][c] =num;
seen[r+1][c-1]=num;
}
if(r+1<n && c+1<m &&chr[r+1][c] =='*' && chr[r+1][c+1]=='*'){
seen[r+1][c] =num;
seen[r+1][c+1]=num;
cnt++;
}
if(r+1<n&& c+1<m && chr[r+1][c] =='*' && chr[r][c+1]=='*'){
seen[r+1][c] =num;
seen[r][c+1]=num;
cnt++;
}
if(r+1<n && c+1<m && chr[r][c+1] =='*' && chr[r+1][c+1]=='*'){
seen[r][c+1] =num;
seen[r+1][c+1]=num;
cnt++;
}
if(cnt!=1)return false;
return true;
}
private static int[] sortByCollections(int[] arr) {
ArrayList<Integer> ls = new ArrayList<>(arr.length);
for (int i = 0; i < arr.length; i++) {
ls.add(arr[i]);
}
Collections.sort(ls);
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
return arr;
}
public static void main(String[] args) {
fs = new FastScanner();
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
int t = fs.nextInt();
for (int i = 1; i <= t; i++) solve(t);
out.close();
// System.err.println( System.currentTimeMillis() - s + "ms" );
}
static boolean DEBUG = true;
static PrintWriter out;
static FastScanner fs;
static void trace(Object... o) {
if (!DEBUG) return;
System.err.println(Arrays.deepToString(o));
}
static void pl(Object o) {
out.println(o);
}
static void p(Object o) {
out.print(o);
}
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
static void sieveOfEratosthenes(int n, int factors[]) {
factors[1] = 1;
for (int p = 2; p * p <= n; p++) {
if (factors[p] == 0) {
factors[p] = p;
for (int i = p * p; i <= n; i += p)
factors[i] = p;
}
}
}
static long mul(long a, long b) {
return a * b % mod;
}
static long fact(int x) {
long ans = 1;
for (int i = 2; i <= x; i++) ans = mul(ans, i);
return ans;
}
static long fastPow(long base, long exp) {
if (exp == 0) return 1;
long half = fastPow(base, exp / 2);
if (exp % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long modInv(long x) {
return fastPow(x, mod - 2);
}
static long nCk(int n, int k) {
return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n - k))));
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!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[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class _Scanner {
InputStream is;
_Scanner(InputStream is) {
this.is = is;
}
byte[] bb = new byte[1 << 15];
int k, l;
byte getc() throws IOException {
if (k >= l) {
k = 0;
l = is.read(bb);
if (l < 0) return -1;
}
return bb[k++];
}
byte skip() throws IOException {
byte b;
while ((b = getc()) <= 32)
;
return b;
}
int nextInt() throws IOException {
int n = 0;
for (byte b = skip(); b > 32; b = getc())
n = n * 10 + b - '0';
return n;
}
}
}
|
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
|
3133609e8283ac0bf6b0d3e6e821b048
|
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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
public class Solution
{
public static class CFScanner
{
BufferedReader br;
StringTokenizer st;
public CFScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
// int n = in.nextInt();
// long n = in.nextLong();
// double n = in.nextDouble();
// String s = in.next();
CFScanner in = new CFScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int T = in.nextInt();
for (int t = 1; t <= T; t++)
{
int rows = in.nextInt();
int cols = in.nextInt();
char[][] mat = new char[rows][cols];
for (int i = 0; i < rows; i++)
{
String s = in.next();
for (int j = 0; j < cols; j++)
{
mat[i][j] = s.charAt(j);
}
}
out.println(getL(rows, cols, mat));
}
out.close();
}
private static String getL(int rows, int cols, char[][] mat)
{
boolean[][] visited = new boolean[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (visited[i][j] || mat[i][j] != '*')
{
continue;
}
List<int[]> ps = new ArrayList<>();
int count = getLHelper(mat, i, j, visited, ps);
if (count != 3)
{
return "NO";
}
Set<Integer> rs = new HashSet<>();
Set<Integer> cs = new HashSet<>();
for (int[] p : ps)
{
rs.add(p[0]);
cs.add(p[1]);
}
if (rs.size() != 2 || cs.size() != 2)
{
return "NO";
}
}
}
return "YES";
}
private static int getLHelper(char[][] mat, int r, int c, boolean[][] visited, List<int[]> ps)
{
int count = 1;
visited[r][c] = true;
ps.add(new int[]{r, c});
for (int d1 = -1; d1 <= 1; d1++)
{
for (int d2 = -1; d2 <= 1; d2++)
{
if (d1 == 0 && d2 == 0)
{
continue;
}
int rNext = r + d1;
int cNext = c + d2;
if (rNext >= 0 && rNext < mat.length && cNext >= 0 && cNext < mat[0].length && mat[rNext][cNext] == '*' && !visited[rNext][cNext])
{
count += getLHelper(mat, rNext, cNext, visited, ps);
}
}
}
return count;
}
private static void getArea(int[][] rects, int[][] qs, PrintWriter out)
{
for (int[] q : qs)
{
int h1 = q[0];
int w1 = q[1];
int h2 = q[2];
int w2 = q[3];
long area = 0L;
for (int[] rect : rects)
{
int h = rect[0];
int w = rect[1];
if (h > h1 && h < h2 && w > w1 && w < w2)
{
area += (long)h * (long)w;
}
}
out.println(area);
}
}
private static String getMax(String s)
{
long score = 0L;
List<Integer> diffs = new ArrayList<>();
for (int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
int lefts = i;
int rights = s.length() - i - 1;
if (ch == 'L')
{
score += lefts;
int diff = Math.max(0, rights - lefts);
diffs.add(diff);
}
else
{
score += rights;
int diff = Math.max(0, lefts - rights);
diffs.add(diff);
}
}
Collections.sort(diffs);
StringBuilder strBd = new StringBuilder();
for (int i = 1; i <= s.length(); i++)
{
score += diffs.get(s.length() - i);
strBd.append(score);
strBd.append(" ");
}
strBd.deleteCharAt(strBd.length() - 1);
return strBd.toString();
}
private static String getScores(List<String> ss1, List<String> ss2, List<String> ss3)
{
Map<String, Integer> map = new HashMap<>();
for (List<String> ss : Arrays.asList(ss1, ss2, ss3))
{
for (String s : ss)
{
map.put(s, map.getOrDefault(s, 0) + 1);
}
}
StringBuilder strBd = new StringBuilder();
for (List<String> ss : Arrays.asList(ss1, ss2, ss3))
{
int score = 0;
for (String s : ss)
{
if (map.get(s) == 1)
{
score += 3;
}
else if (map.get(s) == 2)
{
score += 1;
}
}
strBd.append(score);
strBd.append(" ");
}
strBd.deleteCharAt(strBd.length() - 1);
return strBd.toString();
}
private static String getSame(String s1, String s2)
{
for (int i = 0; i < s1.length(); i++)
{
if (s1.charAt(i) == s2.charAt(i) || (s1.charAt(i) == 'G' || s1.charAt(i) == 'B') && (s2.charAt(i) == 'G' || s2.charAt(i) == 'B'))
{
continue;
}
else
{
return "NO";
}
}
return "YES";
}
private static String getPerm(String name, String s)
{
char[] chs1 = name.toCharArray();
char[] chs2 = s.toCharArray();
Arrays.sort(chs1);
Arrays.sort(chs2);
return Arrays.equals(chs1, chs2) ? "YES" : "NO";
}
}
|
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
|
c70223379c307c5d988b022e6b10eb92
|
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.util.spi.CalendarDataProvider;
import javax.imageio.metadata.IIOInvalidTreeException;
import java.lang.*;
import java.sql.Array;
// import java.lang.invoke.ConstantBootstraps;
// import java.math.BigInteger;
// import java.beans.IndexedPropertyChangeEvent;
import java.io.*;
@SuppressWarnings("unchecked")
public class Main implements Runnable {
static FastReader in;
static PrintWriter out;
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 {
static BufferedReader br;
static 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;
}
}
static long power(long a, long b) {
if (a == 0L)
return 0L;
if (b == 0)
return 1;
long val = power(a, b / 2);
val = val * val;
if ((b % 2) != 0)
val = val * a;
return val;
}
static long power(long a, long b, long mod) {
if (a == 0L)
return 0L;
if (b == 0)
return 1;
long val = power(a, b / 2L, mod) % mod;
val = (val * val) % mod;
if ((b % 2) != 0)
val = (val * a) % mod;
return val;
}
static ArrayList<Long> prime_factors(long n) {
ArrayList<Long> ans = new ArrayList<Long>();
while (n % 2 == 0) {
ans.add(2L);
n /= 2L;
}
for (long i = 3; i * i <= n; i++) {
while (n % i == 0) {
ans.add(i);
n /= i;
}
}
if (n > 2) {
ans.add(n);
}
return ans;
}
static void sort(ArrayList<Long> a) {
Collections.sort(a);
}
static void reverse_sort(ArrayList<Long> a) {
Collections.sort(a, Collections.reverseOrder());
}
static void swap(long[] a, int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(List<Long> a, int i, int j) {
long temp = a.get(i);
a.set(j, a.get(i));
a.set(j, temp);
}
static void sieve(boolean[] prime) {
int n = prime.length - 1;
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = 2 * i; j <= n; j += i) {
prime[j] = false;
}
}
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static HashMap<Long, Integer> map_prime_factors(long n) {
HashMap<Long, Integer> map = new HashMap<>();
while (n % 2 == 0) {
map.put(2L, map.getOrDefault(2L, 0) + 1);
n /= 2L;
}
for (long i = 3; i <= Math.sqrt(n); i++) {
while (n % i == 0) {
map.put(i, map.getOrDefault(i, 0) + 1);
n /= i;
}
}
if (n > 2) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
return map;
}
static List<Long> divisor(long n) {
List<Long> ans = new ArrayList<>();
ans.add(1L);
long count = 0;
for (long i = 2L; i * i <= n; i++) {
if (n % i == 0) {
if (i == n / i)
ans.add(i);
else {
ans.add(i);
ans.add(n / i);
}
}
}
return ans;
}
static void sum_of_divisors(int n) {
int[] dp = new int[n + 1];
for (int i = 1; i <= n; i++) {
dp[i] += i;
for (int j = i + i; j <= n; j += i) {
dp[j] += i;
}
}
}
static void prime_factorization_using_sieve(int n) {
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
for (int i = 2; i <= n; i++) {
dp[i] = Math.min(dp[i], i);// dp[i] stores smallest prime number which divides number i
for (int j = 2 * i; j <= n; j++) {// can calculate prime factorization in O(logn) time by dividing
// val/=dp[val]; till 1 is obtained
dp[j] = Math.min(dp[j], i);
}
}
}
/*
* ----------------------------------------------------Sorting------------------
* ------------------------------------------------
*/
public static void sort(long[] arr, int l, int r) {
if (l >= r)
return;
int mid = (l + r) / 2;
sort(arr, l, mid);
sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
public static void sort(int[] arr, int l, int r) {
if (l >= r)
return;
int mid = (l + r) / 2;
sort(arr, l, mid);
sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
static void merge(int[] arr, int l, int mid, int r) {
int[] left = new int[mid - l + 1];
int[] right = new int[r - mid];
for (int i = l; i <= mid; i++) {
left[i - l] = arr[i];
}
for (int i = mid + 1; i <= r; i++) {
right[i - (mid + 1)] = arr[i];
}
int left_start = 0;
int right_start = 0;
int left_length = mid - l + 1;
int right_length = r - mid;
int temp = l;
while (left_start < left_length && right_start < right_length) {
if (left[left_start] < right[right_start]) {
arr[temp] = left[left_start++];
} else {
arr[temp] = right[right_start++];
}
temp++;
}
while (left_start < left_length) {
arr[temp++] = left[left_start++];
}
while (right_start < right_length) {
arr[temp++] = right[right_start++];
}
}
static void merge(long[] arr, int l, int mid, int r) {
long[] left = new long[mid - l + 1];
long[] right = new long[r - mid];
for (int i = l; i <= mid; i++) {
left[i - l] = arr[i];
}
for (int i = mid + 1; i <= r; i++) {
right[i - (mid + 1)] = arr[i];
}
int left_start = 0;
int right_start = 0;
int left_length = mid - l + 1;
int right_length = r - mid;
int temp = l;
while (left_start < left_length && right_start < right_length) {
if (left[left_start] < right[right_start]) {
arr[temp] = left[left_start++];
} else {
arr[temp] = right[right_start++];
}
temp++;
}
while (left_start < left_length) {
arr[temp++] = left[left_start++];
}
while (right_start < right_length) {
arr[temp++] = right[right_start++];
}
}
// static int[] smallest_prime_factor;
// static int count = 1;
// static int[] p = new int[100002];
// static long[] flat_tree = new long[300002];
// static int[] in_time = new int[1000002];
// static int[] out_time = new int[1000002];
// static long[] subtree_gcd = new long[100002];
// static int w = 0;
// static boolean poss = true;
/*
* (a^b^c)%mod
* Using fermats Little theorem
* x^(mod-1)=1(mod)
* so b^c can be written as b^c=x*(mod-1)+y
* then (a^(x*(mod-1)+y))%mod=(a^(x*(mod-1))*a^(y))mod
* the term (a^(x*(mod-1)))%mod=a^(mod-1)*a^(mod-1)
*
*/
// ---------------------------------------------------Segment_Tree----------------------------------------------------------------//
// static class comparator implements Comparator<node> {
// public int compare(node a, node b) {
// return a.a - b.a > 0 ? 1 : -1;
// }
// }
static class Segment_Tree {
private long[] segment_tree;
public Segment_Tree(int n) {
this.segment_tree = new long[4 * n + 1];
}
void build(int index, int left, int right, int[] a) {
if (left == right) {
segment_tree[index] = a[left];
return;
}
int mid = (left + right) / 2;
build(2 * index + 1, left, mid, a);
build(2 * index + 2, mid + 1, right, a);
segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2];
}
long query(int index, int left, int right, int l, int r) {
if (left > right)
return 0;
if (left >= l && r >= right) {
return segment_tree[index];
}
if (l > right || left > r)
return 0;
int mid = (left + right) / 2;
return query(2 * index + 1, left, mid, l, r) + query(2 * index + 2, mid + 1, right, l, r);
}
void update(int index, int left, int right, int node, int val) {
if (left == right) {
segment_tree[index] += val;
return;
}
int mid = (left + right) / 2;
if (node <= mid)
update(2 * index + 1, left, mid, node, val);
else
update(2 * index + 2, mid + 1, right, node, val);
segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2];
}
}
static class min_Segment_Tree {
private long[] segment_tree;
public min_Segment_Tree(int n) {
this.segment_tree = new long[4 * n + 1];
}
void build(int index, int left, int right, int[] a) {
if (left == right) {
segment_tree[index] = a[left];
return;
}
int mid = (left + right) / 2;
build(2 * index + 1, left, mid, a);
build(2 * index + 2, mid + 1, right, a);
segment_tree[index] = Math.min(segment_tree[2 * index + 1], segment_tree[2 * index + 2]);
}
long query(int index, int left, int right, int l, int r) {
if (left > right)
return Integer.MAX_VALUE;
if (left >= l && r >= right) {
return segment_tree[index];
}
if (l > right || left > r)
return Integer.MAX_VALUE;
int mid = (left + right) / 2;
return Math.min(query(2 * index + 1, left, mid, l, r), query(2 * index + 2, mid + 1, right, l, r));
}
}
static class max_Segment_Tree {
public long[] segment_tree;
public max_Segment_Tree(int n) {
this.segment_tree = new long[4 * n + 1];
}
void build(int index, int left, int right, int[] a) {
// pn(index+" "+left+" "+right);
if (left == right) {
segment_tree[index] = a[left];
return;
}
int mid = (left + right) / 2;
build(2 * index + 1, left, mid, a);
build(2 * index + 2, mid + 1, right, a);
segment_tree[index] = Math.max(segment_tree[2 * index + 1], segment_tree[2 * index + 2]);
}
long query(int index, int left, int right, int l, int r) {
if (left >= l && r >= right) {
return segment_tree[index];
}
if (l > right || left > r)
return Integer.MIN_VALUE;
int mid = (left + right) / 2;
long max = Math.max(query(2 * index + 1, left, mid, l, r), query(2 * index + 2, mid + 1, right, l, r));
// pn(left+" "+right+" "+max);
return max;
}
}
// // ------------------------------------------------------ DSU
// // --------------------------------------------------------------------//
static class dsu {
private int[] parent;
private int[] rank;
private int[] size;
public dsu(int n) {
this.parent = new int[n + 1];
this.rank = new int[n + 1];
this.size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 1;
size[i] = 1;
}
}
int findParent(int a) {
if (parent[a] == a)
return a;
else
return parent[a] = findParent(parent[a]);
}
void join(int a, int b) {
int parent_a = findParent(a);
int parent_b = findParent(b);
if (parent_a == parent_b)
return;
if (rank[parent_a] > rank[parent_b]) {
parent[parent_b] = parent_a;
size[parent_a] += size[parent_b];
} else if (rank[parent_a] < rank[parent_b]) {
parent[parent_a] = parent_b;
size[parent_b] += size[parent_a];
} else {
parent[parent_a] = parent_b;
size[parent_b] += size[parent_a];
rank[parent_b]++;
}
}
}
// ------------------------------------------------Comparable---------------------------------------------------------------------//
public static class rectangle {
int x1, x3, y1, y3;// lower left and upper rigth coordinates
int x2, y2, x4, y4;// remaining coordinates
/*
* (x4,y4) (x3,y3)
* ____________
* | |
* |____________|
*
* (x1,y1) (x2,y2)
*/
public rectangle(int x1, int y1, int x3, int y3) {
this.x1 = x1;
this.y1 = y1;
this.x3 = x3;
this.y3 = y3;
this.x2 = x3;
this.y2 = y1;
this.x4 = x1;
this.y4 = y3;
}
public long area() {
if (x3 < x1 || y3 < y1)
return 0;
return (long) Math.abs(x1 - x3) * (long) Math.abs(y1 - y3);
}
}
static long intersection(rectangle a, rectangle b) {
if (a.x3 < a.x1 || a.y3 < a.y1 || b.x3 < b.x1 || b.y3 < b.y1)
return 0;
long l1 = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1));
long l2 = ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1));
if (l1 < 0 || l2 < 0)
return 0;
long area = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1))
* ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1));
if (area < 0)
return 0;
return area;
}
static class pair implements Comparable<pair> {
int a;
int b;
int dir;
public pair(int a, int b, int dir) {
this.a = a;
this.b = b;
this.dir = dir;
}
public int compareTo(pair p) {
// if (this.b == Integer.MIN_VALUE || p.b == Integer.MIN_VALUE)
// return (int) (this.index - p.index);
return (int) (this.a - p.a);
}
}
static class pair2 implements Comparable<pair2> {
long a;
int index;
public pair2(long a, int index) {
this.a = a;
this.index = index;
}
public int compareTo(pair2 p) {
return (int) (this.a - p.a);
}
}
static class node implements Comparable<node> {
int l;
int r;
public node(int l, int r) {
this.l = l;
this.r = r;
}
public int compareTo(node a) {
if(this.l==a.l){
return this.r-a.r;
}
return (int) (this.l - a.l);
}
}
static long ans = 0;
static int leaf = 0;
static boolean poss = true;
static long mod = 1000000007L;
static int[] dx = { -1, 0, 0, 1 ,-1, -1, 1, 1};
static int[] dy = { 0, -1, 1, 0 ,-1, 1, -1, 1};
static boolean cycle;
int count = 0;
public static void main(String[] args) throws Exception {
// new Thread(null,new Main(), "1", 1 << 26).start();
long start = System.nanoTime();
in = new FastReader();
out = new PrintWriter(System.out, false);
int tc = ni();
while (tc-- > 0) {
int n=ni();
int m=ni();
char[][] c=new char[n][m];
for(int i=0;i<n;i++){
String s=n();
for(int j=0;j<m;j++){
c[i][j]=s.charAt(j);
}
}
boolean poss=true;
boolean[][] visited=new boolean[n][m];
for(int i=0;i<n-1;i++){
for(int j=0;j<m-1;j++){
int count=0;
int a=-1;
int b=-1;
for(int x=i;x<i+2;x++){
for(int y=j;y<j+2;y++){
if(!visited[x][y] && c[x][y]=='*'){
count++;
a=x;
b=y;
}
}
}
if(count==3){
int val=dfs(a, b, visited, c);
if(val!=3){
poss=false;
}
// pn(a+" "+b+" "+val);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!visited[i][j] && c[i][j]=='*'){
poss=false;
}
}
}
if(poss)pn("YES");
else pn("NO");
}
long end = System.nanoTime();
// pn((end-start)*1.0/1000000000);
out.flush();
out.close();
}
static long ncm(long[] fact, long[] fact_inv, int n, int m) {
if (n < m)
return 0L;
long a = fact[n];
long b = fact_inv[n - m];
long c = fact_inv[m];
a = (a * b) % mod;
return (a * c) % mod;
}
public void run() {
try {
in = new FastReader();
out = new PrintWriter(System.out);
int tc = ni();
while (tc-- > 0) {
}
out.flush();
} catch (Exception e) {
}
}
static int dfs(int i, int j,boolean[][] visited,char[][] c) {
visited[i][j] = true;
int count=1;
for(int k=0;k<8;k++){
int x=i+dx[k];
int y=j+dy[k];
if(inside(x, y, c.length, c[0].length) && !visited[x][y] && c[x][y]=='*'){
count+=dfs(x, y, visited, c);
}
}
return count;
}
static boolean inside(int i, int j, int n, int m) {
if (i >= 0 && j >= 0 && i < n && j < m)
return true;
return false;
}
static int binary_search(int[] a, int val) {
int l = 0;
int r = a.length - 1;
int ans = 0;
while (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] <= val) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
return ans;
}
static int[] longest_common_prefix(String s) {
int m = s.length();
int[] lcs = new int[m];
int len = 0;
int i = 1;
lcs[0] = 0;
while (i < m) {
if (s.charAt(i) == s.charAt(len)) {
lcs[i++] = ++len;
} else {
if (len == 0) {
lcs[i] = 0;
i++;
} else
len = lcs[len - 1];
}
}
return lcs;
}
static void swap(char[] a, char[] b, int i, int j) {
char temp = a[i];
a[i] = b[j];
b[j] = temp;
}
static void factorial(long[] fact, long[] fact_inv, int n, long mod) {
fact[0] = 1;
for (int i = 1; i < n; i++) {
fact[i] = (i * fact[i - 1]) % mod;
}
for (int i = 0; i < n; i++) {
fact_inv[i] = power(fact[i], mod - 2, mod);// (1/x)%m can be calculated by fermat's little theoram which is
// (x**(m-2))%m when m is prime
}
// (a^(b^c))%m is equal to, let res=(b^c)%(m-1) then (a^res)%m
// https://www.geeksforgeeks.org/find-power-power-mod-prime/?ref=rp
}
static void find(int i, int n, int[] row, int[] col, int[] d1, int[] d2) {
if (i >= n) {
ans++;
return;
}
for (int j = 0; j < n; j++) {
if (col[j] == 0 && d1[i - j + n - 1] == 0 && d2[i + j] == 0) {
col[j] = 1;
d1[i - j + n - 1] = 1;
d2[i + j] = 1;
find(i + 1, n, row, col, d1, d2);
col[j] = 0;
d1[i - j + n - 1] = 0;
d2[i + j] = 0;
}
}
}
static int answer(int l, int r, int[][] dp) {
if (l > r)
return 0;
if (l == r) {
dp[l][r] = 1;
return 1;
}
if (dp[l][r] != -1)
return dp[l][r];
int val = Integer.MIN_VALUE;
int mid = l + (r - l) / 2;
val = 1 + Math.max(answer(l, mid - 1, dp), answer(mid + 1, r, dp));
return dp[l][r] = val;
}
// static long find(String s, int i, int n, long[] dp) {
// pn(i);
// if (i >= n)
// return 1L;
// if (s.charAt(i) == '0')
// return 0;
// if (i == n - 1)
// return 1L;
// if (dp[i] != -1)
// return dp[i];
// if (s.substring(i, i + 2).equals("10") || s.substring(i, i + 2).equals("20"))
// {
// return dp[i] = (find(s, i + 2, n, dp)) % mod;
// }
// if ((s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) - '0' <=
// 6))
// && ((i + 2 < n ? (s.charAt(i + 2) != '0' ? true : false) : (i + 2 == n ? true
// : false)))) {
// return dp[i] = (find(s, i + 1, n, dp) + find(s, i + 2, n, dp)) % mod;
// } else
// return dp[i] = (find(s, i + 1, n, dp)) % mod;
static void print(int[] a) {
for (int i = 0; i < a.length; i++)
p(a[i] + " ");
pn("");
}
static long count(long n) {
long count = 0;
while (n != 0) {
count += n % 10;
n /= 10;
}
return count;
}
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int LcsOfPrefix(String a, String b) {
int i = 0;
int j = 0;
int count = 0;
while (i < a.length() && j < b.length()) {
if (a.charAt(i) == b.charAt(j)) {
j++;
count++;
}
i++;
}
return a.length() + b.length() - 2 * count;
}
static void reverse(int[] a, int n) {
for (int i = 0; i < n / 2; i++) {
int temp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = temp;
}
}
static char get_char(int a) {
return (char) (a + 'A');
}
static int find1(int[] a, int val) {
int ans = -1;
int l = 0;
int r = a.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] <= val) {
l = mid + 1;
ans = mid;
} else
r = mid - 1;
}
return ans;
}
static int find2(int[] a, int val) {
int l = 0;
int r = a.length - 1;
int ans = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] <= val) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
return ans;
}
// static void dfs(List<List<Integer>> arr, int node, int parent, long[] val) {
// p[node] = parent;
// in_time[node] = count;
// flat_tree[count] = val[node];
// subtree_gcd[node] = val[node];
// count++;
// for (int adj : arr.get(node)) {
// if (adj == parent)
// continue;
// dfs(arr, adj, node, val);
// subtree_gcd[node] = gcd(subtree_gcd[adj], subtree_gcd[node]);
// }
// out_time[node] = count;
// flat_tree[count] = val[node];
// count++;
// }
}
|
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
|
c7c2b3b58ba9b27c95d5b27c3a063d1a
|
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 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
|
eebd61df87329f82ba676fa1cdc73b5d
|
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.text.*;
import java.math.*;
import java.util.regex.*;
public class HarHarShambhu{
static Scanner in=new Scanner();
static long systemTime;
static long mod = 1000000007;
//static ArrayList<ArrayList<Integer>> adj;
static int seive[]=new int[1000001];
static long C[][];
public static void main(String[] args) throws Exception{
int z=in.readInt();
for(int test=1;test<=z;test++) {
//setTime();
solve();
//printTime();
//printMemory();
}
}
static void solve() {
int n=in.readInt();
int m=in.readInt();
char c[][]=new char[n][m];
for(int i=0;i<n;i++) {
c[i]=in.readString().toCharArray();
}
int vis[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(vis[i][j]==0&&c[i][j]=='*') {
ArrayList<Integer[]> al=new ArrayList<>();
dfs(i,j,c,vis,n,m,al);
int cnt=al.size();
// for(Integer p[]:al) {
// print(p);
// }
if(cnt!=3) {
print("NO");
return;
}
Collections.sort(al,(u,v)->(u[0]-v[0]==0?u[1]-v[1]:u[0]-v[0]));
int f=1;
int f2=0;
for(int ii=1;ii<3;ii++) {
if(al.get(ii)[0]-al.get(ii-1)[0]==0) {
}
else {
f2=1;
}
}
if(f2==0) {
print("NO");
return;
}
for(int ii=1;ii<3;ii++) {
if(al.get(ii)[1]-al.get(ii-1)[1]==0) {
}
else {
f2=1;
}
}
if(f2==0) {
print("NO");
return;
}
if(f==1) {
if(al.get(0)[0]-al.get(1)[0]==0) {
if(al.get(0)[1]-al.get(1)[1]==-1) {
if(al.get(1)[1]-al.get(2)[1]==0) {
if(al.get(2)[0]-al.get(1)[0]==1) {
f=0;
}
}
}
}
if(al.get(0)[1]-al.get(1)[1]==0) {
if(al.get(0)[0]-al.get(1)[0]==-1) {
if(al.get(1)[0]-al.get(2)[0]==0) {
if(al.get(2)[1]-al.get(1)[1]==1) {
f=0;
}
}
}
}
if(al.get(0)[0]-al.get(1)[0]==0) {
if(al.get(0)[1]-al.get(1)[1]==-1) {
if(al.get(0)[1]-al.get(2)[1]==0) {
if(al.get(2)[0]-al.get(0)[0]==1) {
f=0;
}
}
}
}
}
if(f==1) {
if(al.get(0)[0]-al.get(2)[0]==0) {
if(al.get(2)[1]-al.get(0)[1]==1) {
if(al.get(2)[0]-al.get(1)[0]==0) {
if(al.get(2)[1]-al.get(1)[1]==1) {
f=0;
}
}
}
}
if(al.get(0)[1]-al.get(2)[1]==0) {
if(al.get(2)[0]-al.get(0)[0]==1) {
if(al.get(2)[1]-al.get(1)[1]==1) {
if(al.get(2)[0]-al.get(1)[0]==0) {
f=0;
}
}
}
}
}
if(f==1) {
print("NO");
return;
}
}
}
}
print("YES");
}
static int dfs(int i,int j,char c[][],int vis[][],int n,int m,ArrayList<Integer[]> al) {
vis[i][j]=1;
int cnt=0;
al.add(new Integer[] {i,j});
if(isvalid(i+1,j,c,vis,n,m)) {
cnt=1+dfs(i+1,j,c,vis,n,m,al);
}
if(isvalid(i,j+1,c,vis,n,m)) {
cnt=1+dfs(i,j+1,c,vis,n,m,al);
}
if(isvalid(i-1,j,c,vis,n,m)) {
cnt=1+dfs(i-1,j,c,vis,n,m,al);
}
if(isvalid(i,j-1,c,vis,n,m)) {
cnt=1+dfs(i,j-1,c,vis,n,m,al);
}
if(isvalid(i+1,j+1,c,vis,n,m)) {
cnt=1+dfs(i+1,j+1,c,vis,n,m,al);
}
if(isvalid(i+1,j-1,c,vis,n,m)) {
cnt=1+dfs(i+1,j-1,c,vis,n,m,al);
}
if(isvalid(i-1,j+1,c,vis,n,m)) {
cnt=1+dfs(i-1,j+1,c,vis,n,m,al);
}
if(isvalid(i-1,j-1,c,vis,n,m)) {
cnt=1+dfs(i-1,j-1,c,vis,n,m,al);
}
return cnt;
}
static boolean isvalid(int i,int j,char c[][],int vis[][],int n,int m) {
if(i<0||i>=n||j<0||j>=m) {
return false;
}
if(vis[i][j]==1||c[i][j]!='*') {
return false;
}
return true;
}
static long pow(long n, long m) {
if(m==0)
return 1;
else if(m==1)
return n;
else {
long r=pow(n,m/2);
if(m%2==0)
return (r*r);
else
return (r*r*n);
}
}
static long maxsumsub(ArrayList<Long> al) {
long max=0;
long sum=0;
for(int i=0;i<al.size();i++) {
sum+=al.get(i);
if(sum<0) {
sum=0;
}
max=Math.max(max,sum);
}
return max;
}
static long abs(long a) {
return Math.abs(a);
}
static void ncr(int n, int k){
C= new long[n + 1][k + 1];
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
}
static boolean isPalin(String s) {
int i=0,j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
static int knapsack(int W, int wt[],int val[], int n){
int []dp = new int[W + 1];
for (int i = 1; i < n + 1; i++) {
for (int w = W; w >= 0; w--) {
if (wt[i - 1] <= w) {
dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]);
}
}
}
return dp[W];
}
static void seive() {
Arrays.fill(seive, 1);
seive[0]=0;
seive[1]=0;
for(int i=2;i*i<1000001;i++) {
if(seive[i]==1) {
for(int j=i*i;j<1000001;j+=i) {
if(seive[j]==1) {
seive[j]=0;
}
}
}
}
}
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[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long[] nla1(int n){
long[] arr= new long[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static Integer[] nIa(int n){
Integer[] arr= new Integer[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static Long[] nLa(int n){
Long[] arr= new Long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static void no() {
System.out.println("NO");
}
static void yes() {
System.out.println("YES");
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void setTime() {
systemTime = System.currentTimeMillis();
}
static void printTime() {
System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
}
static void printMemory() {
System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
}
|
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
|
4f06795492200ca1c4d4be9a2bfd330d
|
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 = 1010;
static int n, m;
static int[][] g;
static int[] p;
static int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
static boolean ans = true;
static int get(int x, int y) {
return m * x + y;
}
static int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
static int dfs(int x, int y) {
g[x][y] = 2;
int cnt = 1;
for (int i = 0; i < 4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || g[nx][ny] != 1) continue;
int pa = find(get(x, y));
int pb = find(get(nx, ny));
p[pa] = pb;
cnt += dfs(nx, ny);
}
return cnt;
}
public static void main(String[] args) throws IOException {
FastScanner f = new FastScanner();
PrintWriter w = new PrintWriter(System.out);
int T = f.nextInt();
while (T-- > 0) {
ans = true;
n = f.nextInt();
m = f.nextInt();
p = new int[n * m];
g = new int[n][m];
for (int i = 0; i < n * m; i++) p[i] = i;
for (int i = 0; i < n; i++) {
String str = f.nextString();
for (int j = 0; j < m; j++) {
g[i][j] = (str.charAt(j) == '*' ? 1 : 0);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == 1) {
int cnt = dfs(i, j);
if (cnt != 3) ans = false;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == 2) {
if (i > 0 && i < n - 1 && g[i - 1][j] == 2 && g[i + 1][j] == 2) ans = false;
if (j > 0 && j < m - 1 && g[i][j - 1] == 2 && g[i][j + 1] == 2) ans = false;
for (int k = 4; k < 8; k++) {
int nx = i + dir[k][0];
int ny = j + dir[k][1];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || g[nx][ny] == 0) continue;
if (find(get(nx, ny)) != find(get(i, j))) ans = false;
}
}
}
}
if (ans) w.println("YES");
else w.println("NO");
}
w.flush();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
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++];
}
}
}
|
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
|
9656de8db438ff738c23f38ac56b40ef
|
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 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(readLine());
int[][] dirs = {{-1,0},{-1,1},{-1,-1},{0,1},{0,-1},{1,0},{1,-1},{1,1}};
while(n-- > 0){
String[] s1 = readLine().split(" ");
int r = Integer.parseInt(s1[0]),c = Integer.parseInt(s1[1]);
String[] s = new String[r];
for(int i = 0 ; i < r;i++){
s[i] = readLine();
}
boolean f= true;
for(int i =0;i<r&&f;i++){
String curr = s[i];
for(int j = 0 ; j < c;j++){
if(curr.charAt(j) =='*'){
int ans = 1;
boolean g = false;
if(i-1>=0&&j-1>=0&&s[i-1].charAt(j)=='*'&&s[i].charAt(j-1)=='*'){
g = true;
}
if(i-1>=0&&j+1<c&&s[i-1].charAt(j)=='*'&&s[i].charAt(j+1)=='*'){
g = true;
}
if(i+1<r&&j-1>=0&&s[i+1].charAt(j)=='*'&&s[i].charAt(j-1)=='*'){
g = true;
}
if(i+1<r&&j+1<c&&s[i+1].charAt(j)=='*'&&s[i].charAt(j+1)=='*'){
g = true;
}
if(i-1>=0&&j-1>=0&&s[i-1].charAt(j)=='*'&&s[i-1].charAt(j-1)=='*'){
g = true;
}
if(i-1>=0&&j+1<c&&s[i-1].charAt(j)=='*'&&s[i-1].charAt(j+1)=='*'){
g = true;
}
if(i+1<r&&j-1>=0&&s[i+1].charAt(j)=='*'&&s[i+1].charAt(j-1)=='*'){
g = true;
}
if(i+1<r&&j+1<c&&s[i+1].charAt(j)=='*'&&s[i+1].charAt(j+1)=='*'){
g = true;
}
if(j-1>=0&&i-1>=0&&s[i].charAt(j-1)=='*'&&s[i-1].charAt(j-1)=='*'){
g = true;
}
if(j-1>=0&&i+1<r&&s[i].charAt(j-1)=='*'&&s[i+1].charAt(j-1)=='*'){
g = true;
}
if(j+1<c&&i-1>=0&&s[i].charAt(j+1)=='*'&&s[i-1].charAt(j+1)=='*'){
g = true;
}
if(j+1<c&&i+1<r&&s[i].charAt(j+1)=='*'&&s[i+1].charAt(j+1)=='*'){
g = true;
}
for(int[] a : dirs){
int ni = i+a[0],ny = j+a[1];
if(ni <0||ny<0||ni>=r||ny>=c){
continue;
}
if(s[ni].charAt(ny)=='*'){
ans++;
}
}
if(ans!=3||!g){
f = false;
}
}
}
}
if(f){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
public static String readLine() 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 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
|
7bec8e156f0620f1b000198240c8c161
|
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.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class f {
static char[][] arr;
static final int[] dx = {-1, 0, 1, 0};
static final int[] dy = {0, 1, 0, -1};
static final int[] dx2 = {-1, -1, 0, 1, 1, 1, 0, -1};
static final int[] dy2 = {0, 1, 1, 1, 0, -1, -1, -1};
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int numCases = sc.nextInt();
outer : for (int c = 0; c < numCases; c++) {
int numR = sc.nextInt();
int numC = sc.nextInt();
arr = new char[numR][numC];
for (int i = 0; i < numR; i++) {
arr[i] = sc.next().toCharArray();
}
DSU dsu = new DSU(numR * numC);
for (int i = 0; i < numR; i++) {
for (int j = 0; j < numC; j++) {
for (int d = 0; d < 4; d++) {
if (inBounds(i + dx[d], j + dy[d]) && arr[i][j] == '*' && arr[i + dx[d]][j + dy[d]] == '*') {
int a = i * numC + j;
int b = (i + dx[d]) * numC + (j + dy[d]);
dsu.union(a, b);
}
}
}
}
for (int i = 0; i < numR; i++) {
for (int j = 0; j < numC; j++) {
int a = i * numC + j;
if (arr[i][j] == '*' && dsu.size[dsu.find(a)] != 3) {
System.out.println("NO");
continue outer;
}
}
}
for (int i = 0; i < numR; i++) {
for (int j = 0; j < numC; j++) {
for (int d = 0; d < 8; d++) {
if (inBounds(i + dx2[d], j + dy2[d]) && arr[i][j] == '*' && arr[i + dx2[d]][j + dy2[d]] == '*') {
int a = i * numC + j;
int b = (i + dx2[d]) * numC + (j + dy2[d]);
if (dsu.find(a) != dsu.find(b))
{
System.out.println("NO");
continue outer;
}
}
}
}
}
for (int i = 0; i < numR; i++) {
for (int j = 0; j < numC; j++) {
for (int d = 0; d < 4; d++) {
// check for lines
if (inBounds(i + dx[d], j + dy[d]) && inBounds(i + 2 * dx[d], j + 2 * dy[d])) {
if (arr[i][j] == '*' && arr[i + dx[d]][j + dy[d]] == '*' && arr[i + 2 * dx[d]][j + 2 * dy[d]] == '*') {
System.out.println("NO");
continue outer;
}
}
}
}
}
System.out.println("YES");
}
}
public static boolean inBounds(int r, int c) {
return r >= 0 && c >= 0 && r < arr.length && c < arr[0].length;
}
}
class DSU
{
int[] parent;
int[] rank;
int[] size;
public DSU(int n) {
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
rank = new int[n];
}
public void union(int x, int y) {
int xRep = find(x);
int yRep = find(y);
if (xRep == yRep)
return;
if (rank[xRep] < rank[yRep]) {
parent[xRep] = yRep;
size[yRep] += size[xRep];
}
else if(rank[yRep] < rank[xRep]) {
parent[yRep] = xRep;
size[xRep] += size[yRep];
}
else
{
parent[yRep] = xRep;
rank[xRep]++;
size[xRep] += size[yRep];
}
}
public int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
}
|
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
|
4774f3bd471f144002be6822d66b8c6e
|
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 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 {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}}
public static void main(String[] args) throws IOException{
// try {
// System.setIn(new FileInputStream("input.txt"));
// System.setOut(new PrintStream(new FileOutputStream("output.txt")));
// } catch (Exception e) {
// System.err.println("Error");
// }
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8")));
int t = in.nextInt();
while(t-- > 0) {
int m = in.nextInt();
int n = in.nextInt();
char[][] mat = new char[m][n];
for(int i = 0; i < m; i++) {
String s = in.nextLine();
char[] str = s.toCharArray();
mat[i] = str;
}
System.out.println(solve(mat, m, n));
}
}
static int[][] DIRS = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
static String solve(char[][] mat, int m, int n) {
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(mat[i][j] != '*') continue;
if(i-1 >= 0 && j+1 < n && mat[i-1][j] == '*' && mat[i][j+1] == '*') {
mat[i-1][j] = '.';
mat[i][j+1] = '.';
mat[i][j] = '.';
if(check(mat, i-1, j) || check(mat, i, j+1) || check(mat, i, j)) return "NO";
}
else if(i-1 >= 0 && j-1 >= 0 && mat[i-1][j] == '*' && mat[i][j-1] == '*') {
mat[i-1][j] = '.';
mat[i][j-1] = '.';
mat[i][j] = '.';
if(check(mat, i-1, j) || check(mat, i, j-1) || check(mat, i, j)) return "NO";
}
else if(i+1 < m && j+1 < n && mat[i+1][j] == '*' && mat[i][j+1] == '*') {
mat[i+1][j] = '.';
mat[i][j+1] = '.';
mat[i][j] = '.';
if(check(mat, i+1, j) || check(mat, i, j+1) || check(mat, i, j)) return "NO";
}
else if(i+1 < m && j-1 >= 0 && mat[i+1][j] == '*' && mat[i][j-1] == '*') {
mat[i+1][j] = '.';
mat[i][j-1] = '.';
mat[i][j] = '.';
if(check(mat, i+1, j) || check(mat, i, j-1) || check(mat, i, j)) return "NO";
}
}
}
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
// System.out.print(mat[i][j] + " ");
if(mat[i][j] == '*') return "NO";
}
// System.out.println();
}
return "YES";
}
static boolean check(char[][] mat, int r, int c) {
for(int[] d : DIRS) {
int row = r + d[0];
int col = c + d[1];
if(row >= 0 && col >= 0 && row < mat.length && col < mat[0].length && mat[row][col] == '*') return true;
}
return false;
}
}
|
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
|
c0c517873de097298f463a483287614e
|
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 C{
static boolean solve(char c[][]){
int n=c.length;
int m=c[0].length;
boolean vis[][]=new boolean[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!vis[i][j] && c[i][j]=='*'){
if(check(i,j,c,vis)==false) return false;
}
}
}
return true;
}
static boolean check(int x,int y,char c[][],boolean vis[][]){
int n=c.length;
int m=vis[0].length;
vis[x][y]=true;
if(y+1<m && x+1<n){
if(c[x+1][y]=='*' && c[x][y+1]=='*'){
vis[x+1][y]=true;
vis[x][y+1]=true;
try{if(c[x-1][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y+1]=='*') return false;}catch(Exception e){}
return true;
}
else if(c[x][y+1]=='*' && c[x+1][y+1]=='*'){
vis[x][y+1]=true;
vis[x+1][y+1]=true;
try{if(c[x-1][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y+1]=='*') return false;}catch(Exception e){}
return true;
}
else if(c[x+1][y]=='*' && c[x+1][y+1]=='*'){
vis[x+1][y]=true;
vis[x+1][y+1]=true;
try{if(c[x+2][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y+2]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y-1]=='*') return false;}catch(Exception e){}
return true;
}
}
if(x+1<n && y-1>=0){
if(c[x+1][y]=='*' && c[x+1][y-1]=='*'){
vis[x+1][y]=true;
vis[x+1][y-1]=true;
try{if(c[x+2][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x-1][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x][y-2]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x+1][y-2]=='*') return false;}catch(Exception e){}
try{if(c[x][y-1]=='*') return false;}catch(Exception e){}
try{if(c[x][y+1]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y-2]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y]=='*') return false;}catch(Exception e){}
try{if(c[x+2][y+1]=='*') return false;}catch(Exception e){}
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0){
int n=s.nextInt();
int m=s.nextInt();
char c[][]=new char[n][m];
for(int i=0;i<n;i++)
c[i]=s.next().toCharArray();
boolean ans=solve(c);
System.out.println(ans?"Yes": "NO");
}
}
}
|
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
|
3147904e5b82628c7f84c0e9becb0bcc
|
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.*;
public class B {
static boolean[][] checked;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
outer:
for (int oo = 0; oo < t; oo++) {
int n = sc.nextInt();
int m = sc.nextInt();
char[][] arr = new char[n][];
for (int i = 0; i < n; i++) {
arr[i] = sc.next().toCharArray();
}
checked = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (checked[i][j] || arr[i][j] == '.') continue;
if (j < m - 1 && arr[i][j + 1] == '*') {
if (i == n - 1) {
System.out.println("NO");
continue outer;
}
if (arr[i + 1][j] == '*') {
if (arr[i + 1][j + 1] == '*' || !check(arr, i, j, i + 2, j + 2)) {
System.out.println("NO");
continue outer;
}
} else if (arr[i + 1][j + 1] == '*') {
if (arr[i + 1][j] == '*' || !check(arr, i, j, i + 2, j - 1)) {
System.out.println("NO");
continue outer;
}
} else {
System.out.println("NO");
continue outer;
}
checked[i][j] = true;
checked[i + 1][j] = true;
checked[i][j + 1] = true;
checked[i + 1][j + 1] = true;
} else if (i + 1 < n && arr[i + 1][j] == '*') {
if (j + 1 < m && arr[i + 1][j + 1] == '*') {
if (arr[i][j + 1] == '*' || !check(arr, i, j, i - 1, j + 2)) {
System.out.println("NO");
continue outer;
}
checked[i][j] = true;
checked[i + 1][j] = true;
checked[i][j + 1] = true;
checked[i + 1][j + 1] = true;
} else if (j > 0 && arr[i + 1][j - 1] == '*') {
if (arr[i][j - 1] == '*' || !check(arr, i, j - 1, i - 1, j - 2)) {
System.out.println("NO");
continue outer;
}
checked[i][j] = true;
checked[i + 1][j] = true;
checked[i][j - 1] = true;
checked[i + 1][j - 1] = true;
} else {
System.out.println("NO");
continue outer;
}
} else {
System.out.println("NO");
continue outer;
}
}
}
System.out.println("YES");
}
}
public static boolean check(char[][] arr, int i, int j, int skipI, int skipJ) {
if (j > 0) {
for (int k = Math.max(i - 1, 0); k < Math.min(arr.length, i + 3); k++) {
if (k == skipI && j - 1 == skipJ) continue;
if (arr[k][j - 1] == '*') return false;
checked[k][j - 1] = true;
}
}
if (i > 0) {
for (int k = Math.max(j - 1, 0); k < Math.min(arr[0].length, j + 3); k++) {
if (i - 1 == skipI && k == skipJ) continue;
if (arr[i - 1][k] == '*') return false;
checked[i - 1][k] = true;
}
}
if (i + 2 < arr.length) {
for (int k = Math.max(j - 1, 0); k < Math.min(arr[0].length, j + 3); k++) {
if (i + 2 == skipI && k == skipJ) continue;
if (arr[i + 2][k] == '*') return false;
checked[i + 2][k] = true;
}
}
if (j + 2 < arr[0].length) {
for (int k = Math.max(i - 1, 0); k < Math.min(arr.length, i + 3); k++) {
if (k == skipI && j + 2 == skipJ) continue;
if (arr[k][j + 2] == '*') return false;
checked[k][j + 2] = true;
}
}
return true;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["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
|
687d514c7f7eef0074db625ac14f9516
|
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 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int t = Integer.parseInt(br.readLine());
while(t-->0){
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
char board[][] = new char[n][m];
for(int i = 0; i<n; i++){
board[i] = br.readLine().toCharArray();
}
boolean visited[][] = new boolean[n][m];
boolean flag = true;
loop:
for(int i = 0; i<n; i++ ){
for(int j = 0; j<m; j++){
if(!visited[i][j] && board[i][j] == '*'){
if(isWrong(board, visited, i, j)){
flag = false;
break loop;
}
}
}
}
bw.write(flag ? "YES\n" : "NO\n");
}
bw.flush();
}
static int caseX[][] = {{1,1},{1,1},{0,1},{1,0}};
static int caseY[][] = {{0,1},{-1,0},{1,1},{0,1}};
static boolean isWrong(char[][] board, boolean visited[][], int x, int y){
int cnt = bfs(board, visited, x, y);
if(cnt != 3) return true; // 틀렸습니다!
for(int i = 0; i<4; i++){
int count = 0;
for(int j = 0; j<2; j++) {
int nx = x + caseX[i][j];
int ny = y + caseY[i][j];
if(nx < 0 || ny < 0 || nx >= board.length || ny >= board[0].length) continue;
if(board[nx][ny] == '*') count++;
}
if(count == 2) return false;
}
return true;
}
static int dx[] = {0,1,0,-1,1,1,-1,-1};
static int dy[] = {1,0,-1,0,1,-1,1,-1};
static int bfs(char[][] board, boolean visited[][], int x, int y){
int ret = 1;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{x,y});
visited[x][y] = true;
while(!q.isEmpty()){
int[] now = q.poll();
for(int i = 0; i < 8; i++){
int nx = now[0] + dx[i];
int ny = now[1] + dy[i];
if(nx < 0 || ny < 0 || nx >= board.length || ny >= board[0].length) continue;
if(visited[nx][ny] || board[nx][ny] != '*') continue;
visited[nx][ny] = true;
ret++;
q.add(new int[]{nx, ny});
}
}
return ret;
}
}
|
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
|
df2528cec792c1472fe78ca13c366f62
|
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.*;
/* Name of the class has to be "Main" only if the class is public. */
public class q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.next());
while(t-->0){
int n = Integer.parseInt(sc.next());
int m = Integer.parseInt(sc.next());
char[][] arr = new char[n][m];
for(int i=0;i<n;i++){
String temp = sc.next();
arr[i] = temp.toCharArray();
}
boolean[][] vis = new boolean[n][m];
boolean flag = true;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!vis[i][j] && arr[i][j]=='*'){
HashSet<Integer> row = new HashSet<>();
HashSet<Integer> col = new HashSet<>();
int[] size = {0};
isL(arr,vis,i,j,row,col,size);
if(size[0]!=3 || row.size()!=2 || col.size()!=2){
flag= false;
break;
}
}
}
if(!flag){
break;
}
}
if(!flag){
System.out.println("NO");
}else{
System.out.println("YES");
}
}
}
public static void isL(char[][] arr,boolean[][] vis, int i,int j,HashSet<Integer> row,HashSet<Integer> col,int[] size){
if(i<0 || i>=arr.length || j<0 || j>=arr[0].length || vis[i][j] || arr[i][j]!='*'){
return;
}
vis[i][j] = true;
size[0]++;
row.add(i);
col.add(j);
isL(arr, vis, i+1, j, row,col, size);
isL(arr, vis, i-1, j, row,col, size);
isL(arr, vis, i, j+1, row,col, size);
isL(arr, vis, i, j-1, row,col, size);
isL(arr, vis, i-1, j-1, row,col, size);
isL(arr, vis, i+1, j-1, row,col, size);
isL(arr, vis, i+1, j+1, row,col, size);
isL(arr, vis, i-1, j+1, row,col, size);
}
public static void getSize(char[][] arr,boolean[][] vis, int i,int j,int[] size){
if(i<0 || i>=arr.length || j<0 || j>=arr[0].length || vis[i][j] || arr[i][j]!='*'){
return;
}
vis[i][j] = true;
size[0]++;
getSize(arr, vis, i+1, j, size);
getSize(arr, vis, i-1, j, size);
getSize(arr, vis, i, j+1,size);
getSize(arr, vis, i, j-1,size);
getSize(arr, vis, i-1, j-1,size);
getSize(arr, vis, i+1, j-1,size);
getSize(arr, vis, i+1, j+1,size);
getSize(arr, vis, i-1, j+1,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 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
|
e483015c30522e87b791a046ebacc4eb
|
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.*;
public class Main
{
static class FastScanner// 用于快速读入大量数据
{
static BufferedReader br;
static StringTokenizer st;
public FastScanner(InputStream in)
{
br = new BufferedReader(new InputStreamReader(in), 16384);
eat("");
}
public static void eat(String s)
{
st = new StringTokenizer(s);
}
public static String nextLine()
{
try
{
return br.readLine();
} catch (IOException e)
{
return null;
}
}
public static boolean hasNext()
{
while (!st.hasMoreTokens())
{
String s = nextLine();
if (s == null) return false;
eat(s);
}
return true;
}
public static String next()
{
hasNext();
return st.nextToken();
}
public static int nextInt()
{
return Integer.parseInt(next());
}
public static long nextLong()
{
return Long.parseLong(next());
}
public static double nextDouble()
{
return Double.parseDouble(next());
}
public BigInteger nextBigInteger()
{
return new BigInteger(next());
}
public BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}
static FastScanner in = new FastScanner(System.in);// 快读
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));// 快速输出
// static int d[] ={ 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30};
// static int[] dx = { -1, 0, 1, 0 };
// static int[] dy = { 0, 1, 0, -1 };
static long INF = (long)Math.pow(10, 15);
// static int INF = 0x3f3f3f3f;
// static int mod = (int)Math.pow(10, 9) + 7;
static long mod = 114514919810L;
static int[] a = { 1, 0, 0, 0, 0, 0, 1, 0, 2, 1 };
public static void main(String[] args)
{
// Scanner in = new Scanner(System.in);
int t=in.nextInt();
while(t-->0)
{
int n=in.nextInt();
int m=in.nextInt();
boolean[][] is=new boolean[1010][1010];
char[][] g=new char[100][100];
int[][] step = new int[1010][1010];
Queue<int []> q=new LinkedList<>();
for(int i=0;i<n;i++)
{
g[i]=in.next().toCharArray();
}
boolean f=true;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(g[i][j]=='*')
{
int cnt=1;
int[] dx={0,1,0,-1,-1,-1,1,1};
int[] dy={1,0,-1,0,-1,1,1,-1};
int h=i,z=j;
for(int k=0;k<8;k++)
{
int x=dx[k]+i;
int y=dy[k]+j;
if(x>=0 && x<n && y>=0 && y<m)
{
if(g[x][y]=='*')
{
cnt++;
h=h+x;
z=z+y;
}
}
}
if(cnt==3 && h%3!=0 && z%3!=0)
{
}else
{
f=false;
}
}
}
}
System.out.println(f?"YES" :"NO");
}
}
}
class Node
{
double a;
String s;
public Node(){};
public Node(double a, String s)
{
this.a = a;
this.s=s;
}
}
|
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
|
832338f9683bc8dd9d44bcced3716630
|
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.*;
public class CodeForce {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int caseNum = scanner.nextInt();
while(caseNum > 0){
int row = scanner.nextInt();
int col = scanner.nextInt();
scanner.nextLine();
char[][] grid = new char[row][col];
for(int i=0;i<row;i++){
String t = scanner.nextLine();
for(int j=0;j<col;j++){
grid[i][j] = t.charAt(j);
}
}
// 有*的地方都是L,并且没有和另外一个L挨着
boolean is = true;
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(grid[i][j] == '*'){
// 意思是什么,其实就是八邻域dfs只能有3个
// 也并不是
grid[i][j] = '.';
List<int[]> x = new ArrayList<>();
x.add(new int[]{i,j});
int c = dfsHelper(grid, i, j, x);
if(c != 3){
is = false;
break;
}else if(c == 3){
// 检查x
for(int c1=0; c1<3;c1++){
for(int c2=c1+1; c2<3;c2++){
int i1 = x.get(c1)[0];
int j1 = x.get(c1)[1];
int i2 = x.get(c2)[0];
int j2 = x.get(c2)[1];
if((Math.abs(i1 -i2) == 0 && Math.abs(j1-j2) == 1)
|| (Math.abs(i1 -i2) == 1 && Math.abs(j1-j2) == 0)
|| (Math.abs(i1 -i2) == 1 && Math.abs(j1-j2) == 1)){
continue;
}else{
is = false;
break;
}
}
}
}
}
}
}
if(is){
System.out.println("YES");
}else{
System.out.println("NO");
}
caseNum --;
}
}
private static int dfsHelper(char[][] gird, int i, int j, List<int[]> x){
int[][] dirs = {{1, 0}, {1, -1}, {1, 1}, {-1, 0}, {-1, 1}, {-1, -1}, {0, 1}, {0, -1},};
int count = 1;
for(int idx=0; idx<8 ;idx++){
int newI = i + dirs[idx][0];
int newJ = j + dirs[idx][1];
if(newI >= 0 && newI < gird.length && newJ >=0 && newJ < gird[0].length && gird[newI][newJ] == '*'){
gird[newI][newJ] = '.';
x.add(new int[]{newI, newJ});
count += dfsHelper(gird, newI, newJ, x);
}
}
return count;
}
}
|
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.