solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast", "no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int exp(long long int x, long long int y, long long int mod) {
long long int res = 1;
x = x % mod;
while (y > 0) {
if (y & 1) res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
long long int modinverse(long long int x, long long int mod) {
return exp(x, mod - 2, mod);
}
using namespace std;
const long long int inf = 0x3f3f3f3f3f3f3f3fll;
void solve() {
long long int n;
cin >> n;
string s[n];
for (long long int i = 0; i < n; i++) cin >> s[i];
if (s[0][1] == '0' && s[1][0] == '0') {
if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 2 << '\n';
cout << n << " " << n - 1 << '\n';
cout << n - 1 << " " << n << '\n';
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '1') {
cout << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '0') {
cout << 1 << '\n';
cout << n - 1 << " " << n << '\n';
} else
cout << 0 << '\n';
} else if (s[0][1] == '1' && s[1][0] == '0') {
if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '1') {
cout << 2 << '\n';
cout << 2 << " " << 1 << '\n';
cout << n - 1 << " " << n << '\n';
} else if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '0') {
cout << 2 << '\n';
cout << 2 << " " << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
}
} else if (s[0][1] == '0' && s[1][0] == '1') {
if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '1') {
cout << 2 << '\n';
cout << 2 << " " << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '0') {
cout << 2 << '\n';
cout << 2 << " " << 1 << '\n';
cout << n - 1 << " " << n << '\n';
} else {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
}
} else {
if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0')
cout << 0 << '\n';
else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '1') {
cout << 1 << '\n';
cout << n - 1 << " " << n << '\n';
} else if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '0') {
cout << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << 2 << '\n';
cout << n << " " << n - 1 << '\n';
cout << n - 1 << " " << n << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t;
cin >> t;
while (t--) solve();
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
void testCase() {
long long n;
cin >> n;
vector<string> grid(n);
vector<pair<long long, long long>> ans;
for (string &s : grid) cin >> s;
char t1 = grid[0][1];
char t2 = grid[1][0];
char b1 = grid[n - 1][n - 2];
char b2 = grid[n - 2][n - 1];
if (t1 == t2) {
if (b1 == t2) ans.push_back({n - 1, n - 2});
if (b2 == t2) ans.push_back({n - 2, n - 1});
} else if (b1 == b2) {
if (b1 == t1) ans.push_back({0, 1});
if (b1 == t2) ans.push_back({1, 0});
} else {
if (t1 == '1') ans.push_back({0, 1});
if (t2 == '1') ans.push_back({1, 0});
if (b1 == '0') ans.push_back({n - 1, n - 2});
if (b2 == '0') ans.push_back({n - 2, n - 1});
}
cout << (long long)(ans.size()) << endl;
for (auto &it : ans) cout << it.first + 1 << " " << it.second + 1 << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cin.tie(nullptr);
long long t_c = 1;
cin >> t_c;
while (t_c--) testCase();
}
| 8 |
CPP
|
for j in range(int(input())):
n=int(input())
a=[list(input()) for i in range(n)]
t=a[0][1]
r=a[1][0]
w=a[-1][-2]
y=a[-2][-1]
d=[]
if t==r=='1':
if w=='1':
d.append([n,n-1])
if y=='1':
d.append([n-1,n])
elif t==r=='0':
if w=='0':
d.append([n,n-1])
if y=='0':
d.append([n-1,n])
elif w==y=='1':
if t=='1':
d.append([1,2])
if r=='1':
d.append([2,1])
elif w==y=='0':
if t=='0':
d.append([1,2])
if r=='0':
d.append([2,1])
else:
if w=='1':
d.append([n,n-1])
if y=='1':
d.append([n-1,n])
if r=='0':
d.append([2,1])
if t=='0':
d.append([1,2])
print(len(d))
for x in d:
print(*x)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n=int(input())
l=[]
for i in range(n):
s=str(input())
l.append(s)
a=int(l[0][1])
b=int(l[1][0])
c=int(l[n-1][n-2])
d=int(l[n-2][n-1])
if(a==0 and b==0):
if(c==1 and d==1):
print(0)
elif(c==1 and d==0):
print(1)
print(n-1,n)
elif(c==0 and d==1):
print(1)
print(n,n-1)
elif(c==0 and d==0):
print(2)
print(1,2)
print(2,1)
elif(a==1 and b==0):
if(c==1 and d==1):
print(1)
print(1,2)
elif(c==1 and d==0):
print(2)
print(1,2)
print(n-1,n)
elif(c==0 and d==1):
print(2)
print(1,2)
print(n,n-1)
elif(c==0 and d==0):
print(1)
print(2,1)
elif(a==0 and b==1):
if(c==0 and d==0):
print(1)
print(1,2)
elif(c==1 and d==0):
print(2)
print(1,2)
print(n,n-1)
elif(c==0 and d==1):
print(2)
print(1,2)
print(n-1,n)
elif(c==1 and d==1):
print(1)
print(2,1)
elif(a==1 and b==1):
if(c==0 and d==0):
print(0)
elif(c==1 and d==0):
print(1)
print(n,n-1)
elif(c==0 and d==1):
print(1)
print(n-1,n)
elif(c==1 and d==1):
print(2)
print(n,n-1)
print(n-1,n)
| 8 |
PYTHON3
|
for _ in range(int(input())):
ls =[]
n = int(input())
for i in range(n):
ls.append(input())
a = ls[0][1]; b = ls[1][0]; c = ls[n-2][n-1]; d= ls[n-1][n-2]
ans =[]
f=1
if a==b :
if a==c:
ans.append([n-1 , n])
if a==d:
ans.append([n, n-1])
print(len(ans))
for i in ans:
print(*i)
elif c==d:
if a==c:
ans.append([1, 2])
if c==b:
ans.append([2 , 1])
print(len(ans))
for i in ans:
print(*i)
else:
if a==c and b==d:
print(2)
print(1, 2)
print(n , n- 1)
else:
print(2)
print(2, 1)
print(n , n-1)
| 8 |
PYTHON3
|
# ===============================================================================================
# importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
# If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
# If the element is already present in the list,
# the right most position where element has to be inserted is returned
# ==============================================================================================
# fast I/O region
BUFSIZE = 8192
from sys import stderr
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
# ===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
print(stack)
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
# ===============================================================================================
# some shortcuts
mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input
def out(var): sys.stdout.write(str(var)) # for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
def nextline(): out("\n") # as stdout.write always print sring.
def testcase(t):
for p in range(t):
solve()
def pow(x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0):
return 0
while (y > 0):
if ((y & 1) == 1): # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
from functools import reduce
def factors(n):
return set(reduce(list.__add__,
([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
def gcd(a, b):
if a == b: return a
while b > 0: a, b = b, a % b
return a
# discrete binary search
# minimise:
# def search():
# l = 0
# r = 10 ** 15
#
# for i in range(200):
# if isvalid(l):
# return l
# if l == r:
# return l
# m = (l + r) // 2
# if isvalid(m) and not isvalid(m - 1):
# return m
# if isvalid(m):
# r = m + 1
# else:
# l = m
# return m
# maximise:
# def search():
# l = 0
# r = 10 ** 15
#
# for i in range(200):
# # print(l,r)
# if isvalid(r):
# return r
# if l == r:
# return l
# m = (l + r) // 2
# if isvalid(m) and not isvalid(m + 1):
# return m
# if isvalid(m):
# l = m
# else:
# r = m - 1
# return m
##to find factorial and ncr
# N=100000
# mod = 10**9 +7
# fac = [1, 1]
# finv = [1, 1]
# inv = [0, 1]
#
# for i in range(2, N + 1):
# fac.append((fac[-1] * i) % mod)
# inv.append(mod - (inv[mod % i] * (mod // i) % mod))
# finv.append(finv[-1] * inv[-1] % mod)
#
#
# def comb(n, r):
# if n < r:
# return 0
# else:
# return fac[n] * (finv[r] * finv[n - r] % mod) % mod
##############Find sum of product of subsets of size k in a array
# ar=[0,1,2,3]
# k=3
# n=len(ar)-1
# dp=[0]*(n+1)
# dp[0]=1
# for pos in range(1,n+1):
# dp[pos]=0
# l=max(1,k+pos-n-1)
# for j in range(min(pos,k),l-1,-1):
# dp[j]=dp[j]+ar[pos]*dp[j-1]
# print(dp[k])
def prefix_sum(ar): # [1,2,3,4]->[1,3,6,10]
return list(accumulate(ar))
def suffix_sum(ar): # [1,2,3,4]->[10,9,7,4]
return list(accumulate(ar[::-1]))[::-1]
def N():
return int(inp())
# =========================================================================================
from collections import defaultdict
def numberOfSetBits(i):
i = i - ((i >> 1) & 0x55555555)
i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24
def solve():
n=N()
ar=[inp() for _ in range(n)]
c=0
ans=[]
if ar[0][1]==ar[1][0]:
t=ar[0][1]
if ar[n-1][n-2]==t:
c+=1
ans.append((n,n-1))
if ar[n-2][n-1]==t:
c+=1
ans.append((n-1,n))
print(c)
for i in ans:
print(*i)
return
if ar[n-1][n-2]==ar[n-2][n-1]:
t=ar[n-1][n-2]
if ar[0][1]==t:
c+=1
ans.append((1,2))
if ar[1][0]==t:
c+=1
ans.append((2,1))
print(c)
for i in ans:
print(*i)
return
if ar[1][0]!="0":
c+=1
ans.append((2,1))
if ar[0][1]!="0":
c+=1
ans.append((1,2))
if ar[n-1][n-2]!="1":
c+=1
ans.append((n,n-1))
if ar[n-2][n-1]!="1":
c+=1
ans.append((n-1,n))
print(c)
for i in ans:
print(*i)
return
# solve()
testcase(int(inp()))
| 8 |
PYTHON3
|
import sys
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
A = []
for _ in range(n):
A.append(sys.stdin.readline().strip())
a = A[0][1]
b = A[1][0]
c = A[-1][-2]
d = A[-2][-1]
cells = []
if a == b:
if c == a:
cells.append((n, n - 1))
if d == a:
cells.append((n - 1, n))
elif c == d:
if a == c:
cells.append((1, 2))
else:
cells.append((2, 1))
else:
cells.append((1, 2))
if b == c:
cells.append((n, n - 1))
else:
cells.append((n - 1, n))
print(len(cells))
for cell in cells:
print(cell[0], cell[1])
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
grid = []
count = 0
ans = []
for i in range(n):
grid.append(list(input()))
if grid[0][1]=='0' and grid[1][0]=='0':
if grid[n-2][n-1]!='1':
count += 1
ans.append([n-1,n])
if grid[n-1][n-2]!='1':
count +=1
ans.append([n,n-1])
elif grid[0][1]=='1' and grid[1][0]=='1':
if grid[n-2][n-1]!='0':
count += 1
ans.append([n-1,n])
if grid[n-1][n-2]!='0':
count +=1
ans.append([n,n-1])
elif grid[n-2][n-1]=='1' and grid[n-1][n-2]=='1':
if grid[0][1]!='0':
count += 1
ans.append([1,2])
if grid[1][0]!='0':
count += 1
ans.append([2,1])
elif grid[n-2][n-1]=='0' and grid[n-1][n-2]=='0':
if grid[0][1]!='1':
count += 1
ans.append([1,2])
if grid[1][0]!='1':
count += 1
ans.append([2,1])
elif grid[1][0]=='0' and grid[0][1]=='1' and grid[n-2][n-1]=='1' and grid[n-1][n-2]=='0':
count = 2
ans.append([1,2])
ans.append([n,n-1])
elif grid[1][0]=='0' and grid[0][1]=='1' and grid[n-2][n-1]=='0' and grid[n-1][n-2]=='1':
count = 2
ans.append([1,2])
ans.append([n-1,n])
elif grid[1][0]=='1' and grid[0][1]=='0' and grid[n-2][n-1]=='0' and grid[n-1][n-2]=='1':
count = 2
ans.append([2,1])
ans.append([n-1,n])
elif grid[1][0]=='1' and grid[0][1]=='0' and grid[n-2][n-1]=='1' and grid[n-1][n-2]=='0':
count = 2
ans.append([2,1])
ans.append([n,n-1])
if count:
print(count)
for i in ans:
print(*i)
else:
print(count)
| 8 |
PYTHON3
|
I=input
exec(int(I())*"n=int(I());a=[[*map(ord,I())]for _ in[0]*n];a=a[0][1],a[1][0],a[-2][-1]^1,a[-1][-2]^1;b=*map(a.count,a),;m=b[0]>3or a[b.index(min(b))];r=[y for x,y in zip(a,('1 2','2 1',f'{n-1} {n}',f'{n} {n-1}'))if x==m];print(len(r),*r);")
| 8 |
PYTHON3
|
import sys
from itertools import permutations
from itertools import combinations
from itertools import combinations_with_replacement
#sys.stdin = open('/Users/pranjalkandhari/Desktop/Template/input.txt', 'r')
for _ in range( int(input()) ):
n = int(input())
a = ''
b = ''
c = ''
d = ''
for i in range(0,n):
li = list(input())
if(i == 0):
a = li[1]
if(i == 1):
b = li[0]
if(i == n-2):
c = li[n-1]
if(i == n-1):
d = li[n-2]
comp = [a,b,c,d]
a1 = ['0' , '0' , '1' , '1']
a2 = ['1' , '1' , '0' , '0']
n1 = 0
n2 = 0
for i in range(0,4):
if(comp[i] != a1[i]):
n1 += 1
if(comp[i] != a2[i]):
n2+=1
ctr = 0
if(n1<=2):
print(n1)
if(comp[0] != a1[0]):
print('1 2')
if(comp[1] != a1[1]):
print('2 1')
if(comp[2] != a1[2]):
print(n-1,n)
if(comp[3] != a1[3]):
print(n,n-1)
else:
print(n2)
if(comp[0] != a2[0]):
print('1 2')
if(comp[1] != a2[1]):
print('2 1')
if(comp[2] != a2[2]):
print(n-1,n)
if(comp[3] != a2[3]):
print(n,n-1)
| 8 |
PYTHON3
|
n = int(input())
for j in range(n):
n = int(input())
f=[]
for j in range(n):
l = input()
if n==3:
if j==0:
f.append(int(l[1]))
if j==1:
f.append(int(l[0]))
f.append(int(l[n-1]))
if j==n-1:
f.append(int(l[n-2]))
else:
if j==0:
f.append(int(l[1]))
if j==1:
f.append(int(l[0]))
if j==n-1:
f.append(int(l[n-2]))
if j==n-2:
f.append(int(l[n-1]))
cnt=0
cnt1=0
if f[0]==0:
cnt+=1
else:
cnt1+=1
if f[1]==0:
cnt+=1
else:
cnt1+=1
if f[2]==1:
cnt+=1
else:
cnt1+=1
if f[3]==1:
cnt+=1
else:
cnt1+=1
if cnt>=cnt1:
print(cnt1)
if f[0]==1:
print(1,2)
if f[1]==1:
print(2,1)
if f[2]==0:
print(n-1,n)
if f[3]==0:
print(n,n-1)
else:
print(cnt)
if f[0]==0:
print(1,2)
if f[1]==0:
print(2,1)
if f[2]==1:
print(n-1,n)
if f[3]==1:
print(n,n-1)
| 8 |
PYTHON3
|
from collections import Counter
from collections import OrderedDict
from collections import defaultdict
def freq_count(mylist):
return Counter(mylist)
t=int(input())
for _ in range(t):
n=int(input())
#arr=[int(x) for x in input().split()]
#a,b=map(int,input().split())
#print(a^b)
grid=[list(input()) for x in range(n)]
li=[]
c1=0
if grid[1][0] == "1":
#print(2,1)
li.append([2,1])
c1+=1
if grid[0][1] == "1":
#print(1,2)
li.append([1,2])
c1+=1
if grid[n-1][n-2] == "0":
#print(n,n-1)
li.append([n,n-1])
c1+=1
if grid[n-2][n-1] == "0":
#print(n-1,n)
li.append([n-1,n])
c1+=1
if c1 > 3:
print(0)
continue
if c1 == 3:
print(1)
if grid[1][0] == grid[0][1]:
if grid[n-1][n-2] == "1":
print(n,n-1)
if grid[n-2][n-1] == "1":
print(n-1,n)
continue
if grid[n-1][n-2] == grid[n-2][n-1]:
if grid[1][0] == "0":
print(2,1)
if grid[0][1] == "0":
print(1,2)
continue
print(c1)
for i in range(c1):
print(li[i][0],li[i][1])
| 8 |
PYTHON3
|
import sys
import math
from functools import reduce
import bisect
def getN():
return int(input())
def getNM():
return map(int, input().split())
def getList():
return list(map(int, input().split()))
def input():
return sys.stdin.readline().rstrip()
def index(a, x):
i = bisect.bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
return False
#############
# MAIN CODE #
#############
for _ in range(getN()):
n = getN()
s = []
for _ in range(n):
s.append(list(input()))
if {s[0][1], s[1][0]} == {s[n - 1][n - 2], s[n - 2][n - 1]} and len({s[0][1], s[1][0]}) == 1 and len(
{s[n - 1][n - 2], s[n - 2][n - 1]}) == 1:
print(2)
print('1 2')
print('2 1')
elif s[0][1] != s[1][0] and s[n - 1][n - 2] != s[n - 2][n - 1]:
if s[0][1] == s[n - 2][n - 1] and s[1][0] == s[n - 1][n - 2]:
print(2)
print('1 2')
print(f'{n} {n - 1}')
elif s[0][1] != s[n - 2][n - 1] and s[1][0] != s[n - 1][n - 2]:
print(2)
print('1 2')
print(f'{n - 1} {n}')
elif {s[0][1], s[1][0]} != {s[n - 1][n - 2], s[n - 2][n - 1]} and len({s[0][1], s[1][0]}) == 1 and len(
{s[n - 1][n - 2], s[n - 2][n - 1]}) == 1:
print(0)
else:
if s[0][1] == s[n - 1][n - 2] == s[n - 2][n - 1]:
print(1)
print(1, 2)
elif s[1][0] == s[n - 1][n - 2] == s[n - 2][n - 1]:
print(1)
print(2, 1)
if s[n - 1][n - 2] == s[0][1] == s[1][0]:
print(1)
print(f'{n} {n - 1}')
elif s[n - 2][n - 1] == s[0][1] == s[1][0]:
print(1)
print(f'{n - 1} {n}')
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void _print(long long t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(long double t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(unsigned long long t) { cerr << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
cerr << "{";
_print(p.first);
cerr << ",";
_print(p.second);
cerr << "}";
}
template <class T>
void _print(vector<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(set<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <typename T, typename T1>
T amax(T &a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T amin(T &a, T1 b) {
if (b < a) a = b;
return a;
}
const int N = 1e9 + 1;
const int x = 201;
int a[200][200];
void solve() {
int n;
cin >> n;
string s[n];
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
int a = s[0][1], b = s[1][0];
int c = s[n - 1][n - 2], d = s[n - 2][n - 1];
vector<pair<int, int>> v;
if (a == b) {
if (a == c) v.push_back({n, n - 1});
if (a == d) v.push_back({n - 1, n});
} else if (c == d) {
if (c == a) v.push_back({1, 2});
if (c == b) v.push_back({2, 1});
} else {
v.push_back({2, 1});
if (a == c) v.push_back({n, n - 1});
if (a == d) v.push_back({n - 1, n});
}
cout << ((int)(v).size()) << '\n';
for (auto x : v) cout << x.first << " " << x.second << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 8 |
CPP
|
"""
Code of Ayush Tiwari
Codeforces: servermonk
Codechef: ayush572000
"""
# import sys
# input = sys.stdin.buffer.readline
def solution():
# This is the main code
n=int(input())
l=[]
for i in range(n):
a=list(input())
l.append(a)
ans=[]
if l[1][0]==l[0][1]:
if l[n-2][n-1]==l[1][0]:
ans.append([n-1,n])
if l[n-1][n-2]==l[1][0]:
ans.append([n,n-1])
elif l[n-1][n-2]==l[n-2][n-1]:
if l[1][0]==l[n-2][n-1]:
ans.append([2,1])
if l[0][1]==l[n-1][n-2]:
ans.append([1,2])
else:
if l[1][0]==l[n-1][n-2]:
ans.append([n,n-1])
if l[1][0]==l[n-2][n-1]:
ans.append([n-1,n])
ans.append([1,2])
print(len(ans))
for i in range(len(ans)):
print(ans[i][0],ans[i][1])
t=int(input())
for _ in range(t):
solution()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
const long double eps = 1e-8;
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a[n + 1][n + 1];
string temp;
for (long long int i = 1; i < n + 1; i++) {
cin >> temp;
for (long long int j = 0; j < n; j++) {
a[i][j + 1] = temp[j] - '0';
}
}
long long int x = a[1][2];
long long int y = a[2][1];
long long int p = a[n][n - 1];
long long int q = a[n - 1][n];
if (x == y) {
if (p == q) {
if (p != x)
cout << 0 << "\n";
else {
cout << 2 << "\n";
cout << "1 2"
<< "\n";
cout << "2 1"
<< "\n";
}
} else {
if (p == x) {
cout << 1 << "\n";
cout << n << " " << n - 1 << "\n";
} else {
cout << 1 << "\n";
cout << n - 1 << " " << n << "\n";
}
}
} else {
if (p == q) {
cout << 1 << "\n";
if (x == p) {
cout << "1 2"
<< "\n";
} else
cout << "2 1"
<< "\n";
} else {
cout << 2 << "\n";
if (x == p) {
cout << "1 2"
<< "\n";
cout << n - 1 << " " << n << "\n";
} else {
cout << "1 2"
<< "\n";
cout << n << " " << n - 1 << "\n";
}
}
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int n = 0, f = 1, ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
n = n * 10 + ch - '0';
ch = getchar();
}
return n * f;
}
char s[305][305];
int main() {
int t, x, sth, n;
t = read();
for (int greg = 1; greg <= t; greg++) {
n = read();
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
if (s[1][2] == s[2][1]) {
sth = 0;
if (s[n][n - 1] == s[1][2]) sth++;
if (s[n - 1][n] == s[1][2]) sth++;
printf("%d\n", sth);
if (s[n][n - 1] == s[1][2]) printf("%d %d\n", n, n - 1);
if (s[n - 1][n] == s[1][2]) printf("%d %d\n", n - 1, n);
} else {
sth = 1;
if (s[n][n - 1] != s[n - 1][n]) sth++;
printf("%d\n", sth);
if (s[1][2] == s[n - 1][n])
printf("1 2\n");
else
printf("2 1\n");
if (sth == 2) printf("%d %d\n", n, n - 1);
}
}
return 0;
}
| 8 |
CPP
|
from sys import stdin, stdout
from math import gcd
input = stdin.readline
for _ in range(int(input())):
n = int(input())
a = [input() for i in range(n)]
ans = [(i, j) for i, j in [(0, 1), (1, 0), (n - 2, n - 1), (n - 1, n - 2)] if (a[i][j] == '0') ^ (min(i, j) == 0)]
if len(ans) <= 2:
print(len(ans))
for i in ans:
print(i[0] + 1, i[1] + 1)
else:
ans = [(i, j) for i, j in [(0, 1), (1, 0), (n - 2, n - 1), (n - 1, n - 2)] if (a[i][j] == '0') ^ (min(i, j) != 0)]
print(len(ans))
for i in ans:
print(i[0] + 1, i[1] + 1)
| 8 |
PYTHON3
|
t=int(input())
for i in range(t):
n=int(input())
grid=[]
for j in range(n):
grid.append(input())
s1=grid[0][1]
s2=grid[1][0]
s3=grid[-2][-1]
s4=grid[-1][-2]
if s1==s2==s3==s4:
print(2)
print(1,2)
print(2,1)
elif s1==s2==s3:
print(1)
print(n-1,n)
elif s1==s2==s4:
print(1)
print(n,n-1)
elif s1==s4==s3:
print(1)
print(1,2)
elif s4==s2==s3:
print(1)
print(2,1)
elif s1==s3:
print(2)
print(1,2)
print(n,n-1)
elif s1==s4:
print(2)
print(1,2)
print(n-1,n)
else:
print(0)
| 8 |
PYTHON3
|
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 19 16:36:57 2020
@author: lakne
"""
t = int(input())
answers = []
for _ in range(t):
n = int(input())
start = []
finish = []
answer = []
grids = []
c = 0
for i in range(n):
grid = input()
grids.append(grid)
start.append(grids[0][1])
start.append(grids[1][0])
finish.append(grids[n-2][n-1])
finish.append(grids[n-1][n-2])
if start[0] != start[1] and finish[0] == finish[1]:
c = 1
if start[0] == finish[0]:
answer.append([str(1), str(2)])
else:
answer.append([str(2), str(1)])
elif start[0] == start[1] and finish[0] == finish[1]:
if start[0] == finish[0]:
c = 2
answer.append([str(1), str(2)])
answer.append([str(2), str(1)])
else:
c = 0
elif start[0] != start[1] and finish[0] != finish[1]:
c = 2
if start[0] == finish[0]:
answer.append([str(1), str(2)])
answer.append([str(n), str(n-1)])
else:
answer.append([str(1), str(2)])
answer.append([str(n-1), str(n)])
elif start[0] == start[1] and finish[0] != finish[1]:
c = 1
if start[0] == finish[0]:
answer.append([str(n-1), str(n)])
else:
answer.append([str(n), str(n-1)])
answers.append([c, answer])
for j in range(t):
print(answers[j][0])
for cord in answers[j][1]:
print(' '.join(cord))
| 8 |
PYTHON3
|
T = int( input() )
for t in range(T):
m = []
n = int( input() )
for i in range(n):
s = list( input() )
m.append(s)
#print(m)
p = [ m[1][0], m[0][1], m[n-2][n-1], m[n-1][n-2] ]
result = []
if p[0] == p[1]:
if p[2] == p[0]:
result.append(2)
if p[3] == p[0]:
result.append(3)
elif p[2] == p[3]:
if p[2] == p[0]:
result.append(0)
if p[2] == p[1]:
result.append(1)
else:
result.append(1)
if p[2] == p[0]:
result.append(2)
if p[3] == p[0]:
result.append(3)
print( len(result) )
for r in result:
if r == 0:
print("2 1")
if r == 1:
print("1 2")
if r == 2:
print(str(n-2+1) + " " + str(n-1+1))
if r == 3:
print(str(n-1+1) + " " + str(n-2+1))
| 8 |
PYTHON3
|
t = int(input())
for case in range(t):
n = int(input())
grid = [input() for row in range(n)]
cnt = 0
ans = []
if grid[0][1] == grid[1][0]:
g = 1 - int(grid[0][1])
if int(grid[-1][-2]) != g:
ans.append((n, n - 1))
if int(grid[-2][-1]) != g:
ans.append((n - 1, n))
elif grid[-1][-2] == grid[-2][-1]:
g = 1 - int(grid[-1][-2])
if int(grid[0][1]) != g:
ans.append((1, 2))
if int(grid[1][0]) != g:
ans.append((2, 1))
else:
if int(grid[0][1]) != 0:
ans.append((1, 2))
if int(grid[1][0]) != 0:
ans.append((2, 1))
if int(grid[-1][-2]) != 1:
ans.append((n, n - 1))
if int(grid[-2][-1]) != 1:
ans.append((n - 1, n))
print(len(ans))
for item in ans:
print(*item)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, a[205][205];
char c;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
cin >> c;
a[i][j] = c - '0';
}
if (a[1][2] == a[2][1]) {
if (a[n - 1][n] == a[n][n - 1]) {
if (a[1][2] == a[n - 1][n]) {
cout << "2\n1 2\n2 1\n";
} else {
cout << "0\n";
}
} else {
if (a[1][2] == a[n - 1][n]) {
cout << "1\n" << n - 1 << " " << n << endl;
} else {
cout << "1\n" << n << " " << n - 1 << endl;
}
}
} else {
if (a[n - 1][n] == a[n][n - 1]) {
if (a[1][2] == a[n - 1][n]) {
cout << "1\n1 2\n";
} else {
cout << "1\n2 1\n";
}
} else {
if (a[1][2] == a[n - 1][n]) {
cout << "2\n1 2\n" << n << " " << n - 1 << endl;
} else {
cout << "2\n1 2\n" << n - 1 << " " << n << endl;
}
}
}
}
return 0;
}
| 8 |
CPP
|
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c for j in range(b)] for i in range(a)]
def list3d(a, b, c, d): return [[[d for k in range(c)] for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10
for _ in range(INT()):
N = INT()
grid = [[]] * N
for i in range(N):
grid[i] = list(input())
s1 = grid[0][1]
s2 = grid[1][0]
g1 = grid[N-1][N-2]
g2 = grid[N-2][N-1]
ans = []
if s1 == s2 == '0':
if g1 == '0':
ans.append((N, N-1))
if g2 == '0':
ans.append((N-1, N))
elif s1 == '1' and s2 == '0':
if g1 == g2 == '1':
ans.append((1, 2))
elif g1 == g2 == '0':
ans.append((2, 1))
elif g1 == '1' and g2 == '0':
ans.append((2, 1))
ans.append((N, N-1))
elif g1 == '0' and g2 == '1':
ans.append((2, 1))
ans.append((N-1, N))
elif s1 == '0' and s2 == '1':
if g1 == g2 == '1':
ans.append((2, 1))
elif g1 == g2 == '0':
ans.append((1, 2))
elif g1 == '1' and g2 == '0':
ans.append((1, 2))
ans.append((N, N-1))
elif g1 == '0' and g2 == '1':
ans.append((1, 2))
ans.append((N-1, N))
else:
if g1 == '1':
ans.append((N, N-1))
if g2 == '1':
ans.append((N-1, N))
print(len(ans))
for a in ans:
print(*a)
| 8 |
PYTHON3
|
# Author : devil9614 - Sujan Mukherjee
from __future__ import division, print_function
import os,sys
import math
import collections
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
class my_dictionary(dict):
def __init__(self):
self = dict()
def add(self,key,value):
self[key] = value
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def sli(): return list(msi())
def dmain():
sys.setrecursionlimit(100000000)
threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import log,sqrt,factorial
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *,threading
#from itertools import permutations
#Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def getKey(item): return item[1]
def sort2(l):return sorted(l, key=getKey,reverse=True)
def d2(n,m,num):return [[num for x in range(m)] for y in range(n)]
def isPowerOfTwo (x): return (x and (not(x & (x - 1))) )
def decimalToBinary(n): return bin(n).replace("0b","")
def ntl(n):return [int(i) for i in str(n)]
def factorial(n): return 1 if (n==1 or n==0) else n * factorial(n - 1)
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(n-r))
def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
def ceil(x,y):
if x%y==0:
return x//y
else:
return x//y+1
def powerMod(x,y,p):
res = 1
x %= p
while y > 0:
if y&1:
res = (res*x)%p
y = y>>1
x = (x*x)%p
return res
def gcd(x, y):
while y:
x, y = y, x % y
return x
def nCr(n, r):
return (fact(n) / (fact(r)
* fact(n - r)))
# Returns factorial of n
def fact(n):
res = 1
for i in range(2, n+1):
res = res * i
return res
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def read():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def padded_bin_with_complement(x):
if x < 0:
return bin((2**16) - abs(x))[2:].zfill(16)
else:
return bin(x)[2:].zfill(16)
def binaryToDecimal(binary):
binary1 = binary
decimal, i, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
print(decimal)
def CountFrequency(my_list):
freq = {}
for item in my_list:
if (item in freq):
freq[item] += 1
else:
freq[item] = 1
return freq
def pos(a):
b = [0]*len(a)
c = sorted(a)
for i in range(len(a)):
for j in range(len(a)):
if c[j] == a[i]:
b[i] = j
break
return b
def smallestDivisor(n):
# if divisible by 2
if (n % 2 == 0):
return 2
# iterate from 3 to sqrt(n)
i = 3
while(i * i <= n):
if (n % i == 0):
return i
i += 2
return n
def commonn(a,b,n):
c = []
for i in range(n):
if a[i] == b[i]:
c.append("-1")
else:
c.append(b[i])
return c
def find_lcm(num1, num2):
if(num1>num2):
num = num1
den = num2
else:
num = num2
den = num1
rem = num % den
while(rem != 0):
num = den
den = rem
rem = num % den
gcd = den
lcm = int(int(num1 * num2)/int(gcd))
return lcm
def sumdigit(n):
n = str(n)
k = 0
for i in range(len(n)):
k+=int(n[i])
return k
def main():
for _ in range(ii()):
n = ii()
a = []
for i in range(n):
jk = si()
a.append((jk))
x = int(a[0][1])
y = int(a[1][0])
z = int(a[n-2][n-1])
s = int(a[n-1][n-2])
d = []
if (x+y) >= (z+s):
if x == 0:
d.append([1,2])
if y == 0:
d.append([2,1])
if z == 1:
d.append([n-1,n])
if s == 1:
d.append([n,n-1])
else:
if x == 1:
d.append([1,2])
if y == 1:
d.append([2,1])
if z == 0:
d.append([n-1,n])
if s == 0:
d.append([n,n-1])
print(len(d))
for i in range(len(d)):
print(*d[i])
# region fastio
# template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
main()
| 8 |
PYTHON3
|
for _ in range (int(input())):
n = int(input())
grid = []
for i in range (n):
grid.append(list(input()))
s1 = grid[0][1]
s2 = grid[1][0]
e1 = grid[n-2][n-1]
e2 = grid[n-1][n-2]
ans = []
if s1 == s2:
if e1 == s1:
ans.append([n-1,n])
if e2 == s2:
ans.append([n,n-1])
else:
if e1!=e2:
if s1=='1':
ans.append([2,1])
else:
ans.append([1,2])
if e1=='1':
ans.append([n-1,n])
else:
ans.append([n,n-1])
else:
if e1=='0':
if s1=='1':
ans.append([2,1])
else:
ans.append([1,2])
else:
if s1=='0':
ans.append([2,1])
else:
ans.append([1,2])
print(len(ans))
for i in ans:
print(*i)
| 8 |
PYTHON3
|
for _ in range(int(input())):
ans = 0
ind = [[0] * 2 for i in range(4)]
n = int(input())
s = [input() for i in range(n)]
#0011
if s[1][0] == '1':
ans += 1
ind[0][0] = 2
ind[0][1] = 1
if s[0][1] == '1':
ans += 1
ind[1][0] = 1
ind[1][1] = 2
if s[n-1][n-2] == '0':
ans += 1
ind[2][0] = n
ind[2][1] = n-1
if s[n-2][n-1] == '0':
ans += 1
ind[3][0] = n-1
ind[3][1] = n
if ans <= 2:
print(ans)
for i in range(4):
if ind[i][0] == 0:
continue
else:
print(ind[i][0],ind[i][1])
else:
ans = 0
ind = [[0] * 2 for i in range(4)]
if s[1][0] == '0':
ans += 1
ind[0][0] = 2
ind[0][1] = 1
if s[0][1] == '0':
ans += 1
ind[1][0] = 1
ind[1][1] = 2
if s[n-1][n-2] == '1':
ans += 1
ind[2][0] = n
ind[2][1] = n-1
if s[n-2][n-1] == '1':
ans += 1
ind[3][0] = n-1
ind[3][1] = n
print(ans)
for i in range(4):
if ind[i][0] == 0:
continue
else:
print(ind[i][0],ind[i][1])
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void edit(vector<int> &skill, int n) {
for (int i = 0; i < n; i++) {
int x;
cin >> x;
skill.push_back(x);
}
}
void achilles() {
int n;
cin >> n;
char arr[n][n];
cin.ignore();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
cin.ignore();
}
int a = arr[0][1] - '0';
int b = arr[1][0] - '0';
int c = arr[n - 2][n - 1] - '0';
int d = arr[n - 1][n - 2] - '0';
if ((a == b)) {
if (c == d) {
if (a == c) {
cout << 2 << "\n";
cout << n - 1 << " " << n << "\n" << n << " " << n - 1 << "\n";
return;
} else {
cout << 0 << "\n";
return;
}
} else {
cout << 1 << "\n";
if (c == a) {
cout << n - 1 << " " << n << "\n";
return;
} else if (d == a) {
cout << n << " " << n - 1 << "\n";
return;
}
}
} else if (c == d) {
{
cout << 1 << "\n";
if (a == c) {
cout << 1 << " " << 2 << "\n";
return;
} else if (b == c) {
cout << 2 << " " << 1 << "\n";
return;
}
}
} else {
cout << 2 << "\n";
if (a == c) {
cout << 2 << " " << 1 << "\n";
cout << n - 1 << " " << n << "\n";
return;
} else {
cout << 2 << " " << 1 << "\n";
cout << n << " " << n - 1 << "\n";
return;
}
}
}
int main() {
ios ::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) achilles();
return 0;
}
| 8 |
CPP
|
def main(input_f):
t = int(input_f())
for _ in range(t):
n = int(input_f())
cells = [list(input_f()) for _ in range(n)]
start_right, start_bottom = cells[0][1], cells[1][0]
finish_left, finish_top = cells[n - 1][n - 2], cells[n - 2][n - 1]
inverted_cells = []
if start_right == start_bottom:
if finish_left == start_right:
inverted_cells.append((n - 1, n - 2))
if finish_top == start_right:
inverted_cells.append((n - 2, n - 1))
elif finish_left == finish_top:
if start_right == finish_left:
inverted_cells.append((0, 1))
if start_bottom == finish_left:
inverted_cells.append((1, 0))
else:
if start_right != '0':
inverted_cells.append((0, 1))
if start_bottom != '0':
inverted_cells.append((1, 0))
if finish_left != '1':
inverted_cells.append((n - 1, n - 2))
if finish_top != '1':
inverted_cells.append((n - 2, n - 1))
print(len(inverted_cells))
for x, y in inverted_cells:
print(x + 1, y + 1)
if __name__ == '__main__':
main(input)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int solve() {
int n;
cin >> n;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
if (s[0][1] == s[1][0] and s[n - 2][n - 1] == s[n - 1][n - 2] and
s[0][1] != s[n - 2][n - 1]) {
puts("0");
return 0;
}
if (s[0][1] == s[1][0]) {
if (s[n - 1][n - 2] == s[0][1] and s[n - 2][n - 1] == s[0][1]) {
puts("2");
puts("1 2");
puts("2 1");
} else {
puts("1");
if (s[n - 2][n - 1] == s[0][1])
cout << n - 1 << " " << n << "\n";
else
cout << n << " " << n - 1 << "\n";
}
return 0;
}
if (s[n - 1][n - 2] == s[n - 2][n - 1]) {
puts("1");
if (s[n - 2][n - 1] == s[0][1])
puts("1 2");
else
puts("2 1");
return 0;
}
puts("2");
if (s[0][1] == '1')
puts("1 2");
else
puts("2 1");
if (s[n - 2][n - 1] == '0')
cout << n - 1 << " " << n << "\n";
else
cout << n << " " << n - 1 << "\n";
return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
long long Min(long long a, long long b) { return (a < b) ? a : b; }
long long Max(long long a, long long b) { return (a < b) ? b : a; }
long long gcd(long long m, long long n) {
if (n == 0) return m;
return gcd(n, m % n);
}
long long lcm(long long m, long long n) { return m * n / gcd(m, n); }
long long dx[6] = {-1, 0, 1, 0, 0, 0}, dy[6] = {0, 1, 0, -1, 0, 0},
dz[6] = {0, 0, 0, 0, 1, -1};
void solve() {
long long n;
cin >> n;
vector<string> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
char c[5] = {v[0][1], v[1][0], v[n - 1][n - 2], v[n - 2][n - 1]};
char ch[2][5] = {"0011", "1100"};
for (int i = 0; i < 2; i++) {
long long cnt = 0;
for (int j = 0; j < 4; j++) {
if (c[j] != ch[i][j]) cnt++;
}
if (cnt <= 2) {
vector<pair<long long, long long> > a;
for (int j = 0; j < 4; j++) {
if (c[j] != ch[i][j]) {
if (j == 0)
a.push_back({1, 2});
else if (j == 1)
a.push_back({2, 1});
else if (j == 2)
a.push_back({n, n - 1});
else
a.push_back({n - 1, n});
}
}
cout << a.size() << "\n";
for (auto t : a) cout << t.first << " " << t.second << "\n";
return;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long tc = 1;
cin >> tc;
while (tc--) solve();
exit(0);
}
| 8 |
CPP
|
def getInverts(arr,N):
inverts = []
if arr[0][1] == arr[1][0]:
if arr[-1][-2] == arr[0][1]:
inverts.append([N-1,N-2])
if arr[-2][-1] == arr[0][1]:
inverts.append([N-2,N-1])
else:
if arr[-1][-2] == arr[-2][-1]:
if arr[0][1] == arr[-1][-2]:
inverts.append([0,1])
else:
inverts.append([1,0])
else:
if arr[0][1] == arr[-1][-2]:
inverts.append([0,1])
inverts.append([N-2,N-1])
else:
inverts.append([1,0])
inverts.append([N-2,N-1])
return inverts
T = int(input())
for t in range(T):
N = int(input())
arr = []
for n in range(N):
arr.append(list(input()))
inverts = getInverts(arr,N)
print(len(inverts))
for i in inverts:
print(i[0]+1,i[1]+1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, sr, sl, eu, ele;
cin >> t;
while (t--) {
int n;
scanf("%d", &n);
getchar();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
char c = getchar();
if (i == 1 && j == 2) sr = c - '0';
if (i == 2 && j == 1) sl = c - '0';
if (i == n - 1 && j == n) eu = c - '0';
if (i == n && j == n - 1) ele = c - '0';
}
getchar();
}
int tot = sr + sl + eu + ele;
int a = 0, f[2], l[2];
if (tot == 0 || tot == 4) {
a = 2;
f[0] = 1, f[1] = 2;
l[0] = 2, l[1] = 1;
} else if (tot == 2) {
if (sr != sl) {
if (sr == eu) {
a = 2;
f[0] = 2, l[0] = 1;
f[1] = n - 1, l[1] = n;
} else {
a = 2;
f[0] = 1, l[0] = 2;
f[1] = n - 1, l[1] = n;
}
}
} else {
if (sr == sl) {
if (eu == sl) {
a = 1;
f[0] = n - 1;
l[0] = n;
} else {
a = 1;
f[0] = n;
l[0] = n - 1;
}
} else {
if (eu == sl) {
a = 1;
f[0] = 2;
l[0] = 1;
} else {
a = 1;
f[0] = 1;
l[0] = 2;
}
}
}
printf("%d\n", a);
for (int i = 0; i < a; i++) {
printf("%d %d\n", f[i], l[i]);
}
}
return 0;
}
| 8 |
CPP
|
for w in range(int(input())):
n=int(input())
l=[]
for i in range(n):
l.append(list(str(input())))
#print(l)
if(l[0][1]==l[1][0]):
if(l[n-1][n-2]==l[n-2][n-1]):
if(l[n-1][n-2]==l[0][1]):
print(2)
print(n,n-1)
print(n-1,n)
else:
print(0)
else:
print(1)
if(l[n-1][n-2]==l[0][1]):
print(n,n-1)
else:
print(n-1,n)
else:
if(l[n-1][n-2]==l[n-2][n-1]):
print(1)
if(l[0][1]==l[n-1][n-2]):
print(1,2)
else:
print(2,1)
else:
print(2)
if(l[0][1]!='0'):
print(1,2)
if(l[1][0]!='0'):
print(2,1)
if(l[n-1][n-2]!='1'):
print(n,n-1)
if(l[n-2][n-1]!='1'):
print(n-1,n)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n=int(input())
l=[input() for i in range(n)]
#print(l)
if l[0][1]==l[1][0]:
if l[-1][-2]==l[-2][-1]:
if l[-1][-2]!=l[1][0]:
print(0)
else:
print(2)
print(1,2)
print(2,1)
else:
if l[0][1]==l[-1][-2]:
print(1)
print(n,n-1)
else:
print(1)
print(n-1,n)
else:
if l[-1][-2]==l[-2][-1]:
if l[0][1]==l[-1][-2]:
print(1)
print(1,2)
else:
print(1)
print(2,1)
else:
if l[0][1]==l[-1][-2]:
print(2)
print(1,2)
print(n-1,n)
else:
print(2)
print(1,2)
print(n,n-1)
| 8 |
PYTHON3
|
t=int(input())
for i in range(t):
n=int(input())
m=[[i for i in input()] for i in range(n)]
#print(m)
q=int(m[1][0])
w=int(m[0][1])
a=int(m[-1][-2])
s=int(m[-2][-1])
e=n-1
if q == w:
if a == s:
if q == a:
print('2','\n','1 2','\n','2 1',sep='')
else:
print('0')
else:
if q == a:
print('1','\n',n,' ',e,sep='')
else:
print('1','\n',e,' ',n,sep='')
else:
if a == s:
if q == a:
print('1','\n','2 1',sep='')
else:
print('1','\n','1 2',sep='')
else:
if q == a:
print('2','\n','1 2','\n',n,' ',e,sep='')
else:
print('2','\n','1 2','\n',e,' ',n,sep='')
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
vector<string> v;
cin >> n;
for (int i = (0); i < int(n); i++) {
cin >> s;
v.push_back(s);
}
vector<pair<int, int> > ans;
if (v[0][1] == v[1][0]) {
if (v[n - 1][n - 2] == v[0][1]) ans.push_back({n - 1, n - 2});
if (v[n - 2][n - 1] == v[0][1]) ans.push_back({n - 2, n - 1});
} else if (v[n - 1][n - 2] == v[n - 2][n - 1]) {
if (v[0][1] == v[n - 1][n - 2]) ans.push_back({0, 1});
if (v[1][0] == v[n - 1][n - 2]) ans.push_back({1, 0});
} else {
ans.push_back({0, 1});
v[0][1] = v[1][0];
if (v[n - 1][n - 2] == v[0][1]) ans.push_back({n - 1, n - 2});
if (v[n - 2][n - 1] == v[0][1]) ans.push_back({n - 2, n - 1});
}
cout << ans.size() << endl;
for (auto t : ans) cout << t.first + 1 << ' ' << t.second + 1 << endl;
}
int main() {
int t;
cin >> t;
for (int i = (0); i < int(t); i++) solve();
return 0;
}
| 8 |
CPP
|
import sys
input = lambda: sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
n = int(input())
s = []
for _ in range(n):
s.append(input())
x0, x1, x2, x3 = int(s[0][1]), int(s[1][0]), int(s[-1][-2]), int(s[-2][-1])
ans = []
if x0==1:
ans.append((1, 2))
if x1==1:
ans.append((2, 1))
if x2==0:
ans.append((n, n-1))
if x3==0:
ans.append((n-1, n))
if len(ans)<=2:
print(len(ans))
for i in ans:
print(*i)
continue
ans = []
if x0==0:
ans.append((1, 2))
if x1==0:
ans.append((2, 1))
if x2==1:
ans.append((n, n-1))
if x3==1:
ans.append((n-1, n))
print(len(ans))
for i in ans:
print(*i)
| 8 |
PYTHON3
|
import sys
import math
from math import *
from collections import Counter,defaultdict,deque
from bisect import bisect_left
import random
lip = lambda : list(map(int, input().split()))
ip = lambda : int(input())
sip = lambda : input().split()
mod = 10**9+7
def main():
n = ip()
mat = []
for i in range(n):
mat.append(input())
ctr = 0
if mat[0][1] == '1':
ctr += 1
if mat[1][0] == '1':
ctr += 1
c = 0
if mat[-1][-2] == '1':
c += 1
if mat[-2][-1] == '1':
c += 1
if ctr == 0:
if c == 2:
print(0)
elif c == 0:
print(2)
print(n,n-1)
print(n-1,n)
else:
print(1)
if mat[-2][-1] == '1':
print(n,n-1)
else:
print(n-1,n)
elif ctr == 2:
if c == 0:
print(0)
elif c == 2:
print(2)
print(n,n-1)
print(n-1,n)
else:
print(1)
if mat[-2][-1] == '0':
print(n,n-1)
else:
print(n-1,n)
else:
if c == 2:
print(1)
if mat[0][1] == '1':
print(1,2)
else:
print(2,1)
elif c == 0:
print(1)
if mat[0][1] == '0':
print(1,2)
else:
print(2,1)
else:
print(2)
if mat[0][1] == '0':
print(1,2)
if mat[1][0] == '0':
print(2,1)
if mat[-1][-2] == '1':
print(n,n-1)
if mat[-2][-1] == '1':
print(n-1,n)
for i in range(ip()):
main()
| 8 |
PYTHON3
|
t = int(input())
for _ in range(0,t):
n = int(input())
s = []
for i in range(0,n):
ss = [k for k in input()]
s.append(ss)
ans = []
kk = [s[0][1],s[1][0],s[-2][-1],s[-1][-2]]
z,o = kk.count('0'),kk.count('1')
if z == 0 or o == 0:
ans.append([1,2])
ans.append([2,1])
if z == 1 and o == 3:
if kk[0] != kk[1]:
if kk[0] == '1':
ans.append([1,2])
else:
ans.append([2,1])
else:
if kk[2] == '1':
ans.append([n-1,n])
else:
ans.append([n,n-1])
if z == 2 and o == 2 and kk[0] != kk[1]:
if kk[0] == '0':
ans.append([1,2])
if kk[1] == '0':
ans.append([2,1])
if kk[2] == '1':
ans.append([n-1,n])
if kk[3] == '1':
ans.append([n,n-1])
if z == 3 and o == 1:
if kk[0] != kk[1]:
if kk[0] == '0':
ans.append([1,2])
else:
ans.append([2,1])
else:
if kk[2] == '0':
ans.append([n-1,n])
else:
ans.append([n,n-1])
print(len(ans))
for i in ans:
print(*i)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
char mp[300][300];
int n;
void solve() {
int cnt = 0;
if ((mp[1][2] - '0') && (mp[2][1] - '0')) {
cnt = 0;
if (mp[n][n - 1] - '0') {
cnt++;
}
if (mp[n - 1][n] - '0') {
cnt++;
}
printf("%d\n", cnt);
if (mp[n][n - 1] - '0') {
printf("%d %d\n", n, n - 1);
}
if (mp[n - 1][n] - '0') {
printf("%d %d\n", n - 1, n);
}
} else if (!(mp[1][2] - '0') && !(mp[2][1] - '0')) {
cnt = 0;
if (!(mp[n][n - 1] - '0')) {
cnt++;
}
if (!(mp[n - 1][n] - '0')) {
cnt++;
}
printf("%d\n", cnt);
if (!(mp[n][n - 1] - '0')) {
printf("%d %d\n", n, n - 1);
}
if (!(mp[n - 1][n] - '0')) {
printf("%d %d\n", n - 1, n);
}
} else if ((mp[1][2] - '0') || (mp[2][1] - '0')) {
if ((mp[n][n - 1] - '0') && (mp[n - 1][n] - '0')) {
printf("1\n");
if (mp[1][2] - '0') {
printf("1 2\n");
} else {
printf("2 1\n");
}
} else if (!(mp[n][n - 1] - '0') && !(mp[n - 1][n] - '0')) {
printf("1\n");
if (mp[1][2] - '0') {
printf("2 1\n");
} else {
printf("1 2\n");
}
} else {
printf("2\n");
if (mp[1][2] - '0') {
printf("2 1\n");
} else {
printf("1 2\n");
}
if (mp[n][n - 1] - '0') {
printf("%d %d\n", n, n - 1);
} else {
printf("%d %d\n", n - 1, n);
}
}
}
}
int main() {
int Case = 1;
scanf("%d", &Case);
getchar();
while (Case--) {
scanf("%d", &n);
char c = getchar();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
c = getchar();
mp[i][j] = c;
}
getchar();
}
solve();
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
char s[205][205];
int main() {
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
vector<pair<int, int>> ans[2];
ans[s[1][2] == '1'].push_back(make_pair(1, 2));
ans[s[2][1] == '1'].push_back(make_pair(2, 1));
ans[s[n - 1][n] == '0'].push_back(make_pair(n - 1, n));
ans[s[n][n - 1] == '0'].push_back(make_pair(n, n - 1));
if (ans[0].size() > 2) swap(ans[0], ans[1]);
printf("%d\n", (int)ans[0].size());
for (auto x : ans[0]) printf("%d %d\n", x.first, x.second);
}
}
| 8 |
CPP
|
def main():
n = int(input())
s1, s2, e1, e2 = 0, 0, 0, 0
for i in range(n):
row = input()
if i == 0:
s1 = row[1]
elif i == 1:
s2 = row[0]
if i + 2 == n:
e1 = row[n-1]
elif i + 1 == n:
e2 = row[n-2]
if s1 == s2:
if e1 == e2:
if e1 == s1:
print(2)
print("{} {}".format(1, 2))
print("{} {}".format(2, 1))
else:
print(0)
else:
if e1 == s1:
print(1)
print("{} {}".format(n-1, n))
else:
print(1)
print("{} {}".format(n, n-1))
else:
if e1 == e2:
if s1 == e1:
print(1)
print("{} {}".format(1, 2))
else:
print(1)
print("{} {}".format(2, 1))
else:
if s1 == e1:
print(2)
print("{} {}".format(1, 2))
print("{} {}".format(n, n-1))
elif s1 == e2:
print(2)
print("{} {}".format(1, 2))
print("{} {}".format(n-1, n))
# # main()
for _ in range(int(input())):
main()
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
x = []
for i in range(n):
x.append(list(input()))
if x[0][1] == x[1][0] and x[-1][-2] == x[-2][-1]:
if x[0][1] != x[-1][-2]:
print(0)
else:
print(2)
print(1, 2)
print(2, 1)
elif x[0][1] != x[1][0]:
if x[-1][-2] != x[-2][-1]:
print(2)
print(1, 2)
if x[1][0] != x[-1][-2]:
print(n - 1, n)
else:
print(n, n - 1)
else:
print(1)
if x[0][1] == x[-1][-2]:
print(1, 2)
else:
print(2, 1)
else:
if x[-1][-2] != x[-2][-1]:
print(1)
if x[0][1] != x[-1][-2]:
print(n - 1, n)
else:
print(n, n - 1)
else:
print(2)
print(1, 2)
print(2, 1)
| 8 |
PYTHON3
|
import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
except:
pass
# input = lambda: sys.stdin.buffer.readline().rstrip()
for _ in range(int(input())):
q=int(input())
a=[]
for i in range(q):
a.append(input())
w=[]
if a[0][1]=='1':
if a[1][0]=='1':
if a[q-1][q-2]=='1':
w.append([q,q-1])
if a[q-2][q-1]=='1':
w.append([q-1,q])
else:
if a[q-1][q-2]=='1' and a[q-2][q-1]=='1':
w.append([1,2])
elif a[q-1][q-2]=='1':
w.append([2,1])
w.append([q,q-1])
elif a[q-2][q-1]=='1':
w.append([2,1])
w.append([q-1,q])
else:
w.append([2,1])
else:
if a[1][0]=='1':
if a[q-1][q-2]=='1' and a[q-2][q-1]=='1':
w.append([2,1])
elif a[q-1][q-2]=='1':
w.append([1,2])
w.append([q,q-1])
elif a[q-2][q-1]=='1':
w.append([1,2])
w.append([q-1,q])
else:
w.append([1,2])
else:
if a[q-1][q-2]=='0':
w.append([q,q-1])
if a[q-2][q-1]=='0':
w.append([q-1,q])
print(len(w))
for i in w:
print(*i)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long T, n, i, j;
long long S_0, S_1, F_0, F_1, count;
cin >> T;
while (T--) {
cin >> n;
string A[n];
for (i = 0; i < n; i++) {
cin >> A[i];
}
S_0 = 0;
S_1 = 0;
F_0 = 0;
F_1 = 0;
if (A[0][1] == '0')
S_0 += 1;
else
S_1 += 1;
if (A[1][0] == '0')
S_0 += 1;
else
S_1 += 1;
if (A[n - 2][n - 1] == '0')
F_0 += 1;
else
F_1 += 1;
if (A[n - 1][n - 2] == '0')
F_0 += 1;
else
F_1 += 1;
count = 0;
vector<long long> X;
vector<long long> Y;
if (S_0 == 2) {
if (A[n - 1][n - 2] == '0') {
X.push_back(n - 1);
Y.push_back(n - 2);
count += 1;
}
if (A[n - 2][n - 1] == '0') {
X.push_back(n - 2);
Y.push_back(n - 1);
count += 1;
}
} else if (S_1 == 2) {
if (A[n - 1][n - 2] == '1') {
X.push_back(n - 1);
Y.push_back(n - 2);
count += 1;
}
if (A[n - 2][n - 1] == '1') {
X.push_back(n - 2);
Y.push_back(n - 1);
count += 1;
}
} else if (F_1 == 2) {
if (A[0][1] == '1') {
X.push_back(0);
Y.push_back(1);
count += 1;
}
if (A[1][0] == '1') {
X.push_back(1);
Y.push_back(0);
count += 1;
}
} else if (F_0 == 2) {
if (A[0][1] == '0') {
X.push_back(0);
Y.push_back(1);
count += 1;
}
if (A[1][0] == '0') {
X.push_back(1);
Y.push_back(0);
count += 1;
}
} else if ((S_0 == 1 || S_1 == 1) && (F_0 == 1 || F_1 == 1)) {
if (A[0][1] == '1') {
count += 1;
X.push_back(0);
Y.push_back(1);
}
if (A[1][0] == '1') {
count += 1;
X.push_back(1);
Y.push_back(0);
}
if (A[n - 2][n - 1] == '0') {
count += 1;
X.push_back(n - 2);
Y.push_back(n - 1);
}
if (A[n - 1][n - 2] == '0') {
count += 1;
X.push_back(n - 1);
Y.push_back(n - 2);
}
}
cout << count << "\n";
for (i = 0; i < X.size(); i++) {
cout << (X[i] + 1) << " " << (Y[i] + 1) << "\n";
}
}
return 0;
}
| 8 |
CPP
|
from sys import stdin
for _ in range(int(stdin.readline())):
side = int(stdin.readline())
board = []
for i in range(side):
board.append(stdin.readline().strip())
a = board[0][1]
b = board[1][0]
c = board[side-2][side-1]
d = board[side-1][side-2]
if a == b:
if a == "1":
if c == d:
if c == "1":
print(2)
print(2,1)
print(1,2)
else:
print(0)
else:
if c == "1":
print(1)
print(side-1, side)
else:
print(1)
print(side, side-1)
else:
if c == d:
if c == "1":
print(0)
else:
print(2)
print(2,1)
print(1,2)
else:
if c == "1":
print(1)
print(side, side-1)
else:
print(1)
print(side-1, side)
else:
if a == "1":
if d == c:
if d == "1":
print(1)
print(1, 2)
else:
print(1)
print(2, 1)
else:
if d == "1":
print(2)
print(1,2)
print(side-1, side)
else:
print(2)
print(1,2)
print(side, side-1)
else:
if d == c:
if d == "1":
print(1)
print(2, 1)
else:
print(1)
print(1, 2)
else:
if d == "1":
print(2)
print(2,1)
print(side-1, side)
else:
print(2)
print(2,1)
print(side, side-1)
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
ans = []
final = []
n = int(input())
for i in range(n):
s = input()
lol = list(s)
ans.append(s)
a = ans[0][1]
b = ans[1][0]
x = ans[n-2][n-1]
y = ans[n-1][n-2]
#print(a,b,x,y)
if(b != '0'):
final.append([2,1])
if(a!='0'):
final.append([1,2])
if(y!='1'):
final.append([n,n-1])
if(x!='1'):
final.append([n-1,n])
if(len(final) <=2):
print(len(final))
for i in final:
print(*i,end ='\n')
else:
final = []
if(b != '1'):
final.append([2,1])
if(a!='1'):
final.append([1,2])
if(y!='0'):
final.append([n,n-1])
if(x!='0'):
final.append([n-1,n])
print(len(final))
for i in final:
print(*i,end ='\n')
| 8 |
PYTHON3
|
t=int(input())
for i in range(t):
n=int(input())
L=[]
for i in range(n):
ch=input()
L.append(ch)
I=[]
if (int(L[0][1])+int(L[1][0]))%2==0:
if L[n-1][n-2]==L[1][0]:
I.append((n,n-1))
if L[n-2][n-1]==L[1][0]:
I.append((n-1,n))
elif (int(L[n-1][n-2])+int(L[n-2][n-1]))%2==0:
if L[n-1][n-2]==L[1][0]:
I.append((2,1))
if L[n-2][n-1]==L[0][1]:
I.append((1,2))
else:
if L[0][1]=='1':
I.append((1,2))
else:
I.append((2,1))
if L[n-1][n-2]=='0':
I.append((n,n-1))
else:
I.append((n-1,n))
print(len(I))
for i in I:
print(i[0],i[1])
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
char arr[n][n];
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++) cin >> arr[i][j];
bool f = false;
if ((arr[0][1] == arr[1][0]) && (arr[n - 1][n - 2] == arr[n - 2][n - 1]) &&
(arr[0][1] != arr[n - 1][n - 2]))
f = true;
if (f)
cout << 0 << '\n';
else {
long long cnt0 = 0, cnt1 = 0;
if (arr[0][1] == '0')
cnt0++;
else
cnt1++;
if (arr[1][0] == '0')
cnt0++;
else
cnt1++;
if (arr[n - 1][n - 2] == '0')
cnt0++;
else
cnt1++;
if (arr[n - 2][n - 1] == '0')
cnt0++;
else
cnt1++;
if (cnt0 == cnt1 + 2) {
if (arr[0][1] == arr[1][0]) {
cout << 1 << '\n';
if (arr[n - 1][n - 2] == '0')
cout << n << " " << n - 1 << '\n';
else
cout << n - 1 << " " << n << '\n';
} else {
cout << 1 << '\n';
if (arr[0][1] == '0')
cout << 1 << " " << 2 << '\n';
else
cout << 2 << " " << 1 << '\n';
}
} else if (cnt1 == cnt0 + 2) {
if (arr[0][1] == arr[1][0]) {
cout << 1 << '\n';
if (arr[n - 1][n - 2] == '1')
cout << n << " " << n - 1 << '\n';
else
cout << n - 1 << " " << n << '\n';
} else {
cout << 1 << '\n';
if (arr[0][1] == '1')
cout << 1 << " " << 2 << '\n';
else
cout << 2 << " " << 1 << '\n';
}
} else if (cnt1 == cnt0) {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
if (arr[n - 1][n - 2] != arr[0][1])
cout << n << " " << n - 1 << '\n';
else
cout << n - 1 << " " << n << '\n';
} else {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
cout << 2 << " " << 1 << '\n';
}
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
long long T, N, M, ans, temp;
char m[200][2001];
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> T;
for (long long t = 1; t <= T; t++) {
cin >> N;
for (long long i = 0; i < N; i++) {
cin >> m[i];
}
vector<pair<long long, long long> > c;
if (m[0][1] == '0' && m[1][0] == '0') {
if (m[N - 1][N - 2] == '0') {
c.push_back({N, N - 1});
}
if (m[N - 2][N - 1] == '0') {
c.push_back({N - 1, N});
}
} else if (m[0][1] == '1' && m[1][0] == '1') {
if (m[N - 1][N - 2] == '1') {
c.push_back({N, N - 1});
}
if (m[N - 2][N - 1] == '1') {
c.push_back({N - 1, N});
}
} else {
if (m[N - 1][N - 2] == '0' && m[N - 2][N - 1] == '0') {
if (m[0][1] == '0') {
c.push_back({1, 2});
}
if (m[1][0] == '0') {
c.push_back({2, 1});
}
} else if (m[N - 1][N - 2] == '1' && m[N - 2][N - 1] == '1') {
if (m[0][1] == '1') {
c.push_back({1, 2});
}
if (m[1][0] == '1') {
c.push_back({2, 1});
}
} else {
if (m[0][1] == '1') {
c.push_back({1, 2});
}
if (m[1][0] == '1') {
c.push_back({2, 1});
}
if (m[N - 1][N - 2] == '0') {
c.push_back({N, N - 1});
}
if (m[N - 2][N - 1] == '0') {
c.push_back({N - 1, N});
}
}
}
cout << c.size() << '\n';
for (long long i = 0; i < c.size(); i++) {
cout << c[i].first << ' ' << c[i].second << '\n';
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
string mp[210];
int n;
bool func(char x, char y) {
int res = (mp[1][0] == x) + (mp[0][1] == x) + (mp[n - 1][n - 2] == y) +
(mp[n - 2][n - 1] == y);
if (res <= 2) {
cout << res << "\n";
if (mp[1][0] != y)
cout << "2 1"
<< "\n";
if (mp[0][1] != y)
cout << "1 2"
<< "\n";
if (mp[n - 1][n - 2] != x) cout << n << " " << n - 1 << "\n";
if (mp[n - 2][n - 1] != x) cout << n - 1 << " " << n << "\n";
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int i, j, k, a, b, c, x, y, z, m, t;
cin >> t;
string s;
while (t--) {
cin >> n;
getline(cin, s);
for (i = 0; i < n; i++) {
getline(cin, mp[i]);
}
if (!func('0', '1')) func('1', '0');
}
}
| 8 |
CPP
|
for _ in range(int(input())):
a = []
for i in range(int(input())):
a.append(input())
if a[0][1] == "1" and a[1][0] == "1" and a[-1][-2] == "1" and a[-2][-1] == "1" or a[0][1] == "0" and a[1][0] == '0' and a[-1][-2] == "0" and a[-2][-1] == "0":
print(2)
print(1, 2)
print(2, 1)
elif a[0][1] == '0' and a[1][0] == '1' and a[-1][-2] == '1' and a[-2][-1] == '1' or a[0][1] == '1' and a[1][0] == '0' and a[-1][-2] == '0' and a[-2][-1] == '0':
print(1)
print(2, 1)
elif a[0][1] == '1' and a[1][0] == '0' and a[-1][-2] == '1' and a[-2][-1] == '1' or a[0][1] == '0' and a[1][0] == '1' and a[-1][-2] == '0' and a[-2][-1] == '0':
print(1)
print(1, 2)
elif a[0][1] == '1' and a[1][0] == '1' and a[-1][-2] == '0' and a[-2][-1] == '1' or a[0][1] == '0' and a[1][0] == '0' and a[-1][-2] == '1' and a[-2][-1] == '0':
print(1)
print(len(a) - 1, len(a))
elif a[0][1] == '1' and a[1][0] == '1' and a[-1][-2] == '1' and a[-2][-1] == '0' or a[0][1] == '0' and a[1][0] == '0' and a[-1][-2] == '0' and a[-2][-1] == '1':
print(1)
print(len(a), len(a) - 1)
elif a[0][1] == '0' and a[1][0] == '0' and a[-1][-2] == '1' and a[-2][-1] == '1' or a[0][1] == '1' and a[1][0] == '1' and a[-1][-2] == '0' and a[-2][-1] == '0':
print(0)
elif a[0][1] == '1' and a[1][0] == '0' and a[-1][-2] == '1' and a[-2][-1] == '0' or a[0][1] == '0' and a[1][0] == '1' and a[-1][-2] == '0' and a[-2][-1] == '1':
print(2)
print(1, 2)
print(len(a) - 1, len(a))
elif a[0][1] == '1' and a[1][0] == '0' and a[-1][-2] == '0' and a[-2][-1] == '1' or a[0][1] == '0' and a[1][0] == '1' and a[-1][-2] == '1' and a[-2][-1] == '0':
print(2)
print(1, 2)
print(len(a), len(a) - 1)
| 8 |
PYTHON3
|
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write('\n'.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
#from decimal import Decimal
#from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod=10**9+7
for t in range(int(data())):
n=int(data())
mat=[data() for i in range(n)]
cnt=0
if mat[0][1]!='0':
cnt+=1
if mat[0][2]!='1':
cnt+=1
if mat[1][0]!='0':
cnt+=1
if mat[1][1]!='1':
cnt+=1
if mat[2][0]!='1':
cnt+=1
if cnt<3:
print(cnt)
if mat[0][1] != '0':
print(1,2)
if mat[0][2] != '1':
print(1,3)
if mat[1][0] != '0':
print(2,1)
if mat[1][1] != '1':
print(2,2)
if mat[2][0] != '1':
print(3,1)
continue
cnt = 0
if mat[0][1] != '1':
cnt += 1
if mat[0][2] != '0':
cnt += 1
if mat[1][0] != '1':
cnt += 1
if mat[1][1] != '0':
cnt += 1
if mat[2][0] != '0':
cnt += 1
if cnt < 3:
print(cnt)
if mat[0][1] != '1':
print(1, 2)
if mat[0][2] != '0':
print(1, 3)
if mat[1][0] != '1':
print(2, 1)
if mat[1][1] != '0':
print(2, 2)
if mat[2][0] != '0':
print(3, 1)
continue
| 8 |
PYTHON3
|
from collections import Counter
import math
import sys
from bisect import bisect,bisect_left,bisect_right
from itertools import permutations
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def mod(): return 10**9+7
for i in range(INT()):
n = INT()
#s = input()
#a,b = MAP()
#a = LIST()
arr = []
for i in range(n):
s = input()
a = [str(i) for i in s]
arr.append(a)
x = arr[0][1]
x1 = arr[1][0]
y = arr[-1][-2]
y1 = arr[-2][-1]
if x1 == x and y1 == y and x1 != y1:
print(0)
elif x1 == x == y1 == y:
print(2)
print(1,2)
print(2,1)
elif x1 == x and y1 != y:
print(1)
if x == y:
print(n,n-1)
else:
print(n-1,n)
elif x1!=x and y == y1:
print(1)
if x == y:
print(1,2)
else:
print(2,1)
elif x1!=x and y!=y1:
print(2)
if x == y:
print(1,2)
print(n-1,n)
else:
print(1,2)
print(n,n-1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, o = 0, c = 0;
cin >> n;
vector<vector<char>> V(n, vector<char>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cin >> V[i][j];
}
if (V[1][0] == V[0][1] && V[n - 1][n - 2] == V[n - 2][n - 1]) {
if (V[1][0] == V[n - 1][n - 2]) {
cout << "2" << endl;
cout << "1"
<< " "
<< "2" << endl;
cout << "2"
<< " "
<< "1";
} else
cout << "0";
} else if (V[0][1] == V[1][0]) {
cout << "1" << endl;
if (V[n - 1][n - 2] != V[0][1])
cout << n - 1 << " " << n;
else
cout << n << " " << n - 1;
} else if (V[n - 1][n - 2] == V[n - 2][n - 1]) {
cout << "1" << endl;
if (V[n - 1][n - 2] != V[0][1])
cout << "2"
<< " "
<< "1";
else
cout << "1"
<< " "
<< "2";
} else {
cout << "2" << endl;
cout << "1"
<< " "
<< "2" << endl;
if (V[n - 2][n - 1] == V[0][1])
cout << n << " " << n - 1;
else
cout << n - 1 << " " << n;
}
cout << endl;
}
return 0;
}
| 8 |
CPP
|
def solve(n, grid):
pos = []
if grid[0][1] == grid[1][0]:
if grid[n - 1][n - 2] == grid[0][1]:
pos.append((n - 1, n - 2))
if grid[n - 2][n - 1] == grid[0][1]:
pos.append((n - 2, n - 1))
elif grid[n - 1][n - 2] == grid[n - 2][n - 1]:
if grid[0][1] == grid[n - 1][n - 2]:
pos.append((0, 1))
if grid[1][0] == grid[n - 1][n - 2]:
pos.append((1, 0))
elif grid[0][1] == grid[n - 2][n - 1] and grid[1][0] == grid[n - 1][n - 2]:
pos.append((0, 1))
pos.append((n - 1, n - 2))
else:
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
def is_valid(x, y, visited):
if x == 0 and y == 0:
return True
elif x < 0 or y < 0 or x >= n or y >= n:
return False
for _ in range(4):
nx, ny = x + dx[_], y + dy[_]
if (nx, ny) in visited:
continue
if not (0 <= nx < n and 0 <= ny < n):
continue
if nx == 0 and ny == 0:
return True
if grid[x][y] != grid[nx][ny]:
continue
visited.add((nx, ny))
if is_valid(nx, ny, visited):
return True
visited.discard((nx, ny))
return False
if is_valid(n - 1, n - 2, set()):
pos.append((n - 1, n - 2))
pos.append((1, 0))
if is_valid(n - 2, n - 1, set()):
pos.append((n - 2, n - 1))
pos.append((0, 1))
res = []
for x, y in pos:
res.append((x + 1, y + 1))
return res
t = int(input())
for _ in range(t):
n = int(input())
g = [input() for _ in range(n)]
pos = solve(n, g)
if not pos:
print(0)
else:
print(len(pos))
print('\n'.join('{} {}'.format(_[0], _[1]) for _ in pos))
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int n, t;
char s[205][205];
int main() {
cin >> t;
int i, j, k;
while (t--) {
cin >> n;
for (i = 1; i <= n; ++i) {
cin >> (s[i] + 1);
}
int cnt = 0;
bool i1 = 0, i2 = 0;
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[1][2]) {
++cnt;
i1 = 1;
}
if (s[n - 1][n] == s[1][2]) {
++cnt;
i2 = 1;
}
cout << cnt << endl;
if (i1) cout << n << " " << n - 1 << endl;
if (i2) cout << n - 1 << " " << n << endl;
continue;
}
if (s[1][2] != s[2][1]) {
if (s[n][n - 1] == s[n - 1][n]) {
cout << 1 << endl;
if (s[1][2] == s[n][n - 1]) {
cout << 1 << " " << 2 << endl;
} else
cout << 2 << " " << 1 << endl;
} else {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
if (s[n][n - 1] == s[1][2]) {
cout << n << " " << n - 1 << endl;
} else
cout << n - 1 << " " << n << endl;
}
}
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n=int(input())
s=input()
c1=s[1]
s=input()
c2=s[0]
for i in range(max(n-4,0)):
s=input()
if n==3:
c3=s[n-1]
s=input()
c4=s[n-2]
else:
s=input()
c3=s[n-1]
s=input()
c4=s[n-2]
if c1==c2:
if c3==c4:
if c3==c1:
print(2)
print(n-1,n)
print(n,n-1)
else:
print(0)
else:
if c3==c1:
print(1)
print(n-1,n)
else:
print(1)
print(n,n-1)
elif c4==c3:
if c1==c3:
print(1)
print(1,2)
else:
print(1)
print(2,1)
else:
if c1==c3:
print(2)
print(1,2)
print(n,n-1)
else:
print(2)
print(1,2)
print(n-1,n)
| 8 |
PYTHON3
|
test_cases = range(int(input()))
for test_case in test_cases:
n = int(input())
grid = []
for i in range(n):
new_line = [c for c in input()]
grid.append(new_line)
sr, sd = grid[0][1], grid[1][0]
fl, fu = grid[n-1][n-2], grid[n-2][n-1]
if sr == sd and fl == fu and sr != fl:
print(0)
continue
if sr == sd and fl == fu and sr == fl:
print(2)
print(1,2)
print(2,1)
continue
if sr == sd and sr == fl:
print(1)
print(n,n-1)
continue
if sr == sd and sr == fu:
print(1)
print(n-1, n)
continue
if fl == fu and fl == sr:
print(1)
print(1,2)
continue
if fl == fu and fl == sd:
print(1)
print(2,1)
continue
if fl == sd:
print(2)
print(n, n-1)
print(1, 2)
continue
if fl == sr:
print(2)
print(n, n-1)
print(2, 1)
continue
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
int n;
int list[201][201];
cin >> t;
while (t > 0) {
t--;
cin >> n;
char qwe;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == 0 && j == 0) || (i == (n - 1) && j == (n - 1))) {
cin >> qwe;
} else {
cin >> qwe;
list[i][j] = (int)qwe;
}
}
}
int b = list[n - 2][n - 1];
int c = list[n - 1][n - 2];
int d = list[n - 3][n - 1];
int e = list[n - 2][n - 2];
int f = list[n - 1][n - 3];
int num = 0;
if (f == e && e == d) {
if (c == e) {
num++;
}
if (b == e) {
num++;
}
cout << num << endl;
if (c == e) {
cout << n - 1 + 1 << " " << n - 2 + 1 << endl;
}
if (b == e) {
cout << n - 2 + 1 << " " << n - 1 + 1 << endl;
}
} else if (e != f && e != d) {
if (b == c) {
if (b == e) {
cout << 1 << endl << n - 2 + 1 << " " << n - 2 + 1 << endl;
} else {
cout << 2 << endl;
cout << n - 1 + 1 << " " << n - 3 + 1 << endl;
cout << n - 3 + 1 << " " << n - 1 + 1 << endl;
}
} else {
cout << 2 << endl;
cout << n - 2 + 1 << " " << n - 2 + 1 << endl;
if (c == e) {
cout << n - 2 + 1 << " " << n - 1 + 1 << endl;
} else {
cout << n - 1 + 1 << " " << n - 2 + 1 << endl;
}
}
} else {
if (b == c && b == e) {
cout << 2 << endl;
cout << n - 2 + 1 << " " << n - 2 + 1 << endl;
if (f == e) {
cout << n - 1 + 1 << " " << n - 3 + 1 << endl;
} else {
cout << n - 3 + 1 << " " << n - 1 + 1 << endl;
}
} else {
if (b == c) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
if (f == e) {
cout << n - 3 + 1 << " " << n - 1 + 1 << endl;
} else {
cout << n - 1 + 1 << " " << n - 3 + 1 << endl;
}
if (b == c) {
} else {
if (e == b) {
cout << n - 2 + 1 << " " << n - 1 + 1 << endl;
} else {
cout << n - 1 + 1 << " " << n - 2 + 1 << endl;
}
}
}
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
clock_t time_p = clock();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
char s[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> s[i][j];
}
}
vector<pair<int, int>> ans;
char a = s[2][1];
char b = s[1][2];
char c = s[n - 1][n];
char d = s[n][n - 1];
if (a == b && c == d && a == c) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << 2 << " " << 1 << "\n";
continue;
}
if (a == b) {
if (c == d) {
if (a != c) {
cout << 0 << "\n";
continue;
}
}
cout << 1 << "\n";
if (a == c) {
cout << n - 1 << " " << n << "\n";
} else {
cout << n << " " << n - 1 << "\n";
}
} else if (c == d) {
if (a == b) {
if (a != c) {
cout << 0 << "\n";
continue;
}
}
cout << 1 << "\n";
if (c == a) {
cout << 2 << " " << 1 << "\n";
} else {
cout << 1 << " " << 2 << "\n";
}
} else {
cout << 2 << "\n";
if (a == '0') {
cout << 2 << " " << 1 << "\n";
} else
cout << 1 << " " << 2 << "\n";
if (c == '1') {
cout << n - 1 << " " << n << "\n";
} else
cout << n << " " << n - 1 << "\n";
}
}
cerr << "\nTime Taken : " << (float)(clock() - time_p) / CLOCKS_PER_SEC
<< "\n";
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 210;
char s[maxn][maxn];
bool vis[maxn][maxn];
int d[][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n;
bool dfs(int x, int y, char ch) {
for (int i = 0; i < 4; ++i) {
int nx = x + d[i][0], ny = y + d[i][1];
if (nx == n && ny == n) return 1;
if (nx < 1 || nx > n || ny < 1 || ny > n || vis[nx][ny] || s[nx][ny] != ch)
continue;
vis[nx][ny] = 1;
if (dfs(nx, ny, ch)) return 1;
}
return 0;
}
bool check() {
memset(vis, 0, sizeof(vis));
return !dfs(1, 1, '0') && !dfs(1, 1, '1');
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> (s[i] + 1);
s[1][2] = ((s[1][2] - '0') ^ 1) + '0';
s[2][1] = ((s[2][1] - '0') ^ 1) + '0';
if (check()) {
cout << 2 << '\n';
cout << "1 2\n2 1\n";
continue;
}
s[1][2] = ((s[1][2] - '0') ^ 1) + '0';
s[2][1] = ((s[2][1] - '0') ^ 1) + '0';
s[n][n - 1] = ((s[n][n - 1] - '0') ^ 1) + '0';
s[n - 1][n] = ((s[n - 1][n] - '0') ^ 1) + '0';
if (check()) {
cout << 2 << '\n';
cout << n << ' ' << n - 1 << '\n';
cout << n - 1 << ' ' << n << '\n';
continue;
}
s[n][n - 1] = ((s[n][n - 1] - '0') ^ 1) + '0';
s[n - 1][n] = ((s[n - 1][n] - '0') ^ 1) + '0';
pair<int, int> a[3] = {make_pair(0, 0), make_pair(1, 2), make_pair(2, 1)};
pair<int, int> b[3] = {make_pair(0, 0), make_pair(n, n - 1),
make_pair(n - 1, n)};
for (int i = 0; i <= 2; ++i) {
int x = a[i].first, y = a[i].second;
if (x) s[x][y] = ((s[x][y] - '0') ^ 1) + '0';
for (int j = 0; j <= 2; ++j) {
int xx = b[j].first, yy = b[j].second;
if (xx) s[xx][yy] = ((s[xx][yy] - '0') ^ 1) + '0';
if (check()) {
if (x && xx) {
cout << 2 << '\n';
cout << x << ' ' << y << '\n';
cout << xx << ' ' << yy << '\n';
} else if (x) {
cout << 1 << '\n';
cout << x << ' ' << y << '\n';
} else if (xx) {
cout << 1 << '\n';
cout << xx << ' ' << yy << '\n';
} else {
cout << 0 << '\n';
}
goto done;
}
if (xx) s[xx][yy] = ((s[xx][yy] - '0') ^ 1) + '0';
}
if (x) s[x][y] = ((s[x][y] - '0') ^ 1) + '0';
}
done:
continue;
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
l = []
for i in range(n):
l.append(list(str(input())))
l2 = []
a1 = 0
a2 = 0
a3 = 0
a4 = 0
c = 0
a1+=2 - ((int(l[0][1])) + (int(l[1][0])))
a2+=((int(l[0][1])) + (int(l[1][0])))
a3 = 2 - ((int(l[n-1][n-2])) + (int(l[n-2][n-1])))
a4+= ((int(l[n-1][n-2])) + (int(l[n-2][n-1])))
if a1 == 0:
if l[n-1][n-2] == "1":
c+=1
l2.append([n,n-1])
if l[n-2][n-1] == "1":
l2.append([n-1,n])
c+=1
elif a1 == 1:
if a3 == 0:
if l[1][0] == "1":
c+=1
l2.append([2,1])
if l[0][1] == "1":
l2.append([1,2])
c+=1
elif a3 == 1:
if l[1][0] == "1":
c+=1
l2.append([2,1])
if l[0][1] == "1":
l2.append([1,2])
c+=1
if l[n-1][n-2] == "0":
c+=1
l2.append([n,n-1])
if l[n-2][n-1] == "0":
l2.append([n-1,n])
c+=1
else:
if l[1][0] == "0":
c+=1
l2.append([2,1])
if l[0][1] == "0":
l2.append([1,2])
c+=1
else:
if l[n-1][n-2] == "0":
c+=1
l2.append([n,n-1])
if l[n-2][n-1] == "0":
l2.append([n-1,n])
c+=1
if len(l2) == 0:
print(0)
else:
print(c)
for i in l2:
print(*i)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
template <typename T>
T min(T x, T y) {
return x < y ? x : y;
}
template <typename T>
T max(T x, T y) {
return x > y ? x : y;
};
const long long INF = 20000000050000;
const long long mod = 998244353;
const long long MAXN = 205;
int N;
char s[MAXN][MAXN];
void invert(char ch) {
if (s[1][3] != ch) printf("1 3\n");
if (s[2][2] != ch) printf("2 2\n");
if (s[3][1] != ch) printf("3 1\n");
}
void solve() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%s", s[i] + 1);
int cnt = 0;
cnt = (s[1][3] == '1') + (s[2][2] == '1') + (s[3][1] == '1');
if (s[1][2] == s[2][1]) {
if ((cnt == 0 || cnt == 3)) {
if (s[1][2] == s[1][3]) {
printf("2\n");
printf("1 2\n");
printf("2 1\n");
} else
printf("0\n");
} else {
if (s[1][2] == '1') {
printf("%d\n", cnt);
invert('0');
} else {
printf("%d\n", 3 - cnt);
invert('1');
}
}
} else {
if ((cnt == 0 || cnt == 3)) {
printf("1\n");
if (s[1][2] == s[1][3])
printf("1 2\n");
else
printf("2 1\n");
} else {
printf("2\n");
if (cnt == 2)
invert('1');
else
invert('0');
char t = (cnt == 2 ? '1' : '0');
if (s[1][2] == t)
printf("1 2\n");
else
printf("2 1\n");
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) solve();
return 0;
}
| 8 |
CPP
|
import itertools
import sys
from typing import List
sys.setrecursionlimit(10 ** 9)
def lmi():
return list(map(int, input().split()))
def main():
for _ in range(int(input())):
n = int(input())
grid = [list(input()) for _ in range(n)]
result = solve(n, grid)
print(len(result))
for point in result:
print(*point)
def solve(n, grid) -> list:
for pattern in itertools.product([False, True], repeat=4):
if pattern[0]:
grid[n - 1][n - 2] = reverse(grid[n - 1][n - 2])
if pattern[1]:
grid[n - 2][n - 1] = reverse(grid[n - 2][n - 1])
if pattern[2]:
grid[0][1] = reverse(grid[0][1])
if pattern[3]:
grid[1][0] = reverse(grid[1][0])
ok = True
for digit in ['0', '1']:
stack = [(0, 0)]
visited = set()
while stack:
y, x = stack.pop()
if y < 0 or n <= y or x < 0 or n <= x:
continue
if (y, x) in visited:
continue
if grid[y][x] == 'F':
ok = False
break
if grid[y][x] not in (digit, 'S'):
continue
visited.add((y, x))
for yd, xd in ((-1, 0), (+1, 0), (0, -1), (0, +1)):
i = y + yd
j = x + xd
stack.append((i, j))
if ok:
ans = []
if pattern[0]:
ans.append((n, n - 1))
if pattern[1]:
ans.append((n - 1, n))
if pattern[2]:
ans.append((1, 2))
if pattern[3]:
ans.append((2, 1))
return ans
if pattern[0]:
grid[n - 1][n - 2] = reverse(grid[n - 1][n - 2])
if pattern[1]:
grid[n - 2][n - 1] = reverse(grid[n - 2][n - 1])
if pattern[2]:
grid[0][1] = reverse(grid[0][1])
if pattern[3]:
grid[1][0] = reverse(grid[1][0])
def reverse(digit):
if digit == '0':
return '1'
else:
return '0'
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
from sys import *
input = stdin.readline
for _ in range(int(input())):
n = int(input())
matrix = []
for i in range(n):
a = []
s = input()
for em in s[:-1]:
if(em == 'S' or em == 'F'):
a.append(em)
else:
a.append(int(em))
matrix.append(a)
ans = []
if(matrix[0][1] == matrix[1][0]):
if(matrix[n-1][n-2] == matrix[0][1]):
ans.append((n,n-1))
if(matrix[n-2][n-1] == matrix[0][1]):
ans.append((n-1,n))
elif(matrix[n-1][n-2] == matrix[n-2][n-1]):
if(matrix[n-1][n-2] == matrix[0][1]):
ans.append((1,2))
if(matrix[n-2][n-1] == matrix[1][0]):
ans.append((2,1))
else:
sn = matrix[n-1][n-2] + matrix[n-2][n-1] + matrix[1][0] + matrix[0][1]
flag = -1
if(sn <= 2):
flag = 1
else:
flag = 0
if(matrix[0][1] != flag and matrix[1][0] != flag):
if(matrix[n-1][n-2] == flag):
ans.append((n,n-1))
if(matrix[n-2][n-1] == flag):
ans.append((n-1,n))
elif(matrix[n-1][n-2] != flag and matrix[n-2][n-1] != flag):
if(matrix[0][1] == flag):
ans.append((1,2))
if(matrix[1][0] == flag):
ans.append((2,1))
else:
if(matrix[n-1][n-2] == flag):
matrix[n-1][n-2] = 1-flag
ans.append((n,n-1))
if(matrix[n-2][n-1] == flag):
matrix[n-2][n-1] = 1-flag
ans.append((n-1,n))
if(matrix[0][1] != flag):
matrix[0][1] = flag
ans.append((1,2))
if(matrix[1][0] != flag):
matrix[1][0] = flag
ans.append((2,1))
stdout.write(str(len(ans))+'\n')
for x,y in ans:
stdout.write(str(x)+' '+str(y)+'\n')
| 8 |
PYTHON3
|
t=int(input())
for _ in range(t):
n=int(input())
l=[]
for i in range(n):
l.append(input())
if l[0][1]==l[1][0]:
if l[-2][-1]==l[-1][-2]:
if l[-2][-1]!=l[0][1]:
print(0)
else:
print(2)
print(n-1,n)
print(n,n-1)
else:
if l[-2][-1]==l[0][1]:
print(1)
print(n-1,n)
elif l[-1][-2]==l[0][1]:
print(1)
print(n,n-1)
else:
if l[-2][-1]==l[-1][-2]:
if l[0][1]==l[-2][-1]:
print(1)
print(1,2)
elif l[1][0]==l[-2][-1]:
print(1)
print(2,1)
else:
print(2)
print(1,2)
if l[-1][-2]==l[1][0]:
print(n,n-1)
elif l[-2][-1]==l[1][0]:
print(n-1,n)
| 8 |
PYTHON3
|
#include <CodeforcesSolutions.h>
#include <ONLINE_JUDGE <solution.cf(contestID = "1421",questionID = "A",method = "GET")>.h>
"""
Author : thekushalghosh
Team : CodeDiggers
I prefer Python language over the C++ language :p :D
Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time
start_time = time.time()
##########################################################################
################# ---- THE ACTUAL CODE STARTS BELOW ---- #################
def solve():
n = inp()
a = []
for i in range(n):
s = insr()
a.append(list(s))
q,w = a[0][1],a[1][0]
qq,ww = a[-1][-2],a[-2][-1]
qw = []
if len(set([q,w,qq,ww])) == 1:
qw = [[1,2],[2,1]]
elif q == w and qq == ww:
qw = []
elif q != w and qq != ww:
if q == "0":
qw.append([1,2])
if w == "0":
qw.append([2,1])
if qq == "1":
qw.append([len(a),len(a) - 1])
if ww == "1":
qw.append([len(a) - 1,len(a)])
else:
if q == w:
if qq == q:
qw.append([len(a),len(a) - 1])
else:
qw.append([len(a) - 1,len(a)])
else:
if q == qq:
qw.append([1,2])
else:
qw.append([2,1])
print(len(qw))
for i in range(len(qw)):
print(*qw[i])
################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
##########################################################################
def main():
global tt
if not ONLINE_JUDGE:
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
t = 1
t = inp()
for tt in range(1,t + 1):
solve()
if not ONLINE_JUDGE:
print("Time Elapsed :",time.time() - start_time,"seconds")
sys.stdout.close()
#---------------------- USER DEFINED INPUT FUNCTIONS ----------------------#
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
return(input().strip())
def invr():
return(map(int,input().split()))
#------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
def counter(a):
q = [0] * max(a)
for i in range(len(a)):
q[a[i] - 1] = q[a[i] - 1] + 1
return(q)
def counter_elements(a):
q = dict()
for i in range(len(a)):
if a[i] not in q:
q[a[i]] = 0
q[a[i]] = q[a[i]] + 1
return(q)
def string_counter(a):
q = [0] * 26
for i in range(len(a)):
q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
return(q)
def factorial(n,m = 1000000007):
q = 1
for i in range(n):
q = (q * (i + 1)) % m
return(q)
def factors(n):
q = []
for i in range(1,int(n ** 0.5) + 1):
if n % i == 0: q.append(i); q.append(n // i)
return(list(sorted(list(set(q)))))
def prime_factors(n):
q = []
while n % 2 == 0: q.append(2); n = n // 2
for i in range(3,int(n ** 0.5) + 1,2):
while n % i == 0: q.append(i); n = n // i
if n > 2: q.append(n)
return(list(sorted(q)))
def transpose(a):
n,m = len(a),len(a[0])
b = [[0] * n for i in range(m)]
for i in range(m):
for j in range(n):
b[i][j] = a[j][i]
return(b)
def power_two(x):
return (x and (not(x & (x - 1))))
def ceil(a, b):
return -(-a // b)
def seive(n):
a = [1]
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2,n + 1, p):
prime[i] = False
p = p + 1
for p in range(2,n + 1):
if prime[p]:
a.append(p)
return(a)
#-----------------------------------------------------------------------#
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
#import io,os
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
input = sys.stdin.readline
main()
| 8 |
PYTHON3
|
def flip(n):
if n=='0':
return '1'
else:
return '0'
def hi(a):
n = len(a)
c = 0
f = []
if a[0][1]==a[1][0] and a[n-1][n-2]==a[n-2][n-1] and a[0][1]!=a[n-1][n-2]:
return c,[]
if a[0][1]==a[1][0]:
p = a[0][1]
if a[n-1][n-2]==p:
a[n-1][n-2] = flip(p)
c+=1
f.append([str(n),str(n-1)])
if a[n-2][n-1]==p:
a[n-2][n-1] = flip(p)
c+=1
f.append([str(n-1),str(n)])
return c,f
else:
if a[n-1][n-2]==a[n-2][n-1]:
p = a[n-1][n-2]
if a[0][1]==p:
a[0][1] = flip(p)
c+=1
f.append(['1','2'])
else:
a[1][0] = flip(p)
c+=1
f.append(['2','1'])
return c,f
else:
p = a[n-1][n-2]
a[n-2][n-1] = p
c+=1
f.append([str(n-1),str(n)])
if a[0][1]==p:
c+=1
a[0][1] = flip(p)
f.append(['1','2'])
else:
c+=1
a[1][0] = flip(p)
f.append(['2','1'])
return c,f
t = int(input())
b = [[] for i in range(2*t)]
for i in range(t):
p = int(input())
a = []
for j in range(p):
a.append(list(input()))
b[2*i],b[2*i+1] = hi(a)
for i in range(len(b)):
if i%2==0:
print(b[i])
else:
if len(b)==0:
pass
else:
for j in range(len(b[i])):
p = " ".join(b[i][j])
print(p)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char ch[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> ch[i][j];
int co = 0;
pair<int, int> p[2];
if (ch[0][1] == ch[1][0]) {
char val;
if (ch[0][1] == '0')
val = '1';
else
val = '0';
if (ch[n - 1][n - 2] != val) {
p[co].first = n - 1;
p[co].second = n - 2;
co++;
}
if (ch[n - 2][n - 1] != val) {
p[co].first = n - 2;
p[co].second = n - 1;
co++;
}
} else if (ch[n - 1][n - 2] == ch[n - 2][n - 1]) {
char val;
if (ch[n - 1][n - 2] == '0')
val = '1';
else
val = '0';
if (ch[0][1] != val) {
p[co].first = 0;
p[co].second = 1;
co++;
}
if (ch[1][0] != val) {
p[co].first = 1;
p[co].second = 0;
co++;
}
} else {
if (ch[0][1] == '0') {
p[co].first = 0;
p[co].second = 1;
co++;
}
if (ch[1][0] == '0') {
p[co].first = 1;
p[co].second = 0;
co++;
}
if (ch[n - 1][n - 2] == '1') {
p[co].first = n - 1;
p[co].second = n - 2;
co++;
}
if (ch[n - 2][n - 1] == '1') {
p[co].first = n - 2;
p[co].second = n - 1;
co++;
}
}
cout << co << "\n";
for (int i = 0; i < co; i++) {
cout << p[i].first + 1 << " " << p[i].second + 1 << "\n";
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
int a[N][N];
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < s.size(); j++) a[i][j] = s[j] - '0';
}
vector<pair<int, int>> targets;
targets.push_back({0, 1});
targets.push_back({1, 0});
targets.push_back({N - 1, N - 2});
targets.push_back({N - 2, N - 1});
a[N - 1][N - 2] ^= 1;
a[N - 2][N - 1] ^= 1;
int one = 0, zero = 0;
for (int inv = 0; inv <= 2; inv++) {
vector<pair<int, int>> inverts;
for (auto Ti : targets) {
if (a[Ti.first][Ti.second] != inv) inverts.push_back(Ti);
}
if (inverts.size() <= 2) {
cout << inverts.size() << '\n';
for (auto& i : inverts)
cout << i.first + 1 << ' ' << i.second + 1 << '\n';
break;
}
}
}
return 0;
}
| 8 |
CPP
|
from collections import Counter,defaultdict
I =lambda:int(input())
M =lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))
for _ in range(I()):
n=I()
a=[]
for i in range(n):
s=input()
s=list(s)
a+=[s]
ans = []
if a[0][1] == a[1][0]:
x = a[0][1]
if a[-1][-2] == x: ans.append((n, n- 1))
if a[-2][-1] == x: ans.append((n - 1, n))
else:
if a[-1][-2] == a[-2][-1]:
x = a[-1][-2]
if a[0][1] == x: ans.append((1, 2))
if a[1][0] == x: ans.append((2, 1))
else:
x = a[0][1]
ans.append((2, 1))
if a[-1][-2] == x: ans.append((n, n-1))
if a[-2][-1] == x: ans.append((n-1, n))
print(len(ans))
for i in range(len(ans)):
print(*ans[i])
| 8 |
PYTHON3
|
import math as mt
import bisect
import sys
#input=sys.stdin.readline
t=int(input())
import collections
import heapq
#t=1
p=10**9+7
def ncr_util():
inv[0]=inv[1]=1
fact[0]=fact[1]=1
for i in range(2,300001):
inv[i]=(inv[i%p]*(p-p//i))%p
for i in range(1,300001):
inv[i]=(inv[i-1]*inv[i])%p
fact[i]=(fact[i-1]*i)%p
def solve():
l1=[]
#print(l[0][1],l[1][0],l[-2][-1],l[-1][-2])
if l[0][1]==l[1][0]:
x=str(int(l[0][1])^1)
#print(123,x)
if l[-1][-2]!=x:
l1.append([n,n-1])
if l[-2][-1]!=x:
l1.append([n-1,n])
else:
if l[n-1][n-2]==l[n-2][n-1]:
x=str(int(l[n-1][n-2])^1)
if l[0][1]!=x:
l1.append([1,2])
if l[1][0]!=x:
l1.append([2,1])
else:
x='1'
if l[0][1]!=x:
l1.append([1,2])
if l[1][0]!=x:
l1.append([2,1])
x='0'
if l[-1][-2]!=x:
l1.append([n,n-1])
if l[-2][-1]!=x:
l1.append([n-1,n])
print(len(l1))
if len(l1)>0:
for i in range(len(l1)):
print(l1[i][0],l1[i][1])
for _ in range(t):
n=int(input())
l=[]
for i in range(n):
l.append(input())
#s=input()
#n=int(input())
#n,x=(map(int,input().split()))
#n1=n
#a=int(input())
#b=int(input())
#n,x,k=map(int,input().split())
#l=list(map(int,input().split()))
#a,b=map(int,input().split())
#n=int(input())
#s=input()
#s1=input()
#p=input()
#l=list(map(int,input().split()))
#l.sort(revrese=True)
#l2=list(map(int,input().split()))
#l=str(n)
#l.sort(reverse=True)
#l2.sort(reverse=True)
#l1.sort(reverse=True)
#print(ans)
(solve())
| 8 |
PYTHON3
|
t=int(input())
for _ in range(t):
n=int(input())
f=[]
for i in range(n):
f.append(str(input()))
a=f[1][0]
b=f[0][1]
c=f[-1][-2]
d=f[-2][-1]
if a==b:
if c==d:
if a!=c:
print(0)
else:
print(2)
print(1,2)
print(2,1)
else:
print(1)
if c==a:
print(n,n-1)
else:
print(n-1,n)
else:
if c==d:
print(1)
if a==c:
print(2,1)
else:
print(1,2)
else:
print(2)
if a==c:
print(2,1)
print(n-1,n)
else:
print(1,2)
print(n-1,n)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
a = []
for i in range(n):
x = input()
s = [i for i in x]
a.append(s)
if a[0][1] == a[1][0]:
if a[-1][-2] == a[-2][-1] == a[0][1]:
print(2)
print(n,n-1)
print(n-1,n)
elif a[0][1] == a[-1][-2]:
print(1)
print(n,n-1)
elif a[1][0] == a[-2][-1]:
print(1)
print(n-1,n)
else:
print(0)
elif a[-1][-2] == a[-2][-1]:
if a[0][1] == a[1][0] == a[-1][-2]:
print(2)
print(1,2)
print(2,1)
elif a[0][1] == a[-1][-2]:
print(1)
print(1,2)
elif a[1][0] == a[-1][-2]:
print(1)
print(2,1)
else:
print(0)
else:
print(2)
if a[0][1] != a[-1][-2]:
print(1,2)
print(n,n-1)
else:
print(1,2)
print(n-1,n)
| 8 |
PYTHON3
|
import sys
input=sys.stdin.readline
for _ in range(int(input())):
n=int(input())
# a,b=map(int,input().split())
# s=input().strip()
# a=list(map(int,input().split()))
g=[]
ans,ans1=[],[]
for i in range(n):g.append(input())
s1,s2=g[0][1],g[1][0]
e1,e2=g[n-1][n-2],g[n-2][n-1]
if s1==s2=='0':
if e1=='0':
ans.append((n-1,n-2))
if e2=='0':
ans.append((n-2,n-1))
elif s1==s2=='1':
if e1=='1':
ans.append((n-1,n-2))
if e2=='1':
ans.append((n-2,n-1))
elif e1==e2=='0':
if s1=='0':
ans.append((0,1))
if s2=='0':
ans.append((1,0))
elif e1==e2=='1':
if s1=='1':
ans.append((0,1))
if s2=='1':
ans.append((1,0))
else:
if s1=='0':
ans.append((1,0))
if e1=='0':
ans.append((n-1,n-2))
if e2=='0':
ans.append((n-2,n-1))
else:
ans.append((1,0))
if e1=='1':
ans.append((n-1,n-2))
if e2=='1':
ans.append((n-2,n-1))
print(len(ans))
for i,j in ans:
print(i+1,j+1)
| 8 |
PYTHON3
|
for t in range(int(input())):
n=int(input())
mat=[]
for i in range(n):
mat.append(list(input()))
out=0
ans=[]
if mat[0][1]==mat[1][0]:
if mat[-1][-2]==mat[0][1]:
out+=1
ans.append([n,n-1])
if mat[n-2][n-1]==mat[0][1]:
out+=1
ans.append([n-1,n])
elif mat[n-1][n-2]==mat[n-2][n-1]:
if mat[0][1]==mat[n-1][n-2]:
ans.append([1,2])
out+=1
if mat[1][0]==mat[n-1][n-2]:
ans.append([2,1])
out+=1
else:
out=2
ans.append([1,2])
if mat[0][1]==mat[n-1][n-2]:
ans.append([n-1,n])
else:
ans.append([n,n-1])
print(out)
for i in ans:
print(*i)
| 8 |
PYTHON3
|
import sys
import math
from collections import defaultdict,Counter
input=sys.stdin.readline
# def print(x):
# sys.stdout.write(str(x)+"\n")
# sys.stdout=open("CP2/output.txt",'w')
# sys.stdin=open("CP2/input.txt",'r')
# mod=pow(10,9)+7
t=int(input())
for i in range(t):
n=int(input())
for j in range(n):
s=input().strip()
if j==0:
flag=int(s[1])
elif j==n-1:
flag3=int(s[-2])
else:
if j==1:
flag1=int(s[0])
if j==n-2:
flag2=int(s[-1])
l=[]
if flag==flag1:
if flag==0:
if flag2==0:
l.append([n-1,n])
if flag3==0:
l.append([n,n-1])
else:
if flag2==1:
l.append([n-1,n])
if flag3==1:
l.append([n,n-1])
elif flag2==flag3:
if flag2==0:
if flag==0:
l.append([1,2])
if flag1==0:
l.append([2,1])
else:
if flag==1:
l.append([1,2])
if flag1==1:
l.append([2,1])
else:
if flag==0:
l.append([2,1])
else:
l.append([1,2])
if flag2==0:
l.append([n-1,n])
else:
l.append([n,n-1])
print(len(l))
for j in l:
print(*j)
| 8 |
PYTHON3
|
# import sys; input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**7)
from collections import defaultdict
mod = 10 ** 9 + 7; INF = float("inf")
def getlist():
return list(map(int, input().split()))
def main():
T = int(input())
for _ in range(T):
N = int(input())
L = []
for i in range(N):
l = list(input())
L.append(l)
a1 = int(L[0][1])
a2 = int(L[1][0])
a3 = int(L[-2][-1])
a4 = int(L[-1][-2])
ans1 = []
cnt1 = 0
if a1 != 0:
ans1.append([1, 2])
cnt1 += 1
if a2 != 0:
ans1.append([2, 1])
cnt1 += 1
if a3 != 1:
ans1.append([N - 1, N])
cnt1 += 1
if a4 != 1:
ans1.append([N, N - 1])
cnt1 += 1
ans2 = []
cnt2 = 0
if a1 != 1:
ans2.append([1, 2])
cnt2 += 1
if a2 != 1:
ans2.append([2, 1])
cnt2 += 1
if a3 != 0:
ans2.append([N - 1, N])
cnt2 += 1
if a4 != 0:
ans2.append([N, N - 1])
cnt2 += 1
if cnt1 <= cnt2:
print(cnt1)
for i in range(len(ans1)):
print(*ans1[i])
else:
print(cnt2)
for i in range(len(ans2)):
print(*ans2[i])
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
arr = [list(input()) for _ in range(n)]
res = []
if arr[n-2][n-1] == arr[n-1][n-2]:
if arr[0][1] == arr[n-2][n-1]:
res.append((1, 2))
if arr[1][0] == arr[n-2][n-1]:
res.append((2, 1))
elif arr[0][1] == arr[1][0]:
if arr[n-2][n-1] == arr[0][1]:
res.append((n-1, n))
if arr[n-1][n-2] == arr[0][1]:
res.append((n, n-1))
else:
if arr[0][1] == "1":
res.append((1, 2))
if arr[1][0] == "1":
res.append((2, 1))
if arr[n-2][n-1] == "0":
res.append((n-1, n))
if arr[n-1][n-2] == "0":
res.append((n, n-1))
print(len(res))
for e in res:
print(*e)
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
n = int(input())
A = []
for _ in range(n):
A.append(input())
if A[0][1] == A[1][0] and A[-1][-2] == A[-2][-1]: # 4 cases
if A[0][1] != A[-1][-2]:
print(0)
else:
print(2)
print(1, 2)
print(2, 1)
elif A[0][1] != A[1][0] and A[-1][-2] != A[-2][-1]: # 4 cases
print(2)
if A[0][1] == A[-1][-2]:
print(2, 1)
print(n, n - 1)
else:
print(1, 2)
print(n, n - 1)
elif A[0][1] != A[1][0]: # 4 cases
print(1)
if A[-1][-2] == '0':
if A[0][1] == '1':
print(2, 1)
else:
print(1, 2)
else:
if A[0][1] == '0':
print(2, 1)
else:
print(1, 2)
else : # A[-1][-2] != A[-2][-1]: # 4 cases
print(1)
if A[1][0] == '0':
if A[-1][-2] == '1':
print(n - 1, n)
else:
print(n, n - 1)
else:
if A[-1][-2] == '0':
print(n - 1, n)
else:
print(n, n - 1)
| 8 |
PYTHON3
|
# your code goes here
for _ in range(int(input())):
n=int(input())
a=[[-1]*n for z in range(n)]
for z in range(n):
a[z]=list(input())
ansx=[]
if a[0][1]==a[1][0]:
v=a[0][1]
if a[-1][-2]==v:
ansx.append([n,n-1])
if a[-2][-1]==v:
ansx.append([n-1,n])
else:
if a[-1][-2]==a[-2][-1]:
v=a[-1][-2]
if a[0][1]==v:
ansx.append([1,2])
if a[1][0]==v:
ansx.append([2,1])
else:
v=a[0][1]
ansx=[[2,1]]
if a[-1][-2]==v:
ansx.append([n,n-1])
if a[-2][-1]==v:
ansx.append([n-1,n])
print(len(ansx))
for z in ansx:
print(*z)
| 8 |
PYTHON3
|
def process():
n=int(input())
li=[]
for i in range(n):
li.append(list(input()))
ans=[]
if(li[0][1]!=li[1][0] and li[n-1][n-2]!=li[n-2][n-1]):
if(li[0][1]!='0'):
ans.append((0,1))
if(li[1][0]!='0'):
ans.append((1,0))
if(li[n-1][n-2]!='1'):
ans.append((n-1,n-2))
if (li[n - 2][n - 1] != '1'):
ans.append((n - 2, n - 1))
elif(li[0][1]==li[1][0] and li[n-1][n-2]==li[n-2][n-1]):
if (li[0][1] == li[n-1][n-2]):
ans.append((0, 1))
if (li[1][0] == li[n-1][n-2]):
ans.append((1, 0))
elif(li[0][1]==li[1][0]):
if (li[n - 1][n - 2] == li[1][0]):
ans.append((n - 1, n - 2))
if (li[n - 2][n - 1] == li[1][0]):
ans.append((n - 2, n - 1))
elif(li[n-1][n-2]==li[n-2][n-1]):
if (li[0][1] == li[n-1][n-2]):
ans.append((0, 1))
if (li[1][0] == li[n-1][n-2]):
ans.append((1, 0))
print(len(ans))
for i in ans:
print(i[0]+1,i[1]+1)
tests=int(input())
for i in range(tests):
process()
| 8 |
PYTHON3
|
t=int(input())
while t>0:
t-=1
n=int(input())
a=[]
for i in range(n):
b=input()
a.append(b)
c=int(a[0][1])
d=int(a[1][0])
e=int(a[-1][-2])
f=int(a[-2][-1])
if c==d and e==f and c!=e:
print(0)
elif c==d and e==f and c==e:
print(2)
print(1,2)
print(2,1)
elif c!=d and e!=f:
print(2)
if c==0:
print(1,2)
if d==0:
print(2,1)
if e==1:
print(n,n-1)
if f==1:
print(n-1,n)
elif c==d:
print(1)
if c==e:
print(n,n-1)
else:
print(n-1,n)
elif e==f:
print(1)
if c==e:
print(1,2)
else:
print(2,1)
| 8 |
PYTHON3
|
def main():
for i in range(int(input())):
n = int(input())
matx = []
for i in range(n):
x = list(input())
matx.append(x)
x = matx[n-2][n-1]
y = matx[n-1][n-2]
a = matx[0][1]
b = matx[1][0]
v=a+b+x+y
if v=='0100' or v=='1011':
print(1)
print(1,2)
elif v=='0011' or v=='1100':
print(0)
elif v=='0111' or v=='1000':
print(1)
print(2,1)
elif v == '0110' or v == '1001':
print(2)
print(2, 1)
print(n,n-1)
elif v == '0000' or v == '1111':
print(2)
print(1,2)
print(2, 1)
elif v == '0101' or v == '1010':
print(2)
print(2, 1)
print(n-1,n)
elif v == '1110' or v == '0001':
print(1)
print(n-1, n)
elif v == '0010' or v == '1101':
print(1)
print(n, n-1)
if __name__ == "__main__":
main()
| 8 |
PYTHON3
|
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
# mod=1000000007
mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('a')
def ceil1(a,b):
return (a+b-1)//b
def solve():
for _ in range(ii()):
n = ii()
a = []
for i in range(n):
a.append(list(si()))
x = [[0,1],[1,0],[n-2,n-1],[n-1,n-2]]
for i in range(16):
ok = True
for j in range(4):
if i&(1<<j):
p = x[j]
x1 = a[p[0]][p[1]]
x1 = int(x1) ^ 1
a[p[0]][p[1]] = str(x1)
q = deque([[0,0]])
vis = [[0]*n for i in range(n)]
vis[0][0] = 1
while len(q) > 0:
u = q[0][0]
v = q[0][1]
if ok == False:
break
q.popleft()
for k in [[0,1],[1,0]]:
x1 = u + k[0]
x2 = v + k[1]
if x1 >= n or x2 >= n or vis[x1][x2]:
continue
if a[x1][x2] == 'F':
ok = False
break
if a[x1][x2] == '0':
continue
vis[x1][x2] = 1
q.append([x1,x2])
q = deque([[0,0]])
vis = [[0]*n for i in range(n)]
vis[0][0] = 1
while len(q) > 0:
u = q[0][0]
v = q[0][1]
if ok == False:
break
q.popleft()
for k in [[0,1],[1,0]]:
x1 = u + k[0]
x2 = v + k[1]
if x1 >= n or x2 >= n or vis[x1][x2]:
continue
if a[x1][x2] == 'F':
ok = False
break
if a[x1][x2] == '1':
continue
vis[x1][x2] = 1
q.append([x1,x2])
ans = []
for j in range(4):
if i&(1<<j):
ans.append(x[j])
p = x[j]
x1 = a[p[0]][p[1]]
x1 = int(x1) ^ 1
a[p[0]][p[1]] = str(x1)
if ok:
print(len(ans))
for i in ans:
print(i[0]+1,i[1]+1)
break
if __name__ =="__main__":
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
char mat[201][201];
int main() {
fast();
int n, t;
bool p, q;
cin >> t;
for (int cas = 0; cas < t; cas++) {
int x = 0;
q = p = false;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> mat[i][j];
if (mat[0][1] == mat[1][0]) p = true;
if (mat[n - 1][n - 2] == mat[n - 2][n - 1]) q = true;
if (q && p) {
if (mat[0][1] == mat[n - 1][n - 2])
cout << 2 << endl << 1 << " " << 2 << endl << 2 << " " << 1 << endl;
else
cout << 0 << endl;
} else if (!p && !q) {
cout << 2 << endl;
if (mat[0][1] == '0')
cout << 2 << " " << 1 << endl;
else
cout << 1 << " " << 2 << endl;
if (mat[n - 1][n - 2] == '0')
cout << n << " " << n - 1 << endl;
else
cout << n - 1 << " " << n << endl;
} else if (p && !q) {
cout << 1 << endl;
if (mat[0][1] == mat[n - 1][n - 2])
cout << n << " " << n - 1 << endl;
else
cout << n - 1 << " " << n << endl;
} else if (!p && q) {
cout << 1 << endl;
if (mat[0][1] == mat[n - 1][n - 2])
cout << 1 << " " << 2 << endl;
else
cout << 2 << " " << 1 << endl;
}
}
return 0;
}
| 8 |
CPP
|
'''Author- Akshit Monga'''
t=int(input())
for _ in range(t):
n=int(input())
mat=[[0for y in range(n)] for x in range(n)]
for i in range(n):
s=input()
for j in range(n):
mat[i][j]=s[j]
if mat[0][1]==mat[1][0]=='0':
if mat[n-2][n-1]==mat[n-1][n-2]=='1':
print(0)
continue
elif mat[n-2][n-1]==mat[n-1][n-2]=='0':
print(2)
print(n-1,n)
print(n,n-1)
continue
elif mat[n-2][n-1]=='1' and mat[n-1][n-2]=='0':
print(1)
print(n,n-1)
continue
elif mat[n-2][n-1]=='0' and mat[n-1][n-2]=='1':
print(1)
print(n-1,n)
continue
if mat[0][1]==mat[1][0]=='1':
if mat[n-2][n-1]==mat[n-1][n-2]=='0':
print(0)
continue
elif mat[n-2][n-1]==mat[n-1][n-2]=='1':
print(2)
print(n-1,n)
print(n,n-1)
continue
elif mat[n-2][n-1]=='0' and mat[n-1][n-2]=='1':
print(1)
print(n,n-1)
continue
elif mat[n-2][n-1]=='1' and mat[n-1][n-2]=='0':
print(1)
print(n-1,n)
continue
if mat[0][1]=='1' and mat[1][0]=='0':
if mat[n-2][n-1]==mat[n-1][n-2]=='1':
print(1)
print(1,2)
continue
elif mat[n-2][n-1]==mat[n-1][n-2]=='0':
print(1)
print(2,1)
continue
elif mat[n-2][n-1]=='1' and mat[n-1][n-2]=='0':
print(2)
print(1,2)
print(n,n-1)
continue
elif mat[n-2][n-1]=='0' and mat[n-1][n-2]=='1':
print(2)
print(2,1)
print(n,n-1)
continue
if mat[0][1]=='0' and mat[1][0]=='1':
if mat[n-2][n-1]==mat[n-1][n-2]=='1':
print(1)
print(2,1)
continue
elif mat[n-2][n-1]==mat[n-1][n-2]=='0':
print(1)
print(1,2)
continue
elif mat[n-2][n-1]=='1' and mat[n-1][n-2]=='0':
print(2)
print(1,2)
print(n-1,n)
continue
elif mat[n-2][n-1]=='0' and mat[n-1][n-2]=='1':
print(2)
print(1,2)
print(n,n-1)
continue
| 8 |
PYTHON3
|
import sys
for i in range(int(sys.stdin.readline())):
a = []
for z in range(int(sys.stdin.readline())):
a.append(str(sys.stdin.readline().strip()))
g1 = a[1][0]
g2 = a[0][1]
c1 = a[len(a)-2][len(a)-1]
c2 = a[len(a)-1][len(a)-2]
stat = False
stat2 = False
gstat = None
cstat = None
if g1 == g2:
stat = True
gstat = g1
if c1 == c2:
stat2 = True
cstat = c1
if gstat != None and cstat != None:
if gstat == cstat:
print(2)
print(1,2)
print(2,1)
else:
print(0)
elif gstat == None and cstat != None:
if g1 == cstat:
print(1)
print(2,1)
else:
print(1)
print(1,2)
elif cstat == None and gstat != None:
if c1 == gstat:
print(1)
print(len(a)-1,len(a))
else:
print(1)
print(len(a),len(a)-1)
else:
if g1 == c1:
print(2)
print(1,2)
print(len(a)-1,len(a))
else:
print(2)
print(1,2)
print(len(a),len(a)-1)
| 8 |
PYTHON3
|
import sys
from math import ceil, factorial, gcd
#from math import comb, perm only in python3
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
MOD = 10**9 + 7
INF = float('inf')
rl = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(int(input())):
n = int(input())
g = [sys.stdin.readline() for _ in range(n)]
ans = []
num = 0
l = [g[0][1], g[1][0], g[n-2][n-1], g[n-1][n-2]]
if len(set(l)) == 1:
num += 2
ans.append([1, 2])
ans.append([2, 1])
elif l[0] == l[1]:
if l[1] == l[2]:
num += 1
ans.append([n-1, n])
if l[1] == l[3]:
num += 1
ans.append([n, n-1])
elif l[0] != l[1] and l[1] == l[2] == l[3]:
num += 1
ans.append([2, 1])
elif l[0] != l[1] and l[0] == l[2] == l[3]:
num += 1
ans.append([1, 2])
elif l[0] != l[1] and l[0] == l[2]:
num += 2
ans.append([1,2])
ans.append([n, n-1])
elif l[0] != l[1] and l[0] == l[3]:
num += 2
ans.append([1, 2])
ans.append([n-1, n])
print(num)
if num:
for a in ans:
print(*a)
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
n = int(input())
brick = [[0 for _ in range(n)] for _ in range(n)]
brick[0][0] = "S"
brick[n-1][n-1] = "F"
for i in range(n):
string = input()
for j in range(len(string)):
if string[j] != "F" and string[j] != "S":
brick[i][j] = int(string[j])
else:
brick[i][j] = string[j]
# print(brick)
if (brick[0][1] == 1) and (brick[1][0] == 1) and (brick[n-2][n-1] == 0) and (brick[n-1][n-2] == 0):
print(0)
if (brick[0][1] == 0) and (brick[1][0] == 0) and (brick[n-2][n-1] == 1) and (brick[n-1][n-2] == 1):
print(0)
if (brick[0][1] == 1) and (brick[1][0] == 1) and (brick[n-2][n-1] == 0) and (brick[n-1][n-2] == 1):
print(1)
print(n, n-1)
if (brick[0][1] == 1) and (brick[1][0] == 1) and (brick[n - 2][n - 1] == 1) and (brick[n - 1][n - 2] == 0):
print(1)
print(n - 1, n)
if (brick[0][1] == 0) and (brick[1][0] == 1) and (brick[n - 2][n - 1] == 1) and (brick[n - 1][n - 2] == 1):
print(1)
print(2, 1)
if (brick[0][1] == 1) and (brick[1][0] == 0) and (brick[n - 2][n - 1] == 1) and (brick[n - 1][n - 2] == 1):
print(1)
print(1, 2)
if (brick[0][1] == 0) and (brick[1][0] == 0) and (brick[n - 2][n - 1] == 0) and (brick[n - 1][n - 2] == 1):
print(1)
print(n-1, n)
if (brick[0][1] == 0) and (brick[1][0] == 0) and (brick[n - 2][n - 1] == 1) and (brick[n - 1][n - 2] == 0):
print(1)
print(n, n-1)
if (brick[0][1] == 1) and (brick[1][0] == 0) and (brick[n - 2][n - 1] == 0) and (brick[n - 1][n - 2] == 0):
print(1)
print(2, 1)
if (brick[0][1] == 0) and (brick[1][0] == 1) and (brick[n - 2][n - 1] == 0) and (brick[n - 1][n - 2] == 0):
print(1)
print(1, 2)
if (brick[0][1] == 1) and (brick[1][0] == 1) and (brick[n - 2][n - 1] == 1) and (brick[n - 1][n - 2] == 1):
print(2)
print(n-1, n)
print(n, n-1)
if (brick[0][1] == 0) and (brick[1][0] == 0) and (brick[n - 2][n - 1] == 0) and (brick[n - 1][n - 2] == 0):
print(2)
print(n-1, n)
print(n, n-1)
if (brick[0][1] == 1) and (brick[1][0] == 0) and (brick[n - 2][n - 1] == 0) and (brick[n - 1][n - 2] == 1):
print(2)
print(1, 2)
print(n-1, n)
if (brick[0][1] == 1) and (brick[1][0] == 0) and (brick[n - 2][n - 1] == 1) and (brick[n - 1][n - 2] == 0):
print(2)
print(1, 2)
print(n, n-1)
if (brick[0][1] == 0) and (brick[1][0] == 1) and (brick[n - 2][n - 1] == 0) and (brick[n - 1][n - 2] == 1):
print(2)
print(1, 2)
print(n, n-1)
if (brick[0][1] == 0) and (brick[1][0] == 1) and (brick[n - 2][n - 1] == 1) and (brick[n - 1][n - 2] == 0):
print(2)
print(1, 2)
print(n-1, n)
| 8 |
PYTHON3
|
t=int(input())
for _ in range(t):
n=int(input())
row=[None]*n
for i in range(n):
row[i]=input()
(a_1,a_2) = ([row[0][1], row[1][0]], [row[n-2][n-1], row[n-1][n-2]])
if a_1==['0','0']:
c=2-a_2.count('1')
print(c)
if c!=0:
if a_2[0]!='1':
print(f'{n-1} {n}')
if a_2[1]!='1':
print(f'{n} {n-1}')
elif a_1==['1','1']:
c=2-a_2.count('0')
print(c)
if c!=0:
if a_2[0]!='0':
print(f'{n-1} {n}')
if a_2[1]!='0':
print(f'{n} {n-1}')
elif a_2==['0','0']:
c=2-a_1.count('1')
print(c)
if c!=0:
if a_1[0]!='1':
print('1 2')
if a_1[1]!='1':
print('2 1')
elif a_2==['1','1']:
c=2-a_1.count('0')
print(c)
if c!=0:
if a_1[0]!='0':
print('1 2')
if a_1[1]!='0':
print('2 1')
else:
print(2)
print('1 2')
if a_1[0]!=a_2[0]:
print(f'{n-1} {n}')
else:
print(f'{n} {n-1}')
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
char m[n][n];
for (long long int i = (0); i < (n); i++) {
for (long long int j = (0); j < (n); j++) cin >> m[i][j];
}
if (m[0][1] == '0' and m[1][0] == '0') {
if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << '2' << '\n';
cout << n - 1 << " " << n << '\n';
cout << n << " " << n - 1 << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << "0" << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "1" << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << "1" << '\n';
cout << n - 1 << " " << n << '\n';
}
}
if (m[0][1] == '1' and m[1][0] == '1') {
if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << '2' << '\n';
cout << n - 1 << " " << n << '\n';
cout << n << " " << n - 1 << '\n';
} else if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << "0" << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "1" << '\n';
cout << n - 1 << " " << n << '\n';
} else {
cout << "1" << '\n';
cout << n << " " << n - 1 << '\n';
}
}
if (m[0][1] == '1' and m[1][0] == '0') {
if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << "1" << '\n';
cout << 1 << " " << 2 << '\n';
} else if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "2" << '\n';
cout << 1 << " " << 2 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << "2" << '\n';
cout << 1 << " " << 2 << '\n';
cout << n - 1 << " " << n << '\n';
}
}
if (m[0][1] == '0' and m[1][0] == '1') {
if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << "1" << '\n';
cout << 2 << " " << 1 << '\n';
} else if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "2" << '\n';
cout << 2 << " " << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << "2" << '\n';
cout << 2 << " " << 1 << '\n';
cout << n - 1 << " " << n << '\n';
}
}
}
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
vector<pair<long long int, long long int>> ans;
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[n - 1][n - 2] == '0') {
if (a[0][1] == '0') {
ans.push_back({1, 2});
}
if (a[1][0] == '0') {
ans.push_back({2, 1});
}
} else {
if (a[0][1] == '1') {
ans.push_back({1, 2});
}
if (a[1][0] == '1') {
ans.push_back({2, 1});
}
}
} else {
if (a[1][0] == '0' && a[0][1] == '0') {
if (a[n - 1][n - 2] == '0') {
ans.push_back({n, n - 1});
}
if (a[n - 2][n - 1] == '0') {
ans.push_back({n - 1, n});
}
} else if (a[1][0] == '1' && a[0][1] == '1') {
if (a[n - 1][n - 2] == '1') {
ans.push_back({n, n - 1});
}
if (a[n - 2][n - 1] == '1') {
ans.push_back({n - 1, n});
}
} else if (a[n - 1][n - 2] == '1') {
ans.push_back({n, n - 1});
if (a[0][1] == '0') {
ans.push_back({1, 2});
}
if (a[1][0] == '0') {
ans.push_back({2, 1});
}
} else {
ans.push_back({n, n - 1});
if (a[0][1] == '1') {
ans.push_back({1, 2});
}
if (a[1][0] == '1') {
ans.push_back({2, 1});
}
}
}
cout << ((int)(ans).size()) << "\n";
for (auto it : ans) {
cout << it.first << " " << it.second << "\n";
}
}
}
| 8 |
CPP
|
t=int(input())
for _ in range(t):
n=int(input())
m=[]
for i in range(n):
l=input()
m.append(l)
a=m[0][1]
b=m[1][0]
c=m[n-2][n-1]
d=m[n-1][n-2]
if a==b:
if c==d:
if a==c:
print("2")
print(n,n-1)
print(n-1,n)
else:
print("0")
else:
print("1")
if a==c:
print(n-1,n)
else:
print(n,n-1)
else:
if c==d:
print("1")
if a==c:
print("1 2")
else:
print("2 1")
else:
print("2")
if a==c:
print("2 1")
print(n-1,n)
else:
print("2 1")
print(n,n-1)
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
n = int(input())
matrix = []
for i in range(n):
arr = list(input())
matrix.append(arr)
ans = []
if matrix[0][1]=='1' and matrix[1][0]=='1':
if matrix[n-1][n-2]=='1':
ans.append((n-1,n-2))
if matrix[n-2][n-1]=='1':
ans.append((n-2,n-1))
elif matrix[0][1]=='0' and matrix[1][0]=='0':
if matrix[n-1][n-2]=='0':
ans.append((n-1,n-2))
if matrix[n-2][n-1]=='0':
ans.append((n-2,n-1))
elif matrix[0][1]!= matrix[1][0]:
if matrix[n-1][n-2]=='1' and matrix[n-2][n-1]=='1':
if matrix[0][1]=='1':
ans.append((0,1))
else:
ans.append((1,0))
elif matrix[n-1][n-2]=='0' and matrix[n-2][n-1]=='0':
if matrix[0][1]=='0':
ans.append((0,1))
else:
ans.append((1,0))
else:
# Make start 0,0 and end as 1,1
if matrix[0][1]=='1':
ans.append((0,1))
else:
ans.append((1,0))
if matrix[n-1][n-2]=='0':
ans.append((n-1,n-2))
else:
ans.append((n-2,n-1))
length = len(ans)
print(length)
for i in range(length):
print(f'{ans[i][0]+1} {ans[i][1]+1}')
| 8 |
PYTHON3
|
def find(arr,N):
R=[]
if(arr[N-1][N-2]==arr[N-2][N-1]):
a=arr[N-1][N-2]
if(arr[1][0]==a): R.append((2,1))
if(arr[0][1]==a): R.append((1,2))
elif(arr[1][0]==arr[0][1]):
a=arr[1][0]
if(arr[N-1][N-2]==a): R.append((N,N-1))
if(arr[N-2][N-1]==a): R.append((N-1,N))
else:
R.append((N,N-1))
a=arr[N-1][N-2]
if(arr[1][0]!=a): R.append((2,1))
else: R.append((1,2))
print(len(R))
for x,y in R:
print(x,y)
def main():
for _ in range(int(input())):
N=int(input())
arr=[input().strip() for _ in range(N)]
find(arr,N)
main()
| 8 |
PYTHON3
|
t = int(input())
for q in range(t):
a = int(input())
arr = []
for w in range(a):
arr.append(list(input()))
x1 = int(arr[0][1])
x2 = int(arr[1][0])
x3 = int(arr[a - 2][a - 1])
x4 = int(arr[a - 1][a - 2])
if x1 == x2:
if x3 == x4:
if x1 == x3:
print(2)
print(1, 2)
print(2, 1)
else:
print(0)
else:
if x3 == x1:
print(1)
print(a - 1, a)
else:
print(1)
print(a, a - 1)
else:
if x1 == x3:
if x3 == x4:
print(1)
print(1, 2)
else:
print(2)
print(2, 1)
print(a - 1, a)
else:
if x3 == x4:
print(1)
print(2, 1)
else:
print(2)
print(2, 1)
print(a, a - 1)
| 8 |
PYTHON3
|
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 18 15:21:50 2020
@author: Dark Soul
"""
t=int(input(''))
for _ in range(t):
ans=[]
n=int(input(''))
for j in range(n):
ans.append(list(input('')))
x=ans[0][1]
y=ans[1][0]
z=ans[n-1][n-2]
a=ans[n-2][n-1]
if x==y:
if z==x:
if x==a:
print(2)
print(1,2)
print(2,1)
else:
print(1)
print(n,n-1)
else:
if x==a:
print(1)
print(n-1,n)
else:
print(0)
continue
if z==a:
if x==y:
if x==z:
print(2)
print(1,2)
print(2,1)
else:
print(0)
else:
if x==z:
print(1)
print(1,2)
else:
print(1)
print(2,1)
continue
if x==z:
print(2)
print(1,2)
print(n-1,n)
else:
print(2)
print(1,2)
print(n,n-1)
| 8 |
PYTHON3
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.