solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
bool db = false;
const long long INF = 1e18 + 100;
const long long MOD = 1e9 + 7;
const long long maxn = 2 * 1e6 + 1;
long long t, n, m, x, k, q;
long long a, b, c, d;
pair<long long, long long> pa, pb, pc, pd;
long long ans = 0;
string s[200];
vector<pair<long long, long long> > v;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
a = (s[1][0] == '1');
b = (s[0][1] == '1');
c = (s[n - 1][n - 2] == '1');
d = (s[n - 2][n - 1] == '1');
if (db) {
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "c: " << c << endl;
cout << "d: " << d << endl;
cout << endl;
}
pa = {1, 0};
pb = {0, 1};
pc = {n - 1, n - 2};
pd = {n - 2, n - 1};
ans = 0;
v.clear();
if (a == 0 and b == 0) {
if (c == 0) v.push_back(pc);
if (d == 0) v.push_back(pd);
} else if (a == 1 and b == 1) {
if (c == 1) v.push_back(pc);
if (d == 1) v.push_back(pd);
} else if (a == 0 and b == 1) {
if (c == 0 and d == 0) {
v.push_back(pa);
} else if (c == 0 and d == 1) {
v.push_back(pb);
v.push_back(pc);
} else if (c == 1 and d == 0) {
v.push_back(pb);
v.push_back(pd);
} else if (c == 1 and d == 1) {
v.push_back(pb);
}
} else if (a == 1 and b == 0) {
if (c == 0 and d == 0) {
v.push_back(pb);
} else if (c == 0 and d == 1) {
v.push_back(pb);
v.push_back(pd);
} else if (c == 1 and d == 0) {
v.push_back(pb);
v.push_back(pc);
} else if (c == 1 and d == 1) {
v.push_back(pa);
}
}
ans = v.size();
cout << ans << endl;
for (auto xy : v) {
cout << xy.first + 1 << " " << xy.second + 1 << endl;
}
if (db) {
cout << endl << endl;
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
bool check(vector<vector<int>> &a, int n, int x) {
deque<pair<int, int>> q;
map<pair<int, int>, bool> was;
q.push_back({1, 1});
was[{1, 1}] = true;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
while (!q.empty()) {
auto u = q.front();
q.pop_front();
for (int i = 0; i < 4; i++) {
int xx = u.first + dx[i];
int yy = u.second + dy[i];
if (xx == n && yy == n) return false;
if (a[xx][yy] == x && !was[{xx, yy}]) {
q.push_back({xx, yy});
was[{xx, yy}] = true;
}
}
}
return true;
}
void v(vector<vector<int>> &a, int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << a[i][j];
}
cout << endl;
}
}
void solve() {
vector<vector<int>> a;
int n;
cin >> n;
a = vector<vector<int>>(n + 2, vector<int>(n + 2, 2));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
char x;
cin >> x;
if (x != 'S' && x != 'F') {
a[i][j] = x - '0';
}
}
}
vector<pair<int, int>> c = {
{n - 1, n - 1}, {n - 1, n}, {n, n - 1}, {n - 2, n}, {n, n - 2}};
if (check(a, n, 1) && check(a, n, 0)) {
cout << 0 << endl;
return;
}
for (int i = 0; i < c.size(); i++) {
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
if (check(a, n, 1) && check(a, n, 0)) {
cout << 1 << '\n';
cout << c[i].first << ' ' << c[i].second << endl;
return;
}
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
}
for (int i = 0; i < c.size(); i++) {
for (int j = i + 1; j < c.size(); j++) {
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
a[c[j].first][c[j].second] = 1 - a[c[j].first][c[j].second];
if (check(a, n, 1) && check(a, n, 0)) {
cout << 2 << '\n';
cout << c[i].first << ' ' << c[i].second << endl;
cout << c[j].first << ' ' << c[j].second << endl;
return;
}
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
a[c[j].first][c[j].second] = 1 - a[c[j].first][c[j].second];
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
}
| 8 |
CPP
|
t = int(input())
for _ in range(t):
n = int(input())
field = [list(input()) for _ in range(n)]
s = (int(field[0][1]), int(field[1][0]))
f = (int(field[n-1][-2]), int(field[n-2][-1]))
ans = 0
coords = []
if len(set(s)) == 1:
if s[0] == f[0]:
ans += 1
coords.append((n, n-1))
if s[0] == f[1]:
ans += 1
coords.append((n-1, n))
elif len(set(f)) == 1:
if f[0] == s[0]:
ans += 1
coords.append((1, 2))
if f[0] == s[1]:
ans += 1
coords.append((2, 1))
else:
ans = 2
if s[0] == 0:
coords.append((2, 1))
elif s[0] == 1:
coords.append((1, 2))
if f[0] == 1:
coords.append((n-1, n))
elif f[0] == 0:
coords.append((n, n-1))
print(ans)
for coord in coords:
print(*coord)
| 8 |
PYTHON3
|
from bisect import *
from collections import *
from math import *
from heapq import *
from typing import List
from itertools import *
from operator import *
from functools import *
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
#------------------------------------------------------------------------
@lru_cache(None)
def fact(x):
if x<2:
return 1
return fact(x-1)*x
@lru_cache(None)
def per(i,j):
return fact(i)//fact(i-j)
@lru_cache(None)
def com(i,j):
return per(i,j)//fact(j)
def linc(f,t,l,r):
while l<r:
mid=(l+r)//2
if t>f(mid):
l=mid+1
else:
r=mid
return l
def rinc(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t<f(mid):
r=mid-1
else:
l=mid
return l
def ldec(f,t,l,r):
while l<r:
mid=(l+r)//2
if t<f(mid):
l=mid+1
else:
r=mid
return l
def rdec(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t>f(mid):
r=mid-1
else:
l=mid
return l
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def binfun(x):
c=0
for w in arr:
c+=ceil(w/x)
return c
def lowbit(n):
return n&-n
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
class smt:
def __init__(self,l,r,arr):
self.l=l
self.r=r
self.value=(1<<31)-1 if l<r else arr[l]
mid=(l+r)//2
if(l<r):
self.left=smt(l,mid,arr)
self.right=smt(mid+1,r,arr)
self.value&=self.left.value&self.right.value
#print(l,r,self.value)
def setvalue(self,x,val):
if(self.l==self.r):
self.value=val
return
mid=(self.l+self.r)//2
if(x<=mid):
self.left.setvalue(x,val)
else:
self.right.setvalue(x,val)
self.value=self.left.value&self.right.value
def ask(self,l,r):
if(l<=self.l and r>=self.r):
return self.value
val=(1<<31)-1
mid=(self.l+self.r)//2
if(l<=mid):
val&=self.left.ask(l,r)
if(r>mid):
val&=self.right.ask(l,r)
return val
class UFS:
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
for v in graph[u]:
if v not in d or d[v]>d[u]+graph[u][v]:
d[v]=d[u]+graph[u][v]
heappush(heap,(d[v],v))
return d
'''
import time
s=time.time()
for i in range(2000):
print(0)
e=time.time()
print(e-s)
'''
t=N()
for i in range(t):
n=N()
grid=[]
for j in range(n):
grid.append(input())
ans=[]
if grid[0][1]==grid[1][0]:
if grid[n-1][n-2]==grid[0][1]:
ans.append([n,n-1])
if grid[n-2][n-1]==grid[0][1]:
ans.append([n-1,n])
else:
if grid[n-1][n-2]==grid[n-2][n-1]:
if grid[n-1][n-2]==grid[0][1]:
ans.append([1,2])
if grid[n-1][n-2]==grid[1][0]:
ans.append([2,1])
else:
if grid[0][1]=='1':
ans.append([1,2])
else:
ans.append([2,1])
if grid[n-1][n-2]=='0':
ans.append([n,n-1])
else:
ans.append([n-1,n])
print(len(ans))
if ans:
for a in ans:
print(*a)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n=int(input())
ar=[]
for i in range(n):
ar.append(list(input()))
st=[int(ar[0][1]),int(ar[1][0])]
fi=[int(ar[n-1][n-2]),int(ar[n-2][n-1])]
ans=[]
if(st[0]!=st[1] and fi[0]!=fi[1]):
if(st[0]!=0):
ans.append([1,2])
if(st[1]!=0):
ans.append([2,1])
if(fi[0]!=1):
ans.append([n,n-1])
if(fi[1]!=1):
ans.append([n-1,n])
elif(st[0]==st[1]):
ty=abs(st[0]-1)
if(fi[0]!=ty):
ans.append([n,n-1])
if(fi[1]!=ty):
ans.append([n-1,n])
elif(fi[0]==fi[1]):
ty=abs(fi[0]-1)
if(st[0]!=ty):
ans.append([1,2])
if(st[1]!=ty):
ans.append([2,1])
print(len(ans))
for i in ans:
print(*i)
| 8 |
PYTHON3
|
from sys import stdin,stdout
# def valid(x,y):
# return 0<=x<n and 0<=y<n
# def fn(x,y,dig):
# global ans
# vis[x][y]=1
# if a[x][y]=='F':return True
# for xx,yy in zip([1,-1,0,0],[0,0,1,-1]):
# if valid(x+xx,y+yy) and not vis[x+xx][y+yy] and (a[x+xx][y+yy] in ['F',dig]):
# # if a[x+xx][y+yy]=='F':
# # ans+=[[x+1,y+1]]
# # return True
# if fn(x+xx,y+yy,dig)==True:return True
# return False
for _ in range(int(stdin.readline())):
n=int(stdin.readline())
# a=[list(map(int,stdin.readline().split())) for _ in range(n)]
a=[list(input()) for _ in range(n)]
ans=[]
# for i in range(n):
# for j in range(n):
# if i==j==0 or (i==j==n-1):continue
#
# ori=a[i][j]
# a[i][j]='1' if ori=='0' else '0'
# if fn(0,0,ori)==False:
# ans+=[[i+1,j+1]]
# a[i][j]=ori
# if len(ans)>=2:break
# if len(ans) >= 2: break
# vis = [[0 for _ in range(n)] for _ in range(n)]
# fn(0,0,'0')
# vis = [[0 for _ in range(n)] for _ in range(n)]
# fn(0,0,'1')
# print(len(ans))
# for k,v in ans:
# print(k,v)
if a[0][1]==a[1][0]=='0':
if a[-2][-1]=='0':
ans+=[[n-1,n]]
if a[-1][-2]=='0':
ans+=[[n,n-1]]
elif a[0][1]==a[1][0]=='1':
if a[-2][-1]=='1':
ans+=[[n-1,n]]
if a[-1][-2]=='1':
ans+=[[n,n-1]]
elif a[-1][-2]==a[-2][-1]=='0':
if a[0][1]=='0':
ans+=[[1,2]]
if a[1][0]=='0':
ans+=[[2,1]]
elif a[-1][-2]==a[-2][-1]=='1':
if a[0][1]=='1':
ans+=[[1,2]]
if a[1][0]=='1':
ans+=[[2,1]]
else:
if a[0][1]!=a[-1][-2]:
ans+=[[1,2],[n,n-1]]
elif a[0][1]!=a[-2][-1]:
ans+=[[1,2],[n-1,n]]
print(len(ans))
for k,v in ans:
print(k,v)
| 8 |
PYTHON3
|
import sys
input = sys.stdin.readline
for f in range(int(input())):
n=int(input())
grid=[]
for i in range(n):
line=input()
line=line[0:n]
grid.append(line)
if grid[0][1]==grid[1][0]:
c=0
cs=[]
if grid[-1][-2]==grid[0][1]:
c+=1
cs.append([n,n-1])
if grid[-2][-1]==grid[0][1]:
c+=1
cs.append([n-1,n])
print(c)
for x in cs:
print(*x)
else:
if grid[-1][-2]==grid[-2][-1]:
print(1)
if grid[0][1]==grid[-1][-2]:
print(1,2)
else:
print(2,1)
else:
print(2)
print(2,1)
if grid[-2][-1]==grid[0][1]:
print(n-1,n)
else:
print(n,n-1)
| 8 |
PYTHON3
|
import math
t = int(input())
for _ in range(t):
n = int(input())
grid = []
for i in range(n):
grid.append(list(input()))
cnt1 = 0
ans1 = []
cnt2 = 0
ans2 = []
for i in range(3):
if grid[n-1-i][n-3+i]!='0':
cnt1 += 1
ans1.append((n-i,n-2+i))
for i in range(2):
if grid[n-1-i][n-2+i]!='1':
cnt1 += 1
ans1.append((n-i,n-1+i))
for i in range(3):
if grid[n-1-i][n-3+i]!='1':
cnt2 += 1
ans2.append((n-i,n-2+i))
for i in range(2):
if grid[n-1-i][n-2+i]!='0':
cnt2 += 1
ans2.append((n-i,n-1+i))
if cnt1 <=2:
print(cnt1)
for i in range(cnt1):
print(ans1[i][0],ans1[i][1])
else:
print(cnt2)
for i in range(cnt2):
print(ans2[i][0],ans2[i][1])
| 8 |
PYTHON3
|
def solve():
# put code here
n=int(input())
mat=[]
for _ in range(n):
mat.append(input())
if (mat[0][1] != mat[1][0] and
mat[n-2][n-1] != mat[n-1][n-2]):
print(2)
if mat[0][1]=='0':
print(1,2)
else:
print(2,1)
if mat[n-2][n-1] == '1':
print(n-1,n)
else:
print(n,n-1)
elif (mat[0][1] == mat[1][0] and
mat[n-2][n-1]!=mat[n-1][n-2]):
print(1)
if mat[0][1]==mat[n-2][n-1]:
print(n-1, n)
else:
print(n, n-1)
elif (mat[n-2][n-1] == mat[n-1][n-2] and
mat[0][1] != mat[1][0]):
print(1)
if mat[n-2][n-1]==mat[0][1]:
print(1,2)
else:
print(2,1)
elif (mat[n-2][n-1] == mat[n-1][n-2] and
mat[0][1] == mat[1][0] and
mat[n-2][n-1] == mat[0][1]):
print(2)
print(1,2)
print(2,1)
elif (mat[n-2][n-1] == mat[n-1][n-2] and
mat[0][1] == mat[1][0] and
mat[n-2][n-1] != mat[0][1]):
print(0)
else:
raise Exception
#print('\n')
t = int(input())
for _ in range(t):
solve()
| 8 |
PYTHON3
|
i = int(input())
for _ in range(i):
ii = int(input())
lz = []
for _ in range(ii):
lz.append(list(input()))
scor = [lz[0][1], lz[1][0]]
lcor = [lz[-2][-1], lz[-1][-2]]
zer = scor.count('0')
zer += lcor.count('0')
if zer == 1:
print(1)
if scor[0] == '0':
print('2 1')
elif scor[1] == '0':
print('1 2')
elif lcor[0] == '0':
print(str(ii)+' '+str(ii-1))
elif lcor[1] == '0':
print(str(ii-1)+' '+str(ii))
elif zer == 3:
print(1)
if scor[0] == '1':
print('2 1')
elif scor[1] == '1':
print('1 2')
elif lcor[0] == '1':
print(str(ii)+' '+str(ii-1))
elif lcor[1] == '1':
print(str(ii-1)+' '+str(ii))
elif zer == 2:
if scor.count('0') == 2 or scor.count('1') == 2:
print(0)
else:
print(2)
print('1 2')
if scor[0] == '1':
if lcor[0] == '1':
print(str(ii)+' '+str(ii-1))
elif lcor[1] == '1':
print(str(ii-1)+' '+str(ii))
else:
if lcor[0] == '0':
print(str(ii)+' '+str(ii-1))
elif lcor[1] == '0':
print(str(ii-1)+' '+str(ii))
else:
print(2)
print('1 2')
print('2 1')
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 2e18;
const long long int N = 2e5 + 5;
bool isPrime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
long long int factorial(long long int n) {
long long int ans = 1;
for (long long int i = 1; i <= n; i++) ans = (ans * i) % 1000000007;
return ans;
}
long long int exp(long long int x, long long int n) {
long long int res = 1;
while (n > 0) {
if (n % 2 == 1) res = (res * x) % 1000000007;
x = (x * x) % 1000000007;
n = n / 2;
}
return res;
}
void null() {
long long int n;
cin >> n;
char a[n][n];
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
long long int ans = 0;
if (a[0][1] == a[1][0]) {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[n - 1][n - 2] == a[0][1]) {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
cout << 2 << " " << 1 << '\n';
return;
} else {
cout << 0 << '\n';
return;
}
}
if (a[n - 1][n - 2] != a[n - 2][n - 1]) {
if (a[n - 1][n - 2] == a[0][1]) {
cout << 1 << '\n';
cout << n << " " << n - 1 << '\n';
return;
} else if (a[n - 2][n - 1] == a[0][1]) {
cout << 1 << '\n';
cout << n - 1 << " " << n << '\n';
return;
}
}
} else {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[0][1] == a[n - 1][n - 2]) {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
return;
} else {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
return;
}
} else {
cout << 2 << '\n';
if (a[0][1] == a[n - 1][n - 2]) {
cout << 1 << " " << 2 << '\n';
cout << n - 1 << " " << n << '\n';
return;
} else if (a[0][1] == a[n - 2][n - 1]) {
cout << 1 << " " << 2 << '\n';
cout << n << " " << n - 1 << '\n';
return;
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t = 1;
clock_t start, end;
start = clock();
cin >> t;
while (t--) {
null();
}
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, i, j, c;
char chr;
cin >> t;
bool first_diag, second_diag;
while (t--) {
cin >> n;
string arr[n];
vector<int> v;
c = 0;
for (i = 0; i < n; i++) cin >> arr[i];
first_diag = (arr[0][1] == arr[1][0]);
second_diag = (arr[n - 2][n - 1] == arr[n - 1][n - 2]);
if (first_diag && second_diag) {
if (arr[n - 2][n - 1] == arr[0][1]) {
c = 2;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(1);
}
} else if (first_diag) {
c = 1;
if (arr[n - 1][n - 2] == arr[0][1]) {
v.push_back(n);
v.push_back(n - 1);
} else {
v.push_back(n - 1);
v.push_back(n);
}
} else if (second_diag) {
c = 1;
if (arr[n - 1][n - 2] == arr[0][1]) {
v.push_back(1);
v.push_back(2);
} else {
v.push_back(2);
v.push_back(1);
}
} else {
c = 2;
v.push_back(1);
v.push_back(2);
if (arr[0][1] == arr[n - 2][n - 1]) {
v.push_back(n);
v.push_back(n - 1);
} else {
v.push_back(n - 1);
v.push_back(n);
}
}
cout << c << "\n";
for (i = 0; i < c; i++) cout << v[2 * i] << " " << v[2 * i + 1] << "\n";
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
s = []
for __ in range(n):
s.append([int(x) for x in input() if x.isdigit()])
s[0].insert(0,0)
s[n-1].insert(n-1,0)
c = 0
ans = []
if s[0][1] and s[1][0]:
if s[n-1][n-2]:
c += 1
ans.append([n,n-1])
if s[n-2][n-1]:
c += 1
ans.append([n-1,n])
print(c)
for i in ans:
print(*i)
continue
elif not(s[0][1] or s[1][0]):
if not s[n-1][n-2]:
c += 1
ans.append([n,n-1])
if not s[n-2][n-1]:
c += 1
ans.append([n-1,n])
print(c)
for i in ans:
print(*i)
continue
elif s[n-1][n-2] and s[n-2][n-1]:
if s[0][1]:
c += 1
ans.append([1,2])
if s[1][0]:
c += 1
ans.append([2,1])
print(c)
for i in ans:
print(*i)
continue
elif not(s[n-1][n-2] or s[n-2][n-1]):
if not s[0][1]:
c += 1
ans.append([1,2])
if not s[1][0]:
c += 1
ans.append([2,1])
print(c)
for i in ans:
print(*i)
continue
else:
#elif s[0][1] or s[1][0]:
if s[n-1][n-2] or s[n-2][n-1]:
if s[n-1][n-2]:
if s[0][1]:
print(2)
print(1, 2)
print(n-1, n)
continue
else:
print(2)
print(2, 1)
print(n-1, n)
continue
else:
if s[0][1]:
print(2)
print(1, 2)
print(n, n-1)
continue
else:
print(2)
print(2, 1)
print(n, n-1)
continue
else:
if not s[0][1]:
print(1)
print(0, 1)
continue
else:
print(1)
print(1, 1)
continue
| 8 |
PYTHON3
|
cases = int(input())
for c in range(cases):
n = int(input())
mat = []
for i in range(n):
row = input()
mat.append(row)
r0c1 = mat[0][1]
r1c0 = mat[1][0]
rncp = mat[n-1][n-2]
rpcn = mat[n-2][n-1]
if r0c1 == r1c0:
changes = []
if rncp == r0c1:
changes.append(str(n-0) + " " + str(n-1))
if rpcn == r0c1:
changes.append(str(n-1) + " " + str(n-0))
print(len(changes))
for u in changes:
print(u)
elif rncp == rpcn:
changes = []
if rncp == r0c1:
changes.append('1' + " " + '2')
if rpcn == r1c0:
changes.append('2' + " " + '1')
print(len(changes))
for u in changes:
print(u)
else:
changes = []
if r0c1 != '0':
changes.append('1' + " " + '2')
if r1c0 != '0':
changes.append('2' + " " + '1')
if rncp != '1':
changes.append(str(n-0) + " " + str(n-1))
if rpcn != '1':
changes.append(str(n-1) + " " + str(n-0))
print(len(changes))
for u in changes:
print(u)
| 8 |
PYTHON3
|
from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = [list(input().rstrip()) for _ in range(n)]
ans = []
if a[0][1] == a[1][0]:
if a[-1][-2] == a[0][1]:
ans.append((n, n-1))
if a[-2][-1] == a[0][1]:
ans.append((n-1, n))
elif a[-1][-2] == a[-2][-1]:
if a[0][1] == a[-1][-2]:
ans.append((1, 2))
if a[1][0] == a[-1][-2]:
ans.append((2, 1))
else:
if a[0][1] == "1":
ans.append((1, 2))
if a[1][0] == "1":
ans.append((2, 1))
if a[-1][-2] == "0":
ans.append((n, n-1))
if a[-2][-1] == "0":
ans.append((n-1, n))
print(len(ans))
for i in ans:
print(*i)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<string> a;
void input() {
a.clear();
cin >> n;
for (int i = 0; i < n; i++) {
string k;
cin >> k;
a.push_back(k);
}
}
void solve() {
long long int z = 0, o = 0;
if (a[0][1] == '0')
z++;
else
o++;
if (a[1][0] == '0')
z++;
else
o++;
if (a[n - 2][n - 1] == '0')
z++;
else
o++;
if (a[n - 1][n - 2] == '0')
z++;
else
o++;
if (a[0][1] == a[1][0] && a[n - 2][n - 1] == a[n - 1][n - 2] &&
a[0][1] == a[n - 2][n - 1]) {
cout << 2 << endl << "1 2" << endl << "2 1" << endl;
} else if (a[0][1] == a[1][0] && a[n - 2][n - 1] == a[n - 1][n - 2] &&
a[0][1] != a[n - 2][n - 1]) {
cout << 0 << endl;
} else if (z == 1) {
if (a[0][1] == '0')
cout << 1 << endl << "2 1" << endl;
else if (a[1][0] == '0')
cout << 1 << endl << "1 2" << endl;
else if (a[n - 2][n - 1] == '0')
cout << 1 << endl << n << " " << n - 1 << endl;
else if (a[n - 1][n - 2] == '0')
cout << 1 << endl << n - 1 << " " << n << endl;
} else if (o == 1) {
if (a[0][1] == '1')
cout << 1 << endl << "2 1" << endl;
else if (a[1][0] == '1')
cout << 1 << endl << "1 2" << endl;
else if (a[n - 2][n - 1] == '1')
cout << 1 << endl << n << " " << n - 1 << endl;
else if (a[n - 1][n - 2] == '1')
cout << 1 << endl << n - 1 << " " << n << endl;
} else {
cout << 2 << endl;
if (a[0][1] == '1') cout << "2 1" << endl;
if (a[1][0] == '1') cout << "1 2" << endl;
if (a[n - 2][n - 1] == '0') cout << n << " " << n - 1 << endl;
if (a[n - 1][n - 2] == '0') cout << n - 1 << " " << n << endl;
}
}
int main() {
long long int t;
cin >> t;
while (t--) {
input();
solve();
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;
vector<pair<int, int>> del{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool dfsf(int n, vector<string>& g) {
vector<vector<bool>> v(n, vector<bool>(n, true));
queue<pair<int, int>> q;
q.push({0, 0});
while (!q.empty()) {
auto tp = q.front();
q.pop();
if (v[tp.first][tp.second]) {
v[tp.first][tp.second] = false;
for (auto x : del) {
int xx = x.first + tp.first, yy = x.second + tp.second;
if (xx >= 0 && yy >= 0 && xx < n && yy < n && v[xx][yy] &&
g[xx][yy] == '.')
q.push({xx, yy});
}
}
}
return !v[n - 1][n - 1];
}
bool f(int n, vector<string> g) {
vector<string> g1(n, string(n, '.')), g2(n, string(n, '.'));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (g[i][j] == '1')
g1[i][j] = '#';
else if (g[i][j] == '0')
g2[i][j] = '#';
return dfsf(n, g1) || dfsf(n, g2);
}
void solve() {
int n;
cin >> n;
vector<string> g(n);
for (int i = 0; i < n; i++) cin >> g[i];
if (!f(n, g)) {
cout << "0\n";
return;
}
vector<pair<int, int>> nodes{{1, 0}, {0, 1}, {n - 1, n - 2}, {n - 2, n - 1}};
for (auto x : nodes) {
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
if (!f(n, g)) {
cout << "1\n";
cout << x.first + 1 << " " << x.second + 1 << "\n";
return;
}
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
}
for (auto x : nodes)
for (auto y : nodes) {
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
g[y.first][y.second] = (g[y.first][y.second] == '1' ? '0' : '1');
if (!f(n, g)) {
cout << "2\n";
cout << x.first + 1 << " " << x.second + 1 << " " << y.first + 1 << " "
<< y.second + 1 << "\n";
return;
}
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
g[y.first][y.second] = (g[y.first][y.second] == '1' ? '0' : '1');
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
long long maxi = LLONG_MAX;
long long mini = LLONG_MIN;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int main() {
fast();
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
char a[n][n];
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
cin >> a[i][j];
}
}
vector<pair<long long, long long> > ans;
if (a[0][1] + a[1][0] == '0' + '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[0][1] + a[1][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] + a[n - 2][n - 1] == '1' + '1') {
if (a[0][1] == '1') ans.push_back({1, 2});
if (a[1][0] == '1') ans.push_back({2, 1});
} else if (a[n - 1][n - 2] + a[n - 2][n - 1] == '0' + '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] + a[1][0] == '1' + '0') &&
(a[n - 1][n - 2] + a[n - 2][n - 1] == '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});
if (a[0][1] == '1') ans.push_back({1, 2});
if (a[1][0] == '1') ans.push_back({2, 1});
}
cout << ans.size() << endl;
for (auto p : ans) {
cout << p.first << ' ' << p.second << "\n";
;
}
}
return 0;
}
| 8 |
CPP
|
#Consistency is the key.
#code by: amritanshu
from sys import stdin,stdout
import math
from collections import deque
input=stdin.readline
def print(*args,end='\n'):
s=[]
for i in args:
s.append(str(i)+' ')
s=''.join(s)
stdout.write(s+end)
def solve():
n=int(input())
mat=[]
for i in range(n):
mat.append(list(input()))
ans=[]
if mat[0][1]==mat[1][0] and mat[n-1][n-2]==mat[n-2][n-1] and mat[n-1][n-2]!=mat[0][1]:
print(0)
return
elif mat[0][1]==mat[1][0] and mat[n-1][n-2]==mat[n-2][n-1] and mat[n-1][n-2]==mat[0][1]:
print(2)
print(1,2)
print(2,1)
return
if mat[0][1]==mat[1][0] or mat[n-1][n-2]==mat[n-2][n-1]:
if mat[0][1]==mat[1][0]:
if mat[n-2][n-1]==mat[0][1]:
print(1)
print(n-1,n)
else:
print(1)
print(n,n-1)
else:
if mat[0][1]==mat[n-1][n-2]:
print(1)
print(1,2)
else:
print(1)
print(2,1)
else:
if mat[0][1]==mat[n-1][n-2]:
print(2)
print(1,2)
print(n-1,n)
elif mat[1][0]==mat[n-2][n-1]:
print(2)
print(2,1)
print(n,n-1)
elif mat[1][0]==mat[n-1][n-2]:
print(2)
print(2,1)
print(n-1,n)
else:
print(2)
print(1,2)
print(n,n-1)
tt=1
tt=int(input())
for __ in range(tt):
solve()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e18;
const long long minN = -1e18;
const int INF = 2e9;
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const long double PI = acos(-1);
const long long limit = 2e5 + 5;
const long long limit1 = 1e6 + 5;
const long long limit2 = 1e3 + 5;
pair<int, int> dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
pair<int, int> NON = {-1, -1};
int t, n;
char c[250][250];
void solveProblem() {
if (c[1][2] == c[2][1]) {
if (c[n - 1][n] == c[n][n - 1]) {
if (c[1][2] == c[n - 1][n]) {
cout << 2 << '\n' << 1 << " " << 2 << '\n' << 2 << " " << 1 << '\n';
}
if (c[1][2] != c[n - 1][n]) {
cout << 0 << '\n';
}
} else {
if (c[1][2] == c[n - 1][n]) {
cout << 1 << '\n' << n - 1 << " " << n << '\n';
} else
cout << 1 << '\n' << n << " " << n - 1 << '\n';
}
} else {
if (c[n][n - 1] == c[n - 1][n]) {
if (c[n][n - 1] == c[1][2]) {
cout << 1 << '\n' << 1 << " " << 2 << '\n';
} else {
cout << 1 << '\n' << 2 << " " << 1 << '\n';
}
} else {
if (c[1][2] == c[n][n - 1]) {
cout << 2 << '\n'
<< 1 << " " << 2 << '\n'
<< " " << n - 1 << " " << n << '\n';
} else {
cout << 2 << '\n'
<< 1 << " " << 2 << '\n'
<< " " << n << " " << n - 1 << '\n';
}
}
}
}
void fastInput() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void readInput() {
cin >> t;
while (t--) {
cin >> n;
for (int i = (1); i <= (n); ++i) {
for (int j = (1); j <= (n); ++j) {
cin >> c[i][j];
}
}
solveProblem();
}
}
int main() {
fastInput();
readInput();
}
| 8 |
CPP
|
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) ]))
def invr():
return(map(int,input().split()))
n = inp()
for _ in range(n):
x = inp()
l = []
for j in range(x):
l.append(insr())
#print(l)
if(l[0][1] == '1' and l[1][0] == '1' and l[-1][-2] == '1' and l[-2][-1] == '1'):
print(2)
print("1 2")
print("2 1")
elif(l[0][1] == '0' and l[1][0] == '0' and l[-1][-2] == '0' and l[-2][-1] == '0'):
print(2)
print("1 2")
print("2 1")
elif(l[0][1] == '1' and l[1][0] == '1' and l[-1][-2] == '0' and l[-2][-1] == '0'):
print(0)
elif(l[0][1] == '0' and l[1][0] == '0' and l[-1][-2] == '1' and l[-2][-1] == '1'):
print(0)
elif(l[0][1] == '1' and l[1][0] == '0' and l[-1][-2] == '1' and l[-2][-1] == '1'):
print(1)
print("1 2")
elif(l[0][1] == '0' and l[1][0] == '1' and l[-1][-2] == '1' and l[-2][-1] == '1'):
print(1)
print("2 1")
elif(l[0][1] == '1' and l[1][0] == '1' and l[-1][-2] == '1' and l[-2][-1] == '0'):
print(1)
print(str(x) + " " + str(x-1))
elif(l[0][1] == '1' and l[1][0] == '1' and l[-1][-2] == '0' and l[-2][-1] == '1'):
print(1)
print(str(x-1) + " " + str(x))
elif(l[0][1] == '0' and l[1][0] == '1' and l[-1][-2] == '0' and l[-2][-1] == '0'):
print(1)
print("1 2")
elif(l[0][1] == '1' and l[1][0] == '0' and l[-1][-2] == '0' and l[-2][-1] == '0'):
print(1)
print("2 1")
elif(l[0][1] == '0' and l[1][0] == '0' and l[-1][-2] == '1' and l[-2][-1] == '0'):
print(1)
print(str(x-1) + " " + str(x))
elif(l[0][1] == '0' and l[1][0] == '0' and l[-1][-2] == '0' and l[-2][-1] == '1'):
print(1)
print(str(x) + " " + str(x-1))
elif(l[0][1] == '1' and l[1][0] == '0' and l[-1][-2] == '0' and l[-2][-1] == '1'):
print(2)
print(str(x) + " " + str(x-1))
print("1 2")
elif(l[0][1] == '0' and l[1][0] == '1' and l[-1][-2] == '0' and l[-2][-1] == '1'):
print(2)
print(str(x) + " " + str(x-1))
print("2 1")
elif(l[0][1] == '0' and l[1][0] == '1' and l[-1][-2] == '1' and l[-2][-1] == '0'):
print(2)
print(str(x) + " " + str(x-1))
print("1 2")
elif(l[0][1] == '1' and l[1][0] == '0' and l[-1][-2] == '1' and l[-2][-1] == '0'):
print(2)
print(str(x) + " " + str(x-1))
print("2 1")
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
a = [input() for i in range(n)]
a1 = []
a2 = []
if a[0][1]!='0':
a1.append([0,1])
if a[1][0]!='0':
a1.append([1,0])
if a[n-1][n-2]!='1':
a1.append([n-1,n-2])
if a[n-2][n-1]!='1':
a1.append([n-2,n-1])
if a[0][1]!='1':
a2.append([0,1])
if a[1][0]!='1':
a2.append([1,0])
if a[n-1][n-2]!='0':
a2.append([n-1,n-2])
if a[n-2][n-1]!='0':
a2.append([n-2,n-1])
if len(a1)<=2:
print(len(a1))
for i,j in a1:
print(i+1,j+1)
else:
print(len(a2))
for i,j in a2:
print(i+1,j+1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int t, n;
char s[207][207];
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> s[i][j];
}
}
int cnt0 = 0, cnt1 = 0;
if (s[1][2] == '0') {
cnt0++;
} else {
cnt1++;
}
if (s[2][1] == '0') {
cnt0++;
} else {
cnt1++;
}
if (s[n - 1][n] == '0') {
cnt0++;
} else {
cnt1++;
}
if (s[n][n - 1] == '0') {
cnt0++;
} else {
cnt1++;
}
if (cnt0 == 4 || cnt1 == 4) {
cout << "2" << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (cnt0 > cnt1) {
if (s[1][2] == '1') {
cout << "1" << endl;
cout << "2 1" << endl;
}
if (s[2][1] == '1') {
cout << "1" << endl;
cout << "1 2" << endl;
}
if (s[n - 1][n] == '1') {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
}
if (s[n][n - 1] == '1') {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
}
} else if (cnt0 < cnt1) {
if (s[1][2] == '0') {
cout << "1" << endl;
cout << "2 1" << endl;
}
if (s[2][1] == '0') {
cout << "1" << endl;
cout << "1 2" << endl;
}
if (s[n - 1][n] == '0') {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
}
if (s[n][n - 1] == '0') {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
}
} else {
if (s[1][2] == s[2][1]) {
cout << "0" << endl;
} else {
cout << "2" << endl;
if (s[1][2] == '0') {
cout << "1 2" << endl;
}
if (s[2][1] == '0') {
cout << "2 1" << endl;
}
if (s[n - 1][n] == '1') {
cout << n - 1 << " " << n << endl;
}
if (s[n][n - 1] == '1') {
cout << n << " " << n - 1 << endl;
}
}
}
}
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
s = [input() for i in range(n)]
ans = []
if s[0][2] == s[1][1] == s[2][0]:
if s[0][1] == s[0][2]:
ans += ['1 2']
if s[1][0] == s[0][2]:
ans += ['2 1']
else:
x = 0
if s[0][1] != s[1][0]:
if s[0][2] == s[1][1]:
ans += ['3 1']
elif s[0][2] == s[2][0]:
ans += ['2 2']
else:
ans += ['1 3']
x = 1
if x == 0:
if s[0][2] == s[0][1]:
ans += ['1 2']
else:
ans += ['2 1']
else:
if s[1][1] == s[0][1]:
ans += ['1 2']
else:
ans += ['2 1']
else:
if s[0][2] == s[0][1]:
ans += ['1 3']
if s[1][1] == s[0][1]:
ans += ['2 2']
if s[2][0] == s[0][1]:
ans += ['3 1']
print(len(ans))
for a in ans:
print(a)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
char s[205][205];
int main() {
long long a, b, t, n, m, i, j;
cin >> t;
while (t--) {
cin >> n;
cin.get();
for (i = 0; i <= n; i++)
for (j = 0; j <= n; j++) s[i][j] = '0';
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) cin >> s[i][j];
if (s[n][n - 1] == s[n - 1][n]) {
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[1][2]) {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
cout << 2 << " " << 1 << '\n';
} else
cout << 0 << '\n';
} else {
if (s[1][2] == s[n][n - 1]) {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
} else {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
}
}
} else {
if (s[1][2] == s[2][1]) {
if (s[1][2] == s[n][n - 1]) {
cout << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << 1 << '\n';
cout << n - 1 << " " << n << '\n';
}
} else {
if (s[1][2] == s[n][n - 1]) {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
cout << n - 1 << " " << n << '\n';
} else {
cout << 2 << '\n';
cout << 2 << " " << 1 << '\n';
cout << n - 1 << " " << n << '\n';
}
}
}
}
return 0;
}
| 8 |
CPP
|
"""
n=int(z())
for _ in range(int(z())):
x=int(z())
l=list(map(int,z().split()))
n=int(z())
l=sorted(list(map(int,z().split())))[::-1]
a,b=map(int,z().split())
l=set(map(int,z().split()))
led=(6,2,5,5,4,5,6,3,7,6)
vowel={'a':0,'e':0,'i':0,'o':0,'u':0}
color4=["G", "GB", "YGB", "YGBI", "OYGBI" ,"OYGBIV",'ROYGBIV' ]
"""
#!/usr/bin/env python
#pyrival orz
import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
def main():
try:
# Solution Here
for _ in range(inp()):
n = int(input())
arr = []
for i in range(n):
arr.append(input())
x1 = int(arr[1][0])
y1 = int(arr[0][1])
x2, y2 = int(arr[n-1][n-2]), int(arr[n-2][n-1])
if x1 == x2 == y1 == y2:
print("2")
print("1 2")
print("2 1")
elif x1 == (not x2) and y1 == (not y2) and x1 == y1:
print("0")
elif x1 == y1 and x2 != y2:
print("1")
if x2 == x1:
print(n, n-1)
else:
print(n-1, n)
elif x2 == y2 and x1 != y1:
print("1")
if x2 == x1:
print("2 1")
else:
print("1 2")
elif x1 != y1 and x2 != y2:
print("2")
if y1 != x2:
print("1 2")
print(n, n-1)
else:
print("2 1")
print(n, n-1)
except Exception as e:
print(e)
# region fastio
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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
| 8 |
PYTHON3
|
import math
import sys
import collections
import bisect
#sys.setrecursionlimit(1000000000)
input = sys.stdin.readline
flush = sys.stdout.flush
readint =lambda: int(input())
readintm =lambda: map(int,input().strip().split())
readintl =lambda: list(map(int,input().strip().split()))
readfloatm =lambda: map(float,input().strip().split())
readfloatl =lambda: list(map(float,input().strip().split()))
readstr =lambda: input().strip()
readstrl =lambda: list(input().strip())
join =lambda x,l: x.join(map(str,l))
print =lambda *x: sys.stdout.write(" ".join(map(str,x))+'\n')
printl =lambda l: sys.stdout.write(" ".join(map(str,l))+'\n')
ceil =lambda x,y: (x+y-1)//y
mod =1000000007
# START HERE
for _ in range(readint()):
n = readint()
board = [readstrl() for i in range(n)]
a, b = board[1][0], board[0][1]
x, y = board[-1][-2], board[-2][-1]
c = 0
out = []
if a == b:
if x == a:
c += 1
out.append(f"{n} {n-1}")
if y == a:
c += 1
out.append(f"{n-1} {n}")
print(c)
else: # a != b
if x == y:
if a == x:
out.append("2 1")
else:
out.append("1 2")
print(1)
else:
if a == '1':
out.append("2 1")
elif b == '1':
out.append("1 2")
if x == '0':
out.append(f"{n} {n-1}")
elif y == '0':
out.append(f"{n-1} {n}")
print(2)
for point in out:
print(point)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
template <class A>
void read(vector<A>& v);
template <class T>
void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d = stod(t);
}
void read(long double& d) {
string t;
read(t);
d = stold(t);
}
template <class H, class... T>
void read(H& h, T&... t) {
read(h);
read(t...);
}
template <class A>
void read(vector<A>& x) {
for (auto& a : x) read(a);
}
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return string(s); }
string to_string(vector<bool> v) {
string res;
for (int i = 0; i < (int)(v).size(); i++) {
res += char('0' + v[i]);
}
return res;
}
template <class T>
string to_string(T v) {
bool f = 1;
string res;
for (auto& x : v) {
if (!f) res += ' ';
f = 0;
res += to_string(x);
}
return res;
}
template <class A>
void write(A x) {
cout << to_string(x);
}
template <class H, class... T>
void write(const H& h, const T&... t) {
write(h);
write(t...);
}
void print() { write("\n"); }
template <class H, class... T>
void print(const H& h, const T&... t) {
write(h);
if (sizeof...(t)) write(' ');
print(t...);
}
void pre() {}
void solve() {
int n;
read(n);
vector<string> s(n);
for (int i = 0; i < n; i++) {
read(s[i]);
}
vector<pair<int, int>> ans;
int tot = 0;
if (s[0][1] == '1') {
tot++;
ans.push_back({0, 1});
}
if (s[1][0] == '1') {
tot++;
ans.push_back({1, 0});
}
if (s[2][0] == '0') {
tot++;
ans.push_back({2, 0});
}
if (s[1][1] == '0') {
tot++;
ans.push_back({1, 1});
}
if (s[0][2] == '0') {
tot++;
ans.push_back({0, 2});
}
if (tot <= 2) {
print(tot);
for (auto itr : ans) {
cout << itr.first + 1 << " " << itr.second + 1 << "\n";
}
return;
}
tot = 0;
ans.clear();
if (s[0][1] == '0') {
tot++;
ans.push_back({0, 1});
}
if (s[1][0] == '0') {
tot++;
ans.push_back({1, 0});
}
if (s[2][0] == '1') {
tot++;
ans.push_back({2, 0});
}
if (s[1][1] == '1') {
tot++;
ans.push_back({1, 1});
}
if (s[0][2] == '1') {
tot++;
ans.push_back({0, 2});
}
if (tot <= 2) {
print(tot);
for (auto itr : ans) {
cout << itr.first + 1 << " " << itr.second + 1 << "\n";
}
return;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pre();
int t = 1;
read(t);
for (int i = 0; i < t; i++) solve();
return 0;
}
| 8 |
CPP
|
import sys
import math
import random
from typing import List
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
class Cell:
def __init__(self, a, row, col):
self.a = a
self.row = row
self.col = col
def value(self):
return self.a[self.row][self.col]
def solve(n, c1, c2, c3, c4) -> List[Cell]:
def convert(c1: Cell, c2: Cell, char):
ans = []
if c1.value() != char:
ans.append(c1)
if c2.value() != char:
ans.append(c2)
return ans
# print(n, c1, c2, c3, c4)
sol1 = convert(c1, c2, '0') + convert(c3, c4, '1')
sol2 = convert(c1, c2, '1') + convert(c3, c4, '0')
return sol1 if len(sol1) < len(sol2) else sol2
T = int(input())
for t in range(T):
N = int(input())
A = []
for i in range(N):
A.append(input())
solution = solve(
N,
Cell(A, 1, 0), Cell(A, 0, 1),
Cell(A, N-1, N-2), Cell(A, N-2, N-1)
)
print(len(solution))
for cell in solution:
print(cell.row + 1, cell.col + 1)
| 8 |
PYTHON3
|
from sys import stdin, stdout
import math,sys,heapq
from itertools import permutations, combinations
from collections import defaultdict,deque,OrderedDict
from os import path
import random
import bisect as bi
def yes():print('YES')
def no():print('NO')
if (path.exists('input.txt')):
#------------------Sublime--------------------------------------#
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
def I():return (int(input()))
def In():return(map(int,input().split()))
else:
#------------------PYPY FAst I/o--------------------------------#
def I():return (int(stdin.readline()))
def In():return(map(int,stdin.readline().split()))
#sys.setrecursionlimit(1500)
def dict(a):
d={}
for x in a:
if d.get(x,-1)!=-1:
d[x]+=1
else:
d[x]=1
return d
def find_gt(a, x):
'Find leftmost value greater than x'
i = bi.bisect_left(a, x)
if i != len(a):
return i
else:
return -1
def main():
try:
n=I()
l=[]
for x in range(n):
l.append(input())
ans=[]
if l[0][1]=='1' and l[1][0]=='1':
if l[n-1][n-2]!='0':
ans.append([n,n-1])
if l[n-2][n-1]!='0':
ans.append([n-1,n])
elif l[0][1]=='0' and l[1][0]=='0':
if l[n-1][n-2]!='1':
ans.append([n,n-1])
if l[n-2][n-1]!='1':
ans.append([n-1,n])
else:
if l[0][1]=='1':
if l[n-1][n-2]=='1' and l[n-2][n-1]=='1':
ans.append([1,2])
else:
ans.append([2,1])
if l[n-2][n-1]!='0':
ans.append([n-1,n])
elif l[n-1][n-2]!='0':
ans.append([n,n-1])
else:
if l[n-2][n-1]=='0' and l[n-1][n-2]=='0':
ans.append([1,2])
else:
ans.append([2,1])
if l[n-2][n-1]!='1':
ans.append([n-1,n])
elif l[n-1][n-2]!='1':
ans.append([n,n-1])
print(len(ans))
if len(ans):
for x in ans:
print(*x)
except:
pass
M = 998244353
P = 1000000007
if __name__ == '__main__':
for _ in range(I()):main()
#for _ in range(1):main()
| 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 = []
xx=0
yy=0
xx=xx+yy
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(2,1)
print(1,2)
elif x1 == x and y1 != y:
print(1)
if x != y:
print(n-1,n)
else:
print(n,n-1)
elif x1!=x and y == y1:
print(1)
if x != y:
print(2,1)
else:
print(1,2)
elif x1!=x and y!=y1:
print(2)
if x != y:
print(1,2)
print(n,n-1)
else:
print(1,2)
print(n-1,n)
| 8 |
PYTHON3
|
t=int(input())
for _ in range(t):
n=int(input())
g=[]
for i in range(n):
g.append(input())
a,b,c,d=g[0][1],g[1][0],g[n-1][n-2],g[n-2][n-1]
if a==b and c==d and a!=c:
print(0)
elif a==b and c!=d:
print(1)
if c==a:
print(n,n-1)
elif d==a:
print(n-1,n)
elif c==d and a!=b:
print(1)
if a==c:
print(1,2)
elif b==c:
print(2,1)
elif a==b and c==d and a==c:
print(2)
print(1,2)
print(2,1)
elif a!=b and c!=d:
print(2)
if a==c:
print(1,2)
print(n-1,n)
elif a==d:
print(1,2)
print(n,n-1)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
arr = [list(input()) for i in range(n)]
t = []
s = [arr[0][1],arr[1][0]]
f = [arr[-1][-2],arr[-2][-1]]
if f[0]==f[1]:
if f[0]=='0':
if s[0]=='0':
t.append([1,2])
if s[1]=='0':
t.append([2,1])
else:
if s[0]=='1':
t.append([1,2])
if s[1]=='1':
t.append([2,1])
else:
if s[0]==s[1]:
if s[0]=='0':
if f[0]=='0':
t.append([n,n-1])
if f[1]=='0':
t.append([n-1,n])
if s[0]=='1':
if f[0]=='1':
t.append([n,n-1])
if f[1]=='1':
t.append([n-1,n])
else:
if s[0]=='1':
t.append([1,2])
elif s[1]=='1':
t.append([2,1])
if f[0]=='0':
t.append([n,n-1])
elif f[1]=='0':
t.append([n-1,n])
print(len(t))
for i in t:
print(*i)
| 8 |
PYTHON3
|
import re
import sys
exit=sys.exit
from bisect import bisect_left as bsl,bisect_right as bsr
from collections import Counter,defaultdict as ddict,deque
from functools import lru_cache
cache=lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum=enumerate
ri=lambda:int(rln())
ris=lambda:list(map(int,rfs()))
rln=sys.stdin.readline
rl=lambda:rln().rstrip('\n')
rfs=lambda:rln().split()
mod=1000000007
d4=[(0,-1),(1,0),(0,1),(-1,0)]
d8=[(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]
########################################################################
t=ri()
for _ in range(t):
n=ri()
grid=[rl() for _ in range(n)]
pos=[(0,1),(1,0),(n-2,n-1),(n-1,n-2)]
have=''.join(grid[r][c] for r,c in pos)
for want in '0011','1100':
ans=[]
for rc,x,y in zip(pos,have,want):
if x!=y:
ans.append(rc)
if len(ans)<=2:
print(len(ans))
for r,c in ans:
print(r+1,c+1)
break
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
grid = [input() for i in range(n)]
moves = []
def set_to(i, j, val):
if grid[i][j] != val:
moves.append((i+1,j+1))
def other(x):
return '0' if x=='1' else '1'
if grid[0][1] == grid[1][0]:
for i,j in [(n-2,n-1),(n-1,n-2)]:
set_to(i,j,other(grid[0][1]))
elif grid[n-2][n-1] == grid[n-1][n-2]:
for i,j in [(0,1),(1,0)]:
set_to(i,j,other(grid[n-2][n-1]))
else:
for i,j in [(0,1),(1,0)]:
set_to(i,j,'0')
for i,j in [(n-2,n-1),(n-1,n-2)]:
set_to(i,j,'1')
print(len(moves))
for move in moves:
print(*move)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100001;
const int MAX_D = 17;
const long long MOD = 1000000007;
const int INF32 = 0x3f3f3f3f;
const long long INF = 1e18;
void solve() {
int n;
cin >> n;
char grid[n][n];
for (int i = (0); i < (n); i++)
for (int j = (0); j < (n); j++) cin >> grid[i][j];
int a = grid[1][0] + grid[0][1] - 2 * '0',
b = grid[n - 1][n - 2] + grid[n - 2][n - 1] - 2 * '0';
if (a == 2) {
cout << b << endl;
if (grid[n - 1][n - 2] == '1') cout << n << ' ' << n - 1 << endl;
if (grid[n - 2][n - 1] == '1') cout << n - 1 << ' ' << n << endl;
}
if (!a) {
cout << 2 - b << endl;
if (grid[n - 1][n - 2] == '0') cout << n << ' ' << n - 1 << endl;
if (grid[n - 2][n - 1] == '0') cout << n - 1 << ' ' << n << endl;
}
if (a == 1) {
if (b == 1) {
cout << 2 << endl;
if (grid[0][1] == '0') cout << "1 2\n";
if (grid[1][0] == '0') cout << "2 1\n";
if (grid[n - 1][n - 2] == '1') cout << n << ' ' << n - 1 << endl;
if (grid[n - 2][n - 1] == '1') cout << n - 1 << ' ' << n << endl;
}
if (b == 2) {
cout << 1 << endl;
if (grid[0][1] == '1') cout << "1 2\n";
if (grid[1][0] == '1') cout << "2 1\n";
}
if (!b) {
cout << 1 << endl;
if (grid[0][1] == '0') cout << "1 2\n";
if (grid[1][0] == '0') cout << "2 1\n";
}
}
return;
}
int main(int argc, const char* argv[]) {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 8 |
CPP
|
from collections import deque, defaultdict
for _ in range(int(input())):
n = int(input())
mat = [[]]
for i in range(n):
a = [0]+list(input().strip())
mat.append(a)
one = [mat[1][2], mat[2][1]]
two = [mat[n][n-1], mat[n-1][n]]
if len(set(one+two))==1:
print(2)
print(n,n-1)
print(n-1,n)
elif len(set(one))==1 and len(set(two))==1 and set(one)!=set(two):
print(0)
else:
if one[0]==one[1]:
if one[0]==two[0]:
print(1)
print(n,n-1)
else:
print(1)
print(n-1,n)
elif two[0]==two[1]:
if two[0] == one[0]:
print(1)
print(1,2)
else:
print(1)
print(2,1)
else:
print(2)
if one[0]=="1":
print(2,1)
else:
print(1,2)
if two[0]=="0":
print(n-1,n)
else:
print(n,n-1)
| 8 |
PYTHON3
|
import os
import sys
from io import BytesIO, IOBase
_str = str
BUFSIZE = 8192
def str(x=b''):
return x if type(x) is bytes else _str(x).encode()
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 inp():
return sys.stdin.readline().rstrip()
def mpint():
return map(int, inp().split(' '))
def itg():
return int(inp())
# ############################## import
def to_list(binary_num, size=None):
"""
to_list(11, 5) -> [0, 1, 0, 1, 1]
"""
if size is None:
size = binary_num.bit_length()
result = [0] * size
index = size - 1
while binary_num:
if binary_num & 1:
result[index] = 1
index -= 1
binary_num >>= 1
return result
# ############################## main
def invert(x):
return '0' if x == '1' else '1'
def solve():
n = itg()
g = [inp() for _ in range(n)]
arr = (g[0][1], g[1][0], g[n - 2][-1], g[-1][n - 2])
pos = [(1, 2), (2, 1), (n - 1, n), (n, n - 1)]
for bit in range(2 ** 4):
bit = to_list(bit, 4)
if sum(bit) > 2:
continue
tmp = list(arr)
for i, b in enumerate(bit):
if b:
tmp[i] = invert(tmp[i])
a, b, c, d = tmp
if a == b and c == d and a != c:
print(sum(bit))
for i, b in enumerate(bit):
if b:
print(*pos[i])
return
while True:
pass
def main():
# print("YES" if solve() else "NO")
# print("yes" if solve() else "no")
# solve()
# print(solve())
for _ in range(itg()):
# print(solve())
solve()
DEBUG = 0
URL = 'https://codeforces.com/contest/1421/problem/0'
if __name__ == '__main__':
if DEBUG == 1:
import requests # ImportError: cannot import name 'md5' from 'sys' (unknown location)
from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
runner = TestCaseRunner(main, URL)
inp = runner.input_stream
print = runner.output_stream
runner.checking()
elif DEBUG == 2:
main()
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
main()
# Please check!
| 8 |
PYTHON3
|
#!/usr/bin/env pypy3
def ans(N, grid):
grid = [list(row) for row in grid]
a = grid[0][1]
b = grid[1][0]
c = grid[N-2][N-1]
d = grid[N-1][N-2]
a = int(a)
b = int(b)
c = int(c)
d = int(d)
def flipa():
grid[0][1] = 1-a
print(1, 2)
def flipb():
grid[1][0] = 1-b
print(2, 1)
def flipc():
grid[N-2][N-1] = 1-c
print(N-1, N)
def flipd():
grid[N-1][N-2] = 1-d
print(N, N-1)
def check():
a = grid[0][1]
b = grid[1][0]
c = grid[N-2][N-1]
d = grid[N-1][N-2]
a = int(a)
b = int(b)
c = int(c)
d = int(d)
assert(a == b)
assert(c == d)
assert(b != c)
if a == b and c == d:
if b == c:
print(2)
flipa()
flipb()
check()
return
else:
print(0)
check()
return
if a == b:
if c == a:
print(1)
flipc()
check()
return
elif d == a:
print(1)
flipd()
check()
return
else:
assert(False)
if c == d:
if a == c:
print(1)
flipa()
check()
return
elif b == c:
print(1)
flipb()
check()
return
if a == d:
assert(b == c)
print(2)
flipa()
flipc()
check()
return
if a == c:
assert(b == d)
print(2)
flipa()
flipd()
check()
return
assert(False)
def random_grid():
import random
return [
['S', random.choice("01"), random.choice("01")],
[random.choice("01"), random.choice("01"), random.choice("01")],
[random.choice("01"), random.choice("01"), 'F'],
]
# for _ in range(1000000):
# ans(3, random_grid())
for _ in range(int(input())):
N = int(input())
grid = []
for _ in range(N):
row = input()
grid += [row]
ans(N, grid)
| 8 |
PYTHON3
|
t = int(input())
for i in range(t):
n = int(input())
r = []
for j in range(n):
temp = input()
r.append(temp)
s1, s2 = int(r[0][1]), int(r[1][0])
f1, f2 = int(r[-1][-2]), int(r[-2][-1])
if s1 == s2 and f1 == f2:
if s1 != f1:
print(0)
else:
print(2)
print(1, 2)
print(2, 1)
elif s1 == s2 and f1 != f2:
if s1 == f1:
print(1)
print(n, n - 1)
else:
print(1)
print(n - 1, n)
elif s1 != s2 and f1 == f2:
if f1 == s1:
print(1)
print(1, 2)
else:
print(1)
print(2, 1)
else:
if s1 == f1:
print(2)
print(1, 2)
print(n - 1, n)
else:
print(2)
print(2, 1)
print(n - 1, n)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
inline void solve() {
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];
}
}
bool top = false;
bool bottom = false;
if (A[1][0] == A[0][1]) {
top = true;
}
if (A[n - 1][n - 2] == A[n - 2][n - 1]) {
bottom = true;
}
if (top && bottom) {
if (A[1][0] != A[n - 1][n - 2])
cout << 0 << "\n";
else {
cout << 2 << "\n";
cout << n << " " << n - 1 << "\n";
cout << n - 1 << " " << n << "\n";
}
} else if (top) {
cout << 1 << "\n";
if (A[n - 1][n - 2] == A[1][0]) {
cout << n << " " << n - 1 << "\n";
} else {
cout << n - 1 << " " << n << "\n";
}
} else if (bottom) {
cout << 1 << "\n";
if (A[n - 1][n - 2] == A[1][0]) {
cout << 2 << " " << 1 << "\n";
} else {
cout << 1 << " " << 2 << "\n";
}
} else {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
if (A[n - 1][n - 2] == A[1][0]) {
cout << n << " " << n - 1 << "\n";
} else {
cout << n - 1 << " " << n << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) solve();
return 0;
}
| 8 |
CPP
|
import math
for _ in range(int(input())):
n=int(input())
s=[]
for i in range(n):
l=input()
s.append(l)
if (s[0][1]=='1' and s[1][0]=='1' and s[n-1][n-2]=='0' and s[n-2][n-1]=='0' ) or (s[0][1]=='0' and s[1][0]=='0' and s[n-1][n-2]=='1' and s[n-2][n-1]=='1' ):
#print(1)
print(0)
elif s[0][1]=='1' and s[1][0]=='1':
#print(2)
if s[n-1][n-2]=='1' and s[n-2][n-1]=='1':
print(2)
print(n,n-1)
print(n-1,n)
elif s[n-1][n-2]=='1' and s[n-2][n-1]=='0':
print(1)
print(n,n-1)
elif s[n-1][n-2]=='0' and s[n-2][n-1]=='1':
print(1)
print(n-1,n)
else:
print(0)
elif s[0][1]=='0' and s[1][0]=='0':
#print(3)
if s[n-1][n-2]=='0' and s[n-2][n-1]=='0':
print(2)
print(n,n-1)
print(n-1,n)
elif s[n-1][n-2]=='0' and s[n-2][n-1]=='1':
print(1)
print(n,n-1)
elif s[n-1][n-2]=='1' and s[n-2][n-1]=='0':
print(1)
print(n-1,n)
else:
print(0)
else:
#print(4)
if (s[0][1]=='1' and s[1][0]=='0' and s[n-1][n-2]=='1' and s[n-2][n-1]=='0') or (s[0][1]=='0' and s[1][0]=='1' and s[n-1][n-2]=='0' and s[n-2][n-1]=='1'):
print(2)
print(2,1)
print(n,n-1)
elif (s[0][1]=='1' and s[1][0]=='0' and s[n-1][n-2]=='0' and s[n-2][n-1]=='1') or (s[0][1]=='0' and s[1][0]=='1' and s[n-1][n-2]=='1' and s[n-2][n-1]=='0'):
print(2)
print(1,2)
print(n,n-1)
elif (s[0][1]=='1' and s[1][0]=='0' and s[n-1][n-2]=='0' and s[n-2][n-1]=='0') or (s[0][1]=='0' and s[1][0]=='1' and s[n-1][n-2]=='1' and s[n-2][n-1]=='1'):
print(1)
print(2,1)
elif (s[0][1]=='1' and s[1][0]=='0' and s[n-1][n-2]=='1' and s[n-2][n-1]=='1') or (s[0][1]=='0' and s[1][0]=='1' and s[n-1][n-2]=='0' and s[n-2][n-1]=='0') :
print(1)
print(1,2)
| 8 |
PYTHON3
|
import math
t=int(input())
for w in range(t):
n=int(input())
l=[]
for i in range(n):
l.append(list(input()))
c=0
l1=[]
if(l[0][1]=='0' and l[1][0]=='0'):
if(l[-1][-2]=='0'):
c+=1
l1.append((n,n-1))
if(l[-2][-1]=='0'):
c+=1
l1.append((n-1,n))
elif(l[0][1]=='1' and l[1][0]=='1'):
if(l[-1][-2]=='1'):
c+=1
l1.append((n,n-1))
if(l[-2][-1]=='1'):
c+=1
l1.append((n-1,n))
elif(l[0][1]=='0' and l[1][0]=='1'):
if(l[-1][-2]=='0' and l[-2][-1]=='0'):
c+=1
l1.append((1,2))
elif(l[-1][-2]=='1' and l[-2][-1]=='1'):
c+=1
l1.append((2,1))
elif(l[-1][-2]=='0' and l[-2][-1]=='1'):
c+=2
l1.append((1,2))
l1.append((n-1,n))
else:
c+=2
l1.append((1,2))
l1.append((n,n-1))
else:
if(l[-1][-2]=='0' and l[-2][-1]=='0'):
c+=1
l1.append((2,1))
elif(l[-1][-2]=='1' and l[-2][-1]=='1'):
c+=1
l1.append((1,2))
elif(l[-1][-2]=='0' and l[-2][-1]=='1'):
c+=2
l1.append((2,1))
l1.append((n-1,n))
else:
c+=2
l1.append((2,1))
l1.append((n,n-1))
print(c)
for i in l1:
print(i[0],i[1])
| 8 |
PYTHON3
|
test_count = int(input())
tests = []
for _ in range(test_count):
x = int(input())
y = []
for _ in range(x):
y.append(list(input()))
tests.append((x, y))
for test in tests:
size, grid = test
start_right = int(grid[0][1])
start_down = int(grid[1][0])
end_up = int(grid[size - 2][size - 1])
end_left = int(grid[size - 1][size - 2])
x = (start_right, start_down, end_up, end_left)
if x == (0, 0, 0, 0):
print(2)
print(size - 1, size)
print(size, size - 1)
elif x == (0, 0, 0, 1):
print(1)
print(size - 1, size)
elif x == (0, 0, 1, 0):
print(1)
print(size, size - 1)
elif x == (0, 0, 1, 1):
print(0)
elif x == (0, 1, 0, 0):
print(1)
print(1, 2)
elif x == (0, 1, 0, 1):
print(2)
print(1, 2)
print(size, size - 1)
elif x == (0, 1, 1, 0):
print(2)
print(1, 2)
print(size - 1, size)
elif x == (0, 1, 1, 1):
print(1)
print(2, 1)
elif x == (1, 0, 0, 0):
print(1)
print(2, 1)
elif x == (1, 0, 0, 1):
print(2)
print(1, 2)
print(size - 1, size)
elif x == (1, 0, 1, 0):
print(2)
print(1, 2)
print(size, size - 1)
elif x == (1, 0, 1, 1):
print(1)
print(1, 2)
elif x == (1, 1, 0, 0):
print(0)
elif x == (1, 1, 0, 1):
print(1)
print(size, size - 1)
elif x == (1, 1, 1, 0):
print(1)
print(size - 1, size)
elif x == (1, 1, 1, 1):
print(2)
print(size - 1, size)
print(size, size - 1)
| 8 |
PYTHON3
|
def main():
t = int(input())
for i in range(t):
n = int(input())
matrix = [input() for i in range(n)]
c1, c2, c3, c4 = int(matrix[0][1]), int(matrix[1][0]), int(matrix[n-1][n-2]), \
int(matrix[n-2][n-1])
answer = []
if c1 == c2:
if c3 == c1:
answer.append((n, n-1))
if c4 == c1:
answer.append((n-1, n))
elif c3 == c4:
if c1 == c3:
answer.append((1, 2))
if c2 == c3:
answer.append((2, 1))
else:
if c1 != 0:
answer.append((1,2))
if c2 != 0:
answer.append((2, 1))
if c3 != 1:
answer.append((n, n-1))
if c4 != 1:
answer.append((n-1, n))
print(len(answer))
for j in answer:
print(*j)
main()
| 8 |
PYTHON3
|
t=int(input())
for _ in range(t):
n=int(input())
imp=[]
for i in range(n):
temp=input()
if(i==0):
imp.append(temp[1])
imp.append(temp[2])
elif(i==1):
imp.append(temp[0])
imp.append(temp[1])
elif(i==2):
imp.append(temp[0])
count=0
ans=[]
if(imp[0]!='0'):
ans.append([1,2])
count+=1
if(imp[2]!='0'):
ans.append([2,1])
count+=1
if(imp[1]!='1'):
ans.append([1,3])
count+=1
if(imp[3]!='1'):
ans.append([2,2])
count+=1
if(imp[4]!='1'):
ans.append([3,1])
count+=1
if(count<=2):
print(count)
for i in ans:
print(i[0],i[1])
else:
count=0
ans=[]
if(imp[0]!='1'):
ans.append([1,2])
count+=1
if(imp[2]!='1'):
ans.append([2,1])
count+=1
if(imp[1]!='0'):
ans.append([1,3])
count+=1
if(imp[3]!='0'):
ans.append([2,2])
count+=1
if(imp[4]!='0'):
ans.append([3,1])
count+=1
if(count<=2):
print(count)
for i in ans:
print(i[0],i[1])
| 8 |
PYTHON3
|
from sys import stdin, stdout
n=int(stdin.readline())
for i in range(n):
arr=[]
b=int(stdin.readline())
for _ in range(b):
arr.append(stdin.readline())
count1s=(arr[0][1])+(arr[1][0])
count2s=(arr[b-1][b-2])+(arr[b-2][b-1])
noms=0
cod=[]
if count1s=='00':
if count2s[0]=='0':
noms+=1
cod.append([b,b-1])
if count2s[1]=='0':
noms+=1
cod.append([b-1,b])
elif count1s=='11':
if count2s[0]=='1':
noms+=1
cod.append([b,b-1])
if count2s[1]=='1':
noms+=1
cod.append([b-1,b])
elif count1s=='10':
if count2s=='00':
noms+=1
cod.append([2,1])
elif count2s=='01':
noms=2
cod.append([1,2])
cod.append([b,b-1])
elif count2s=='10':
noms=2
cod.append([1,2])
cod.append([b-1,b])
elif count2s=='11':
noms=1
cod.append([1,2])
elif count1s=='01':
if count2s=='00':
noms+=1
cod.append([1,2])
elif count2s=='01':
noms=2
cod.append([1,2])
cod.append([b-1,b])
elif count2s=='10':
noms=2
cod.append([1,2])
cod.append([b,b-1])
elif count2s=='11':
noms=1
cod.append([2,1])
print(noms)
for i in cod:
print("{} {}".format(i[0],i[1]))
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char s[n + 1][n + 1];
for (int i = 1; i <= n; ++i) cin >> s[i] + 1;
vector<pair<int, int>> ans;
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[1][2]) ans.push_back(pair<int, int>(n, n - 1));
if (s[n - 1][n] == s[1][2]) ans.push_back(pair<int, int>(n - 1, n));
} else if (s[n][n - 1] == s[n - 1][n]) {
if (s[n][n - 1] == s[1][2]) ans.push_back(pair<int, int>(1, 2));
if (s[n - 1][n] == s[2][1]) ans.push_back(pair<int, int>(2, 1));
} else {
if (s[1][2] == '1')
ans.push_back(pair<int, int>(1, 2));
else
ans.push_back(pair<int, int>(2, 1));
if (s[n][n - 1] == '0')
ans.push_back(pair<int, int>(n, n - 1));
else
ans.push_back(pair<int, int>(n - 1, n));
}
cout << ans.size() << '\n';
for (auto x : ans) cout << x.first << " " << x.second << '\n';
}
return 0;
}
| 8 |
CPP
|
# cook your dish here
# cook your dish here
#from functools import reduce
#mod=int(1e9+7)
#import resource
#resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY])
#import threading
#threading.stack_size(2**26)
"""fact=[1]
#for i in range(1,100001):
# fact.append((fact[-1]*i)%mod)
#ifact=[0]*100001
#ifact[100000]=pow(fact[100000],mod-2,mod)
#for i in range(100000,0,-1):
# ifact[i-1]=(i*ifact[i])%mod"""
#from collections import deque, defaultdict, Counter, OrderedDict
#from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
#from heapq import heappush, heappop, heapify, nlargest, nsmallest
# sys.setrecursionlimit(10**6)
from sys import stdin, stdout
import bisect #c++ upperbound
from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
from bisect import bisect_right as br #c++ upperbound
import itertools
from collections import Counter
from math import sqrt
import collections
import math
import heapq
import re
def modinv(n,p):
return pow(n,p-2,p)
def cin():
return map(int,sin().split())
def ain(): #takes array as input
return list(map(int,sin().split()))
def sin():
return input()
def inin():
return int(input())
def Divisors(n) :
l = []
for i in range(1, int(math.sqrt(n) + 1)) :
if (n % i == 0) :
if (n // i == i) :
l.append(i)
else :
l.append(i)
l.append(n//i)
return l
def most_frequent(list):
return max(set(list), key = list.count)
def GCD(x,y):
while(y):
x, y = y, x % y
return x
def ncr(n,r,p): #To use this, Uncomment 19-25
t=((fact[n])*((ifact[r]*ifact[n-r])%p))%p
return t
def Convert(string):
li = list(string.split(""))
return li
def SieveOfEratosthenes(n):
global prime
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
f=[]
for p in range(2, n):
if prime[p]:
f.append(p)
return f
prime=[]
q=[]
def dfs(n,d,v,c):
global q
v[n]=1
x=d[n]
q.append(n)
j=c
for i in x:
if i not in v:
f=dfs(i,d,v,c+1)
j=max(j,f)
# print(f)
return j
#Implement heapq
#grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90]
#print(heapq.nlargest(3, grades)) #top 3 largest
#print(heapq.nsmallest(4, grades))
#Always make a variable of predefined function for ex- fn=len
#n,k=map(int,input().split())
"""*******************************************************"""
def main():
for _ in range(int(input())):
n=inin()
a=[]
z1=0
o1=0
z2=0
o2=0
o=[]
z=[]
x=[]
for i in range(n):
a.append(sin())
if a[0][1] == '0':
z1+=1
x.append(1)
else:
o1+=1
if a[1][0]=='0':
z1+=1
x.append(2)
else:
o1+=1
if a[n-2][n-1] == '0':
z2+=1
z.append(1)
else:
o2+=1
o.append(1)
if a[n-1][n-2]=='0':
z2+=1
z.append(2)
else:
o2+=1
o.append(2)
if (z1==0 and o2==0) or (z2==0 and o1==0):
print('0')
continue
if z1==2:
if z2==2:
print('2')
print(n,n-1)
print(n-1,n)
elif z2==1:
print('1')
if z[-1]==2:
print(n,n-1)
else:
print(n-1,n)
continue
if o1==2:
if o2==2:
print('2')
print(n,n-1)
print(n-1,n)
elif z2==1:
print('1')
if o[-1]==2:
print(n,n-1)
else:
print(n-1,n)
continue
if o1==1:
if z2==1:
print(2)
if x[-1]==1:
print(1,2)
else:
print(2,1)
if o[-1]==1:
print(n-1,n)
else:
print(n,n-1)
else:
if z2==0:
print(1)
if x[-1]==1:
print(2,1)
else:
print(1,2)
else:
print(1)
if x[-1]==1:
print(1,2)
else:
print(2,1)
"""*******************************************************"""
######## Python 2 and 3 footer by Pajenegod and c1729
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
if __name__== "__main__":
main()
#threading.Thread(target=main).start()
| 8 |
PYTHON3
|
for _ in range(int(input())):
n=int(input())
l=[]
for i in range(n):
t=list(input())
l.append(t)
if l[0][1]=='0' and l[1][0]=='0':
c=0
out=[]
if l[n-1][n-2]=='0':
c+=1
out.append((n,n-1))
if l[n-2][n-1]=='0':
c+=1
out.append((n-1,n))
print(c)
for i in out:
print(i[0],i[1])
elif l[0][1]=='1' and l[1][0]=='1':
c=0
out=[]
if l[n-1][n-2]=='1':
c+=1
out.append((n,n-1))
if l[n-2][n-1]=='1':
c+=1
out.append((n-1,n))
print(c)
for i in out:
print(i[0],i[1])
else:
if l[0][1]=='0':
if l[n-1][n-2]=='0' and l[n-2][n-1]=='0':
print(1)
print(1,2)
elif l[n-1][n-2]=='1' and l[n-2][n-1]=='1':
print(1)
print(2,1)
else:
print(2)
print(2,1)
if l[n-1][n-2]=='0':
print(n,n-1)
elif l[n-2][n-1]=='0':
print(n-1,n)
elif l[1][0]=='0':
if l[n-1][n-2]=='0' and l[n-2][n-1]=='0':
print(1)
print(2,1)
elif l[n-1][n-2]=='1' and l[n-2][n-1]=='1':
print(1)
print(1,2)
else:
print(2)
print(1,2)
if l[n-1][n-2]=='0':
print(n,n-1)
elif l[n-2][n-1]=='0':
print(n-1,n)
| 8 |
PYTHON3
|
t=int(input())
while(t>0):
n=int(input())
l=[[] for x in range(n)]
for x in range(0,n):
a=list(input())
l[x].extend(a)
l1=list(l)
v1=l[0][1]
v2=l[1][0]
w1=l[n-1][n-2]
w2=l[n-2][n-1]
ans=0
a=[]
if((v1=='0' and v2=='0') and (w1=='1' and w2=='1')):
ans=0
elif((v1=='1' and v2=='1') and (w1=='0' and w2=='0')):
ans=0
elif(v1=='1' and v2=='1'):
if(w1!='0'):
ans+=1
a.append((n,n-1))
if(w2!='0'):
ans+=1
a.append((n-1,n))
elif(v1=='0' and v2=='0'):
if(w1!='1'):
ans+=1
a.append((n,n-1))
if(w2!='1'):
ans+=1
a.append((n-1,n))
elif(w1=='1' and w2=='1'):
if(v1!='0'):
ans+=1
a.append((1,2))
if(v2!='0'):
ans+=1
a.append((2,1))
elif(w1=='0' and w2=='0'):
if(v1!='1'):
ans+=1
a.append((1,2))
if(v2!='1'):
ans+=1
a.append((2,1))
else:
if(v1=='1'):
ans+=1
a.append((1,2))
else:
ans+=1
a.append((2,1))
if(w1=='0'):
ans+=1
a.append((n,n-1))
else:
ans+=1
a.append((n-1,n))
print(ans)
for x in a:
print(x[0],x[1])
t-=1
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
mat = []
for i in range(n):
s = list(map(str,input().split()))
slist = [j for j in s[0]]
mat.append(slist)
#for 0
a,b,c,d = mat[0][1],mat[1][0],mat[n-2][n-1],mat[n-1][n-2]
ai,bi,ci,di = [1,2],[2,1],[n-1,n],[n,n-1]
if a =='1' and b == '1': #11
if c=='0' and d=='0':
print(0)
continue
elif c=='0' and d=='1':
print(1)
print(*di)
elif c=='1' and d=='0':
print(1)
print(*ci)
else:
print(2)
print(*ci)
print(*di)
elif a=='0' and b=='0': #00
if c=='1' and d=='1':
print(0)
continue
elif c=='0' and d=='1':
print(1)
print(*ci)
elif c=='1' and d=='0':
print(1)
print(*di)
else:
print(2)
print(*ci)
print(*di)
elif a=='0' and b=='1':#01
if c=='1' and d=='1':
print(1)
print(*bi)
continue
elif c=='0' and d=='1':
print(2)
print(*ai)
print(*di)
elif c=='1' and d=='0':
print(2)
print(*ai)
print(*ci)
else:
print(1)
print(*ai)
else:#10
if c=='1' and d=='1':
print(1)
print(*ai)
continue
elif c=='0' and d=='1':
print(2)
print(*bi)
print(*di)
elif c=='1' and d=='0':
print(2)
print(*bi)
print(*ci)
else:
print(1)
print(*bi)
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
n = int(input())
tab = list()
for i in range(n):
s = str(input())
tab.append(s)
if tab[0][1] == tab[1][0] and tab[0][1] == '1':
if tab[n-1][n-2] == tab[n-2][n-1]:
if tab[n-1][n-2] == '1':
print(2)
print(n-1, n)
print(n, n-1)
else:
print(0)
else:
print(1)
if tab[n - 1][n - 2] == '1':
print(n, n - 1)
else:
print(n - 1, n)
elif tab[0][1] == tab[1][0] and tab[0][1] == '0':
if tab[n-1][n-2] == tab[n-2][n-1]:
if tab[n-1][n-2] == '0':
print(2)
print(n-1, n)
print(n, n-1)
else:
print(0)
else:
print(1)
if tab[n-1][n-2] == '0':
print(n, n-1)
else:
print(n-1, n)
elif tab[0][1] != tab[1][0] and tab[0][1] == '1':
if tab[n - 1][n - 2] == tab[n - 2][n - 1]:
if tab[n - 1][n - 2] == '0':
print(1)
print(2, 1)
else:
print(1)
print(1, 2)
else:
if tab[n - 1][n - 2] == '0':
print(2)
print(2, 1)
print(n-1, n)
else:
print(2)
print(2, 1)
print(n, n-1)
else:
if tab[n - 1][n - 2] == tab[n - 2][n - 1]:
if tab[n - 1][n - 2] == '1':
print(1)
print(2, 1)
else:
print(1)
print(1, 2)
else:
if tab[n - 1][n - 2] == '1':
print(2)
print(2, 1)
print(n-1, n)
else:
print(2)
print(2, 1)
print(n, n-1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char fi[n][n];
for (int i = 0; i < (n); ++i) {
for (int j = 0; j < (n); ++j) {
cin >> fi[i][j];
}
}
int a = fi[0][1] - '0';
int b = fi[1][0] - '0';
int c = fi[n - 2][n - 1] - '0';
int d = fi[n - 1][n - 2] - '0';
if (a == 0 && b == 0 && c == 0 && d == 0) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
} else if (a == 0 && b == 0 && c == 0 && d == 1) {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 0 && b == 0 && c == 1 && d == 0) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 0 && b == 0 && c == 1 && d == 1) {
cout << 0 << endl;
} else if (a == 0 && b == 1 && c == 0 && d == 0) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else if (a == 0 && b == 1 && c == 0 && d == 1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 0 && b == 1 && c == 1 && d == 0) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 0 && b == 1 && c == 1 && d == 1) {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
} else if (a == 1 && b == 0 && c == 0 && d == 0) {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
} else if (a == 1 && b == 0 && c == 0 && d == 1) {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 1 && b == 0 && c == 1 && d == 0) {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 1 && b == 0 && c == 1 && d == 1) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else if (a == 1 && b == 1 && c == 0 && d == 0) {
cout << 0 << endl;
} else if (a == 1 && b == 1 && c == 0 && d == 1) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 1 && b == 1 && c == 1 && d == 0) {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 1 && b == 1 && c == 1 && d == 1) {
cout << 2 << endl;
cout << n - 1 << " " << n << endl;
cout << n << " " << n - 1 << endl;
}
}
}
| 8 |
CPP
|
import sys
input = lambda :sys.stdin.readline().rstrip()
from math import log
for _ in range(int(input())):
n=int(input())
a=[input() for _ in range(n)]
ans = []
if a[0][1] == a[1][0]:
if a[n-1][n-2]==a[n-2][n-1]:
if a[n-1][n-2] == a[0][1]:
ans.append([n-1,n])
ans.append([n,n-1])
else:
if int(a[n-1][n-2])!=1-int(a[0][1]):
ans.append([n,n-1])
else:
ans.append([n-1,n])
else:
if a[n-1][n-2] == a[n-2][n-1]:
if int(a[0][1])!= 1-int(a[n-1][n-2]):
ans.append([1,2])
else:
ans.append([2,1])
else:
if a[0][1]=='0':
ans.append([1,2])
else:
ans.append([2,1])
if a[n-1][n-2]=='0':
ans.append([n-1,n])
else:
ans.append([n,n-1])
print(len(ans))
for x in ans:
print(*x)
| 8 |
PYTHON3
|
t = int(input())
while t > 0:
n = int(input())
mat = [input() for i in range(n)]
ans = []
if mat[0][1] == mat[1][0]:
if mat[n-1][n-2] == mat[0][1]:
ans.append([n-1, n-2])
if mat[n-2][n-1] == mat[0][1]:
ans.append([n-2, n-1])
elif mat[n-1][n-2] == mat[n-2][n-1]:
if mat[0][1] == mat[n-1][n-2]:
ans.append([0, 1])
if mat[1][0] == mat[n-1][n-2]:
ans.append([1, 0])
else:
ans.append([1, 0])
if mat[n-1][n-2] == mat[0][1]:
ans.append([n-1, n-2])
if mat[n-2][n-1] == mat[0][1]:
ans.append([n-2, n-1])
print(len(ans))
for x, y in ans:
print(x+1, y+1)
t -= 1
| 8 |
PYTHON3
|
for i in range(int(input())):
n=int(input())
l=[]
for i in range(n):
ll=input()
if i==0:
a=int(ll[1])
elif i==1:
b=int(ll[0])
if i==n-2:
c=int(ll[n-1])
elif i==n-1:
d=int(ll[n-2])
# print(a,b,c,d)
if a!=b and c!=d:
print(2)
if a==1 and c==1:
print(2,1)
print(n-1,n)
elif a==1 and c==0:
print(2,1)
print(n,n-1)
elif a==0 and c==1:
print(1,2)
print(n-1,n)
elif a==0 and c==0:
print(2,1)
print(n,n-1)
elif a==b and c==d:
if a==1 and c==1:
print(2)
print(1,2)
print(2,1)
elif a==0 and c==0:
print(2)
print(1,2)
print(2,1)
else:print(0)
elif a==b and c!=d:
if a==1:
if c==1:
print(1)
print(n-1,n)
elif d==1:
print(1)
print(n,n-1)
elif a==0:
if c==0:
print(1)
print(n-1,n)
elif d==0:
print(1)
print(n,n-1)
elif c==d:
if c==1:
if a==1:
print(1)
print(1,2)
elif b==1:
print(1)
print(2,1)
elif c==0:
if a==0:
print(1)
print(1,2)
elif b==0:
print(1)
print(2,1)
| 8 |
PYTHON3
|
from math import *
from sys import *
t=int(stdin.readline())
for _ in range(t):
n=int(stdin.readline())
m = []
ans=[]
for i in range(n):
a = list(input())
m.append(a)
x=m[0][1]
a=0
f=0
if m[n-1][n-2]==x and m[n-2][n-1]==x:
a+=1
ans.append([1,2])
if x==0:
m[0][1]=1
else:
m[0][1]=0
elif m[n-1][n-2]==x and m[n-2][n-1]!=x:
a+=1
ans.append([n,n-1])
f=1
elif m[n-1][n-2]!=x and m[n-2][n-1]==x:
a+=1
ans.append([n-1,n])
f=1
if m[1][0]!=m[0][1]:
if f==1:
ans.append([2,1])
a+=1
else:
if m[1][0]==m[n-1][n-2]:
ans.append([2,1])
a += 1
print(a)
for i in range(a):
print(*ans[i])
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long T = 1;
cin >> T;
while (T--) {
long long n;
cin >> n;
char a[n][n];
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++) cin >> a[i][j];
vector<pair<long long, long long> > v;
if (a[1][0] == '0' && a[0][1] == '0') {
if (a[n - 1][n - 2] != '1') v.push_back({n - 1, n - 2});
if (a[n - 2][n - 1] != '1') v.push_back({n - 2, n - 1});
} else if (a[1][0] == '0' && a[0][1] == '1') {
if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '0') {
v.push_back({1, 0});
} else if (a[n - 1][n - 2] == '1' && a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n - 2});
v.push_back({1, 0});
} else if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '1') {
v.push_back({n - 2, n - 1});
v.push_back({1, 0});
} else {
v.push_back({0, 1});
}
} else if (a[1][0] == '1' && a[0][1] == '1') {
if (a[n - 1][n - 2] != '0') v.push_back({n - 1, n - 2});
if (a[n - 2][n - 1] != '0') v.push_back({n - 2, n - 1});
} else if (a[1][0] == '1' && a[0][1] == '0') {
if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '0') {
v.push_back({0, 1});
} else if (a[n - 1][n - 2] == '1' && a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n - 2});
v.push_back({0, 1});
} else if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '1') {
v.push_back({n - 2, n - 1});
v.push_back({0, 1});
} else {
v.push_back({1, 0});
}
}
cout << v.size() << "\n";
for (auto it = (v).begin(); it != (v).end(); it++)
cout << it->first + 1 << " " << it->second + 1 << "\n";
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n = 0;
cin >> n;
vector<vector<long long>> arr(n, vector<long long>(n));
for (long long i = 0; i < n; i++) {
string s;
cin >> s;
for (long long j = 0; j < n; j++) {
for (long long j = 0; j < n; j++) arr[i][j] = s[j] - '0';
}
}
long long n1 = arr[0][1];
long long n2 = arr[1][0];
long long n3 = arr[n - 1][n - 2];
long long n4 = arr[n - 2][n - 1];
if (n1 == n2 && n2 == n3 && n3 == n4) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << 2 << " " << 1 << "\n";
} else if (n1 == n2 && n3 == n4) {
cout << 0 << "\n";
} else if (n1 == n2) {
cout << 1 << "\n";
if (n2 == n3)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
} else if (n3 == n4) {
cout << 1 << "\n";
if (n1 == n3)
cout << 1 << " " << 2 << "\n";
else
cout << 2 << " " << 1 << "\n";
} else if (n1 == n3) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << n - 1 << " " << n << "\n";
} else {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << n << " " << n - 1 << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char arr[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
vector<pair<int, int>> vec;
if (arr[0][1] == arr[1][0]) {
if (arr[0][1] == '1') {
if (arr[n - 2][n - 1] != '0') {
vec.push_back({n - 1, n});
}
if (arr[n - 1][n - 2] != '0') {
vec.push_back({n, n - 1});
}
} else {
if (arr[0][1] == '0') {
if (arr[n - 2][n - 1] != '1') {
vec.push_back({n - 1, n});
}
if (arr[n - 1][n - 2] != '1') {
vec.push_back({n, n - 1});
}
}
}
} else if (arr[n - 1][n - 2] == arr[n - 2][n - 1]) {
if (arr[n - 1][n - 2] == '1') {
if (arr[0][1] != '0') {
vec.push_back({1, 2});
}
if (arr[1][0] != '0') {
vec.push_back({2, 1});
}
} else {
if (arr[n - 1][n - 2] == '0') {
if (arr[1][0] != '1') {
vec.push_back({2, 1});
}
if (arr[0][1] != '1') {
vec.push_back({1, 2});
}
}
}
} else {
if (arr[n - 1][n - 2] != '1') {
vec.push_back({n, n - 1});
}
if (arr[n - 2][n - 1] != '1') {
vec.push_back({n - 1, n});
}
if (arr[0][1] != '0') {
vec.push_back({1, 2});
}
if (arr[1][0] != '0') {
vec.push_back({2, 1});
}
}
cout << vec.size() << endl;
for (auto x : vec) {
cout << x.first << " " << x.second << endl;
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int T;
int n, i, j;
char num[300][300];
cin >> T;
while (T--) {
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cin >> num[i][j];
}
}
int cnt = 0;
cnt = num[2][1] + num[1][2] + num[n - 1][n] + num[n][n - 1] - 4 * '0';
int x, y, t;
if (cnt == 0 || cnt == 4) {
printf("2\n1 2\n2 1\n");
} else if (cnt == 1 || cnt == 3) {
printf("1\n");
if (cnt == 1)
t = '0';
else
t = '1';
if (num[2][1] != num[1][2]) {
if (t == num[2][1])
x = 2, y = 1;
else
x = 1, y = 2;
} else {
if (t == num[n][n - 1])
x = n, y = n - 1;
else
x = n - 1, y = n;
}
printf("%d %d\n", x, y);
} else {
if (num[1][2] == num[2][1]) {
printf("0\n");
} else {
printf("2\n1 2\n");
if (num[n - 1][n] == num[1][2])
printf("%d %d\n", n, n - 1);
else
printf("%d %d\n", n - 1, n);
}
}
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n=int(input())
grid=[[None]*n]*n
c=0
changed=[]
for i in range(n):
grid[i]=list(input())
if grid[0][1]==grid[1][0]:
a=grid[0][1]
if grid[n-1][n-2]==a:
c+=1
changed.append([n,n-1])
if grid[n-2][n-1]==a:
c+=1
changed.append([n-1,n])
elif grid[n-2][n-1]==grid[n-1][n-2]:
a=grid[n-2][n-1]
if grid[1][0]==a:
c+=1
changed.append([2,1])
if grid[0][1]==a:
c+=1
changed.append([1,2])
else:
c=2
changed.append([1,2])
if grid[n-2][n-1]==grid[0][1]:
changed.append([n,n-1])
else:
changed.append([n-1,n])
print(c)
for i in changed:
print(*i)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000002;
const int MOD2 = 998244353;
const int MOD = 1000000007;
const int INF = 1e8;
const long double EPS = 1e-7;
long long int mul(long long int x, long long int y) {
return (x * 1ll * y) % MOD;
}
long long int fastpow(long long int x, long long int y) {
long long int z = 1;
while (y) {
if (y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
long long int modinv(long long int n, long long int p) {
return fastpow(n, p - 2);
}
struct Comp {
bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b) {
if (a.first != b.first) {
return a.first > b.first;
}
return a.second > b.second;
}
};
int main(int argc, char** argv) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cout << setprecision(10);
int tt;
cin >> tt;
while (tt--) {
long long int n;
cin >> n;
string s[n];
for (int i = 0; i < (int)n; i++) cin >> s[i];
long long int c = (s[n - 2][n - 1] - '0'), d = (s[n - 1][n - 2] - '0');
long long int a = (s[0][1] - '0'), b = (s[1][0] - '0');
if (a + b + c + d == 4 || a + b + c + d == 0) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << 2 << " " << 1 << "\n";
continue;
} else if (a + b + c + d == 1) {
cout << 1 << "\n";
if (a == 1) {
cout << 2 << " " << 1 << "\n";
} else if (b == 1) {
cout << 1 << " " << 2 << "\n";
} else if (c == 1)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
continue;
} else if (a + b + c + d == 3) {
cout << 1 << "\n";
if (a == 0) {
cout << 2 << " " << 1 << "\n";
} else if (b == 0) {
cout << 1 << " " << 2 << "\n";
} else if (c == 0)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
} else {
if (a == b) {
cout << 0 << "\n";
} else {
if (a == c) {
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";
}
}
}
}
return 0;
}
| 8 |
CPP
|
def solve():
n = int(input())
d = [list(input()) for i in range(n)]
cnt1 = 0
que1 = []
if d[0][1] != "1":
cnt1 += 1
que1.append((1, 2))
if d[1][0] != "1":
cnt1 += 1
que1.append((2, 1))
if d[-2][-1] != "0":
cnt1 += 1
que1.append((n-1, n))
if d[-1][-2] != "0":
cnt1 += 1
que1.append((n, n-1))
if cnt1 <= 2:
print(cnt1)
for x, y in que1:
print(x, y)
return
cnt1 = 0
que1 = []
if d[0][1] != "0":
cnt1 += 1
que1.append((1, 2))
if d[1][0] != "0":
cnt1 += 1
que1.append((2, 1))
if d[-2][-1] != "1":
cnt1 += 1
que1.append((n-1, n))
if d[-1][-2] != "1":
cnt1 += 1
que1.append((n, n-1))
if cnt1 <= 2:
print(cnt1)
for x, y in que1:
print(x, y)
return
def main():
t = int(input())
for i in range(t):
solve()
return
if __name__ == "__main__":
main()
| 8 |
PYTHON3
|
import os
import sys
from io import BytesIO, IOBase
# region fastio
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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
print(' '.join(map(str,l)))
# import sys
# sys.setrecursionlimit(5010)
# from heapq import *
# from collections import deque as dq
# from math import ceil,floor,sqrt,pow
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
for _ in range(N()):
n = N()
g = []
for _ in range(n):
g.append(input().strip())
s = int(g[0][1]+g[1][0],base=2)
e = int(g[-1][-2]+g[-2][-1],base=2)
if s==3:
if e==3:
print(2)
print(n,n-1)
print(n-1,n)
if e==0:
print(0)
if e==1:
print(1)
print(n-1,n)
if e==2:
print(1)
print(n,n-1)
elif s==0:
if e==0:
print(2)
print(n,n-1)
print(n-1,n)
if e==3:
print(0)
if e==1:
print(1)
print(n,n-1)
if e==2:
print(1)
print(n-1,n)
elif s==1:
if e==0:
print(1)
print(1,2)
if e==1:
print(2)
print(1,2)
print(n-1,n)
if e==2:
print(2)
print(1,2)
print(n,n-1)
if e==3:
print(1)
print(2,1)
elif s==2:
if e==0:
print(1)
print(2,1)
if e==1:
print(2)
print(2,1)
print(n-1,n)
if e==2:
print(2)
print(2,1)
print(n,n-1)
if e==3:
print(1)
print(1,2)
| 8 |
PYTHON3
|
t = int(input())
#
for _ in range(t):
n = int(input())
arr = []
for _ in range(n):
arr.append(input())
topright = int(arr[0][1])
topbtm = int(arr[1][0])
btmup = int(arr[-2][-1])
btmleft = int(arr[-1][-2])
if topright == 0 and topbtm == 0:
if btmup == 0 and btmleft == 0:
print(2)
print('%i %i' % (n-1,n))
print('%i %i' % (n,n-1))
elif btmup == 0 and btmleft == 1:
print(1)
print('%i %i' % (n-1,n))
elif btmup == 1 and btmleft == 0:
print(1)
print('%i %i' % (n,n-1))
else:
print(0)
elif topright == 1 and topbtm == 1:
if btmup == 0 and btmleft == 0:
print(0)
elif btmup == 0 and btmleft == 1:
print(1)
print('%i %i' % (n,n-1))
elif btmup == 1 and btmleft == 0:
print(1)
print('%i %i' % (n-1,n))
else:
print(2)
print('%i %i' % (n-1,n))
print('%i %i' % (n,n-1))
elif topright == 0 and topbtm == 1:
if btmup == 0 and btmleft == 0:
print(1)
print('1 2')
elif btmup == 0 and btmleft == 1:
print(2)
print('1 2')
print('%i %i' % (n,n-1))
elif btmup == 1 and btmleft == 0:
print(2)
print('1 2')
print('%i %i' % (n-1,n))
else:
print(1)
print('2 1')
elif topright == 1 and topbtm == 0:
if btmup == 0 and btmleft == 0:
print(1)
print('2 1')
elif btmup == 0 and btmleft == 1:
print(2)
print('2 1')
print('%i %i' % (n,n-1))
elif btmup == 1 and btmleft == 0:
print(2)
print('2 1')
print('%i %i' % (n-1,n))
else:
print(1)
print('1 2')
| 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 = 1;
cin >> t;
while (t--) {
int n, m, i, x = 0, y, z;
cin >> n;
string s[n + 1];
for (int i = 0; i < n; i++) cin >> s[i];
vector<pair<int, int>> v;
if (s[0][1] == s[1][0]) {
if (s[n - 1][n - 2] == s[0][1]) {
v.push_back({n, n - 1});
}
if (s[n - 2][n - 1] == s[0][1]) {
v.push_back({n - 1, n});
}
} else if (s[n - 1][n - 2] == s[n - 2][n - 1]) {
if (s[n - 1][n - 2] == s[0][1]) {
v.push_back({1, 2});
}
if (s[n - 2][n - 1] == s[1][0]) {
v.push_back({2, 1});
}
} else {
if (s[0][1] != '0') {
v.push_back({1, 2});
}
if (s[1][0] != '0') {
v.push_back({2, 1});
}
if (s[n - 2][n - 1] != '1') {
v.push_back({n - 1, n});
}
if (s[n - 1][n - 2] != '1') {
v.push_back({n, n - 1});
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << endl;
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
ll fac[1000001];
void pre() {
fac[0] = fac[1] = 1;
for (int i = 2; i <= 1000001; i++) {
fac[i] = (fac[i - 1] * 1LL * i) % mod;
}
}
ll binpower(ll a, ll n) {
ll res = 1;
while (n) {
if (n % 2) res = (res * 1LL * a) % mod;
n /= 2;
a = (a * 1LL * a) % mod;
}
return res;
}
ll nCrmod(ll n, ll r) {
ll res = fac[n];
res = (res * 1LL * binpower(fac[r], mod - 2)) % mod;
res = (res * 1LL * binpower(fac[n - r], mod - 2)) % mod;
return res;
}
long long ncr(int n, int r) {
if (r > n - r) r = n - r;
long long ans = 1;
int i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
ll modexp(ll a, ll b, ll m) {
if (b == 0) return 1;
if (b % 2 == 0) {
ll y = modexp(a, b / 2, m);
return (y * y) % m;
} else {
return ((a % m) * modexp(a, b - 1, m)) % m;
}
}
ll modinv(ll a, ll m) { return modexp(a, m - 2, m); }
void SieveOfEratosthenes(ll n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (ll p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (ll i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (ll p = 2; p <= n; p++)
if (prime[p]) cout << p << " ";
}
vector<int> Centroid(vector<int> g[], int n) {
vector<int> centroid;
vector<int> sz(n + 1);
function<void(int, int)> dfs = [&](int u, int prev) {
sz[u] = 1;
bool is_centroid = true;
for (auto v : g[u])
if (v != prev) {
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > n / 2) is_centroid = false;
}
if (n - sz[u] > n / 2) is_centroid = false;
if (is_centroid) centroid.push_back(u);
};
dfs(1, 0);
return centroid;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char grid[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 1; j <= n; j++) {
grid[i][j] = s[j - 1];
}
}
char a = grid[1][2], b = grid[2][1], c = grid[n][n - 1], d = grid[n - 1][n];
if (a == b) {
int ans = 0;
vector<pair<int, int>> v;
if (a == '0') {
if (c == '0') {
ans++;
v.push_back({n, n - 1});
}
if (d == '0') {
ans++;
v.push_back({n - 1, n});
}
}
if (a == '1') {
if (c == '1') {
ans++;
v.push_back({n, n - 1});
}
if (d == '1') {
ans++;
v.push_back({n - 1, n});
}
}
cout << ans << "\n";
for (auto x : v) {
cout << x.first << " " << x.second << "\n";
}
} else {
int ans = 0;
vector<pair<int, int>> v;
if (c == d) {
if (c == '0') {
if (a == '0') {
ans++;
v.push_back({1, 2});
}
if (b == '0') {
ans++;
v.push_back({2, 1});
}
} else {
if (a == '1') {
ans++;
v.push_back({1, 2});
}
if (b == '1') {
ans++;
v.push_back({2, 1});
}
}
} else {
if (c == '1') {
ans++;
v.push_back({n, n - 1});
if (a == '0') {
ans++;
v.push_back({1, 2});
}
if (b == '0') {
ans++;
v.push_back({2, 1});
}
} else {
ans++;
v.push_back({n, n - 1});
if (a == '1') {
ans++;
v.push_back({1, 2});
}
if (b == '1') {
ans++;
v.push_back({2, 1});
}
}
}
cout << ans << "\n";
for (auto x : v) {
cout << x.first << " " << x.second << "\n";
}
}
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
mat = []
for i in range(n):
mat.append(list(map(str,input())))
ans = []
tl = [[0,1],[1,0]]
tr = [[n-1,n-2],[n-2,n-1]]
if mat[tl[0][0]][tl[0][1]]==mat[tl[1][0]][tl[1][1]]:
x = int(mat[tl[0][0]][tl[0][1]])^1
x = str(x)
if mat[tr[0][0]][tr[0][1]]!=x:
ans.append([n,n-1])
if mat[tr[1][0]][tr[1][1]]!=x:
ans.append([n-1,n])
elif mat[tr[0][0]][tr[0][1]]==mat[tr[1][0]][tr[1][1]]:
x = int(mat[tr[0][0]][tr[0][1]])^1
x = str(x)
if mat[tl[0][0]][tl[0][1]]!=x:
ans.append([1,2])
if mat[tl[1][0]][tl[1][1]]!=x:
ans.append([2,1])
else:
if mat[0][1]=='1':
ans.append([1,2])
if mat[1][0]=='1':
ans.append([2,1])
if mat[-1][-2]=='0':
ans.append([n,n-1])
if mat[-2][-1]=='0':
ans.append([n-1,n])
print(len(ans))
for i in ans:
print(*i)
| 8 |
PYTHON3
|
import sys
tests = int(sys.stdin.readline())
for i in range(tests):
msize = int(sys.stdin.readline())
matrix = []
for j in range(msize):
line = sys.stdin.readline().strip()
matrix.append(line)
changes = []
# Check parts surrounding S and F
sr = int(matrix[0][1])
sd = int(matrix[1][0])
fl = int(matrix[msize-1][msize-2])
fu = int(matrix[msize-2][msize-1])
#print(f"{sr} {sd} {fl} {fu}")
if sr == sd:
# check finish
if fl == sr:
fl = (fl + 1) % 2
changes.append(((msize),(msize-1)))
if fu == sr:
fu = (fl + 1) % 2
changes.append(((msize-1),(msize)))
if sr != sd:
# find out which to change
if fl == fu:
if sr == fl:
sr = (fl + 1) % 2
changes.append((1,2))
else:
sd = (fl + 1) % 2
changes.append((2,1))
else:
if sr == fl:
# change sd and fl
sd = (sd + 1) % 2
changes.append((2,1))
fl = (fl + 1) % 2
changes.append(((msize),(msize-1)))
else:
# change sr and fl
sr = (sr + 1) % 2
changes.append((1,2))
fl = (fl + 1) % 2
changes.append(((msize),(msize-1)))
print(len(changes))
for change in changes:
print(f"{change[0]} {change[1]}")
| 8 |
PYTHON3
|
T, = map(int, input().split())
for _ in range(T):
N, = map(int, input().split())
X=[]
for _ in range(N):
X.append(input().strip())
a,b=int(X[0][1]),int(X[1][0])
c,d=int(X[-1][-2]),int(X[-2][-1])
if (a,b,c,d)==(1,1,0,0):
print(0)
if (a,b,c,d)==(0,0,1,1):
print(0)
if (a,b,c,d)==(0,0,0,0):
print(2)
print(1, 2)
print(2, 1)
if (a,b,c,d)==(1,1,1,1):
print(2)
print(1, 2)
print(2, 1)
if (a,b,c,d)==(0,1,0,1):
print(2)
print(1, 2)
print(N-1, N)
if (a,b,c,d)==(1,0,1,0):
print(2)
print(1, 2)
print(N-1, N)
if (a,b,c,d)==(1,0,0,1):
print(2)
print(1, 2)
print(N, N-1)
if (a,b,c,d)==(0,1,1,0):
print(2)
print(1, 2)
print(N, N-1)
if (a,b,c,d)==(0,0,0,1):
print(1)
print(N, N-1)
if (a,b,c,d)==(1,1,1,0):
print(1)
print(N, N-1)
if (a,b,c,d)==(0,0,1,0):
print(1)
print(N-1, N)
if (a,b,c,d)==(1,1,0,1):
print(1)
print(N-1, N)
if (a,b,c,d)==(0,1,0,0):
print(1)
print(1, 2)
if (a,b,c,d)==(1,0,1,1):
print(1)
print(1, 2)
if (a,b,c,d)==(1,0,0,0):
print(1)
print(2, 1)
if (a,b,c,d)==(0,1,1,1):
print(1)
print(2, 1)
| 8 |
PYTHON3
|
from collections import Counter, deque
# Make GRID a global variable to avoid copying data during function calls
GRID = []
SMALL_DIAGONAL = [(0, 1), (1, 0)]
BIG_DIAGONAL = [(0, 2), (1, 1), (2, 0)]
def get_input():
return input().split()
def get_int_input():
return map(int, get_input())
def _is_valid_cell(cell, n):
if cell[0] >= n or cell[1] >= n:
return False
return True
def is_there_a_path(n):
'''
Do a bfs traversal to find for any valid path
'''
queue = deque([((0, 0), '0'), ((0, 0), '1')])
FINAL_CELLS = [(n - 1, n - 2), (n - 2, n - 1)]
while queue:
cell, digit = queue.popleft()
if cell in FINAL_CELLS:
return True
# Move right
new_cell = (cell[0] + 1, cell[1])
if _is_valid_cell(new_cell, n):
if GRID[new_cell[0]][new_cell[1]] == digit:
queue.append((new_cell, digit))
# Move down
new_cell = (cell[0], cell[1] + 1)
if _is_valid_cell(new_cell, n):
if GRID[new_cell[0]][new_cell[1]] == digit:
queue.append((new_cell, digit))
return False
def C1421B(n):
ans_count = 0
ans_cells = [] # list of string
if (
len(Counter([GRID[cell[0]][cell[1]] for cell in BIG_DIAGONAL])) == 1 and
len(Counter([GRID[cell[0]][cell[1]] for cell in SMALL_DIAGONAL])) == 1 and
GRID[BIG_DIAGONAL[0][0]][BIG_DIAGONAL[0][1]] != GRID[SMALL_DIAGONAL[0][0]][SMALL_DIAGONAL[0][1]]
):
return 0
elif len(Counter([GRID[cell[0]][cell[1]] for cell in BIG_DIAGONAL])) == 1:
big_diagonal_value = GRID[BIG_DIAGONAL[0][0]][BIG_DIAGONAL[0][1]]
for cell in SMALL_DIAGONAL:
if GRID[cell[0]][cell[1]] == big_diagonal_value:
ans_count += 1
ans_cells.append(' '.join(map(str, [val + 1 for val in cell])))
elif GRID[0][1] == GRID[1][0]:
small_diagonal_value = GRID[SMALL_DIAGONAL[0][0]][SMALL_DIAGONAL[0][1]]
for cell in BIG_DIAGONAL:
if GRID[cell[0]][cell[1]] == small_diagonal_value:
ans_count += 1
ans_cells.append(' '.join(map(str, [val + 1 for val in cell])))
else:
counter = Counter([GRID[cell[0]][cell[1]] for cell in BIG_DIAGONAL])
inv_counter = {counter[c]: c for c in counter}
more_occurring_value = inv_counter.get(3, '') or inv_counter.get(2, '')
for cell in SMALL_DIAGONAL:
if GRID[cell[0]][cell[1]] == more_occurring_value:
ans_count += 1
ans_cells.append(' '.join(map(str, [val + 1 for val in cell])))
for cell in BIG_DIAGONAL:
if GRID[cell[0]][cell[1]] != more_occurring_value:
ans_count += 1
ans_cells.append(' '.join(map(str, [val + 1 for val in cell])))
return str(ans_count) + '\n' + '\n'.join(ans_cells)
def main():
t, = get_int_input()
for _ in range(t):
globals()['GRID'] = []
n, = get_int_input()
for _ in range(n):
GRID.append(list(input()))
# n is equal to len(GRID)
print(C1421B(n))
main()
| 8 |
PYTHON3
|
def problem_sol(n, arr_field):
num_changes = 0
sol_list = []
s_right = int(arr_field[0][1])
s_down = int(arr_field[1][0])
f_left = int(arr_field[n - 1][n - 2])
f_up = int(arr_field[n - 2][n - 1])
if f_left == f_up:
if f_left == s_right:
sol_list.append('1 2')
num_changes += 1
if f_left == s_down:
sol_list.append('2 1')
num_changes += 1
elif s_right == s_down:
if s_right == f_left:
sol_list.append(str(n) + ' ' + str(n - 1))
num_changes += 1
if s_right == f_up:
sol_list.append(str(n - 1) + ' ' + str(n))
num_changes += 1
else:
if f_left == 1:
sol_list.append(str(n) + ' ' + str(n - 1))
num_changes += 1
else:
sol_list.append(str(n - 1) + ' ' + str(n))
num_changes += 1
if s_right == 0:
sol_list.append('1 2')
num_changes += 1
else:
sol_list.append('2 1')
num_changes += 1
print(num_changes)
for entry in sol_list:
print(entry)
no_inp = int(input())
list_of_var1 = []
while no_inp > 0:
n = int(input())
arr_field = [input() for i in range(n)]
list_of_var1.append([n, arr_field])
no_inp -= 1
for entry in list_of_var1:
problem_sol(entry[0], entry[1])
| 8 |
PYTHON3
|
t=int(input())
for i in range(t):
n=int(input())
a=[]
an=[]
for j in range(n):
a.append(input().strip())
p,q=int(a[0][1]),int(a[1][0])
r,s=int(a[-1][-2]),int(a[-2][-1])
if r==s:
pt=r^1
if p!=pt:
an.append((1,2))
if q!=pt:
an.append((2,1))
else:
if p==q:
pt=p^1
if r!=pt:
an.append((n,n-1))
else:
an.append((n-1,n))
else:
an.append((2,1))
pt=p^1
if r!=pt:
an.append((n,n-1))
else:
an.append((n-1,n))
print(len(an))
for u,v in an:
print(u,v)
| 8 |
PYTHON3
|
t = int(input())
while t:
t -= 1
n = int (input())
a= []
for i in range (n):
line = list (input())
#line = [int(x) for x in line if x not in {'S', 'F'} else x]
a.append (line)
#a,b,c,d = cells which over start and finish points
x, y, z, w = a[0][1], a[1][0], a[n-2][n-1], a[n-1][n-2]
if x==y and z==w:
if x != z:
print (0)
continue
elif x == z:
print (2)
print ('1 2')
print ('2 1')
continue
elif x == y and z != w:
if x == z:
print (1)
print (str (n-1)+' '+str (n))
continue
else:
print (1)
print (str(n)+' '+str(n-1))
continue
elif x!=y and z==w:
if x == z:
print (1)
print ('1 2')
continue
else:
print (1)
print ('2 1')
continue
elif x!=y and z!=w:
if x == z:
print (2)
print ('1 2')
print (str(n)+' '+str(n-1))
continue
else:
print (2)
print ('1 2')
print (str(n-1)+' '+str(n))
continue
| 8 |
PYTHON3
|
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def output(*args):
sys.stdout.buffer.write(
('\n'.join(map(str, args)) + '\n').encode('utf-8')
)
def main():
t = int(input())
ans_a = [''] * t
for ti in range(t):
n = int(input())
mat = [input().rstrip() for _ in range(n)]
cells = [mat[0][1], mat[1][0], mat[-1][-2], mat[-2][-1]]
ans = []
if len(set(cells)) == 1:
ans.extend(['1 2', '2 1'])
elif cells[0] == cells[1] and cells[2] == cells[3]:
pass
elif cells[0] == cells[1]:
if cells[0] == cells[2]:
ans.append(f'{n} {n-1}')
if cells[0] == cells[3]:
ans.append(f'{n-1} {n}')
elif cells[2] == cells[3]:
if cells[0] == cells[3]:
ans.append('1 2')
if cells[1] == cells[3]:
ans.append('2 1')
else:
if cells[0] == '1':
ans.append('1 2')
if cells[1] == '1':
ans.append('2 1')
if cells[2] == '0':
ans.append(f'{n} {n-1}')
if cells[3] == '0':
ans.append(f'{n-1} {n}')
ans_a[ti] = f'{len(ans)}\n' + '\n'.join(ans)
output(*ans_a)
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2e5 + 7, mod = 1e9 + 7;
const ll inf = 2e18;
int n;
string s[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
int a = s[0][1] + s[1][0] - 2 * '0',
b = s[n - 1][n - 2] + s[n - 2][n - 1] - 2 * '0';
vector<pair<int, int> > ans;
if (a == 0) {
if (s[n - 1][n - 2] == '0') ans.push_back({n, n - 1});
if (s[n - 2][n - 1] == '0') ans.push_back({n - 1, n});
} else if (a == 1) {
if (b == 1) {
if (s[0][1] == '0') ans.push_back({1, 2});
if (s[1][0] == '0') ans.push_back({2, 1});
if (s[n - 1][n - 2] == '1') ans.push_back({n, n - 1});
if (s[n - 2][n - 1] == '1') ans.push_back({n - 1, n});
} else if (b == 0) {
if (s[0][1] == '0') ans.push_back({1, 2});
if (s[1][0] == '0') ans.push_back({2, 1});
} else {
if (s[0][1] == '1') ans.push_back({1, 2});
if (s[1][0] == '1') ans.push_back({2, 1});
}
} else {
if (s[n - 1][n - 2] == '1') ans.push_back({n, n - 1});
if (s[n - 2][n - 1] == '1') ans.push_back({n - 1, n});
}
cout << (int)ans.size() << "\n";
for (auto u : ans) cout << u.first << " " << u.second << "\n";
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
a = []
s = ""
for i in range(n):
a.append(input())
if i == 0:
s += a[i][1] + a[i][2]
if i == 1:
s += a[i][0] + a[i][1]
if i == 2:
s += a[i][0]
count = 0
x = "01011"
y = "10100"
xl = []
yl = []
for i in range(5):
if s[i] != x[i]:
xl.append(i)
else:
yl.append(i)
if len(xl) < len(yl):
print(len(xl))
for i in range(len(xl)):
if xl[i] == 0:
print(1, 2)
elif xl[i] == 1:
print(1, 3)
elif xl[i] == 2:
print(2, 1)
elif xl[i] == 3:
print(2, 2)
else:
print(3, 1)
else:
print(len(yl))
for i in range(len(yl)):
if yl[i] == 0:
print(1, 2)
elif yl[i] == 1:
print(1, 3)
elif yl[i] == 2:
print(2, 1)
elif yl[i] == 3:
print(2, 2)
else:
print(3, 1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t, n, i, j, y, ans;
string s;
char ch;
cin >> t;
while (t--) {
cin >> n;
vector<int> a(4);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cin >> ch;
if (i == 0 && j == 1) {
a[0] = ch - '0';
}
if (i == 1 && j == 0) {
a[1] = ch - '0';
}
if (i == n - 2 && j == n - 1) {
a[2] = ch - '0';
}
if (i == n - 1 && j == n - 2) {
a[3] = ch - '0';
}
}
}
if (a[0] != a[1]) {
if (a[2] == a[3]) {
if (a[0] != a[2]) {
cout << 1 << "\n"
<< "2 1"
<< "\n";
} else {
cout << 1 << "\n"
<< "1 2"
<< "\n";
}
} else {
if (a[0] == a[2]) {
cout << 2 << "\n"
<< "2 1"
<< "\n"
<< (n - 1) << " " << n << "\n";
} else {
cout << 2 << "\n"
<< "2 1"
<< "\n"
<< (n) << " " << (n - 1) << "\n";
}
}
} else {
if (a[2] == a[3]) {
if (a[0] != a[2]) {
cout << 0 << "\n";
} else {
cout << 2 << "\n"
<< n - 1 << " " << n << "\n"
<< n << " " << n - 1 << "\n";
}
} else {
if (a[0] == a[2]) {
cout << 1 << "\n" << n - 1 << " " << n << "\n";
} else {
cout << 1 << "\n" << n << " " << n - 1 << "\n";
}
}
}
}
}
| 8 |
CPP
|
t=int(input())
for i in range(0,t):
n=int(input())
sl1=sl2=cz=0
for j in range(1,n+1):
w=input()
if(j==n-2):
a=w[n-1]
if(a=='1'): sl1+=1
if(j==n-1):
b=w[n-2]
if(b=='1'): sl1+=1
e=w[n-1]
if(e=='1'): sl2+=1
if(j==n):
c=w[n-3]
if(c=='1'): sl1+=1
f=w[n-2]
if(f=='1'): sl2+=1
if((sl1==0 and sl2==0) or (sl1==3 and sl2==2)):
print(2)
print('%d %d' %(n-1,n))
print('%d %d' %(n,n-1))
if((sl1==0 and sl2==2) or (sl1==3 and sl2==0)):
print(0)
if((sl1==0 or sl1==3) and sl2==1):
print(1)
if(sl1==0):
if(e=='0'): print('%d %d' %(n-1,n))
else: print('%d %d' %(n,n-1))
else:
if(e=='1'): print('%d %d' %(n-1,n))
else: print('%d %d' %(n,n-1))
if((sl1==1 or sl1==2) and sl2==1):
print(2)
if(sl1==1):
if(e=='0'): print('%d %d' %(n-1,n))
else: print('%d %d' %(n,n-1))
else:
if(e=='1'): print('%d %d' %(n-1,n))
else: print('%d %d' %(n,n-1))
if(sl1==1):
if(a=='1'): print('%d %d' %(n-2,n))
if(b=='1'): print('%d %d' %(n-1,n-1))
if(c=='1'): print('%d %d' %(n,n-2))
else:
if(a=='0'): print('%d %d' %(n-2,n))
if(b=='0'): print('%d %d' %(n-1,n-1))
if(c=='0'): print('%d %d' %(n,n-2))
if((sl1==1 or sl1==2) and (sl2==0 or sl2==2)):
if(sl2==0):
print(3-sl1)
if(a=='0'): print('%d %d' %(n-2,n))
if(b=='0'): print('%d %d' %(n-1,n-1))
if(c=='0'): print('%d %d' %(n,n-2))
else:
print(sl1)
if(a=='1'): print('%d %d' %(n-2,n))
if(b=='1'): print('%d %d' %(n-1,n-1))
if(c=='1'): print('%d %d' %(n,n-2))
| 8 |
PYTHON3
|
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
def main():
for _ in range(int(input())):
n=int(input())
a=[list(input()) for x in range(n)]
tl=int(a[1][0])
td=int(a[0][1])
ru=int(a[n-2][n-1])
rr=int(a[n-1][n-2])
ans=[]
if tl!=td and ru!=rr:
if tl==ru:
print(2)
print(2,1)
print(n,n-1)
elif tl==rr:
print(2)
print(1,2)
print(n,n-1)
elif tl==td==ru==rr:
print(2)
print(2,1)
print(1,2)
elif (tl==td==0 and ru==rr==1) or (tl==td==1 and ru==rr==0):
print(0)
elif (tl==td or ru==rr):
print(1)
if tl==td:
if tl==rr:
print(n,n-1)
else:
print(n-1,n)
elif ru==rr:
if rr==tl:
print(2,1)
else:
print(1,2)
else:
print(2)
print(2,1)
print(1,2)
#-----------------------------BOSS-------------------------------------!
# region fastio
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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
mat = []
n = int(input())
for _ in range(n):
mat.append(input())
start1 = mat[0][1]
start2 = mat[1][0]
end1 = mat[n-1][n-2]
end2 = mat[n-2][n-1]
# if start1 == '0' and start2 == '0':
# if end1 == '1' and end2 == '1':
# print(0)
# continue
# if end2 == '1' and end2 == '0':
# print(1)
# print(n-2, n-1)
# continue
# if end2 == '0' and end2 == '1':
# print(1)
# print(n-1, n-2)
ans = 0
changes= []
if start1 != start2:
if end1 == end2:
if start1 == end1:
ans+=1
changes.append([0, 1])
else:
ans += 1
changes.append([1,0])
elif start1 != end1:
ans += 2
changes.append([0,1])
changes.append([n-1, n-2])
else:
ans += 2
changes.append([1,0])
changes.append([n-1, n-2])
else:
if start1 != end1 and start2 != end2:
print(0)
continue
if start1 == end1 and start2 != end2:
ans += 1
changes.append([n-1, n-2])
elif start1 != end1 and start2 == end2:
ans += 1
changes.append([n-2, n-1])
else:
ans += 2
changes.append([n-1, n-2])
changes.append([n-2, n-1])
print(ans)
for line in changes:
print(int(line[0]) + 1, int(line[1]) + 1)
| 8 |
PYTHON3
|
import sys
for i in range(0,int(sys.stdin.readline())):
msize=int(sys.stdin.readline())
for j in range(0,msize):
q=sys.stdin.readline()
if j==0:
a=int(q[1])
elif j==1:
b=int(q[0])
if j==msize-2:
c=int(q[msize-1])
elif j==msize-1:
d=int(q[msize-2])
if a==b:
if c==d:
if a==c:
sys.stdout.write("2\n")
sys.stdout.write("1 2\n2 1\n")
else:
sys.stdout.write("0\n")
pass
else:
sys.stdout.write("1\n")
if a==c:
sys.stdout.write(str(msize-1)+" "+str(msize)+"\n")
else:
sys.stdout.write(str(msize)+" "+str(msize-1)+"\n")
else:
if c==d:
sys.stdout.write("1\n")
if a==c:
sys.stdout.write("1 2\n")
else:
sys.stdout.write("2 1\n")
else:
sys.stdout.write("2\n")
sys.stdout.write("1 2\n")
if a==c:
sys.stdout.write(str(msize)+" "+str(msize-1)+"\n")
else:
sys.stdout.write(str(msize-1)+" "+str(msize)+"\n")
| 8 |
PYTHON3
|
for t in range(int(input())):
n=int(input())
arr=[]
for i in range(n):
row=list(input())
arr.append(row)
d1=[int(arr[1][0]),int(arr[0][1])]
d2=[int(arr[n-1][n-2]),int(arr[n-2][n-1])]
if d1==[0,0]:
if d2==[0,0]:
print(2)
print(2,1)
print(1,2)
elif d2==[1,0]:
print(1)
print(n-1,n)
elif d2==[0,1]:
print(1)
print(n,n-1)
else:
print(0)
elif d1==[1,0]:
if d2==[0,0]:
print(1)
print(1,2)
elif d2==[1,0]:
print(2)
print(2,1)
print(n,n-1)
elif d2==[0,1]:
print(2)
print(1,2)
print(n-1,n)
elif d2==[1,1]:
print(1)
print(2,1)
elif d1==[0,1]:
if d2==[0,0]:
print(1)
print(2,1)
elif d2==[1,0]:
print(2)
print(1,2)
print(n-1,n)
elif d2==[0,1]:
print(2)
print(1,2)
print(n,n-1)
elif d2==[1,1]:
print(1)
print(1,2)
elif d1==[1,1]:
if d2==[0,0]:
print(0)
elif d2==[1,0]:
print(1)
print(n,n-1)
elif d2==[0,1]:
print(1)
print(n-1,n)
elif d2==[1,1]:
print(2)
print(n-1,n)
print(n,n-1)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
A = []
for _ in range(n):
A.append(input())
cnt = 0
ans = []
if A[0][1] == A[1][0]:
cur = A[0][1]
if A[-1][-2] == cur:
cnt += 1
ans.append([n, n - 1])
if A[-2][-1] == cur:
cnt += 1
ans.append([n - 1, n])
print(cnt)
for a in ans:
print(*a)
else:
cur = A[0][1] + A[1][0] + A[-1][-2] + A[-2][-1]
n1 = cur.count('1')
if n1 == 2:
if A[0][1] == '1':
ans.append([1, 2])
if A[1][0] == '1':
ans.append([2, 1])
if A[-2][-1] == '0':
ans.append([n - 1, n])
if A[-1][-2] == '0':
ans.append([n, n - 1])
print(2)
for a in ans:
print(*a)
else:
t = '1' if n1 == 1 else '0'
if '1' in A[0][1] + A[1][0]:
if A[0][1] == t:
ans.append([2, 1])
else:
ans.append([1, 2])
else:
if A[-2][-1] == t:
ans.append([n, n - 1])
else:
ans.append([n - 1, n])
print(1)
for a in ans:
print(*a)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n, p, i, c, d, e, f, q = 0, r = 0, l = 0, m = 0;
cin >> n;
p = n * n;
for (i = 0; i < p; i++) {
char s;
cin >> s;
if (i == 1) {
c = s - '0';
} else if (i == n) {
d = s - '0';
} else if (i == p - n - 1) {
e = s - '0';
} else if (i == p - 2) {
f = s - '0';
}
}
if (c == d) {
if (e == c || e == d) {
q = 1;
r++;
l = 1;
}
if (f == c || f == d) {
q = 1;
r++;
m = 1;
}
if (l == 1 && r == 1) {
cout << r << endl;
cout << (p - n) / n << " " << n << endl;
} else if (m == 1 && r == 1) {
cout << r << endl;
cout << n << " " << n - 1 << endl;
} else if (r == 2) {
cout << r << endl;
cout << (p - n) / n << " " << n << endl;
cout << n << " " << n - 1 << endl;
}
} else if (e == f) {
if (c == e || c == f) {
q = 1;
l = 1;
r++;
}
if (d == e || d == f) {
q = 1;
r++;
m = 1;
}
if (l == 1 && r == 1) {
cout << r << endl;
cout << 1 << " " << 2 << endl;
} else if (m == 1 && r == 1) {
cout << r << endl;
cout << 2 << " " << 1 << endl;
} else if (r == 2) {
cout << r << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
}
} else if (c != d && e != f) {
if (c == 1) {
q = 1;
r++;
l = 1;
} else if (d == 1) {
q = 1;
r++;
l = 1;
}
if (e == 0) {
q = 1;
r++;
m = 1;
} else if (f == 0) {
q = 1;
r++;
m = 1;
}
cout << r << endl;
if (l == 1) {
if (c == 1) {
cout << 1 << " " << 2 << endl;
} else if (d == 1) {
cout << 2 << " " << 1 << endl;
}
}
if (m == 1) {
if (e == 0) {
cout << (p - n) / n << " " << n << endl;
} else if (f == 0) {
cout << n << " " << n - 1 << endl;
}
}
}
if (q == 0) {
cout << 0 << endl;
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tests;
cin >> tests;
while (tests--) {
int n;
cin >> n;
vector<vector<char>> t(n);
char c;
for (auto& i : t) {
for (int j = 0; j < n; ++j) {
cin >> c;
i.push_back(c);
}
}
vector<pair<int, int>> ans;
char right_s = t[0][1];
char down_s = t[1][0];
char left_f = t[n - 1][n - 2];
char up_f = t[n - 2][n - 1];
if (right_s == down_s) {
if (right_s == '0')
c = '1';
else
c = '0';
if (left_f != c) ans.push_back(make_pair(n, n - 1));
if (up_f != c) ans.push_back(make_pair(n - 1, n));
} else if (left_f == up_f) {
if (left_f == '0')
c = '1';
else
c = '0';
if (right_s != c) ans.push_back(make_pair(1, 2));
if (down_s != c) ans.push_back(make_pair(2, 1));
} else {
c = right_s;
ans.push_back(make_pair(1, 2));
if (left_f != c) ans.push_back(make_pair(n, n - 1));
if (up_f != c) ans.push_back(make_pair(n - 1, n));
}
cout << ans.size() << '\n';
for (auto i : ans) {
cout << i.first << ' ' << i.second << '\n';
}
}
return 0;
}
| 8 |
CPP
|
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return list(map(int,input().split()))
def main():
t = inp()
for _ in range(0, t):
line = inp()
matrix = []
for _ in range(0, line):
matrix.append(insr())
a = helper(matrix, '1', '0')
if (a == None):
a = helper(matrix, '0', '1')
if a != None:
print(len(a))
for c in a:
print('{} {}'.format(c[0], c[1]))
def helper(matrix, top, bottom):
ans = []
if matrix[0][1] != top:
ans.append((1,2))
if matrix[1][0] != top:
ans.append((2,1))
if matrix[len(matrix)-1][len(matrix[0])- 2] != bottom:
ans.append((len(matrix),len(matrix[0]) - 1))
if matrix[len(matrix)-2][len(matrix[0]) - 1] != bottom:
ans.append((len(matrix) - 1,len(matrix[0])))
if len(ans) <= 2:
return ans
return None
if __name__ == "__main__":
main()
| 8 |
PYTHON3
|
t=int(input())
for x in range (t):
n=int(input())
a=[]
for i in range (n):
a.append(input())
if a[1][0]==a[0][1] and a[n-1][n-2]!=a[n-2][n-1]:
print(1)
if a[1][0]==a[n-1][n-2]:
print(n,n-1)
else:
print(n-1,n)
elif a[1][0]!=a[0][1] and a[n-1][n-2]==a[n-2][n-1]:
print(1)
if a[1][0] == a[n - 1][n - 2]:
print(2, 1)
else:
print(1, 2)
elif a[1][0]!=a[0][1] and a[n-1][n-2]!=a[n-2][n-1]:
print(2)
if a[1][0] == a[n - 1][n - 2]:
print(2, 1)
print(n-1, n)
else:
print(1, 2)
print(n-1, n)
elif a[1][0] == a[0][1] and a[n - 1][n - 2] == a[n - 2][n - 1]:
if a[1][0] == a[n - 1][n - 2]:
print(2)
print(2, 1)
print(1, 2)
else:
print(0)
| 8 |
PYTHON3
|
t = int(input())
while t:
t-=1
n = int(input())
a = []
for i in range(n):
a.append(input())
ans = []
if a[0][1] == "0" and a[1][0] == "0":
if a[n-1][n-2] == "1" and a[n-2][n-1] == "1":
pass
elif a[n-1][n-2] == "0" and a[n-2][n-1] == "0":
ans.append([0,1])
ans.append([1,0])
elif (a[n-1][n-2] == "0" and a[n-2][n-1] == "1"):
ans.append([n-1, n-2])
else:
ans.append([n-2, n-1])
elif a[0][1] == "1" and a[1][0] == "1":
if a[n-1][n-2] == "1" and a[n-2][n-1] == "1":
ans.append([0,1])
ans.append([1,0])
elif a[n-1][n-2] == "0" and a[n-2][n-1] == "0":
pass
elif (a[n-1][n-2] == "0" and a[n-2][n-1] == "1"):
ans.append([n-2, n-1])
else:
ans.append([n-1, n-2])
elif a[0][1] == "1" and a[1][0] == "0":
if a[n-1][n-2] == "1" and a[n-2][n-1] == "1":
ans.append([0, 1])
elif a[n-1][n-2] == "0" and a[n-2][n-1] == "0":
ans.append([1,0])
elif (a[n-1][n-2] == "0" and a[n-2][n-1] == "1"):
ans.append([n-2, n-1])
ans.append([1, 0])
else:
ans.append([n-1, n-2])
ans.append([1,0])
else:
if a[n-1][n-2] == "1" and a[n-2][n-1] == "1":
ans.append([1, 0])
elif a[n-1][n-2] == "0" and a[n-2][n-1] == "0":
ans.append([0,1])
elif (a[n-1][n-2] == "0" and a[n-2][n-1] == "1"):
ans.append([n-1, n-2])
ans.append([1, 0])
else:
ans.append([n-2, n-1])
ans.append([1,0])
print(len(ans))
for i in ans:
print(i[0]+1, i[1]+1)
| 8 |
PYTHON3
|
#import sys
# import math
# import bisect
# import collections
# import itertools
# #from sys import stdin,stdout
# from math import gcd,floor,sqrt,log
from collections import Counter as ctr
# from bisect import bisect_left as bl, bisect_right as br
# from itertools import permutations as pr, combinations as cb
#sys.setrecursionlimit(100000000)
#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$
inp = lambda: int(input())
# strng = lambda: input().strip()
# jn = lambda x,l: x.join(map(str,l))
strl = lambda: list(input().strip())
# mul = lambda: map(int,input().strip().split())
# mulf = lambda: map(float,input().strip().split())
seq = lambda: list(map(int,input().strip().split()))
#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$#$
# p_inf = float('inf')
# n_inf = float('-inf')
#To find mex
# def mex(arr):
# nList = set(arr)
# mex = 0
# while mex in nList:
# mex += 1
# return(mex)
def results(arr, n):
a, b, c, d = arr[0][1], arr[1][0], arr[n-1][-2], arr[n-2][-1]
cd = ctr([a, b, c, d])
if(len(cd) == 1): # 4 0 0 4
return([2, [[1, 2], [2, 1]]])
else:
if(cd['1'] == cd['0']): # 2 2
if(a == b):
return([0])
elif(a == c):
return([2, [[1, 2], [n - 1, n]]])
else:
return([2, [[1, 2], [n, n - 1]]])
if(cd['0'] > cd['1']): # 3 1
if(a != '0'):
return([1, [[2, 1]]])
elif(b != '0'):
return([1, [[1, 2]]])
elif(c != '0'):
return([1, [[n - 1, n]]])
else:
return([1, [[n, n - 1]]])
else: # 1 3
if(a != '1'):
return([1, [[2, 1]]])
if(b != '1'):
return([1, [[1, 2]]])
if(c != '1'):
return([1, [[n - 1, n]]])
else:
return([1, [[n, n - 1]]])
def main():
t = inp()
for _ in range(t):
n = inp()
a = []
for _ in range(n):
a.append(strl())
result = results(a, n)
if(len(result) == 1):
print(result[0])
else:
print(result[0])
for i in range(len(result[1])):
print(*result[1][i])
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
import math,sys
from sys import stdin,stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda:int(input())
li = lambda:list(map(int,input().split()))
def solve():
n=I()
a=[]
for i in range(n):
a.append(input().strip())
p=a[0][1]
q=a[1][0]
r=a[n-1][n-2]
s=a[n-2][n-1]
ans=[]
if(p=='0' and q=='0'):
if(r=='0'):
ans.append([n,n-1])
if(s=='0'):
ans.append([n-1,n])
elif(p=='1' and q=='1'):
if(r=='1'):
ans.append([n,n-1])
if(s=='1'):
ans.append([n-1,n])
elif(r=='0' and s=='0'):
if(p=='0'):
ans.append([1,2])
if(q=='0'):
ans.append([2,1])
elif(r=='1' and s=='1'):
if(p=='1'):
ans.append([1,2])
if(q=='1'):
ans.append([2,1])
else:
if(p=='1'):
ans.append([1,2])
if(q=='1'):
ans.append([2,1])
if(r=='0'):
ans.append([n,n-1])
if(s=='0'):
ans.append([n-1,n])
print(len(ans))
for i in ans:
print(*i)
for _ in range(I()):
solve()
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
g = [input() for _ in range(n)]
x1 = g[0][1] + g[1][0]
x2 = g[-1][-2] + g[-2][-1]
if x1[0] == x1[1] == x2[0] == x2[1]:
print(2)
print(1, 2)
print(2, 1)
continue
if (x1[0] != x2[0]) == (x1[1] != x2[1]):
if x1[0] == x1[1] and x2[0] == x2[1]:
print(0)
continue
if x1[0] == x2[1] and x2[0] == x1[1]:
print(2)
print(1, 2)
print(n, n-1)
continue
if x1[0] == x2[0] and x1[1] == x2[1]:
print(2)
print(1, 2)
print(n-1, n)
continue
if x1[0] == x1[1] == x2[0]:
print(1)
print(n, n-1)
continue
if x1[0] == x1[1] == x2[1]:
print(1)
print(n-1, n)
continue
if x2[0] == x2[1] == x1[0]:
print(1)
print(1, 2)
continue
if x2[0] == x2[1] == x1[1]:
print(1)
print(2, 1)
continue
| 8 |
PYTHON3
|
T=int(input())
for z in range(T):
n=int(input())
l=list()
for i in range(n):
s=input()
l.append(s)
det1=(l[n-1][n-3]+l[n-2][n-2]+l[n-3][n-1])
det2=(l[n-1][n-2]+l[n-2][n-1])
if det2=="11":
if det1=="111":
print(2)
print(n-1,n)
print(n,n-1)
elif det1=="110":
print(2)
print(n,n-2)
print(n-1,n-1)
elif det1=="101":
print(2)
print(n,n-2)
print(n-2,n)
elif det1=="100":
print(1)
print(n,n-2)
elif det1=="011":
print(2)
print(n-1,n-1)
print(n-2,n)
elif det1=="010":
print(1)
print(n-1,n-1)
elif det1=="001":
print(1)
print(n-2,n)
elif det1=="000":
print(0)
elif det2=="00":
if det1=="000":
print(2)
print(n-1,n)
print(n,n-1)
elif det1=="001":
print(2)
print(n,n-2)
print(n-1,n-1)
elif det1=="010":
print(2)
print(n,n-2)
print(n-2,n)
elif det1=="011":
print(1)
print(n,n-2)
elif det1=="100":
print(2)
print(n-1,n-1)
print(n-2,n)
elif det1=="101":
print(1)
print(n-1,n-1)
elif det1=="110":
print(1)
print(n-2,n)
elif det1=="111":
print(0)
elif det2=="10":
if det1=="111":
print(1)
print(n,n-1)
elif det1=="110":
print(2)
print(n,n-1)
print(n-2,n)
elif det1=="101":
print(2)
print(n-1,n-1)
print(n,n-1)
elif det1=="100":
print(2)
print(n,n-2)
print(n-1,n)
elif det1=="011":
print(2)
print(n,n-2)
print(n,n-1)
elif det1=="010":
print(2)
print(n-1,n-1)
print(n-1,n)
elif det1=="001":
print(2)
print(n-2,n)
print(n-1,n)
elif det1=="000":
print(1)
print(n-1,n)
elif det2=="01":
if det1=="000":
print(1)
print(n,n-1)
elif det1=="001":
print(2)
print(n,n-1)
print(n-2,n)
elif det1=="010":
print(2)
print(n-1,n-1)
print(n,n-1)
elif det1=="011":
print(2)
print(n,n-2)
print(n-1,n)
elif det1=="100":
print(2)
print(n,n-2)
print(n,n-1)
elif det1=="101":
print(2)
print(n-1,n-1)
print(n-1,n)
elif det1=="110":
print(2)
print(n-2,n)
print(n-1,n)
elif det1=="111":
print(1)
print(n-1,n)
| 8 |
PYTHON3
|
t = int(input())
for i in range(t):
n = int(input())
arr = []
for i in range(n):
temp = list(input())
arr.append(temp)
res = []
cnt = 0
if arr[0][1]=='0' and arr[1][0]=='0':
if arr[n-1][n-2] == '0':
cnt += 1
res.append([n,n-1])
if arr[n-2][n-1] == '0':
cnt += 1
res.append([n-1,n])
if arr[0][1]=='1' and arr[1][0]=='1':
if arr[n-1][n-2] == '1':
cnt += 1
res.append([n,n-1])
if arr[n-2][n-1] == '1':
cnt += 1
res.append([n-1,n])
if arr[0][1]=='0' and arr[1][0]=='1':
if arr[n-1][n-2] == '0' and arr[n-2][n-1] == '0':
cnt += 1
res.append([1,2])
if arr[n-1][n-2] == '1' and arr[n-2][n-1] == '1':
cnt += 1
res.append([2,1])
if arr[n-1][n-2] == '1' and arr[n-2][n-1] == '0':
cnt += 2
res.append([2,1])
res.append([n-1,n])
if arr[n-1][n-2] == '0' and arr[n-2][n-1] == '1':
cnt += 2
res.append([2,1])
res.append([n,n-1])
if arr[0][1]=='1' and arr[1][0]=='0':
if arr[n-1][n-2] == '0' and arr[n-2][n-1] == '0':
cnt += 1
res.append([2,1])
if arr[n-1][n-2] == '1' and arr[n-2][n-1] == '1':
cnt += 1
res.append([1,2])
if arr[n-1][n-2] == '1' and arr[n-2][n-1] == '0':
cnt += 2
res.append([2,1])
res.append([n,n-1])
if arr[n-1][n-2] == '0' and arr[n-2][n-1] == '1':
cnt += 2
res.append([2,1])
res.append([n-1,n])
if cnt == 0:
print(0)
else:
print(cnt)
for i in res:
print(*i)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int testcase;
cin >> testcase;
for (int i = 0; i < testcase; i++) {
int size;
cin >> size;
char map[size][size];
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
cin >> map[j][k];
}
}
char start1 = map[0][1], start2 = map[1][0], end1 = map[size - 1][size - 2],
end2 = map[size - 2][size - 1];
if (start1 == start2) {
if (end1 == end2 && start1 != end1) {
cout << "0" << endl;
continue;
} else if (end1 == end2) {
cout << "2" << endl;
cout << size << " " << size - 1 << endl;
cout << size - 1 << " " << size << endl;
continue;
} else if (end1 != end2) {
if (end1 == start1) {
cout << "1" << endl;
cout << size << " " << size - 1 << endl;
continue;
} else if (end2 == start1) {
cout << "1" << endl;
cout << size - 1 << " " << size << endl;
continue;
}
}
}
if (end1 == end2) {
if (start1 == start2 && start1 != end1) {
cout << "0" << endl;
continue;
} else if (start1 == start2) {
cout << "2" << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
continue;
} else if (start1 != start2) {
if (end1 == start1) {
cout << "1" << endl;
cout << "1 2" << endl;
continue;
} else if (end1 == start2) {
cout << "1" << endl;
cout << "2 1" << endl;
continue;
}
}
}
if (end1 != end2 && start1 != start2) {
cout << "2" << endl;
if (start1 == '0') {
cout << "1 2" << endl;
}
if (start2 == '0') {
cout << "2 1" << endl;
}
if (end1 == '1') {
cout << size << " " << size - 1 << endl;
}
if (end2 == '1') {
cout << size - 1 << " " << size << endl;
}
}
}
return 0;
}
| 8 |
CPP
|
r = int(input())
for t in range(r):
n = int(input())
mas = [[x for x in input()] for i in range(n)]
sright = mas[0][1]
sbottom = mas[1][0]
fleft = mas[-1][-2]
ftop = mas[-2][-1]
if sright == sbottom and fleft == ftop and sright != fleft:
print(0)
elif sright == sbottom:
if fleft == ftop:
print(2)
print("1 2")
print("2 1")
elif fleft == sright:
print(1)
print(str(n) + " " + str(n - 1))
elif ftop == sright:
print(1)
print(str(n - 1) + " " + str(n))
elif fleft == ftop:
if sright == fleft:
print(1)
print("1 2")
elif sbottom == fleft:
print(1)
print("2 1")
elif sright != ftop:
print(2)
print("1 2")
print(str(n - 1) + " " + str(n))
else:
print(2)
print("1 2")
print(str(n) + " " + str(n - 1))
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
a = []
for i in range(n):
d = list(input())
a.append(d)
c = 0
if a[0][1] == a[1][0]:
if a[n - 1][n - 2] == a[0][1]:
c += 1
if a[n - 2][n - 1] == a[0][1]:
c += 1
print(c)
print(n - 1, n)
print(n, n - 1)
else:
print(c)
print(n, n - 1)
elif a[n - 2][n - 1] == a[1][0]:
c += 1
print(c)
print(n - 1, n)
else:
print("0")
else:
c += 1
if a[n - 1][n - 2] == a[n - 2][n - 1]:
if a[n - 1][n - 2] == a[0][1]:
print(c)
print(1, 2)
else:
print(c)
print(2, 1)
else:
c += 1
if a[0][1] == a[n - 1][n - 2]:
print(c)
print(1, 2)
print(n - 1, n)
else:
print(c)
print(1, 2)
print(n, n - 1)
| 8 |
PYTHON3
|
import sys
from collections import deque
from itertools import product
from copy import deepcopy
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
def bfs(num):
que = deque([[0, 0]])
visited = [[False] * n for _ in range(n)]
visited[0][0] = True
while que:
h, w = que.popleft()
for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
next_h, next_w = h + dh, w + dw
if next_h < 0 or next_h >= n or next_w < 0 or next_w >= n:
continue
if visited[next_h][next_w]:
continue
elif S[next_h][next_w] == num or S[next_h][next_w] == "F":
visited[next_h][next_w] = True
que.append([next_h, next_w])
return visited[-1][-1]
t = int(input())
for j in range(t):
n = int(input())
S_init = [list(input().rstrip()) for _ in range(n)]
target = [(0, 1), (1, 0), (n - 2, n - 1), (n - 1, n - 2)]
flg = False
for pattern in product([0, 1], repeat=4):
if flg:
break
if sum(pattern) > 2:
continue
S = deepcopy(S_init)
res = []
for idx, p in enumerate(pattern):
if p == 1:
y, x = target[idx]
S[y][x] = "0" if S[y][x] == "1" else "1"
res.append([y + 1, x + 1])
if not bfs("0") and not bfs("1"):
print(len(res))
for i in res:
print(*i)
flg = True
if __name__ == '__main__':
resolve()
| 8 |
PYTHON3
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.