solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 9;
const int INF = (int)1e9 + 7;
int mod = (int)1e9 + 7;
string tab[209];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> tab[i];
char a0 = tab[0][1], a1 = tab[1][0];
char b0 = tab[n - 1][n - 2], b1 = tab[n - 2][n - 1];
if (0)
cout << "a0"
<< " = " << a0 << " "
<< "a1"
<< " = " << a1 << " "
<< "b0"
<< " = " << b0 << " "
<< "b1"
<< " = " << b1 << " " << endl;
if (a0 == a1) {
if (b0 == b1) {
if (b0 == a0)
cout << "2\n1 2\n2 1\n";
else
cout << "0\n";
} else {
if (b0 == a0)
cout << "1\n" << n << " " << n - 1 << "\n";
else
cout << "1\n" << n - 1 << " " << n << "\n";
}
} else {
if (b0 != b1) {
if (a0 == b0)
cout << "2\n1 2\n" << n - 1 << " " << n << "\n";
else
cout << "2\n1 2\n" << n << " " << n - 1 << "\n";
} else {
if (a0 == b0)
cout << "1\n1 2\n";
else
cout << "1\n2 1\n";
}
}
}
int main(int argc, char* argv[]) {
ios::sync_with_stdio(false);
if (0)
;
else
cin.tie(NULL), cout.tie(NULL);
int cases;
cin >> cases;
while (cases--) solve();
return 0;
}
| 8 |
CPP
|
t = int(input())
for i in range(t):
n = int(input())
a = [['.'] * n for j in range(n)]
res = 0
ans = []
for k in range(n):
a[k] = list(input())
if a[0][1] == '0' and a[1][0] == '0':
if a[-2][-1] == '0':
res += 1
ans.append((n - 1, n))
if a[-1][-2] == '0':
res += 1
ans.append((n, n - 1))
if a[0][1] == '1' and a[1][0] == '1':
if a[-2][-1] == '1':
res += 1
ans.append((n - 1, n))
if a[-1][-2] == '1':
res += 1
ans.append((n, n - 1))
if a[0][1] == '0' and a[1][0] == '1':
if a[-2][-1] == '0' and a[-1][-2] == '0':
res += 1
ans.append((1, 2))
if a[-2][-1] == '1' and a[-1][-2] == '1':
res += 1
ans.append((2, 1))
if a[-2][-1] == '0' and a[-1][-2] == '1':
res += 1
ans.append((2, 1))
res += 1
ans.append((n - 1, n))
if a[-2][-1] == '1' and a[-1][-2] == '0':
res += 1
ans.append((2, 1))
res += 1
ans.append((n, n - 1))
if a[0][1] == '1' and a[1][0] == '0':
if a[-2][-1] == '0' and a[-1][-2] == '0':
res += 1
ans.append((2, 1))
if a[-2][-1] == '1' and a[-1][-2] == '1':
res += 1
ans.append((1, 2))
if a[-2][-1] == '0' and a[-1][-2] == '1':
res += 1
ans.append((1, 2))
res += 1
ans.append((n - 1, n))
if a[-2][-1] == '1' and a[-1][-2] == '0':
res += 1
ans.append((1, 2))
res += 1
ans.append((n, n - 1))
print(res)
for x in ans:
print(*x)
| 8 |
PYTHON3
|
##############--->>>>> Deepcoder Amit Kumar Bhuyan <<<<<---##############
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import log,sqrt,factorial,cos,tan,sin,radians
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *
#import threading
#from itertools import permutations
#Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy
import sys
input = sys.stdin.readline
scanner = lambda: int(input())
string = lambda: input().rstrip()
get_list = lambda: list(read())
read = lambda: map(int, input().split())
get_float = lambda: map(float, input().split())
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // math.gcd(a, b)
def is_integer(n):
return math.ceil(n) == math.floor(n)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
if y == 0:
return 1
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
def next_prime(n, primes):
while primes[n] != True:
n += 1
return n
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
def binary_search(low, high, w, h, n):
while low < high:
mid = low + (high - low) // 2
# print(low, mid, high)
if check(mid, w, h, n):
low = mid + 1
else:
high = mid
return low
## for checking any conditions
def check(moves, n):
val = (moves + 1) // 2
rem = moves - val
sol = (val + 1) * (rem + 1)
return sol < n
## for sorting according to second position
def sortSecond(val):
return val[1]
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
alphs = "abcdefghijklmnopqrstuvwxyz"
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal
# from sys import stdout
# import numpy as np
"""
_______________
rough work here
_______________
roger waters has a square of size n x n
he wants to traverse his grid from upper left(1, 1)
to the lower right (n, n)
waters can move from a square to any other square
adjacent by a side as long as he is still in the grid
"""
def solve():
n = scanner()
grid = [string() for x in range(n)]
if grid[0][1] == '0' and grid[1][0] == '0':
if grid[-1][-2] == '0' and grid[-2][-1] == '0':
print(2)
print(n - 1, n)
print(n, n - 1)
return
elif grid[-1][-2] == '0':
print(1)
print(n, n - 1)
return
elif grid[-2][-1] == '0':
print(1)
print(n - 1, n)
return
else:
print(0)
return
if grid[0][1] == '1' and grid[1][0] == '1':
if grid[-1][-2] == '1' and grid[-2][-1] == '1':
print(2)
print(n - 1, n)
print(n, n - 1)
return
elif grid[-1][-2] == '1':
print(1)
print(n, n - 1)
return
elif grid[-2][-1] == '1':
print(1)
print(n - 1, n)
return
else:
print(0)
return
if grid[0][1] == '0' and grid[1][0] == '1':
if grid[-1][-2] == '1' and grid[-2][-1] == '1':
print(1)
print(2, 1)
return
elif grid[-1][-2] == '1' and grid[-2][-1] == '0':
print(2)
print(1, 2)
print(n, n -1)
return
elif grid[-1][-2] == '0' and grid[-2][-1] == '0':
print(1)
print(1, 2)
return
else:
print(2)
print(1, 2)
print(n - 1, n)
return
if grid[0][1] == '1' and grid[1][0] == '0':
if grid[-1][-2] == '1' and grid[-2][-1] == '1':
print(1)
print(1, 2)
return
elif grid[-1][-2] == '1' and grid[-2][-1] == '0':
print(2)
print(2, 1)
print(n, n -1)
return
elif grid[-1][-2] == '0' and grid[-2][-1] == '0':
print(1)
print(2, 1)
return
else:
print(2)
print(1, 2)
print(n, n - 1)
return
# region fastio
# template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
t = scanner()
for i in range(t):
solve()
#dmain()
# Comment Read()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
n = int(input())
grid = []
for i in range(n):
grid.append([i for i in input().strip()])
first = grid[1][0]
second = grid[0][1]
third = grid[n-1][n-2]
fourth = grid[n-2][n-1]
if first == second == third == fourth:
print(2)
print(2, 1)
print(1, 2)
elif first == second and third == fourth:
print(0)
elif first == "1" and second == "1" and third == "1" and fourth == "0":
print(1)
print(n, n-1)
elif first == "1" and second == "1" and third == "0" and fourth == "1":
print(1)
print(n-1, n)
elif first == "1" and second == "0" and third == "1" and fourth == "1":
print(1)
print(2, 1)
elif first == "0" and second == "1" and third == "1" and fourth == "1":
print(1)
print(1, 2)
elif first != "1" and second != "1" and third != "1" and fourth != "0":
print(1)
print(n, n-1)
elif first != "1" and second != "1" and third != "0" and fourth != "1":
print(1)
print(n-1, n)
elif first != "1" and second != "0" and third != "1" and fourth != "1":
print(1)
print(2, 1)
elif first != "0" and second != "1" and third != "1" and fourth != "1":
print(1)
print(1, 2)
elif first == "0" and second == "1" and third == "1" and fourth == "0":
print(2)
print(2, 1)
print(n, n-1)
elif first == "1" and second == "0" and third == "0" and fourth == "1":
print(2)
print(2, 1)
print(n, n-1)
elif first == "1" and second == "0" and third == "1" and fourth == "0":
print(2)
print(1, 2)
print(n, n-1)
elif first == "0" and second == "1" and third == "0" and fourth == "1":
print(2)
print(1, 2)
print(n, n-1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s[n];
for (long long i = 0; i < n; i++) {
cin >> s[i];
}
vector<pair<long long, long long> > arr;
if (s[0][1] == '0' || s[1][0] == '0') {
if (s[n - 1][n - 2] == '1' || s[n - 2][n - 1] == '1') {
if (s[0][1] == '1') {
arr.push_back(make_pair(1, 2));
}
if (s[1][0] == '1') {
arr.push_back(make_pair(2, 1));
}
if (s[n - 1][n - 2] == '0') {
arr.push_back(make_pair(n, n - 1));
}
if (s[n - 2][n - 1] == '0') {
arr.push_back(make_pair(n - 1, n));
}
} else {
if (s[0][1] == '0') {
arr.push_back(make_pair(1, 2));
}
if (s[1][0] == '0') {
arr.push_back(make_pair(2, 1));
}
}
} else {
if (s[n - 1][n - 2] == '1') {
arr.push_back(make_pair(n, n - 1));
}
if (s[n - 2][n - 1] == '1') {
arr.push_back(make_pair(n - 1, n));
}
}
cout << arr.size() << endl;
for (long long i = 0; i < arr.size(); i++) {
cout << arr[i].first << " " << arr[i].second << endl;
}
}
}
| 8 |
CPP
|
import sys
def swap(x, y, a):
temp = a[x]
a[x] = a[y]
a[y] = temp
return a
def solve():
return None
def main():
q = []
for line in sys.stdin.readlines():
q.append(line)
for i in range(len(q)):
q[i] = q[i].rstrip().split(' ')
i = 1
while i < len(q):
n = int(q[i][0])
matrix = [q[i+j+1][0] for j in range(n)]
c = 0
flag1 = 0
flag2 = 0
# print(matrix)
# start/end cases: [0,0],[1,0],[0,1],[1,1]
if matrix[0][1] == '0' and matrix[1][0] == '0':
if matrix[n-1][n-2] == '0':
flag1 = 1
c += 1
if matrix[n-2][n-1] == '0':
flag2 = 1
c+=1
print(c)
if flag1 == 1: print(n, n-1)
if flag2 == 1: print(n-1, n)
if matrix[0][1] == '1' and matrix[1][0] == '1':
if matrix[n-1][n-2] == '1':
flag1 = 1
c += 1
if matrix[n-2][n-1] == '1':
flag2 = 1
c+=1
print(c)
if flag1 == 1: print(n, n-1)
if flag2 == 1: print(n-1, n)
if matrix[0][1] == '1' and matrix[1][0] == '0':
# check ends and flip the one with less
if matrix[n-1][n-2] == '0':
flag1 = 1
c += 1
if matrix[n-2][n-1] == '0':
flag2 = 1
c+=1
if c == 0: # means both '1' so flip the 1->0
print(1)
print(1, 2)
elif c == 2: # means both '0' so flip the 0-> 1
print(1)
print(2, 1)
else: # means 1
print(2)
print(1,2)
if flag1 == 1: print(n, n-1)
if flag2 == 1: print(n-1, n)
if matrix[0][1] == '0' and matrix[1][0] == '1':
# check ends and flip the one with less
if matrix[n-1][n-2] == '0':
flag1 = 1
c += 1
if matrix[n-2][n-1] == '0':
flag2 = 1
c+=1
if c == 0: # means both '1' so flip the 1->0
print(1)
print(2, 1)
elif c == 2: # means both '0' so flip the 0-> 1
print(1)
print(1, 2)
else: # means 1
print(2)
print(2,1)
if flag1 == 1: print(n, n-1)
if flag2 == 1: print(n-1, n)
i+=n + 1
# solve(q[i])
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
import sys
def gt():
return list(map(int, input().split()))
pr = lambda x: print(*x[1])
t,=gt()
while t:
t -= 1
mp = []
n ,= gt()
for i in range(n):
mp.append(input())
a = int(mp[0][1]),(1,2)
b = int(mp[1][0]),(2,1)
c = int(mp[-1][-2]),(n,n-1)
d = int(mp[-2][-1]),(n-1,n)
if(c[0] == d[0]): a,b, c,d = c,d , a,b
if a[0] == b[0]:
if c[0] == d[0]:
if a[0] == c[0]:
print(2)
pr(c); pr(d)
else:
print(0)
else:
if a[0] == c[0]:
print(1)
pr(c)
else:
print(1)
pr(d)
else:
if a[0] == c[0]:
print(2)
pr(b); pr(c);
else:
print(2)
pr(a); pr(c);
| 8 |
PYTHON3
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
grid = []
for i in range(n):
grid.append(list(input())[:-1])
cnt = 0
ans = []
if grid[0][1] == grid[1][0] == "0":
if grid[n-2][n-1] == "0":
ans.append((n-1,n))
cnt += 1
if grid[n-1][n-2] == "0":
ans.append((n,n-1))
cnt += 1
elif grid[0][1] == grid[1][0] == "1":
if grid[n-2][n-1] == "1":
ans.append((n-1,n))
cnt += 1
if grid[n-1][n-2] == "1":
ans.append((n,n-1))
cnt += 1
elif grid[n-2][n-1] == grid[n-1][n-2] == "0":
if grid[0][1] == "0":
ans.append((1,2))
cnt += 1
if grid[1][0] == "0":
ans.append((2,1))
cnt += 1
elif grid[n-2][n-1] == grid[n-1][n-2] == "1":
if grid[0][1] == "1":
ans.append((1,2))
cnt += 1
if grid[1][0] == "1":
ans.append((2,1))
cnt += 1
else:
if grid[0][1] == "1":
ans.append((1,2))
cnt += 1
if grid[1][0] == "1":
ans.append((2,1))
cnt += 1
if grid[n-2][n-1] == "0":
ans.append((n-1,n))
cnt += 1
if grid[n-1][n-2] == "0":
ans.append((n,n-1))
cnt += 1
print(cnt)
for i in range(cnt):
print(ans[i][0],ans[i][1])
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char c = getchar();
long long x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
const long long M = 210;
long long n, a[M][M];
void check(long long x, long long y, long long p) {
if (a[x][y] != p) cout << x << ' ' << y << endl;
}
signed main() {
long long T = read();
while (T--) {
long long n = read();
for (long long i = 1; i <= n; i++) {
string s;
cin >> s;
for (long long j = 1; j <= n; j++) a[i][j] = s[j - 1] == '0' ? 0 : 1;
}
long long cnt = (a[n][n - 1] ^ 1) + (a[n - 1][n] ^ 1) + a[n - 1][n - 1] +
a[n][n - 2] + a[n - 2][n];
if (cnt <= 2) {
cout << cnt << endl;
check(n, n - 1, 1), check(n - 1, n, 1), check(n - 1, n - 1, 0);
check(n - 2, n, 0), check(n, n - 2, 0);
} else {
cout << 5 - cnt << endl;
check(n, n - 1, 0), check(n - 1, n, 0), check(n - 1, n - 1, 1);
check(n - 2, n, 1), check(n, n - 2, 1);
}
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
matrix = []
for i in range(n):
s = input()
matrix.append(s)
s = ""
s += matrix[0][1] + matrix[1][0] + matrix[-1][-2] + matrix[-2][-1]
z, o = s.count("0"), s.count("1")
if s[:2] == "00" and s[2:] == "11" or s[:2] == "11" and s[2:] == "00":
print(0)
elif z == 4 or o == 4:
print(2)
print(1, 2)
print(2, 1)
else:
print(min(z, o))
if z != o:
if s[:2] == "00":
if s[2] == "0":
print(n, n-1)
elif s[3] == "0":
print(n-1, n)
elif s[:2] == "11":
if s[2] == "1":
print(n, n-1)
elif s[3] == "1":
print(n-1, n)
elif s[2:] == "00":
if s[0] == "0":
print(1, 2)
elif s[1] == "0":
print(2, 1)
elif s[2:] == "11":
if s[0] == "1":
print(1, 2)
elif s[1] == "1":
print(2, 1)
else:
if s[:2] == "01" and s[2:] == "01":
print(1, 2)
print(n-1, n)
elif s[:2] == "10" and s[2:] == "01":
print(2, 1)
print(n-1, n)
elif s[:2] == "01" and s[2:] == "10":
print(1, 2)
print(n, n-1)
elif s[:2] == "10" and s[2:] == "10":
print(2, 1)
print(n, n-1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
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];
}
int w, x, y, z, cnt = 0, ans[4];
w = a[0][1];
x = a[1][0];
y = a[n - 1][n - 2];
z = a[n - 2][n - 1];
if (w == x) {
if (y == z) {
if (w == y) {
cnt = 2;
ans[0] = 0;
ans[1] = 1;
ans[2] = 1;
ans[3] = 0;
}
} else {
if (w == y) {
cnt++;
ans[0] = n - 1;
ans[1] = n - 2;
} else if (w == z) {
cnt++;
ans[0] = n - 2;
ans[1] = n - 1;
}
}
} else {
if (y == z) {
if (w == y) {
cnt++;
ans[0] = 0;
ans[1] = 1;
} else if (x == y) {
cnt++;
ans[0] = 1;
ans[1] = 0;
}
} else {
cnt++;
ans[0] = 1;
ans[1] = 0;
x = w;
if (x == y) {
cnt++;
ans[2] = n - 1;
ans[3] = n - 2;
} else if (x == z) {
cnt++;
ans[2] = n - 2;
ans[3] = n - 1;
}
}
}
cout << cnt << endl;
cnt *= 2;
for (int i = 0; i < cnt; i += 2) {
cout << ans[i] + 1 << " " << ans[i + 1] + 1;
cout << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
| 8 |
CPP
|
t = int(input())
for _ in range(t):
n = int(input())
grid = []
for i in range(n):
grid.append(input())
if grid[0][1] != grid[1][0] and grid[n-1][n-2] != grid[n-2][n-1]:
print(2)
if grid[0][1] != grid[n-1][n-2]:
print(1, 2)
print(n, n-1)
else:
print(1, 2)
print(n-1, n)
elif grid[0][1] != grid[1][0] and grid[n-1][n-2] == grid[n-2][n-1]:
print(1)
if grid[0][1] == grid[n-1][n-2]:
print(1, 2)
else:
print(2, 1)
elif grid[0][1] == grid[1][0] and grid[n-1][n-2] == grid[n-2][n-1] and grid[0][1] != grid[n-1][n-2]:
print(0)
elif grid[0][1] == grid[1][0] and grid[n-1][n-2] == grid[n-2][n-1] and grid[0][1] == grid[n-1][n-2]:
print(2)
print(1, 2)
print(2, 1)
elif grid[0][1] == grid[1][0] and grid[n-1][n-2] != grid[n-2][n-1] and grid[0][1] == grid[n-1][n-2]:
print(1)
print(n, n-1)
elif grid[0][1] == grid[1][0] and grid[n-1][n-2] != grid[n-2][n-1] and grid[0][1] != grid[n-1][n-2]:
print(1)
print(n-1, n)
| 8 |
PYTHON3
|
for i in range(int(input())):
a=int(input())
b=[]
for i in range(a):
b.append(input().strip())
if b[1][1]==b[0][2] and b[1][1]==b[2][0]:
if b[0][1]==b[1][0]:
if b[0][1]==b[1][1]:
print(2)
print(1,2)
print(2,1)
else:
print(0)
else:
if b[0][1]==b[1][1]:
print(1)
print(1,2)
else:
print(1)
print(2,1)
elif b[0][2]==b[2][0]:
if b[0][1]==b[1][0]:
if b[0][1]==b[1][1]:
print(1)
print(2,2)
else:
print(2)
print(1,3)
print(3,1)
else:
if b[0][1]==b[1][1]:
print(2)
print(2,2)
print(2,1)
else:
print(2)
print(2,2)
print(1,2)
elif b[0][2]==b[1][1]:
if b[0][1]==b[1][0]:
if b[0][1]==b[1][1]:
print(2)
print(2,2)
print(1,3)
else:
print(1)
print(3,1)
else:
if b[0][1]==b[1][1]:
print(2)
print(1,2)
print(3,1)
else:
print(2)
print(2,1)
print(3,1)
elif b[2][0]==b[1][1]:
if b[0][1]==b[1][0]:
if b[0][1]==b[1][1]:
print(2)
print(2,2)
print(3,1)
else:
print(1)
print(1,3)
else:
if b[0][1]==b[1][1]:
print(2)
print(1,2)
print(1,3)
else:
print(2)
print(2,1)
print(1,3)
| 8 |
PYTHON3
|
import sys
import math
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
for _ in range(int(input())):
n=int(input())
z=[]
for i in range(n):
z.append(input())
ans=[]
if z[0][1]=="0" and z[1][0]=="0":
if z[n-2][n-1]=="0":
ans.append(list([n-2+1,n-1+1]))
if z[n-1][n-2]=="0":
ans.append(list([n-1+1,n-2+1]))
elif z[0][1]=="1" and z[1][0]=="1":
if z[n-2][n-1]=="1":
ans.append(list([n-2+1,n-1+1]))
if z[n-1][n-2]=="1":
ans.append(list([n-1+1,n-2+1]))
elif z[0][1] == "1" and z[1][0] == "0":
if z[n - 2][n - 1] == "0" and z[n-1][n-2]=="0":
ans.append(list([2,1]))
elif z[n - 1][n - 2] == "1" and z[n-2][n-1]=="1":
ans.append(list([1,2]))
elif z[n-1][n-2]=="1" and z[n-2][n-1]=="0":
ans.append(list([1,2]))
ans.append(list([n-2+1,n-1+1]))
elif z[n-1][n-2]=="0" and z[n-2][n-1]=="1":
ans.append(list([2,1]))
ans.append(list([n-2+1,n-1+1]))
elif z[0][1] == "0" and z[1][0] == "1":
if z[n - 2][n - 1] == "0" and z[n-1][n-2]=="0":
ans.append(list([1,2]))
elif z[n - 1][n - 2] == "1" and z[n-2][n-1]=="1":
ans.append(list([2,1]))
elif z[n-1][n-2]=="1" and z[n-2][n-1]=="0":
ans.append(list([2,1]))
ans.append(list([n-2+1,n-1+1]))
elif z[n-1][n-2]=="0" and z[n-2][n-1]=="1":
ans.append(list([1,2]))
ans.append(list([n-2+1,n-1+1]))
if len(ans)==0:
print(0)
else:
print(len(ans))
for i in range(len(ans)):
print(*ans[i])
| 8 |
PYTHON3
|
import os
import sys
from io import BytesIO, IOBase
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")
##########################################################
import math
import bisect
from collections import Counter
# sys.setrecursionlimit(10**6)
# dp=[[-1 for i in range(n+5)]for j in range(cap+5)]
# arr= list(map(int, input().split()))
# n,m= map(int, input().split())
# arr= list(map(int, input().split()))
# for _ in range(int(input())):
import bisect
# n=int(input())
#for _ in range(int(input())):
import heapq as h
#n=int(input())
from collections import defaultdict
#n,m=map(int, input().split())
#ls= list(map(int, input().split()))
# Python3 program to count inversions using
from collections import deque
for _ in range(int(input())):
n=int(input())
g=[]
for i in range(n):
s=list(input())
g.append(s)
if g[0][1]==g[1][0] and g[0][1]=="0":
ans=[]
if g[n-1][-2]=="0":
ans.append([n,n-1])
if g[n-2][n-1]=="0":
ans.append([n-1,n])
print(len(ans))
for k in ans:
print(*k)
elif g[0][1]==g[1][0] and g[0][1]=="1":
ans=[]
if g[n-1][-2]=="1":
ans.append([n,n-1])
if g[n-2][n-1]=="1":
ans.append([n-1,n])
print(len(ans))
for k in ans:
print(*k)
else:
if g[n-1][n-2]==g[n-2][n-1]=="0":
ans=[]
if g[0][1]!= "1":
ans.append([1,2])
if g[1][0]!="1":
ans.append([2,1])
print(len(ans))
for k in ans:
print(*k)
elif g[n-1][n-2]==g[n-2][n-1]=="1":
ans=[]
if g[0][1]!= "0":
ans.append([1,2])
if g[1][0]!="0":
ans.append([2,1])
print(len(ans))
for k in ans:
print(*k)
else:
ans=[]
if g[0][1]!= "1":
ans.append([1,2])
if g[1][0]!="1":
ans.append([2,1])
if g[n-1][n-2]!="0":
ans.append([n,n-1])
if g[n-2][n-1]!="0":
ans.append([n-1,n])
print(len(ans))
for k in ans:
print(*k)
| 8 |
PYTHON3
|
t=int(input())
for _ in range(t):
n=int(input())
mt=[]
for i in range(n):
s=input()
mt.append(s)
a=mt[0][1]
b=mt[1][0]
c=mt[-1][-2]
d=mt[-2][-1]
if c==d=='1':
if a==b=='0':
print(0)
elif a==b=='1':
print(2)
print(1,2)
print(2,1)
elif a=='0' and b=='1':
print(1)
print(2,1)
else:
print(1)
print(1,2)
elif c==d=='0':
if a==b=='1':
print(0)
elif a==b=='0':
print(2)
print(1,2)
print(2,1)
elif a=='0' and b=='1':
print(1)
print(1,2)
else:
print(1)
print(2,1)
elif c=='0' and d=='1':
if a=='0' and b=='0':
print(1)
print(n,n-1)
elif a=='0' and b=='1':
print(2)
print(2,1)
print(n,n-1)
elif a=='1' and b=='0':
print(2)
print(1,2)
print(n,n-1)
else:
print(1)
print(n-1,n)
else:
if a=='0' and b=='0':
print(1)
print(n-1,n)
elif a=='0' and b=='1':
print(2)
print(1,2)
print(n,n-1)
elif a=='1' and b=='0':
print(2)
print(2,1)
print(n,n-1)
else:
print(1)
print(n,n-1)
| 8 |
PYTHON3
|
from sys import stdin,stdout
ans = []
def main():
n = int(stdin.readline())
arr = [list(stdin.readline())[:-1] for _ in range(n)]
sr,sb,fl,ft= [int(arr[0][1]),int(arr[1][0]),int(arr[-1][-2]),int(arr[-2][-1])]
count =0
sumval = int(sr) + int(sb)+int(fl)+int(ft)
if sumval == 4 or sumval == 0:
ans.append(str(2))
ans.append('1 2')
ans.append('2 1')
return
if sumval == 2:
if sr == sb:
ans.append('0')
return
if sr == fl:
ans.append('2')
ans.append('1 2')
ans.append(str(n-1) +' '+str(n))
else:
ans.append('2')
ans.append('1 2')
ans.append(str(n) +' '+str(n-1))
if sumval == 1 or sumval == 3:
check = 1 if sumval == 1 else 0
ans.append('1')
if sr == check:
ans.append('2 1')
if sb == check:
ans.append('1 2')
if fl == check:
ans.append(str(n-1) + ' ' + str(n))
if ft == check:
ans.append(str(n) + ' ' + str(n-1))
return
if __name__== '__main__':
for i in range(int(stdin.readline())):
main()
stdout.write('\n'.join(ans))
| 8 |
PYTHON3
|
for t in range(int(input())):
s = []
n = int(input())
for i in range(n):
s.append(input())
n = n-1
seti = {s[0][1],s[1][0],s[n][n-1],s[n-1][n]}
if seti=={'1'} or seti =={'0'}:
print(2)
print("1 2")
print("2 1")
elif s[0][1] == s[1][0] and s[n][n-1]==s[n-1][n]:
print(0)
elif s[0][1] == s[n][n-1] and s[1][0]==s[n-1][n]:
print(2)
print("1 2")
print("%i %i"%(n,n+1))
elif s[0][1] == s[n-1][n] and s[1][0] == s[n][n-1]:
print(2)
print("1 2")
print("%i %i"%(n+1,n))
elif s[0][1] != s[1][0]:
print(1)
if s[0][1] == s[n][n-1]:
print("1 2")
else:
print("2 1")
elif s[n][n-1]!=s[n-1][n]:
print(1)
if s[0][1] == s[n][n-1]:
print("%i %i"%(n+1,n))
else:
print("%i %i"%(n,n+1))
| 8 |
PYTHON3
|
t=int(input())
for k in range(t):
n=int(input())
grid=[]
for i in range(n):
grid.append(input())
a,b,c,d=grid[0][1],grid[1][0],grid[n-1][n-2],grid[n-2][n-1]
if a==b and c==d and a!=c:
print(0)
elif a==b and c!=d:
print(1)
if a==c:
print(n,n-1)
else:
print(n-1,n)
elif a!=b and c==d:
print(1)
if a==c:
print(1,2)
else:
print(2,1)
elif a==b==c==d:
print(2)
print(1,2)
print(2,1)
elif a==d and b==c and a!=b:
print(2)
print(1,2)
print(n,n-1)
elif a==c and b==d and a!=b:
print(2)
print(1,2)
print(n-1,n)
| 8 |
PYTHON3
|
import sys
import math
from collections import defaultdict,deque
input = sys.stdin.readline
def inar():
return [int(el) for el in input().split()]
def main():
t=int(input())
for _ in range(t):
n=int(input())
matrix=[]
for i in range(n):
matrix.append(input().strip())
count1=0
ans1=[]
if matrix[1][0]!='1':
count1+=1
ans1.append([2,1])
if matrix[0][1]!='1':
count1+=1
ans1.append([1,2])
if matrix[n-1][n-2]!='0':
count1+=1
ans1.append([n, n-1])
if matrix[n-2][n-1]!='0':
count1+=1
ans1.append([n-1, n])
count2=0
ans2=[]
if matrix[1][0]!='0':
count2+=1
ans2.append([2,1])
if matrix[0][1]!='0':
count2+=1
ans2.append([1,2])
if matrix[n-1][n-2]!='1':
count2+=1
ans2.append([n, n-1])
if matrix[n-2][n-1]!='1':
count2+=1
ans2.append([n-1, n])
if count1<count2:
print(len(ans1))
for i in range(len(ans1)):
print(*ans1[i])
else:
print(len(ans2))
for i in range(len(ans2)):
print(*ans2[i])
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 205;
int n, ans, T;
char a[maxn][maxn];
int main() {
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cin >> a[i][j];
}
int p1 = a[1][2] - '0', p2 = a[2][1] - '0', q1 = a[n][n - 1] - '0',
q2 = a[n - 1][n] - '0';
int cnt = p1 + p2 + q1 + q2;
if (cnt == 0 || cnt == 4) {
cout << 2 << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (cnt == 1 || cnt == 3) {
cout << 1 << endl;
int f = 1;
if (cnt == 3) f = 0;
if (p1 == f) cout << "2 1" << endl;
if (p2 == f) cout << "1 2" << endl;
if (q1 == f) cout << n - 1 << ' ' << n << endl;
if (q2 == f) cout << n << ' ' << n - 1 << endl;
} else {
if (p1 == p2)
cout << 0 << endl;
else {
cout << 2 << endl;
if (p1 == 0) cout << "1 2" << endl;
if (p2 == 0) cout << "2 1" << endl;
if (q1 == 1) cout << n << ' ' << n - 1 << endl;
if (q2 == 1) cout << n - 1 << ' ' << n << endl;
}
}
}
return 0;
}
| 8 |
CPP
|
cases=int(input())
for _ in range(cases):
n = int(input())
grid = []
for _ in range(n):
grid.append(list(input()))
op = cl = None
perms = []
if grid[0][1] == grid[1][0]:
op = grid[0][1]
if grid[-1][-2] == grid[-2][-1]:
cl = grid[-1][-2]
if op and cl and op == cl:
print(2)
print(1, 2)
print(2, 1)
elif op and cl:
print(0)
elif op:
print(1)
if grid[-1][-2] == op:
print(n, n-1)
else:
print(n-1, n)
elif cl:
print(1)
if grid[0][1] == cl:
print(1, 2)
else:
print(2, 1)
else:
print(2)
print(1, 2)
if grid[0][1] == grid[-1][-2]:
print(n-1, n)
else:
print(n, n-1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T a) {
return a * a;
}
template <class T>
T abs(T x) {
if (x < 0) return -x;
return x;
}
const double eps = 1e-8;
const double pi = acos(-1.0);
struct node {
int x, y, val;
};
char str[210][210];
vector<node> vec;
int main() {
int tks, ks = 1;
scanf("%d", &tks);
while (tks--) {
vec.clear();
node a, b, c, d;
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%s", str[i]);
a.x = 0;
a.y = 1;
a.val = str[a.x][a.y] - '0';
b.x = 1;
b.y = 0;
b.val = str[b.x][b.y] - '0';
c.x = n - 1;
c.y = n - 2;
c.val = str[c.x][c.y] - '0';
d.x = n - 2;
d.y = n - 1;
d.val = str[d.x][d.y] - '0';
if (a.val == b.val) {
if (a.val == c.val) vec.push_back(c);
if (a.val == d.val) vec.push_back(d);
} else if (c.val == d.val) {
if (a.val == c.val) vec.push_back(a);
if (b.val == c.val) vec.push_back(b);
} else {
if (a.val == 1)
vec.push_back(b);
else
vec.push_back(a);
if (c.val == 0)
vec.push_back(d);
else
vec.push_back(c);
}
printf("%d\n", vec.size());
for (i = 0; i < vec.size(); i++)
printf("%d %d\n", vec[i].x + 1, vec[i].y + 1);
}
return 0;
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
arr = []
for i in range(n):
arr.append([ch for ch in input()])
count = 0
changes = []
if arr[0][1] == arr[1][0]:
if arr[n-1][n-2] == arr[1][0]:
count+=1
changes.append("{} {}".format(n, n-1))
if arr[n-2][n-1] == arr[1][0]:
count+=1
changes.append("{} {}".format(n-1, n))
elif arr[0][1] != arr[1][0]:
if arr[n-1][n-2] == arr[n-2][n-1]:
if arr[1][0] == arr[n-1][n-2]:
count += 1
changes.append("{} {}".format(2,1))
if arr[0][1] == arr[n-1][n-2]:
count += 1
changes.append("{} {}".format(1,2))
else:
count += 1
changes.append("{} {}".format(1,2))
if arr[n-1][n-2] == arr[1][0]:
count += 1
changes.append("{} {}".format(n,n-1))
if arr[n-2][n-1] == arr[1][0]:
count += 1
changes.append("{} {}".format(n-1, n))
print(count)
# if count == 0:
# break
print("\n".join(changes))
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<string> arr(n);
for (long long i = 0; i < n; i++) cin >> arr[i];
char sa = arr[0][1], sb = arr[1][0], fa = arr[n - 1][n - 2],
fb = arr[n - 2][n - 1];
vector<vector<long long>> res;
if (sa == sb) {
if (fa == sa) res.push_back({n - 1, n - 2});
if (fb == sa) res.push_back({n - 2, n - 1});
} else if (fa == fb) {
if (fa == sa) res.push_back({0, 1});
if (fa == sb) res.push_back({1, 0});
} else {
if (sa == '0') res.push_back({0, 1});
if (sb == '0') res.push_back({1, 0});
if (fa == '1') res.push_back({n - 1, n - 2});
if (fb == '1') res.push_back({n - 2, n - 1});
}
cout << res.size() << endl;
for (auto v : res) cout << v[0] + 1 << " " << v[1] + 1 << endl;
}
}
| 8 |
CPP
|
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n = getint()
s = []
for _ in range(n):
s.append(input())
a, b, c, d = s[0][1], s[1][0], s[n-2][n-1], s[n-1][n-2]
ans = []
if a == b:
if c == a:
ans.append((n-1, n))
if d == a:
ans.append((n, n-1))
elif c == d:
if c == a:
ans.append((1, 2))
if c == b:
ans.append((2, 1))
else:
ans.append((2, 1))
if c == a:
ans.append((n-1, n))
if d == a:
ans.append((n, n-1))
print(len(ans))
for x, y in ans:
print(x, y)
# 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)
def input(): return sys.stdin.readline().rstrip("\r\n")
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint1(): return list(map(lambda x: int(x) - 1, input().split()))
# endregion
if __name__ == "__main__":
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int solve() {
int n;
cin >> n;
vector<vector<char>> gr(n, vector<char>(n));
vector<vector<int>> g(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> gr[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == 0 && j == 0) || (i == n - 1 && j == n - 1))
g[i][j] = 2;
else
g[i][j] = gr[i][j] - '0';
}
}
if (g[0][1] == 0 && g[1][0] == 1) {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << "2 1" << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n - 1 << " " << n << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << "1" << endl;
cout << "1 2" << endl;
}
} else if (g[0][1] == 1 && g[1][0] == 1) {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "2" << endl;
cout << n - 1 << " " << n << endl;
cout << n << " " << n - 1 << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << "0" << endl;
}
} else if (g[0][1] == 1 && g[1][0] == 0) {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << "1 2" << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n << " " << n - 1 << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << "1" << endl;
cout << "2 1" << endl;
}
} else {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "0" << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << "2" << endl;
cout << n - 1 << " " << n << endl;
cout << n << " " << n - 1 << endl;
}
}
return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 8 |
CPP
|
from collections import deque
from heapq import heapify, heappush, heappop
t = int(input())
# ar = list(map(int, input().split()))
# ar = []
for _ in range(t):
n = int(input())
ar = []
for _ in range(n):
ar.append(input())
kek = [ar[1][0], ar[0][1], ar[-1][-2], ar[-2][-1]]
kek = list(map(int, kek))
p = kek[0] + kek[1]
z = kek[2] + kek[3]
res = []
if p == 0:
if kek[2] == 0:
res.append((n, n-1))
if kek[3] == 0:
res.append((n-1, n))
if p == 2:
if kek[2] == 1:
res.append((n, n-1))
if kek[3] == 1:
res.append((n-1, n))
if p == 1:
if z == 1:
if kek[0] == 1:
res.append((2, 1))
if kek[1] == 1:
res.append((1, 2))
if kek[2] == 0:
res.append((n, n-1))
if kek[3] == 0:
res.append((n-1, n))
else:
if z == 0:
if kek[0] == 0:
res.append((2, 1))
if kek[1] == 0:
res.append((1, 2))
if z == 2:
if kek[0] == 1:
res.append((2, 1))
if kek[1] == 1:
res.append((1, 2))
print(len(res))
for x in res:
print(x[0], x[1])
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n, i, j, count = 0;
cin >> n;
char ch[n + 1][n + 1];
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cin >> ch[i][j];
}
}
long long int b[5][5];
b[1][2] = b[2][1] = 0;
b[3][1] = b[2][2] = b[1][3] = 1;
long long int c[5][5];
c[1][2] = c[2][1] = 1;
c[3][1] = c[2][2] = c[1][3] = 0;
count = 0;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != b[i][j]) {
count++;
}
}
}
}
if (count <= 2) {
cout << count << '\n';
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != b[i][j]) {
cout << i << " " << j << '\n';
}
}
}
}
} else {
count = 0;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != c[i][j]) {
count++;
}
}
}
}
cout << count << '\n';
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != c[i][j]) {
cout << i << " " << j << '\n';
}
}
}
}
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long int ceil(long long int a, long long int b) {
if ((a >= 0 && b >= 0) || (a < 0 && b < 0)) return (a / b) + ((a % b) != 0);
return a / b;
}
long long int floor(long long int a, long long int b) {
if ((a >= 0 && b >= 0) || (a < 0 && b < 0)) return a / b;
return a / b - ((a % b) != 0);
}
void IO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void solve(vector<string>& grid, int n) {
char a = grid[1][0];
char b = grid[0][1];
char x = grid[n - 1][n - 2];
char y = grid[n - 2][n - 1];
int num0 = 0;
if (a == '0') num0++;
if (b == '0') num0++;
if (x == '0') num0++;
if (y == '0') num0++;
if (num0 == 0 || num0 == 4) {
cout << 2 << "\n";
cout << "2 1\n1 2\n";
} else if (num0 == 1 || num0 == 3) {
cout << 1 << "\n";
if (a == b) {
if (x == a) cout << n << " " << n - 1 << "\n";
if (y == a) cout << n - 1 << " " << n << "\n";
}
if (x == y) {
if (a == x) cout << "2 1\n";
if (b == x) cout << "1 2\n";
}
} else {
if (a == b) {
cout << "0\n";
return;
}
cout << 2 << "\n";
if (a == '1') cout << "2 1\n";
if (b == '1') cout << "1 2\n";
if (x == '0') cout << n << " " << n - 1 << "\n";
if (y == '0') cout << n - 1 << " " << n << "\n";
}
}
int main() {
IO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<string> grid(n);
for (int i = 0; i < n; i++) cin >> grid[i];
solve(grid, n);
}
}
| 8 |
CPP
|
t=int(input())
for _ in range(t):
n=int(input())
mat=[input() for _ in range(n)]
ans=[]
#if both the adjacent cell are same(of S)
if(mat[0][1]==mat[1][0]):
if(mat[n-2][n-1]==mat[0][1]):
#print(n-2,n-1)
ans.append([n-2+1,n-1+1])
if(mat[n-1][n-2]==mat[0][1]):
#print(n-1,n-2)
ans.append([n-1+1,n-2+1])
#if both the adjacent cells are different(of S)
else:
#(if both the adjacent cells of E are same)
if(mat[n-2][n-1]==mat[n-1][n-2]):
if(mat[n-2][n-1]==mat[0][1]):
ans.append([1,2])
else:
ans.append([2,1])
#if both the adjacent cells are different(0f E)
else:
if(mat[n-2][n-1]==mat[0][1]):
ans.append([1,2])
ans.append([n-1+1,n-2+1])
elif(mat[n-1][n-2]==mat[0][1]):
ans.append([1,2])
ans.append([n-2+1,n-1+1])
elif(mat[n-2][n-1]==mat[1][0]):
ans.append([2,1])
ans.append([n-1+1,n-2+1])
elif(mat[n-1][n-2]==mat[1][0]):
ans.append([2,1])
ans.append([n-2+1,n-1+1])
print(len(ans))
for i in ans:
print(i[0],i[1])
| 8 |
PYTHON3
|
ncases = int(input())
for tcase in range(ncases):
n = int(input())
arr = [input() for i in range(n)]
n1 = arr[0][1]
n2 = arr[1][0]
n3 = arr[n-2][n-1]
n4 = arr[n-1][n-2]
if n1 == n2 and n3 == n4:
if not n1 == n3:
print(0)
else:
print(2)
print('1 2')
print('2 1')
elif not n1 == n2 and n3 == n4:
if n1 == n3: # flip n1
print(1)
print('1 2')
else: # flip n2
print(1)
print('2 1')
elif n1 == n2 and not n3 == n4:
if n1 == n3: # flip n3
print(1)
print(n-1, n)
else: # flip n4:
print(1)
print(n, n-1)
else:
if n1 == n3: # flip n1 n4
print(2)
print('1 2')
print(n, n-1)
else:
print(2)
print('2 1')
print(n, n-1)
| 8 |
PYTHON3
|
t=int(input())
for x in range(t):
n=int(input())
grid=[]
for i in range(n):
a=input()
b=[]
for element in a:
b.append(element)
grid.append(b)
relevant=[grid[0][1],grid[1][0],grid[n-1][n-2],grid[n-2][n-1]]
a=[]
for i in range (1,4):
if relevant[i]==relevant[0]:
a.append(i)
if len(a)==0:
print(1)
print(2, 1)
elif len(a)==1:
if int(a[0])<2:
print(0)
else:
if int(a[0])==2:
print(2)
print(2, 1)
print(n, n-1)
else:
print(2)
print(2, 1)
print(n-1, n)
elif len(a)==2:
if 2 in a and 3 in a:
print(1)
print(1, 2)
elif 2 in a:
print(1)
print(n, n-1)
else:
print(1)
print(n-1,n)
else:
print(2)
print(1, 2)
print(2, 1)
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
n = int(input())
grid = []
for _ in range(n):
s = input()
grid.append(s)
if grid[0][1] == grid[1][0]:
if grid[n-2][n-1] == grid[n-1][n-2]:
if grid[0][1] == grid[n-2][n-1]:
print(2)
print(1, 2)
print(2, 1)
else:
print(0)
else:
print(1)
if grid[n-2][n-1] == grid[0][1]:
print(n-1, n)
else:
print(n, n-1)
else:
if grid[n-2][n-1] == grid[n-1][n-2]:
print(1)
if grid[0][1] == grid[n-2][n-1]:
print(1, 2)
else:
print(2, 1)
else:
print(2)
print(1, 2) # make (1,2) same color as (2,1)
if grid[n-2][n-1] == grid[1][0]:
print(n-1, n)
else:
print(n, n-1)
| 8 |
PYTHON3
|
def solve():
n = int(input())
start_numbers = set()
finish_numbers = set()
matrix = list()
inversion = list()
for _ in range(n):
inp = list(input())
matrix.append(inp)
start_numbers.add(matrix[0][1])
start_numbers.add(matrix[1][0])
finish_numbers.add(matrix[-1][-2])
finish_numbers.add(matrix[-2][-1])
if len(start_numbers) > 1:
if len(finish_numbers) > 1:
inversion_number = matrix[1][0]
inversion.append((1, 2))
if matrix[-1][-2] == inversion_number:
inversion.append((n, n - 1))
else:
inversion.append((n - 1, n))
else:
if matrix[0][1] == matrix[-1][-2]:
inversion.append((1, 2))
else:
inversion.append((2, 1))
else:
inversion_number = matrix[0][1]
if matrix[-1][-2] == inversion_number:
inversion.append((n, n - 1))
if matrix[-2][-1] == inversion_number:
inversion.append((n - 1, n))
print(len(inversion))
for coords in inversion:
print(*coords)
def main():
for _ in range(int(input())):
solve()
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
bool isPowerOfTwo(long long int x) { return x && (!(x & (x - 1))); }
long long int Modular_Exponentiation(long long int x, long long int n,
long long int M) {
if (n == 0) return 1;
if (n % 2)
return (x * Modular_Exponentiation((x * x) % M, n / 2, M)) % M;
else
return (Modular_Exponentiation((x * x) % M, n / 2, M)) % M;
}
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 += 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
void solve() {
long long int n;
cin >> n;
string str[n];
for (long long int i = 0; i < n; ++i) cin >> str[i];
long long int a, b, c, d, one = 0, zero = 0;
a = str[0][1] - '0';
b = str[1][0] - '0';
c = str[n - 2][n - 1] - '0';
d = str[n - 1][n - 2] - '0';
if (a == 0)
zero++;
else
one++;
if (b == 0)
zero++;
else
one++;
if (c == 0)
zero++;
else
one++;
if (d == 0)
zero++;
else
one++;
if (zero < one) {
if (zero == 1) {
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";
return;
} else {
cout << "2\n";
cout << "1 2\n2 1\n";
return;
}
} else if (one < zero) {
if (one == 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";
return;
} else {
cout << "2\n";
cout << "1 2\n2 1\n";
return;
}
} else {
if (a != b) {
cout << "2\n";
cout << "1 2\n";
if (a == c)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
return;
} else
cout << "0\n";
}
}
int32_t main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
while (T--) solve();
cerr << "Time taken : " << fixed << setprecision(5)
<< ((float)clock() / CLOCKS_PER_SEC) * 1000 << " ms"
<< "\n";
cerr << "My CLOCKS_PER_SEC= " << CLOCKS_PER_SEC << "\n";
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
for (int p = 1; p <= t; p++) {
int n, z;
cin >> n;
vector<vector<int>> grid(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
char c;
cin >> c;
if ((i == 0 && j == 0) || (i == n - 1 && j == n - 1)) {
continue;
}
grid[i][j] = c - 48;
}
}
int ans = 0, p1x = -1, p1y = -1, p2x = -1, p2y = -1, x1 = grid[0][1],
x2 = grid[1][0], x3 = grid[n - 2][n - 1], x4 = grid[n - 1][n - 2];
if (x1 == 0 && x2 == 0) {
if (x3 == 0 && x4 == 0) {
ans = 2;
p1x = 0, p1y = 1, p2x = 1, p2y = 0;
} else if (x3 == 1 && x4 == 1) {
ans = 0;
} else if (x3 == 1 && x4 == 0) {
ans = 1;
p1x = n - 1, p1y = n - 2;
} else if (x3 == 0 && x4 == 1) {
ans = 1;
p1x = n - 2, p1y = n - 1;
}
} else if (x1 == 1 && x2 == 1) {
if (x3 == 0 && x4 == 0) {
ans = 0;
} else if (x3 == 1 && x4 == 1) {
ans = 2;
p1x = 0, p1y = 1, p2x = 1, p2y = 0;
} else if (x3 == 1 && x4 == 0) {
ans = 1;
p1x = n - 2, p1y = n - 1;
} else if (x3 == 0 && x4 == 1) {
ans = 1;
p1x = n - 1, p1y = n - 2;
}
} else if (x1 == 1 && x2 == 0) {
if (x3 == 0 && x4 == 0) {
ans = 1;
p1x = 1, p1y = 0;
} else if (x3 == 1 && x4 == 1) {
ans = 1;
p1x = 0, p1y = 1;
} else if (x3 == 1 && x4 == 0) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 1, p2y = n - 2;
} else if (x3 == 0 && x4 == 1) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 2, p2y = n - 1;
}
} else if (x1 == 0 && x2 == 1) {
if (x3 == 0 && x4 == 0) {
ans = 1;
p1x = 0, p1y = 1;
} else if (x3 == 1 && x4 == 1) {
ans = 1;
p1x = 1, p1y = 0;
} else if (x3 == 1 && x4 == 0) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 2, p2y = n - 1;
} else if (x3 == 0 && x4 == 1) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 1, p2y = n - 2;
}
}
cout << ans << endl;
if (ans == 0)
;
else if (ans == 1) {
cout << p1x + 1 << " " << p1y + 1 << endl;
} else {
cout << p1x + 1 << " " << p1y + 1 << endl
<< p2x + 1 << " " << p2y + 1 << endl;
}
}
return 0;
}
| 8 |
CPP
|
tt = int(input())
for _ in range(tt):
n = int(input())
s = []
for i in range(n):
s += [input()]
a = (s[0][1], (1, 2))
b = (s[1][0], (2, 1))
c = (s[n-2][n-1], (n-1, n))
d = (s[n-1][n-2], (n, n-1))
A = [a, b]
B = [c, d]
res = []
if A[0][0] != A[1][0] and B[0][0] != B[1][0]:
if A[0][0] == '0': res += [A[0][1]]
else: res += [A[1][1]]
if B[0][0] == '1': res += [B[0][1]]
else: res += [B[1][1]]
elif A[0][0] == A[1][0]:
if B[0][0] == A[0][0]: res += [B[0][1]]
if B[1][0] == A[0][0]: res += [B[1][1]]
elif B[0][0] == B[1][0]:
if A[0][0] == B[0][0]: res += [A[0][1]]
if A[1][0] == B[0][0]: res += [A[1][1]]
print(len(res))
for x, y in res:
print(x, y)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n=int(input())
l=[input() for i in range(n)]
if l[0][1]==l[1][0] and l[n-1][n-2]==l[n-2][n-1]:
if l[0][1]!=l[n-1][n-2]:
print(0)
else:
print(2)
print(0+1,2)
print(2,1)
elif l[0][1]!=l[1][0] and l[n-1][n-2]==l[n-2][n-1]:
if l[0][1]==l[n-1][n-2]:
print(1)
print(1,2)
else:
print(1)
print(2,1)
elif l[0][1]==l[1][0] and l[n-1][n-2]!=l[n-2][n-1]:
if l[0][1]==l[n-1][n-2]:
print(1)
print(n,n-1)
else:
print(1)
print(n-1,n)
else:
if l[0][1]==l[n-1][n-2]:
print(2)
print(2,1)
print(n,n-1)
elif l[0][1]==l[n-2][n-1]:
print(2)
print(2,1)
print(n-1,n)
elif l[1][0]==l[n-1][n-2]:
print(2)
print(1,2)
print(n,n-1)
else:
print(2)
print(1,2)
print(n-1,n)
| 8 |
PYTHON3
|
t = int(input())
for _ in range(t):
n = int(input())
a = [0] * n
for i in range(n):
a[i] = input()
if a[0][1] == a[1][0]:
if a[n-1][n-2] == a[n-2][n-1]:
if a[n-2][n-1] == a[1][0]:
print(2)
print(*[2, 1])
print(*[1, 2])
else:
print(0)
else:
print(1)
if a[n-1][n-2] == a[1][0]:
print(*[n, n-1])
else:
print(*[n-1, n])
else:
if a[n-1][n-2] == a[n-2][n-1]:
print(1)
if a[n-1][n-2] == a[1][0]:
print(*[2, 1])
else:
print(*[1, 2])
else:
print(2)
print(1, 2)
if a[n-1][n-2] == a[1][0]:
print(*[n, n-1])
else:
print(*[n-1, n])
| 8 |
PYTHON3
|
# Author : raj1307 - Raj Singh
# Date : 18.10.2020
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import log,sqrt,factorial,cos,tan,sin,radians
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *
#import threading
#from itertools import permutations
#Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def getKey(item): return item[1]
def sort2(l):return sorted(l, key=getKey,reverse=True)
def d2(n,m,num):return [[num for x in range(m)] for y in range(n)]
def isPowerOfTwo (x): return (x and (not(x & (x - 1))) )
def decimalToBinary(n): return bin(n).replace("0b","")
def ntl(n):return [int(i) for i in str(n)]
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def ceil(x,y):
if x%y==0:
return x//y
else:
return x//y+1
def powerMod(x,y,p):
res = 1
x %= p
while y > 0:
if y&1:
res = (res*x)%p
y = y>>1
x = (x*x)%p
return res
def gcd(x, y):
while y:
x, y = y, x % y
return x
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def read():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def main():
for _ in range(ii()):
n=ii()
l=[]
for i in range(n):
l.append(si())
x1=int(l[0][1])+int(l[1][0])
x2=int(l[n-1][n-2])+int(l[n-2][n-1])
if (x1==2 and x2==0) or (x1==0 and x2==2):
print(0)
continue
if (x1==0 and x2==0) or (x1==2 and x2==2):
print(2)
print(1,2)
print(2,1)
continue
x=[]
x.append([int(l[0][1]),1,2])
x.append([int(l[1][0]),2,1])
x.append([int(l[n-1][n-2]),n,n-1])
x.append([int(l[n-2][n-1]),n-1,n])
if (x1==1 and x2==1):
ans=[]
for i in range(2):
if x[i][0]==1:
ans.append([x[i][1],x[i][2]])
for i in range(2,4):
if x[i][0]==0:
ans.append([x[i][1],x[i][2]])
print(len(ans))
for i in ans:
print(*i)
continue
if x[0][0]==x[1][0] and x[2][0]!=x[3][0]:
ans=[]
for i in range(2,4):
if x[i][0]==x[0][0]:
ans.append([x[i][1],x[i][2]])
print(len(ans))
for i in ans:
print(*i)
continue
if x[0][0]!=x[1][0] and x[2][0]==x[3][0]:
ans=[]
for i in range(2):
if x[i][0]==x[2][0]:
ans.append([x[i][1],x[i][2]])
print(len(ans))
for i in ans:
print(*i)
continue
# region fastio
# template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
main()
#dmain()
# Comment Read()
| 8 |
PYTHON3
|
"""
If FastIO not needed, used this and don't forget to strip
import sys, math
input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
self.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")
import time
start_time = time.time()
import collections as col
import math, string
from functools import reduce
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
MOD = 10**9+7
"""
Make both cells adjacent to the start equal to X, and both adjacent to the finish equal to 1-X
"""
def solve():
N = getInt()
S = []
for n in range(N):
S.append(getStr())
ans = 0
inv = []
S1, S2 = int(S[0][1]), int(S[1][0])
E1, E2 = int(S[N-1][N-2]), int(S[N-2][N-1])
if S1 == S2:
targ = 1-S1
if E1 != targ:
ans += 1
inv.append((N,N-1))
if E2 != targ:
ans += 1
inv.append((N-1,N))
elif E1 == E2:
targ = 1-E1
if S1 != targ:
ans += 1
inv.append((1,2))
if S2 != targ:
ans += 1
inv.append((2,1))
else:
ans = 2
targ = 1-S1
inv.append((2,1))
if E1 != targ:
inv.append((N,N-1))
else:
inv.append((N-1,N))
print(ans)
for op in inv:
print(*op)
return
for _ in range(getInt()):
solve()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string arr[n];
for (long long int i = 0; i < n; i++) cin >> arr[i];
char s1 = arr[0][1];
char s2 = arr[1][0];
char e1 = arr[n - 2][n - 1];
char e2 = arr[n - 1][n - 2];
if ((s1 == s2) && (e1 == e2) && s1 == e1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
} else if ((s1 == s2) && (e1 == e2) && s1 != e1)
cout << 0 << endl;
else if ((s1 != s2) && (e1 != e2) && s1 == e1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n << " " << n - 1 << endl;
} else if ((s1 != s2) && (e1 != e2) && s1 != e1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n - 1 << " " << n << endl;
} else if ((s1 == s2) && (e1 != e2) && s1 == e1) {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if ((s1 == s2) && (e1 != e2) && s1 == e2) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if ((s1 != s2) && (e1 == e2) && s1 == e1) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else if ((s1 != s2) && (e1 == e2) && s2 == e1) {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
}
}
return 0;
}
| 8 |
CPP
|
for t in range(int(input())):
n = int(input())
sr = 0
sd = 0
fu = 0
fl = 0
for i in range(n):
l = input()
if i == 0:
sr = l[1]
elif i == 1:
sd = l[0]
if i == n - 2:
fu = l[n-1]
elif i == n - 1:
fl = l[n-2]
ans=[]
#print(sr,sd,fu,fl)
if fu==fl:
if sr==fu:
ans+=[[1,2]]
if sd==fu:
ans+=[[2,1]]
elif fu!=fl:
if sd==sr:
if sr == fu:
ans += [[n-1, n]]
if sr == fl:
ans += [[n, n-1]]
else:
if sd==fu:
ans+=[[2,1]]
ans+=[[n,n-1]]
elif sd==fl:
ans += [[2, 1]]
ans += [[n-1, n]]
print(len(ans))
for i in ans:
print(str(i[0]),str(i[1]))
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
if (0 <= second && second < n && 0 <= first && first < m) return true;
return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
string a[222];
int dp[222][222], n;
bool can(int second, int first) {
if (second == n - 1 && first == n - 1) return true;
dp[second][first] = 1;
for (int e = 0; e < 4; e++) {
int ny = second + dy[e];
int nx = first + dx[e];
if (isrange(ny, nx, n, n) && dp[ny][nx] == 0) {
if (a[ny][nx] == 'F' || a[ny][nx] == a[second][first]) {
if (can(ny, nx)) return true;
}
}
}
return false;
}
void init() {
for (int e = 0; e < n; e++)
for (int p = 0; p < n; p++) dp[e][p] = 0;
dp[0][0] = 1;
}
bool isit() {
init();
bool suc1 = can(0, 1);
init();
bool suc2 = can(1, 0);
if (!suc1 && !suc2) return false;
return true;
}
void inverse(int second, int first) {
if (a[second][first] == '0')
a[second][first] = '1';
else
a[second][first] = '0';
}
vector<pair<int, int> > vv;
int main(void) {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
vv.clear();
vv.push_back(make_pair(0, 1));
vv.push_back(make_pair(1, 0));
vv.push_back(make_pair(1, 1));
vv.push_back(make_pair(n - 2, n - 1));
vv.push_back(make_pair(n - 1, n - 2));
if (n != 3) vv.push_back(make_pair(n - 2, n - 2));
for (int e = 0; e < n; e++) cin >> a[e];
bool suc = false;
if (!isit()) {
printf("0\n");
suc = true;
}
if (!suc) {
for (int e = 0; e < (int)vv.size(); e++) {
inverse(vv[e].first, vv[e].second);
if (!isit()) {
printf("1\n%d %d\n", vv[e].first + 1, vv[e].second + 1);
suc = true;
break;
}
inverse(vv[e].first, vv[e].second);
}
}
if (!suc) {
for (int e = 0; e < (int)vv.size(); e++) {
for (int p = e + 1; p < (int)vv.size(); p++) {
inverse(vv[e].first, vv[e].second);
inverse(vv[p].first, vv[p].second);
if (!isit()) {
printf("2\n");
printf("%d %d\n", vv[e].first + 1, vv[e].second + 1);
printf("%d %d\n", vv[p].first + 1, vv[p].second + 1);
suc = true;
break;
}
inverse(vv[e].first, vv[e].second);
inverse(vv[p].first, vv[p].second);
}
if (suc) break;
}
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int i, n, t, j = 0, p, x, a = 0, b, l = 0, r = 0, y, k, c, sum = 0,
m, d, cnts = 0;
cin >> t;
while (t--) {
cin >> n;
char s[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cin >> s[i][j];
}
}
if (s[0][1] == '1' && s[1][0] == '1') {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 0 << endl;
} else if (s[n - 1][n - 2] == '1') {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else if (s[0][1] == '0' && s[1][0] == '0') {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 0 << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0') {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else if (s[0][1] == '1') {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 1 << endl;
cout << "1 2" << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 1 << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n - 1 << " " << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 1 << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 1 << endl;
cout << "1 2" << endl;
} else if (s[n - 1][n - 2] == '0') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n << " " << n - 1 << endl;
}
}
}
}
| 8 |
CPP
|
for _ in range(int(input())):
n = int(input())
grid = []
v1 = []
v0 = []
for i in range(n):
r = input()
grid.append(r)
st = grid[0][1]+grid[1][0]
en = grid[n-1][n-2]+grid[n-2][n-1]
s_z = st.count('0')
e_z = en.count('0')
if abs(s_z-e_z)==2:
print(0)
continue
if s_z==e_z:
print(2)
if '0' not in st or '1' not in st:
print(1,2)
print(2,1)
continue
if grid[0][1]=='0':print(1,2)
else: print(2,1)
if grid[n-1][n-2]=='0': print(n-1,n)
else: print(n, n-1)
else:
print(1)
if '0' not in st:
if grid[n-1][n-2]=='0': print(n-1,n)
else: print(n, n-1)
continue
if '1' not in st:
if grid[n-1][n-2]=='1': print(n-1,n)
else: print(n, n-1)
continue
if '0' not in en:
if grid[0][1]=='0': print(2,1)
else: print(1,2)
continue
if '1' not in en:
if grid[0][1]=='1': print(2,1)
else: print(1,2)
continue
| 8 |
PYTHON3
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
plan = [[char for char in input().strip()] for i in range(n)]
count_a, inv_a, count_b, inv_b = 0, [], 0, []
if plan[0][1] == "1":
inv_a.append([1, 2])
count_a += 1
if plan[1][0] == "1":
inv_a.append([2, 1])
count_a += 1
if plan[n - 2][n - 1] == "0":
inv_a.append([n - 1, n])
count_a += 1
if plan[n - 1][n - 2] == "0":
inv_a.append([n, n - 1])
count_a += 1
if plan[0][1] == "0":
inv_b.append([1, 2])
count_b += 1
if plan[1][0] == "0":
inv_b.append([2, 1])
count_b += 1
if plan[n - 2][n - 1] == "1":
inv_b.append([n - 1, n])
count_b += 1
if plan[n - 1][n - 2] == "1":
inv_b.append([n, n - 1])
count_b += 1
if count_a <= count_b:
print(count_a)
for inv in inv_a:
print(*inv)
else:
print(count_b)
for inv in inv_b:
print(*inv)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void testCase() {
long long n;
cin >> n;
vector<string> grid(n);
vector<pair<long long, long long>> ans;
for (string &s : grid) cin >> s;
if (grid[0][1] == grid[1][0]) {
if (grid[n - 1][n - 2] == grid[0][1]) ans.push_back({n - 1, n - 2});
if (grid[n - 2][n - 1] == grid[0][1]) ans.push_back({n - 2, n - 1});
} else if (grid[n - 1][n - 2] == grid[n - 2][n - 1]) {
if (grid[n - 1][n - 2] == grid[0][1]) ans.push_back({0, 1});
if (grid[n - 1][n - 2] == grid[1][0]) ans.push_back({1, 0});
} else {
if (grid[0][1] == '1') ans.push_back({0, 1});
if (grid[1][0] == '1') ans.push_back({1, 0});
if (grid[n - 1][n - 2] == '0') ans.push_back({n - 1, n - 2});
if (grid[n - 2][n - 1] == '0') ans.push_back({n - 2, n - 1});
}
cout << (long long)(ans.size()) << endl;
for (auto &it : ans) cout << it.first + 1 << " " << it.second + 1 << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cin.tie(nullptr);
long long t_c = 1;
cin >> t_c;
while (t_c--) testCase();
}
| 8 |
CPP
|
for _ in " "*int(input()):
a=int(input())
z=[[0]*(a+1)]
for i in range(a):
f=[0]+list(map(str,input().replace('',' ').split()))
z.append(f)
k=z[1][2]
k1=z[2][1]
k2=z[a-1][a]
k3=z[a][a-1]
o=[k,k1,k2,k3]
z,z1=0,0
for i in o:
if i=='0':z+=1
else:z1+=1
if z==z1:
if k==k1:print(0)
else:
print(2)
if k=='0':print(1,2)
else:print(2,1)
if k2=='1':print(a-1,a)
else:print(a,a-1)
elif z==3 or z1==3:
print(1)
if k==k1:
if k2==k:print(a-1,a)
else:print(a,a-1)
else:
if k2==k:print(1,2)
else:print(2,1)
else:
print(2)
print(1,2)
print(2,1)
| 8 |
PYTHON3
|
import sys
t = int(input())
for _ in range(t):
n = int(input())
arr = []
for _ in range(n):
arr.append(list(input()))
candidate = [[1, 2], [2, 1], [n - 1, n], [n, n - 1]]
ans = [arr[0][1], arr[1][0], arr[n - 2][n - 1], arr[n - 1][n - 2]]
tmp = [['1', '1', '0', '0'],['0', '0', '1', '1']]
for i in range(2):
realAns = []
for j in range(4):
if tmp[i][j] != ans[j]:
realAns.append(candidate[j])
if len(realAns) <= 2:
break
print(len(realAns))
for x in realAns:
print("%d %d" % (x[0], x[1]))
| 8 |
PYTHON3
|
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n = int(input())
arr = [list(input().strip()) for _ in range(n)]
a,b,c,d = int(arr[0][1]),int(arr[1][0]),int(arr[n-1][n-2]),int(arr[n-2][n-1])
ch = int((a!=0)+(b!=0)+(c!=1)+(d!=1))
ch1 = int((a!=1)+(b!=1)+(c!=0)+(d!=0))
if ch <= 2:
print(ch)
if a:print(1,2)
if b:print(2,1)
if not c:print(n,n-1)
if not d:print(n-1,n)
else:
print(ch1)
if not a: print(1, 2)
if not b: print(2, 1)
if c: print(n, n - 1)
if d: print(n - 1, n)
#Fast IO Region
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")
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
t=int(input())
while t:
t-=1
n=int(input())
g=[]
for i in range (n):
l=list(map(str,input()))
ln=len(l)
g.append(l)
s=[int(g[0][1]),int(g[1][0])]
s1=s[0]
s2=s[1]
f=[int(g[n-1][ln-2]),int(g[n-2][ln-1])]
f1=f[0]
f2=f[1]
if len(set(s))==1:
if s1==1:
if f1==f2==1:
print(2)
print(n,ln-1)
print(n-1,ln)
elif f1==f2==0:
print(0)
else:
if f2==1:
print(1)
print(n-1,ln)
else:
print(1)
print(n,ln-1)
else:
if f1==f2==0:
print(2)
print(n,ln-1)
print(n-1,ln)
elif f1==f2==1:
print(0)
else:
if f2==0:
print(1)
print(n-1,ln)
else:
print(1)
print(n,ln-1)
else:
if s1==0 and s2==1:
if f1==f2==1:
print(1)
print(2,1)
elif f1==f2==0:
print(1)
print(1,2)
else:
if f2==1:
print(2)
print(1,2)
print(n-1,ln)
else:
print(2)
print(1,2)
print(n,ln-1)
else:
if f1==f2==1:
print(1)
print(1,2)
elif f1==f2==0:
print(1)
print(2,1)
else:
if f2==1:
print(2)
print(2,1)
print(n-1,ln)
else:
print(2)
print(2,1)
print(n,ln-1)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
if (b < a) a = b;
return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 0;
void solve() {
long long n;
cin >> n;
string s[n];
for (long long i = 0; i < n; i++) {
cin >> s[i];
}
long long a = s[0][1], b = s[1][0];
long long c = s[n - 1][n - 2], d = s[n - 2][n - 1];
vector<pair<long long, long long> > pr;
if (a == b) {
if (c == a) pr.push_back({n, n - 1});
if (d == a) pr.push_back({n - 1, n});
} else if (c == d) {
if (a == c) pr.push_back({1, 2});
if (b == c) pr.push_back({2, 1});
} else {
pr.push_back({2, 1});
if (c == a) pr.push_back({n, n - 1});
if (d == a) pr.push_back({n - 1, n});
}
cout << (long long)((pr).size()) << "\n";
for (auto x : pr) {
cout << x << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long ll_scan() {
long long x;
cin >> x;
return x;
}
string str_scan() {
string x;
cin >> x;
return x;
}
int main() {
long long t = 1, T = 1;
t = ll_scan(), T = t;
while (t--) {
long long n = ll_scan();
vector<string> s(n);
for (long long i = 0; i < n; i++) cin >> s[i];
if (s[0][1] == '1') {
if (s[1][0] == '1') {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "2" << endl;
cout << n - 1 << ' ' << n << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "1" << endl;
cout << n - 1 << ' ' << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "0" << endl;
}
}
} else {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << 1 << ' ' << 2 << endl;
} else {
cout << "2" << endl;
cout << 2 << ' ' << 1 << endl;
cout << n - 1 << ' ' << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "2" << endl;
cout << 2 << ' ' << 1 << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "1" << endl;
cout << 2 << ' ' << 1 << endl;
}
}
}
} else {
if (s[1][0] == '1') {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << 2 << ' ' << 1 << endl;
} else {
cout << "2" << endl;
cout << 1 << ' ' << 2 << endl;
cout << n - 1 << ' ' << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "2" << endl;
cout << 1 << ' ' << 2 << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "1" << endl;
cout << 1 << ' ' << 2 << endl;
}
}
} else {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "0" << endl;
} else {
cout << "1" << endl;
cout << n << ' ' << n - 1 << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << n - 1 << ' ' << n << endl;
} else {
cout << "2" << endl;
cout << n - 1 << ' ' << n << endl;
cout << n << ' ' << n - 1 << endl;
}
}
}
}
}
}
| 8 |
CPP
|
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
n = int(input())
s = [list(input()) for _ in range(n)]
start = [s[0][1],s[1][0]]
goal = [s[n-1][n-2],s[n-2][n-1]]
ans = []
if goal[0] == goal[1]:
memo = goal[0]
if start[0] == memo:
ans.append([1,2])
if start[1] == memo:
ans.append([2,1])
else:
memo = start[0]
if start[1] != memo:
ans.append([2,1])
if goal[0] == memo:
ans.append([n,n-1])
if goal[1] == memo:
ans.append([n-1,n])
print(len(ans))
for j in ans:
print(j[0],j[1])
| 8 |
PYTHON3
|
t=int(input())
while(t):
n=int(input())
s=[list(input()) for x in range(n)]
c=0
x1=y1=x2=y2=-1
if(s[0][1]==s[1][0]):
if s[n-1][n-2]==s[0][1]:
x1,y1=n,n-1
c+=1
if s[n-2][n-1]==s[0][1]:
x2,y2=n-1,n
c+=1
print(c)
if x1!=-1:
print(x1,y1)
if x2!=-1:
print(x2,y2)
elif(s[n-1][n-2]==s[n-2][n-1]):
if s[n-1][n-2]==s[0][1]:
c+=1
x1,y1=1,2
if s[n-1][n-2]==s[1][0]:
c+=1
x2,y2=2,1
print(c)
if x1!=-1:
print(x1,y1)
if x2!=-1:
print(x2,y2)
else:
c=2
if s[n-1][n-2]==s[0][1]:
x1,y1,x2,y2=n,n-1,2,1
elif s[n-1][n-2]==s[1][0]:
x1,y1,x2,y2=n,n-1,1,2
print(c)
print(x1,y1)
print(x2,y2)
t-=1
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void testcase() {
int n, chng = 0;
cin >> n;
vector<string> a(n);
vector<pair<int, int>> b;
for (int i = 0; i < n; i++) cin >> a[i];
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 << 0 << endl;
return;
} else {
cout << 2 << endl;
cout << n << " " << n - 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else {
if (a[n - 1][n - 2] == a[0][1]) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
}
} else {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[0][1] == a[n - 1][n - 2]) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
}
} else {
cout << 2 << endl;
if (a[0][1] == '1')
cout << 1 << " " << 2 << endl;
else {
cout << 2 << " " << 1 << endl;
}
if (a[n - 1][n - 2] == '0')
cout << n << " " << n - 1 << endl;
else {
cout << n - 1 << " " << n << endl;
}
}
}
}
int main() {
long long t;
cin >> t;
while (t--) {
testcase();
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
vector<vector<long long> > grid;
long long n;
void doTest() {
cin >> n;
grid = vector<vector<long long> >(n, vector<long long>(n, 0));
for (long long i = (0); i < (n); i++) {
string s;
cin >> s;
for (long long j = (0); j < (n); j++) {
if (s[j] == '0') grid[i][j] = 0;
if (s[j] == '1') grid[i][j] = 1;
if (s[j] == 'S') grid[i][j] = -1;
if (s[j] == 'F') grid[i][j] = -1;
}
}
vector<pair<long long, long long> > change =
vector<pair<long long, long long> >();
if (grid[1][0] == grid[0][1]) {
if (grid[n - 2][n - 1] == grid[1][0]) {
change.push_back(make_pair(n - 1, n));
}
if (grid[n - 1][n - 2] == grid[1][0]) {
change.push_back(make_pair(n, n - 1));
}
} else if (grid[n - 2][n - 1] == grid[n - 1][n - 2]) {
if (grid[n - 2][n - 1] == grid[1][0]) {
change.push_back(make_pair(2, 1));
}
if (grid[n - 2][n - 1] == grid[0][1]) {
change.push_back(make_pair(1, 2));
}
} else {
if (grid[1][0] != grid[n - 2][n - 1]) {
change.push_back(make_pair(2, 1));
change.push_back(make_pair(n - 1, n));
} else {
change.push_back(make_pair(2, 1));
change.push_back(make_pair(n, n - 1));
}
}
cout << change.size() << endl;
for (long long i = (0); i < (change.size()); i++) {
cout << change[i].first << ' ' << change[i].second << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
for (long long i = (0); i < (t); i++) doTest();
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 208;
int n, mat[MAXN][MAXN];
vector<pair<int, int> > changes;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
cin >> n;
changes.clear();
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 1; j <= n; j++) {
mat[i][j] = s[j - 1] - '0';
}
}
if (mat[1][2] == 1 && mat[2][1] == 1) {
if (mat[n][n - 1] == 1) changes.push_back(make_pair(n, n - 1));
if (mat[n - 1][n] == 1) changes.push_back(make_pair(n - 1, n));
} else {
if (mat[n][n - 1] == 0 && mat[n - 1][n] == 0) {
if (mat[1][2] == 0) changes.push_back(make_pair(1, 2));
if (mat[2][1] == 0) changes.push_back(make_pair(2, 1));
} else {
if (mat[1][2] == 1) changes.push_back(make_pair(1, 2));
if (mat[2][1] == 1) changes.push_back(make_pair(2, 1));
if (mat[n][n - 1] == 0) changes.push_back(make_pair(n, n - 1));
if (mat[n - 1][n] == 0) changes.push_back(make_pair(n - 1, n));
}
}
cout << (int)changes.size() << endl;
for (pair<int, int> par : changes)
cout << par.first << " " << par.second << endl;
}
}
| 8 |
CPP
|
def answer():
n=int(input())
a=[]
for i in range(n):
z=input()
a.append([x for x in str(z)])
b1={a[0][1],a[1][0]}
b2={a[n-1][n-2],a[n-2][n-1]}
c=b1&b2
if (len(c)==0):
print('0')
return
else:
if len(c)==1:
if int(a[0][1])^int(a[1][0]):
if int(a[0][1])^int(a[n-1][n-2]):
print(len(c))
print("2 1")
return
else:
print(len(c))
print("1 2")
return
if int(a[n-1][n-2])^int(a[n-2][n-1]):
if int(a[0][1])^int(a[n-1][n-2]):
print(len(c))
print(n-1,n)
return
else:
print(len(c))
print(n,n-1)
return
if(int(a[0][1])==int(a[1][0])):
print(len(c)+1)
print('1 2')
print('2 1')
return
if len(c)==2:
if int(a[0][1])^int(a[1][0]):
if int(a[0][1])^int(a[n-1][n-2]):
print(len(c))
print("1 2")
print(n,n-1)
return
else:
print(len(c))
print("1 2") #never do this kindof mistake,...keepin' ans extra comma in the previous submission
print(n-1,n)
return
test=int(input())
i=0
while (i<test):
answer()
i+=1
| 8 |
PYTHON3
|
tc = int(input())
def solve(grid):
n = len(grid)
ans = [(1, 2), (2, 1), (n, n - 1), (n - 1, n)]
a, b = int(grid[0][1]), int(grid[1][0])
c, d = int(grid[-1][-2]), int(grid[-2][-1])
cur = [a, b, c, d]
ans1 = [1, 1, 0, 0]
ans2 = [0, 0, 1, 1]
diff1 = []
for a, b in zip(cur, ans1):
diff1.append(abs(a - b))
diff2 = []
for a, b in zip(cur, ans2):
diff2.append(abs(a - b))
ret = []
if sum(diff1) <= sum(diff2):
for i in range(len(diff1)):
if diff1[i] == 1:
ret.append(ans[i])
else:
for i in range(len(diff2)):
if diff2[i] == 1:
ret.append(ans[i])
return len(ret), ret
for _ in range(tc):
n = int(input())
grid = []
for _ in range(n):
line = str(input())
grid.append(line)
l, lst = solve(grid)
print(l)
for a,b in lst:
print(a,b)
| 8 |
PYTHON3
|
import math
# import sys
from collections import Counter, defaultdict, deque
#def BFS(start, grid):
def f(n, grid):
r1c2 = grid[0][1]
r2c1 = grid[1][0]
up = grid[n-2][n-1]
left = grid[n-1][n-2]
if(r1c2 == r2c1 and up == left):
if r1c2 != up:
return[0]
else:
return([2, "{0} {1}".format(2,1), "{0} {1}".format(1,2)])
elif(r1c2 != r2c1 and up == left):
if(r1c2 != up):
return([1, "{0} {1}".format(2,1)])
else:
return([1, "{0} {1}".format(1,2)])
elif(r1c2 == r2c1 and up != left):
if(r1c2 != up):
return([1, "{0} {1}".format(n,n-1)])
else:
return([1, "{0} {1}".format(n-1,n)])
else:
#1,2
if(r1c2 != up):
#left
return([2, "{0} {1}".format(2,1), "{0} {1}".format(n, n-1)])
else:
#up
return([2, "{0} {1}".format(2,1), "{0} {1}".format(n-1,n)])
t = int(input())
result = []
for i in range(t):
n = int(input())
grid = []
for row in range(n):
grid.append(input())
result.append(f(n, grid))
#print(result)
for i in range(t):
print(result[i][0])
for j in range(result[i][0]):
print(result[i][j+1])
| 8 |
PYTHON3
|
import sys, collections, itertools, copy
def bfs(num):
check = [[False] * n for _ in range(n)]
q = collections.deque()
q.append((0,0))
check[0][0] = True
while q:
x, y = q.popleft()
if x == n - 1 and y == n - 1:
return True
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if temp[nx][ny] != num:
if not check[nx][ny]:
check[nx][ny] = True
q.append((nx, ny))
return False
tc = int(sys.stdin.readline())
dx = [0,1,0,-1]
dy = [1,0,-1,0]
for _ in range(tc):
n = int(sys.stdin.readline())
arr = [list(sys.stdin.readline().rstrip()) for _ in range(n)]
find = False
for i in itertools.combinations([(0,1), (1,0), (n - 2, n - 1), (n - 1, n - 2)], 2):
temp = copy.deepcopy(arr)
for j in i:
if temp[j[0]][j[1]] == '1':
temp[j[0]][j[1]] = '0'
else:
temp[j[0]][j[1]] = '1'
if not bfs('0') and not bfs('1'):
find = True
print(2)
for j in i:
print(j[0] + 1, j[1] + 1)
break
if not find:
for i in [(0,1), (1,0), (n - 2, n - 1), (n - 1, n - 2)]:
temp = copy.deepcopy(arr)
if temp[i[0]][i[1]] == '1':
temp[i[0]][i[1]] = '0'
else:
temp[i[0]][i[1]] = '1'
if not bfs('0') and not bfs('1'):
find = True
print(1)
print(i[0] + 1, i[1] + 1)
break
if not find:
print(0)
| 8 |
PYTHON3
|
import sys,math
from collections import deque,defaultdict
import operator as op
from functools import reduce
sys.setrecursionlimit(10**6)
I=sys.stdin.readline
#s="abcdefghijklmnopqrstuvwxyz"
"""
x_move=[-1,0,1,0,-1,1,1,-1]
y_move=[0,1,0,-1,1,1,-1,-1]
"""
def ii():
return int(I().strip())
def li():
return list(map(int,I().strip().split()))
def mi():
return map(int,I().strip().split())
def ncr(n, r):
r = min(r, n-r)
numer = reduce(op.mul, range(n, n-r, -1), 1)
denom = reduce(op.mul, range(1, r+1), 1)
return numer // denom
def gcd(x, y):
while y:
x, y = y, x % y
return x
def valid(row,col,rows,cols,rcross,lcross):
return rows[row]==0 and cols[col]==0 and rcross[col+row]==0 and lcross[col-row]==0
def main():
ans=""
for _ in range(ii()):
n=ii()
grid=[]
for i in range(n):
grid.append(I().strip())
if grid[0][1]==grid[1][0]:
if grid[-1][-2]==grid[-2][-1]:
if grid[0][1]==grid[-1][-2]:
print(2)
print(n-1,len(grid[0]))
print(n,len(grid[0])-1)
else:
print(0)
else:
if grid[-1][-2]==grid[0][1]:
print(1)
print(n,len(grid[0])-1)
else:
print(1)
print(n-1,len(grid[0]))
else:
if grid[-1][-2]!=grid[-2][-1]:
if grid[0][1]=="1":
print(2)
print(1,2)
if grid[-1][-2]=="0":
print(n,len(grid[0])-1)
else:
print(n-1,len(grid[0]))
else:
print(2)
print(1,2)
if grid[-1][-2]=="1":
print(n,len(grid[0])-1)
else:
print(n-1,len(grid[0]))
else:
if grid[0][1]==grid[-1][-2]:
print(1)
print(1,2)
else:
print(1)
print(2,1)
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int t, n;
vector<pair<int, int> > ho1, ho2;
char a[207][207];
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] + 1;
}
ho1.clear();
ho2.clear();
if (a[1][2] == '0') {
ho1.push_back({1, 2});
} else
ho2.push_back({1, 2});
if (a[2][1] == '0') {
ho1.push_back({2, 1});
} else
ho2.push_back({2, 1});
if (a[n][n - 1] == '1') {
ho1.push_back({n, n - 1});
} else
ho2.push_back({n, n - 1});
if (a[n - 1][n] == '1') {
ho1.push_back({n - 1, n});
} else
ho2.push_back({n - 1, n});
if (ho1.size() > ho2.size()) {
printf("%d\n", ho2.size());
for (int i = 0; i < ho2.size(); i++) {
printf("%d %d\n", ho2[i].first, ho2[i].second);
}
} else {
printf("%d\n", ho1.size());
for (int i = 0; i < ho1.size(); i++) {
printf("%d %d\n", ho1[i].first, ho1[i].second);
}
}
}
}
| 8 |
CPP
|
t=int(input())
for i in range(t):
n=int(input())
l=[]
for j in range(n):
l.append(list(input()))
a,b,c,d=l[0][1],l[1][0],l[n-2][n-1],l[n-1][n-2]
if (a=="1"):
if(b=="1"):
if (c=="1"):
if(d=="1"):
print(2)
print("1 2")
print("2 1")
else:
print("1")
print(str(n-1)+ " "+str(n))
else:
if(d=="1"):
print("1")
print(str(n)+ " "+str(n-1))
else:
print(0)
else:
if (c=="1"):
if(d=="1"):
print("1")
print("1 2")
else:
print(2)
print(str(n-1)+ " "+str(n))
print("2 1")
else:
if(d=="1"):
print("2")
print(str(n-1)+ " "+str(n))
print("1 2")
else:
print("1")
print("2 1")
else:
if(b=="1"):
if (c=="1"):
if(d=="1"):
print("1")
# print("1 2")
print("2 1")
else:
print("2")
print(str(n-1)+ " "+str(n))
print("1 2")
else:
if(d=="1"):
print(2)
print(str(n)+ " "+str(n-1))
print("1 2")
else:
print("1")
print("1 2")
else:
if (c=="1"):
if(d=="1"):
print(0)
else:
print("1")
print(str(n)+ " "+str(n-1))
else:
if(d=="1"):
print("1")
print(str(n-1)+ " "+str(n))
else:
print("2")
print(str(n-1)+ " "+str(n))
print(str(n)+ " "+str(n-1))
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
S = []
for i in range(n):
s = input()
S.append(s)
a = S[0][1];b = S[1][0]
c = S[n-1][n-2];d = S[n-2][n-1]
ans = []
if(a==b):
if(c==a):
ans.append([n,n-1])
if(d==a):
ans.append([n-1,n])
else:
if(c==d):
if(a==c):
ans.append([1,2])
if(b==c):
ans.append([2,1])
else:
if(a==c):
ans.append([1,2])
ans.append([n-1,n])
if(a==d):
ans.append([1,2])
ans.append([n,n-1])
print(len(ans))
for i in ans:
print(*i)
| 8 |
PYTHON3
|
def solve(n):
s=[input() for _ in range(n)]
ans=[]
cnt=0
if s[0][1]==s[1][0] :
if s[n-2][n-1]==s[0][1]:
ans.append((n-1,n))
cnt+=1
if s[n-1][n-2]==s[0][1] :
ans.append((n,n-1))
cnt+=1
elif s[n-1][n-2]==s[n-2][n-1] :
if s[0][1]==s[n-1][n-2]:
ans.append((1,2))
cnt+=1
if s[1][0]==s[n-1][n-2] :
ans.append((2,1))
cnt+=1
else:
if s[0][1]!='0' :
ans.append((1,2))
cnt+=1
else:
ans.append((2,1))
cnt+=1
if s[n-1][n-2]!='1' :
ans.append((n,n-1))
cnt+=1
else:
ans.append((n-1,n))
cnt+=1
print(cnt)
for i in ans:
print(i[0],i[1])
for _ in range(int(input())):
n=int(input())
solve(n)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
using TMatrix = vector<vector<T>>;
template <class T>
using TVector = vector<T>;
using TString = string;
template <class T1, class T2>
using THashMap = unordered_map<T1, T2>;
void read(int& x) { scanf("%i", &x); }
void write(const int x) { printf("%i", x); }
void read(long long& x) { scanf("%lli", &x); }
void write(const long long& x) { printf("%lli", x); }
void read(double& x) { scanf("%lf", &x); }
void write(const double& x) { printf("%lf", x); }
void read(char& c, bool whiteSpaces = false) {
while (1) {
c = getchar();
if (whiteSpaces || !isspace(c)) {
break;
}
}
}
void write(const char c) { printf("%c", c); }
void read(TString& result, bool untilEol = false) {
result.clear();
char c;
if (!untilEol) {
while (1) {
c = getchar();
if (!isspace(c) || c == EOF) break;
}
result.push_back(c);
}
while (1) {
c = getchar();
if (c == EOF || c == '\n' || (!untilEol && isspace(c))) break;
result.push_back(c);
}
}
void write(const TString& s) { printf("%s", s.c_str()); }
void writeYES(const bool condition) {
printf("%s\n", condition ? "YES" : "NO");
}
void writeYes(const bool condition) {
printf("%s\n", condition ? "Yes" : "No");
}
template <class T>
void writeIf(const bool condition, const T& forTrue, const T& forFalse) {
cout << (condition ? forTrue : forFalse) << endl;
}
template <class T1, class T2>
void read(pair<T1, T2>& x) {
read(x.first);
read(x.second);
}
template <class T1, class T2>
void write(pair<T1, T2>& x) {
write(x.first);
write(' ');
write(x.second);
}
template <class T>
void writeln(const T& x) {
write(x);
write('\n');
}
template <class T1, class T2>
void read(T1& x1, T2& x2) {
read(x1);
read(x2);
}
template <class T1, class T2, class T3>
void read(T1& x1, T2& x2, T3& x3) {
read(x1);
read(x2);
read(x3);
}
template <class T1, class T2, class T3, class T4>
void read(T1& x1, T2& x2, T3& x3, T4& x4) {
read(x1);
read(x2);
read(x3);
read(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
void read(T1& x1, T2& x2, T3& x3, T4& x4, T5& x5) {
read(x1);
read(x2);
read(x3);
read(x4);
read(x5);
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void read(T1& x1, T2& x2, T3& x3, T4& x4, T5& x5, T6& x6) {
read(x1);
read(x2);
read(x3);
read(x4);
read(x5);
read(x6);
}
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
void read(T1& x1, T2& x2, T3& x3, T4& x4, T5& x5, T6& x6, T7& x7) {
read(x1);
read(x2);
read(x3);
read(x4);
read(x5);
read(x6);
read(x7);
}
template <class T1, class T2>
void write(const T1& x1, const T2& x2) {
write(x1);
write(' ');
write(x2);
}
template <class T1, class T2>
void writeln(const T1& x1, const T2& x2) {
write(x1, x2);
write('\n');
}
template <class T1, class T2, class T3>
void write(const T1& x1, const T2& x2, const T3& x3) {
write(x1, x2);
write(' ');
write(x3);
}
template <class T1, class T2, class T3>
void writeln(const T1& x1, const T2& x2, const T3& x3) {
write(x1, x2, x3);
write('\n');
}
template <class T1, class T2, class T3, class T4>
void write(const T1& x1, const T2& x2, const T3& x3, const T4& x4) {
write(x1, x2, x3);
write(' ');
write(x4);
}
template <class T1, class T2, class T3, class T4>
void writeln(const T1& x1, const T2& x2, const T3& x3, const T4& x4) {
write(x1, x2, x3, x4);
write('\n');
}
namespace NVector {
template <class T>
void Read(TVector<T>& v, int length = -1) {
if (length == -1) {
read(length);
}
v.resize(length);
for (auto& elem : v) {
read(elem);
}
}
template <class T>
void Write(const TVector<T>& v, TString del = " ", bool needEndl = true) {
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i < v.size() - 1) {
cout << del;
}
}
if (needEndl) {
cout << endl;
}
}
template <class T>
TVector<T> Filter(const TVector<T>& v, std::function<bool(T)> filter) {
TVector<T> result;
for (const auto& elem : v) {
if (filter(elem)) {
result.push_back(elem);
}
}
return move(result);
}
template <class T>
T Max(const TVector<T>& v) {
return *max_element(v.begin(), v.end());
}
template <class T>
void Sort(TVector<T>& v) {
sort(v.begin(), v.end());
}
template <class T>
void SortR(TVector<T>& v) {
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
}
template <class T>
void Reverse(TVector<T>& v) {
reverse(v.begin(), v.end());
}
template <class T>
T Min(const TVector<T>& v) {
return *min_element(v.begin(), v.end());
}
template <class T>
T Sum(const TVector<T>& v) {
T result = 0;
for (const auto& elem : v) {
result += elem;
}
return result;
}
template <class T>
T Mult(const TVector<T>& v) {
T result = 1;
for (const auto& elem : v) {
result *= elem;
}
return result;
}
template <class T>
TVector<int> FromInt(T number) {
TVector<int> v;
while (number) {
v.push_back(number % 10);
number /= 10;
}
reverse(v.begin(), v.end());
return move(v);
}
template <class T>
T ToInt(const TVector<int>& v) {
T ans = 0;
for (const auto e : v) {
ans = ans * 10 + e;
}
return ans;
}
template <class T>
T Mex(const TVector<T>& v) {
unordered_set<T> s(v.begin(), v.end());
T value;
for (value = 1; s.find(value) != s.end(); value += 1) {
}
return value;
}
template <class T>
bool Contains(const TVector<T>& v, const T& searchedElem) {
for (const auto& elem : v) {
if (elem == searchedElem) {
return true;
}
}
return false;
}
} // namespace NVector
namespace NMath {
TVector<bool> SieveOfEratosthenes;
template <class T>
T Gcd(T x, T y) {
while (x) {
y %= x;
swap(x, y);
}
return y;
}
template <class T>
T Lcm(T x, T y) {
return (x / Gcd(x, y)) * y;
}
template <class T>
T Gcd(const vector<T>& v) {
T ans = v.front();
for (const auto& elem : v) {
ans = Gcd(ans, elem);
}
return ans;
}
template <class T>
T Lcm(const vector<T>& v) {
T ans = v.front();
for (const auto& elem : v) {
ans = Lcm(ans, elem);
}
return ans;
}
int GcdEx(const int a, const int b, int& x, int& y) {
if (!a) {
x = 0;
y = 1;
return b;
}
int x1, y1;
int d = GcdEx(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
int CalcInverseNumber(const int number, const int mod) {
int x, y;
int g = GcdEx(number, mod, x, y);
if (g != 1) {
return -1;
}
return x = (x % mod + mod) % mod;
}
template <class T>
T Factorial(T n) {
T result = 1;
while (n > 0) {
result *= n--;
}
return result;
}
template <class T>
T FactorialWithMod(T n, T mod) {
if (n >= mod) {
return 0;
}
T result = 1;
while (n > 0) {
result = (result * n) % mod;
--n;
}
return result;
}
template <class T1, class T2>
T1 BinPow(T1 value, T2 extent) {
T1 res = 1;
while (extent > 0) {
if (extent & 1) {
res *= value;
}
extent >>= 1;
value *= value;
}
return res;
}
template <class T1, class T2>
T1 BinPowWithMod(T1 value, T2 extent, T1 mod) {
T1 res = 1;
value %= mod;
while (extent > 0) {
if (extent & 1) {
res = (value * res) % mod;
}
extent >>= 1;
value = (value * value) % mod;
}
return res;
}
void PrecalcPrimes(const int length) {
SieveOfEratosthenes.resize(length + 1, true);
SieveOfEratosthenes[0] = SieveOfEratosthenes[1] = false;
for (int i = 2; i * i <= length; ++i) {
if (SieveOfEratosthenes[i]) {
for (int j = i * i; j <= length; j += i) {
SieveOfEratosthenes[j] = false;
}
}
}
}
template <class T>
bool IsPrime(T value) {
if (value < SieveOfEratosthenes.size() - 1) return SieveOfEratosthenes[value];
for (T i = 2; i * i <= value; ++i) {
if (value % i == 0) {
return false;
}
}
return true;
}
template <class T>
T NMemberOfArithmeticProgression(const T& a1, const T& n, const T& d) {
return a1 + d * (n - 1);
}
template <class T>
T SumOfArithmeticProgression(const T& a1, const T& n, const T& d) {
return (a1 + NMemberOfArithmeticProgression(a1, n, d)) * n / 2;
}
template <class T>
T Min(const T& a, const T& b) {
return a < b ? a : b;
}
template <class T>
T Max(const T& a, const T& b) {
return a > b ? a : b;
}
template <class T>
T Abs(const T& value) {
return value >= 0 ? value : -value;
}
template <class T>
TVector<T> Factorize(T value) {
TVector<T> res;
for (T i = 2; i * i <= value; i++) {
while (value % i == 0) {
res.push_back(i);
value /= i;
}
}
if (value > 1) {
res.push_back(value);
}
return res;
}
template <class T>
TVector<T> GetDivisors(T value) {
TVector<T> res;
for (T i = 1; i * i <= value; i++) {
if (value % i == 0) {
res.push_back(i);
if (i * i != value) res.push_back(value / i);
}
}
sort(res.begin(), res.end());
return res;
}
double Log(const double a, const double b) { return log(b) / log(a); }
} // namespace NMath
int solve();
int main(int argc, char* argv[]) {
return solve();
return 0;
}
template <class T>
void Read(TMatrix<T>& matrix, int n = -1, int m = -1) {
if (n == -1) {
read(n, m);
} else if (m == -1) {
m = n;
}
matrix.clear();
matrix.resize(n, TVector<T>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
read(matrix[i][j]);
}
}
}
int solve() {
int t;
read(t);
while (t--) {
TMatrix<char> mat;
int n;
read(n);
Read<char>(mat, n);
TVector<pair<int, int>> ans;
if (mat[0][1] == mat[1][0]) {
if (mat[n - 1][n - 2] == mat[1][0]) {
ans.push_back({n, n - 1});
}
if (mat[n - 2][n - 1] == mat[1][0]) {
ans.push_back({n - 1, n});
}
} else if (mat[n - 1][n - 2] == mat[n - 2][n - 1]) {
if (mat[n - 1][n - 2] == mat[1][0]) {
ans.push_back({2, 1});
}
if (mat[n - 2][n - 1] == mat[0][1]) {
ans.push_back({1, 2});
}
} else {
ans.push_back({1, 2});
if (mat[n - 1][n - 2] == mat[1][0]) {
ans.push_back({n, n - 1});
}
if (mat[n - 2][n - 1] == mat[1][0]) {
ans.push_back({n - 1, n});
}
}
writeln<int>((int)ans.size());
for (int i = 0; i < ans.size(); i++) {
writeln(ans[i].first, ans[i].second);
}
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
T data = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
data = (data << 3) + (data << 1) + ch - '0';
ch = getchar();
}
x = f * data;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
read(t);
read(args...);
}
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e5 + 9;
char maze[209][209];
signed main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", maze[i] + 1);
}
if (maze[1][2] == maze[2][1] && maze[n][n - 1] == maze[n - 1][n]) {
if (maze[1][2] == maze[n][n - 1])
printf("2\n1 2\n2 1\n");
else
printf("0\n");
} else if (maze[1][2] == maze[2][1]) {
if (maze[1][2] == maze[n][n - 1])
printf("1\n%d %d\n", n, n - 1);
else
printf("1\n%d %d\n", n - 1, n);
} else if (maze[n][n - 1] == maze[n - 1][n]) {
if (maze[1][2] == maze[n][n - 1])
printf("1\n1 2\n");
else
printf("1\n2 1\n");
} else {
printf("2\n2 1\n");
if (maze[1][2] == maze[n][n - 1])
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=[]
for i in range(N):
grid.append(list(input()))
if grid[1][0]==grid[0][1] and grid[N-2][N-1]==grid[N-1][N-2] and grid[1][0]!=grid[N-2][N-1]:
print(0)
elif grid[1][0]==grid[0][1] and grid[N-2][N-1]==grid[N-1][N-2] and grid[1][0]==grid[N-2][N-1]:
print(2)
print(1,2)
print(2,1)
elif grid[1][0]!=grid[0][1] and grid[N-2][N-1]!=grid[N-1][N-2]:
if grid[1][0]==grid[N-2][N-1]:
print(2)
print(2,1)
print(N,N-1)
else:
print(2)
print(2,1)
print(N-1,N)
else:
print(1)
tobechecked=[grid[1][0],grid[0][1],grid[N-2][N-1],grid[N-1][N-2]]
frequency={}
for i in tobechecked:
if i in frequency:
frequency[i]+=1
else:
frequency[i]=1
for i in frequency:
if frequency[i]==1:
death=i
for i in range(4):
if tobechecked[i]==death:
if i==0:
print(1,2)
elif i==1:
print(2,1)
elif i==2:
print(N,N-1)
else:
print(N-1,N)
| 8 |
PYTHON3
|
I=input
exec(int(I())*"n=int(I());a=[[*map(ord,I())]for _ in[0]*n];a=a[0][1],a[1][0],a[-2][-1]^1,a[-1][-2]^1;a=*zip(map(a.count,a),a,('1 2','2 1',f'{n-1} {n}',f'{n} {n-1}')),;r=[z for x,y,z in a if y!=max(a)[1]];print(len(r),*r);")
| 8 |
PYTHON3
|
# import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
from collections import defaultdict, deque
def solve():
n = int(input())
sq = []
ans = []
c = 0
for i in range(n):
sq.append(input())
if sq[0][1] == sq[1][0]:
if sq[-1][-2]==sq[0][1]:
c+=1
ans.append((n, n-1))
if sq[-2][-1]==sq[0][1]:
c+=1
ans.append((n-1, n))
elif sq[-1][-2] == sq[-2][-1]:
#print("working")
if sq[0][1]==sq[-1][-2]:
c+=1
ans.append((1, 2))
if sq[1][0]==sq[-1][-2]:
c+=1
ans.append((2, 1))
else:
c = 2
if sq[0][1] == '1':
ans.append((1, 2))
else:
ans.append((2, 1))
#print(sq[-1][-2])
if sq[-1][-2] == '0':
ans.append((n, n-1))
else:
ans.append((n-1, n))
print(c)
for i in ans:
print(i[0], i[1])
def main():
t = 1
t = int(input())
for _ in range(t):
solve()
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a;
cin >> a;
for (long long i = 0; i < a; i++) {
long long b;
cin >> b;
string s[b + 1];
for (long long j = 0; j < b; j++) {
cin >> s[j];
}
vector<pair<long long, long long>> ans;
if (s[0][1] == s[1][0]) {
if (s[b - 1][b - 2] == s[0][1]) {
ans.push_back({b - 1, b - 2});
}
if (s[b - 2][b - 1] == s[0][1]) {
ans.push_back({b - 2, b - 1});
}
} else {
if (s[b - 1][b - 2] == s[b - 2][b - 1]) {
if (s[0][1] == s[b - 1][b - 2]) {
ans.push_back({0, 1});
}
if (s[b - 2][b - 1] == s[1][0]) {
ans.push_back({1, 0});
}
} else {
if (s[0][1] == '1') {
ans.push_back({0, 1});
} else {
ans.push_back({1, 0});
}
if (s[b - 1][b - 2] == '0') {
ans.push_back({b - 1, b - 2});
} else {
ans.push_back({b - 2, b - 1});
}
}
}
cout << ans.size() << "\n";
for (long long j = 0; j < ans.size(); j++) {
cout << ans[j].first + 1 << " " << ans[j].second + 1 << "\n";
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
void speed() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
}
int main() {
long long int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
vector<pair<int, int>> ans;
char x;
if (a[0][1] == a[1][0]) {
x = a[0][1];
if (x == a[n - 1][n - 2]) {
ans.push_back(make_pair(n, n - 1));
}
if (x == a[n - 2][n - 1]) {
ans.push_back(make_pair(n - 1, n));
}
} else if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
x = a[n - 1][n - 2];
if (x == a[0][1]) {
ans.push_back(make_pair(1, 2));
}
if (x == a[1][0]) {
ans.push_back(make_pair(2, 1));
}
} else {
x = a[0][1];
ans.push_back(make_pair(2, 1));
if (x == a[n - 1][n - 2]) {
ans.push_back(make_pair(n, n - 1));
}
if (x == a[n - 2][n - 1]) {
ans.push_back(make_pair(n - 1, n));
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << endl;
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> pos[10] = {make_pair(1, 2), make_pair(2, 1), make_pair(-1, 0),
make_pair(0, -1)};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char c;
int up = 0, down = 0;
vector<int> v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> c;
if ((i == 1 && j == 2) || (i == 2 && j == 1)) {
up += (c - '0');
v.push_back(c - '0');
}
if ((i == n - 1 && j == n) || (i == n && j == n - 1)) {
down += (c - '0');
v.push_back(c - '0');
}
}
}
vector<int> afis;
int myPosition = 0;
if (v[1] != v[0] && (v[0] != v[2] || v[0] != v[3])) {
afis.push_back(1);
myPosition = 0;
} else if (v[1] != v[0] && (v[1] != v[2] || v[1] != v[3])) {
afis.push_back(0);
myPosition = 1;
}
for (int i = 2; i < 4; i++)
if (v[i] == v[myPosition]) afis.push_back(i);
cout << afis.size() << "\n";
for (int i = 0; i < afis.size(); i++) {
if (afis[i] < 2)
cout << pos[afis[i]].first << " " << pos[afis[i]].second << "\n";
else
cout << n + pos[afis[i]].first << " " << n + pos[afis[i]].second
<< "\n";
}
}
return 0;
}
| 8 |
CPP
|
from collections import *
from itertools import *
from bisect import *
def inp():
return int(input())
def arrinp():
return [int(x) for x in input().split()]
def main():
t = inp()
for _ in range(t):
n = inp()
grid = []
for i in range(n):
grid.append([x for x in input()])
x,y = grid[1][0], grid[0][1]
p,q = grid[n-1][n-2], grid[n-2][n-1]
#print(x,y, 'x and y')
#print(p,q, 'p and q')
ans_coordinates = []
ans = 0
if(x==y):
if(x=='0'):
#make p and q: 1
if(p!='1'):
ans +=1
ans_coordinates.append([n, n-1])
if(q!='1'):
ans +=1
ans_coordinates.append([n-1, n])
elif(x=='1'):
#make p and q: 0
if(p!='0'):
ans +=1
ans_coordinates.append([n, n-1])
if(q!='0'):
ans +=1
ans_coordinates.append([n-1, n])
elif(x!=y):
if(p==q):
if(p=='0'):
#make both x and y as 1
if(x == '0' and y=='1'):
ans +=1
ans_coordinates.append([2,1])
elif(x=='1' and y=='0'):
ans += 1
ans_coordinates.append([1,2])
elif(p=='1'):
#make both x and y as 0
if(x == '0' and y=='1'):
ans +=1
ans_coordinates.append([1,2])
elif(x=='1' and y=='0'):
ans += 1
ans_coordinates.append([2,1])
elif(p!=q):
#make both x and y as 1
#make both p and q as 0
if(x=='1' and y=='0'):
ans +=1
ans_coordinates.append([1,2])
elif(x=='0' and y=='1'):
ans += 1
ans_coordinates.append([2,1])
if(p=='1' and q == '0'):
ans +=1
ans_coordinates.append([n,n-1])
elif(p=='0' and q=='1'):
ans +=1
ans_coordinates.append([n-1,n])
print(ans)
for co in ans_coordinates:
print(*co, sep = ' ')
if __name__ == '__main__':
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
cin >> n;
vector<vector<char>> a(n, vector<char>(n));
vector<int> ans1;
vector<int> ans2;
int res = 0;
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++) cin >> a[x][y];
int mia, mib;
mia = ((a[0][1] == '0') + (a[1][0] == '0'));
mib = ((a[n - 1][n - 2] == '0') + (a[n - 2][n - 1] == '0'));
if (mia >= mib) {
if (a[0][1] != '0') {
res++;
ans1.push_back(1);
ans2.push_back(2);
}
if (a[1][0] != '0') {
res++;
ans1.push_back(2);
ans2.push_back(1);
}
if (a[n - 1][n - 2] != '1') {
res++;
ans1.push_back(n);
ans2.push_back(n - 1);
}
if (a[n - 2][n - 1] != '1') {
res++;
ans1.push_back(n - 1);
ans2.push_back(n);
}
} else {
if (a[0][1] != '1') {
res++;
ans1.push_back(1);
ans2.push_back(2);
}
if (a[1][0] != '1') {
res++;
ans1.push_back(2);
ans2.push_back(1);
}
if (a[n - 1][n - 2] != '0') {
res++;
ans1.push_back(n);
ans2.push_back(n - 1);
}
if (a[n - 2][n - 1] != '0') {
res++;
ans1.push_back(n - 1);
ans2.push_back(n);
}
}
cout << res << endl;
for (int f = 0; f < res; f++) {
cout << ans1[f] << " " << ans2[f] << endl;
}
}
}
| 8 |
CPP
|
for _ in range(int(input())):
n=int(input())
a=[]
for i in range(n):
a.append(list(input()))
c=0
s=[]
if (a[0][1]==a[1][0]) and (a[n-1][n-2]==a[n-2][n-1]):
if a[0][1]!=a[n-1][n-2]:
print(0)
else:
print(2)
print('1 2')
print('2 1')
elif (a[0][1]==a[1][0]) and (a[n-1][n-2]!=a[n-2][n-1]):
if a[n-1][n-2]==a[0][1]:
print(1)
print(f'{n} {n-1}')
else:
print(1)
print(f'{n-1} {n}')
elif (a[0][1]!=a[1][0]) and (a[n-1][n-2]==a[n-2][n-1]):
if a[n-1][n-2]==a[0][1]:
print(1)
print('1 2')
else:
print(1)
print('2 1')
else:
print(2)
if a[0][1]=='0':
print('2 1')
else:
print('1 2')
if a[n-1][n-2]=='1':
print(f'{n-1} {n}')
else:
print(f'{n} {n-1}')
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, int> &left, const pair<int, int> &right) {
return left.first > right.first ||
(left.first == right.first && left.second < right.second);
}
int main() {
int t, cs = 1;
cin >> t;
while (t--) {
int n, m, a, b, c, i, j, k, mx = 0, mn = 1e9;
string ar[2000];
int bs[2000] = {0};
cin >> n;
for (i = 0; i < n; i++) cin >> ar[i];
if (ar[0][1] == ar[1][0] && ar[n - 1][n - 2] == ar[n - 2][n - 1]) {
if (ar[0][1] != ar[n - 1][n - 2])
cout << 0 << endl;
else {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
}
} else if (ar[0][1] == ar[1][0] && ar[n - 1][n - 2] != ar[n - 2][n - 1]) {
if (ar[0][1] == ar[n - 1][n - 2]) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else if (ar[0][1] != ar[1][0] && ar[n - 1][n - 2] == ar[n - 2][n - 1]) {
if (ar[0][1] == ar[n - 1][n - 2]) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
}
} else {
if (ar[0][1] == ar[n - 1][n - 2]) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
cout << n - 1 << " " << n << endl;
}
}
}
return 0;
}
| 8 |
CPP
|
T = int(input())
for t in range(T):
n = int(input())
g = [input() for i in range(n)]
a,b,c,d=g[0][1],g[1][0],g[-1][-2],g[-2][-1]
#a,b,c,d=0,0,1,1
#print(a,b,c,d)
ans = 0
if (a==b=='0' and c==d=='1') :
ans = 0
print(0)
elif (a==b=='1' and c==d=='0'):
ans = 0
print(ans)
elif a==b==c==d:
ans =2
print(ans)
print(1,2)
print(2,1)
elif(a!=b and c!=d):
if a==c:
print(2)
print(2,1)
print(n,n-1)
elif a==d:
print(2)
print(2,1)
print(n-1,n)
elif (a!=b and c==d):
ans = 1
print(ans)
if a==c:
print(1,2)
else:
print(2,1)
elif(a==b and c!=d) :
ans = 1
print(ans)
if a==c:
print(n,n-1)
else:
print(n-1,n)
| 8 |
PYTHON3
|
import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict,deque,Counter
from bisect import *
from math import sqrt,pi,ceil
import math
from itertools import permutations
from copy import deepcopy
for t in range(int(input())):
n = int(input())
a = []
for i in range(n):
a.append(list(input().rstrip()))
f = [a[n - 1][-2], a[n - 2][-1]]
s = [a[0][1], a[1][0]]
if f[0] == "0" and f[1] == "0":
if s[0] == "1" and s[1] == "1":
print(0)
elif s[0] == "1" and s[1] == "0":
print(1)
print(2, 1)
elif s[0] == "0" and s[1] == "0":
print(2)
print(1, 2)
print(2, 1)
else:
print(1)
print(1, 2)
elif f[0] == "1" and f[1] == "1":
if s[0] == "1" and s[1] == "1":
print(2)
print(1, 2)
print(2, 1)
elif s[0] == "1" and s[1] == "0":
print(1)
print(1, 2)
elif s[0] == "0" and s[1] == "1":
print(1)
print(2, 1)
else:
print(0)
elif f[0] == "1" and f[1] == "0":
if s[0] == "1" and s[1] == "1":
print(1)
print(n, n - 1)
elif s[0] == "1" and s[1] == "0":
print(2)
print(2, 1)
print(n, n - 1)
elif s[0] == "0" and s[1] == "1":
print(2)
print(2, 1)
print(n - 1, n)
else:
print(1)
print(n - 1, n)
else:
if s[0] == "1" and s[1] == "1":
print(1)
print(n - 1, n)
elif s[0] == "1" and s[1] == "0":
print(2)
print(2, 1)
print(n - 1, n)
elif s[0] == "0" and s[1] == "1":
print(2)
print(2, 1)
print(n, n - 1)
else:
print(1)
print(n, n - 1)
| 8 |
PYTHON3
|
import sys, os.path
from collections import*
from copy import*
import math
import heapq
mod=10**9+7
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
for t in range(int(input())):
n=int(input())
s=[]
for i in range(n):
k=input()
s.append(k)
a=s[0][1]
b=s[1][0]
c=s[n-2][n-1]
d=s[n-1][n-2]
if(a==b=='1' and c==d=='0'):
print(0)
elif(a==b=='0' and c==d=='1'):
print(0)
elif(a=='0' and b=='0' and c=='0' and d=='0'):
print(2)
print(1,2)
print(2,1)
elif(a=='0' and b=='0' and c=='0' and d=='1'):
print(1)
print(n-1,n)
elif(a=='0' and b=='0' and c=='1' and d=='0'):
print(1)
print(n,n-1)
elif(a=='0' and b=='1' and c=='0' and d=='0'):
print(1)
print(1,2)
elif(a=='0' and b=='1' and c=='0' and d=='1'):
print(2)
print(1,2)
print(n,n-1)
elif(a=='0' and b=='1' and c=='1' and d=='0'):
print(2)
print(1,2)
print(n-1,n)
elif(a=='0' and b=='1' and c=='1' and d=='1'):
print(1)
print(2,1)
elif(a=='1' and b=='0' and c=='0' and d=='0'):
print(1)
print(2,1)
elif(a=='1' and b=='0' and c=='0' and d=='1'):
print(2)
print(2,1)
print(n,n-1)
elif(a=='1' and b=='0' and c=='1' and d=='0'):
print(2)
print(2,1)
print(n-1,n)
elif(a=='1' and b=='0' and c=='1' and d=='1'):
print(1)
print(1,2)
elif(a=='1' and b=='1' and c=='0' and d=='1'):
print(1)
print(n,n-1)
elif(a=='1' and b=='1' and c=='1' and d=='0'):
print(1)
print(n-1,n)
elif(a=='1' and b=='1' and c=='1' and d=='1'):
print(2)
print(1,2)
print(2,1)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
mat = []
for i in range(n):
mat.append(list(input()))
# a = 0
ans = []
if mat[0][1] == mat[1][0]:
v = int(mat[0][1])
if int(mat[n - 1][n - 2]) != abs(v - 1):
ans.append([n, n - 1])
if int(mat[n - 2][n - 1]) != abs(v - 1):
ans.append([n - 1, n])
elif int(mat[n - 2][n - 1]) == int(mat[n - 1][n - 2]):
v = int(mat[n - 2][n - 1])
if int(mat[0][1]) != abs(v - 1):
ans.append([1, 2])
if int(mat[1][0]) != abs(v - 1):
ans.append([2, 1])
else:
if mat[0][1] == '1':
ans.append([1, 2])
else:
ans.append([2, 1])
if mat[n - 1][n - 2] == '0':
ans.append([n, n - 1])
else:
ans.append([n - 1, n])
print(len(ans))
for i in ans:
print(i[0], i[1])
| 8 |
PYTHON3
|
I=input
exec(int(I())*"n=int(I());a=*map(ord,''.join(I()for _ in[0]*n)),;a=a[1],a[n],a[~n]^1,a[~1]^1;r=[y for x,y in zip(a,('1 2','2 1',f'{n-1} {n}',f'{n} {n-1}'))if(sum(a)>194)^x&1];print(len(r),*r);")
| 8 |
PYTHON3
|
def f(c, ans):
if c != 0:
print(c)
for i in range(c):
print(ans[i][0], ans[i][1])
else:
print(c)
def main():
t = int(input())
for i in range(t):
x = int(input())
a = [input() for i in range(x)]
a1 = a[0][1]
a2 = a[1][0]
a3 = a[x - 2][x - 1]
a4 = a[x - 1][x - 2]
c = 0
ans = []
if a1 == a2:
v = str(a1)
if a3 == v:
c += 1
ans.append((x-1, x))
if a4 == v:
c += 1
ans.append((x, x-1))
f(c, ans)
elif a3 == a4:
v = str(a3)
if a1 == v:
c += 1
ans.append((1, 2))
if a2 == v:
c += 1
ans.append((2, 1))
f(c, ans)
else:
if a1 != '0':
c += 1
ans.append((1, 2))
if a2 != '0':
c += 1
ans.append((2, 1))
if a3 != '1':
c += 1
ans.append((x - 1, x))
if a4 != '1':
c += 1
ans.append((x, x - 1))
f(c, ans)
main()
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int T, n, res, a1, a2, a3, a4;
char s[210][210];
bool check(char x, char y) {
res =
(s[1][2] == y) + (s[2][1] == y) + (s[n][n - 1] == x) + (s[n - 1][n] == x);
if (res <= 2) {
printf("%d\n", res);
if (s[1][2] == y) printf("1 2\n");
if (s[2][1] == y) printf("2 1\n");
if (s[n][n - 1] == x) printf("%d %d\n", n, n - 1);
if (s[n - 1][n] == x) printf("%d %d\n", n - 1, n);
return 1;
}
return 0;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
if (check('0', '1'))
;
else
check('1', '0');
}
}
| 8 |
CPP
|
a=int(input())
for i in range(0,a):
b=int(input())
arr=[]
for i in range(0,b):
arr.append(input())
n=b-1
if(arr[0][1]==arr[1][0]):
count=0
if(arr[n-1][n]==arr[0][1]):
count=count+1
if(arr[n][n-1]==arr[0][1]):
count=count+1
print(count)
if(arr[n-1][n]==arr[0][1]):
print(n,n+1)
if(arr[n][n-1]==arr[0][1]):
print(n+1,n)
elif(arr[n][n-1]==arr[n-1][n]):
count=0
if(arr[n-1][n]==arr[0][1]):
count=count+1
if(arr[n][n-1]==arr[1][0]):
count=count+1
print(count)
if(arr[n-1][n]==arr[0][1]):
print(1,2)
if(arr[n][n-1]==arr[1][0]):
print(2,1)
elif(arr[1][1]=='0'):
count=0
if(arr[1][0]=='1'):
if(arr[2][0]=='1'):
count=count+ 1
elif(arr[0][1]=='1'):
if(arr[0][2]=='1'):
count=count+1
if(arr[n-1][n]=='0'):
count=count+1
else:
count=count+1
print(count)
if(arr[1][0]=='1'):
if(arr[2][0]=='1'):
print(3,1)
elif(arr[0][1]=='1'):
if(arr[0][2]=='1'):
print(1,3)
if(arr[n-1][n]=='0'):
print(n,n+1)
else:
print(n+1,n)
elif(arr[1][1]=='1'):
count=0
if(arr[1][0]=='0'):
if(arr[2][0]=='0'):
count=count+1
elif(arr[0][1]=='0'):
if(arr[0][2]=='0'):
count=count+1
if(arr[n-1][n]=='1'):
count=count+1
else:
count=count+1
print(count)
if(arr[1][0]=='0'):
if(arr[2][0]=='0'):
print(3,1)
elif(arr[0][1]=='0'):
if(arr[0][2]=='0'):
print(1,3)
if(arr[n-1][n]=='1'):
print(n,n+1)
else:
print(n+1,n)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
grid = [input() for i in range(n)]
a, b, c, d = grid[0][1], grid[1][0], grid[-2][-1], grid[-1][-2]
e = []
f = []
if a != '0':e.append((1, 2))
if b != '0':e.append((2, 1))
if c != '1':e.append((n-1, n))
if d != '1':e.append((n, n-1))
if c != '0':f.append((n-1,n))
if d != '0':f.append((n, n-1))
if a != '1':f.append((1,2))
if b != '1':f.append((2,1))
if len(e) < len(f):
print(len(e))
for i in e:
print(*i)
else:
print(len(f))
for i in f:
print(*i)
| 8 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int t = 1;
cin >> t;
while (t--) {
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
int cnt = 0;
int c = 0;
vector<pair<int, int>> v;
int ans = 0;
if (a[0][1] == '1') {
cnt++;
}
if (a[1][0] == '1') {
cnt++;
}
if (a[n - 1][n - 2] == '1') {
c++;
}
if (a[n - 2][n - 1] == '1') {
c++;
}
if (cnt == 2) {
if (a[n - 2][n - 1] == '1') {
v.push_back({n - 1, n});
ans++;
}
if (a[n - 1][n - 2] == '1') {
v.push_back({n, n - 1});
ans++;
}
} else if (c == 2) {
if (a[1][0] == '1') {
v.push_back({2, 1});
ans++;
}
if (a[0][1] == '1') {
v.push_back({1, 2});
ans++;
}
} else if (cnt == 0) {
if (a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n});
ans++;
}
if (a[n - 1][n - 2] == '0') {
v.push_back({n, n - 1});
ans++;
}
} else if (c == 0) {
if (a[1][0] == '0') {
v.push_back({2, 1});
ans++;
}
if (a[0][1] == '0') {
v.push_back({1, 2});
ans++;
}
} else if (cnt == 1 && c == 1) {
if (a[1][0] == '1') {
v.push_back({2, 1});
ans++;
}
if (a[0][1] == '1') {
v.push_back({1, 2});
ans++;
}
if (a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n});
ans++;
}
if (a[n - 1][n - 2] == '0') {
v.push_back({n, n - 1});
ans++;
}
}
cout << ans << "\n";
for (auto u : v) {
cout << u.first << " " << u.second << "\n";
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string m[n];
for (int i = 0; i < n; i++) cin >> m[i];
char a = m[1][0], b = m[0][1], c = m[n - 1][n - 2], d = m[n - 2][n - 1];
if (a == b && c == d) {
if (a == c)
cout << "2\n2 1\n1 2\n";
else
cout << 0 << endl;
} else if (a == b) {
if (c == a)
cout << "1\n" << n << ' ' << n - 1 << endl;
else
cout << "1\n" << n - 1 << ' ' << n << endl;
} else if (c == d) {
if (c == a)
cout << "1\n2 1\n";
else
cout << "1\n1 2\n";
} else {
if (a == c)
cout << "2\n2 1\n" << n - 1 << ' ' << n << endl;
else
cout << "2\n2 1\n" << n << ' ' << n - 1 << endl;
}
}
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long b) {
long long res = 1;
while (b != 0) {
if (1 & b) res *= a;
b >>= 1;
a *= a;
}
return res;
}
void solve() {
long long n, m;
cin >> n;
long long a, b, c, d, temp;
string s;
for (long long i = 1; i < n + 1; i++) {
cin >> s;
if (i == 1) {
a = s[1];
}
if (i == 2) {
b = s[0];
}
if (i == n - 1) {
c = s[n - 1];
}
if (i == n) {
d = s[n - 2];
}
}
if (a == b && c == d && a != c) {
cout << "0\n";
} else if (a == b && c == d && a == b) {
cout << "2\n1 2\n";
cout << "2 1\n";
} else if (a == b && c != d) {
if (c == a) {
cout << "1\n" << n - 1 << " " << n << "\n";
}
if (d == a) {
cout << "1\n" << n << " " << n - 1 << "\n";
}
} else if (a != b && c == d) {
if (c == a) {
cout << "1\n1 2\n";
}
if (c == b) {
cout << "1\n2 1\n";
}
} else {
if (a == c) {
cout << "2\n1 2\n" << n << " " << n - 1 << "\n";
}
if (a == d) {
cout << "2\n1 2\n" << n - 1 << " " << n << "\n";
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tt;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
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>> ans;
int a, b, c, d;
a = (int)arr[0][1] - '0';
b = (int)arr[1][0] - '0';
c = (int)arr[n - 2][n - 1] - '0';
d = (int)arr[n - 1][n - 2] - '0';
int sum = 0;
sum += (a != 0) + (b != 0) + (c != 1) + (d != 1);
if (sum <= 2) {
if (a != 0) ans.push_back({1, 2});
if (b != 0) ans.push_back({2, 1});
if (c != 1) ans.push_back({n - 1, n});
if (d != 1) ans.push_back({n, n - 1});
} else {
if (a != 1) ans.push_back({1, 2});
if (b != 1) ans.push_back({2, 1});
if (c != 0) ans.push_back({n - 1, n});
if (d != 0) ans.push_back({n, n - 1});
}
cout << ans.size() << "\n";
for (auto x : ans) cout << x.first << " " << x.second << "\n";
}
return 0;
}
| 8 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace {
template <typename T>
inline void read(T &x) {
x = 0;
T f = 1;
char s = getchar();
for (; !isdigit(s); s = getchar())
if (s == '-') f = -1;
for (; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
x *= f;
}
} // namespace
const int N = 205;
char s[N][N];
int main() {
int t;
read(t);
while (t--) {
int n;
read(n);
for (int i = 1; i <= n; i++) {
scanf("%s", s[i] + 1);
}
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[n - 1][n] && s[1][2] != s[n][n - 1])
puts("0");
else if (s[n][n - 1] == s[n - 1][n]) {
printf("2\n%d %d\n%d %d\n", n, n - 1, n - 1, n);
} else {
printf("1\n");
if (s[1][2] == s[n][n - 1]) {
printf("%d %d\n", n, n - 1);
} else
printf("%d %d\n", n - 1, n);
}
} else {
if (s[n][n - 1] != s[n - 1][n]) {
printf("2\n");
if (s[1][2] == s[n][n - 1])
printf("1 2\n%d %d\n", n - 1, n);
else
printf("1 2\n%d %d\n", n, n - 1);
} else {
printf("1\n");
if (s[1][2] == s[n][n - 1])
printf("1 2\n");
else
printf("2 1\n");
}
}
}
}
| 8 |
CPP
|
t = int(input())
for _ in range(t):
n = int(input())
a = []
for i in range(n):
a += [input()]
s1, s2, f1, f2 = a[0][1], a[1][0], a[n-1][n-2], a[n-2][n-1]
if s1 == s2:
if f1 != f2:
if f1 == s1:
print(1)
print(n, n-1)
elif f2 == s1:
print(1)
print(n-1, n)
else:
if f1 != s1:
print(0)
else:
print(2)
print(n, n-1)
print(n-1, n)
else:
if f1 != f2:
print(2)
if f1 == s1:
print(n, n-1)
print(2, 1)
else:
print(n-1, n)
print(2, 1)
else:
print(1)
if f1 == s1:
print(1, 2)
else:
print(2, 1)
| 8 |
PYTHON3
|
for _ in range(int(input())):
n = int(input())
strs = [list(input()) for _ in range(n)]
startright = strs[0][1]
startdown = strs[1][0]
endup = strs[n - 2][n - 1]
endleft = strs[n - 1][n - 2]
ans = []
# print(startright, startdown)
# print(endup, endleft)
if startright == startdown:
if endup == startright:
ans += [(n - 1, n)]
if endleft == startright:
ans += [(n, n - 1)]
elif endleft == endup:
if startright == endleft:
ans += [(1, 2)]
if startdown == endleft:
ans += [(2, 1)]
else:
if startright == '0':
# print('here')
ans += [(1, 2)]
if startdown == '0':
ans += [(2, 1)]
# print('here!')
if endleft == '1':
# print('here2')
ans += [(n, n - 1)]
if endup == '1':
# print('here2!')
ans += [(n - 1, n)]
print(len(ans))
for res in ans:
print(*res)
| 8 |
PYTHON3
|
tc = int(input())
for _ in range(tc):
n = int(input())
mat = []
for i in range(n):
x = input()
mat.append([j for j in x])
count = []
if mat[1][0] == mat[0][1]:
if mat[n-1][n-2] == mat[n-2][n-1]:
if mat[1][0] == mat[n-1][n-2]:
count.append([1,2])
count.append([2,1])
else:
if mat[n-1][n-2] == mat[1][0]:
count.append([n,n-1])
else:
count.append([n-1,n])
else:
if mat[n-1][n-2] == mat[n-2][n-1]:
if mat[0][1] == mat[n-1][n-2]:
count.append([1,2])
else:
count.append([2,1])
else:
if mat[n-1][n-2] == mat[1][0]:
count.append([1,2])
count.append([n,n-1])
else:
count.append([1,2])
count.append([n-1,n])
print(len(count))
for i in count:
print(i[0],i[1])
| 8 |
PYTHON3
|
import math
for _ in range(int(input())):
n = int(input())
mat = []
for i in range(n):
temp = input()
mat.append(list(temp))
#print(mat)
a = mat[0][1]
b = mat[1][0]
c = mat[n-1][n-2]
d = mat[n-2][n-1]
#print(a,b,c,d)
if a == b and c == d:
if a != c:
print(0)
else:
print(2)
print(n,n-1)
print(n-1,n)
elif a == d and b == c:
#print('x')
print(2)
print(1,2)
print(n,n-1)
elif a == c and b ==d and a!= b:
#print('y')
print(2)
print(1,2)
print(n-1,n)
elif a == b and c != d:
if c == a:
print(1)
print(n,n-1)
elif d == a:
print(1)
print(n-1,n)
elif c == d and a != b:
if a == c:
print(1)
print(1,2)
elif c == b:
print(1)
print(2,1)
| 8 |
PYTHON3
|
nn = int(input())
mm = []
for _ in range(nn):
n = int(input())
mm.append([])
for _ in range(n):
s = input()
mm[-1].append(s)
for i in range(nn):
m = mm[i]
n = len(m)
a, b, c, d = int(m[0][1]), int(m[1][0]), int(m[-1][-2]), int(m[-2][-1])
if (a == b == 0 and c == d == 1) or (a == b == 1 and c == d == 0):
print(0)
elif (a == b == c == d == 0) or (a == b == c == d == 1):
print(2)
print('1 2')
print('2 1')
elif a + b == 1 and c + d == 2:
print(1)
if a == 1:
print('1 2')
else:
print('2 1')
elif a + b == 1 and c + d == 0:
print(1)
if a == 0:
print('1 2')
else:
print('2 1')
elif a + b == 1 and c + d == 1:
print(2)
if a == 1:
print('1 2')
else:
print('2 1')
if c == 0:
print('{} {}'.format(n, n-1))
else:
print('{} {}'.format(n-1, n))
elif c + d == 1 and a + b == 2:
print(1)
if c == 1:
print('{} {}'.format(n, n-1))
else:
print('{} {}'.format(n-1, n))
elif c + d == 1 and a + b == 0:
print(1)
if c == 0:
print('{} {}'.format(n, n-1))
else:
print('{} {}'.format(n-1, n))
| 8 |
PYTHON3
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.