solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
t=int(input()) for i in range(t): l=[] nl=[] c=0 n=int(input()) zc=0 oc=0 k=0 a='' b='' c='' d='' for i2 in range(n): ml='' mlt=[] ml=input() l.append(ml) a=l[n-1][n-3] b=l[n-2][n-2] c=l[n-3][n-1] e=l[n-1][n-2] f=l[n-2][n-1] while k==0: if (a==b)and(a==c): if a=='1': oc=3 else: zc=3 elif (a==b)and(a!=c): d='c' if a=='1': oc=2 zc=1 else: oc=1 zc=2 elif (a==c)and(a!=b): d='b' if a=='1': oc=2 zc=1 else: oc=1 zc=2 elif (c==b)and(a!=c): d='a' if c=='1': oc=2 zc=1 else: oc=1 zc=2 k=1 #if(l[0][1]=='0')and(l[1][0]=='0'): # if l[n-1][n-2]=='0': # c=c+1 # nl.append(n) # nl.append(n-1) #if l[n-2][n-1]=='0': # c=c+1 # nl.append(n-1) # nl.append(n) # elif(l[0][1]=='1')and(l[1][0]=='1'): # if l[n-1][n-2]=='1': # c=c+1 # nl.append(n) # nl.append(n-1) #if l[n-2][n-1]=='1': # c=c+1 # nl.append(n-1) # nl.append(n) if 1>0: if (e==f)and(e=='1'): if oc==3: c=2 nl=[n,n-1,n-1,n] elif oc==0: c=0 #elif zc=3: elif oc==2: c=2 if d=='a': nl=[n-1,n-1,n-2,n] elif d=='b': nl=[n,n-2,n-2,n] elif d=='c': nl=[n,n-2,n-1,n-1] elif oc==1: c=1 if d=='a': nl=[n,n-2] elif d=='b': nl=[n-1,n-1] elif d=='c': nl=[n-2,n] elif (e==f)and(e=='0'): if zc==3: c=2 nl=[n,n-1,n-1,n] elif zc==0: c=0 #elif zc=3: elif zc==2: c=2 if d=='a': nl=[n-1,n-1,n-2,n] elif d=='b': nl=[n,n-2,n-2,n] elif d=='c': nl=[n,n-2,n-1,n-1] elif zc==1: c=1 if d=='a': nl=[n,n-2] elif d=='b': nl=[n-1,n-1] elif d=='c': nl=[n-2,n] elif (e!=f)and(e=='1'): if oc==3: c=1 nl=[n,n-1] elif oc==0: c=1 nl=[n-1,n] #elif zc=3: elif oc==2: c=2 if d=='a': nl=[n,n-1,n,n-2] elif d=='b': nl=[n,n-1,n-1,n-1] elif d=='c': nl=[n,n-1,n-2,n] elif oc==1: c=2 if d=='a': nl=[n-1,n,n,n-2] elif d=='b': nl=[n-1,n,n-1,n-1] elif d=='c': nl=[n-1,n,n-2,n] elif (e!=f)and(e=='0'): if zc==3: c=1 nl=[n,n-1] elif zc==0: c=1 nl=[n-1,n] #elif zc=3: elif zc==2: c=2 if d=='a': nl=[n,n-1,n,n-2] elif d=='b': nl=[n,n-1,n-1,n-1] elif d=='c': nl=[n,n-1,n-2,n] elif zc==1: c=2 if d=='a': nl=[n-1,n,n,n-2] elif d=='b': nl=[n-1,n,n-1,n-1] elif d=='c': nl=[n-1,n,n-2,n] print(c) if c>0: for i4 in range(c): print(f'{nl[2*i4]} {nl[2*i4+1]}')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { long long n; cin >> n; char a[n + 1][n + 1]; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { cin >> a[i][j]; } } vector<pair<long long, long long>> v; long long z = 0, c = 0; if (a[1][2] == '0') z++; if (a[2][1] == '0') z++; if (a[n - 1][n] == '0') c++; if (a[n][n - 1] == '0') c++; if ((z == 2 && c == 0) || (z == 0 && c == 2)) cout << 0 << "\n"; else { if (z == 0) { if (a[n][n - 1] == '1') v.push_back(make_pair(n, n - 1)); if (a[n - 1][n] == '1') v.push_back(make_pair(n - 1, n)); } else if (z == 1) { if (c == 2) { if (a[1][2] == '0') v.push_back(make_pair(1, 2)); if (a[2][1] == '0') v.push_back(make_pair(2, 1)); } else { if (a[n][n - 1] == '0') v.push_back(make_pair(n, n - 1)); if (a[n - 1][n] == '0') v.push_back(make_pair(n - 1, n)); if (a[1][2] == '1') v.push_back(make_pair(1, 2)); if (a[2][1] == '1') v.push_back(make_pair(2, 1)); } } else if (z == 2) { if (a[n][n - 1] == '0') v.push_back(make_pair(n, n - 1)); if (a[n - 1][n] == '0') v.push_back(make_pair(n - 1, n)); } cout << v.size() << "\n"; for (long long i = 0; i < v.size(); i++) { cout << v[i].first << " " << v[i].second << "\n"; } } } return 0; }
8
CPP
for _ in range(int(input())): n = int(input()) mat = [] for i in range(n): mat.append(input()) d = [(1,2),(2,1),(n-1,n),(n,n-1)] items = [int(mat[0][1]),int(mat[1][0]),int(mat[-2][-1]),int(mat[-1][-2])] ans = [] if items[:2]==[0,0] or items[2:]==[1,1]: for i in range(4): if i<2 and items[i]:ans.append(d[i]) elif i>1 and not items[i]:ans.append(d[i]) else: for i in range(4): if i>1 and items[i]:ans.append(d[i]) elif i<2 and not items[i]:ans.append(d[i]) print(len(ans)) for i in ans: print(*i)
8
PYTHON3
from sys import stdin def main(): input = lambda: stdin.readline()[:-1] T = int(input()) for _ in [0] * T: N = int(input()) S = [input() for _ in [0] * N] sd, sr = int(S[1][0]), int(S[0][1]) fu, fl = int(S[N - 2][N - 1]), int(S[N - 1][N - 2]) if sd + sr + fu + fl == 0 or sd + sr + fu + fl == 4: ans1 = 2 ans2 = [(1, 2), (2, 1)] elif sd == sr and fu == fl: ans1 = 0 ans2 = [] elif sd == sr: ans1 = 1 if fu == sd: ans2 = [(N - 1, N)] else: ans2 = [(N, N - 1)] elif fu == fl: ans1 = 1 if sd == fu: ans2 = [(2, 1)] else: ans2 = [(1, 2)] else: ans1 = 2 ans2 = [] if sd: ans2.append((2, 1)) if sr: ans2.append((1, 2)) if not fu: ans2.append((N - 1, N)) if not fl: ans2.append((N, N - 1)) print(ans1) for a in ans2: print(*a) main()
8
PYTHON3
import sys input = sys.stdin.readline for _ in range(int(input())): N = int(input()) a = [list(input())[: -1] for _ in range(N)] res = [] if a[0][1] == a[1][0]: x = a[0][1] if a[-1][-2] == x: res.append((N, N - 1)) if a[-2][-1] == x: res.append((N - 1, N)) else: if a[-1][-2] == a[-2][-1]: x = a[-1][-2] if a[0][1] == x: res.append((1, 2)) if a[1][0] == x: res.append((2, 1)) else: x = a[0][1] res.append((2, 1)) if a[-1][-2] == x: res.append((N, N - 1)) if a[-2][-1] == x: res.append((N - 1, N)) print(len(res)) for r in res: print(*r)
8
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) grid=[] for i in range(n): l=input() grid.append(l) a=grid[0][1] b=grid[1][0] c=grid[n-2][n-1] d=grid[n-1][n-2] if a==b: if c==d: if a==c: print("2") print(n,n-1) print(n-1,n) else: print("0") else: print("1") if a==c: print(n-1,n) else: print(n,n-1) else: if c==d: print("1") if a==c: print("1 2") else: print("2 1") else: print("2") if a==c: print("2 1") print(n-1,n) else: print("2 1") print(n,n-1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int tryFirstState(vector<vector<char> > &a, int n) { int sol = 0; vector<vector<int> > ans(5, vector<int>(2, 0)); if (a[1][0] != '0') { ans[sol][0] = 2; ans[sol][1] = 1; sol++; } if (a[0][1] != '0') { ans[sol][0] = 1; ans[sol][1] = 2; sol++; } if (a[n - 1][n - 2] != '1') { ans[sol][0] = n; ans[sol][1] = n - 1; sol++; } if (a[n - 2][n - 1] != '1') { ans[sol][0] = n - 1; ans[sol][1] = n; sol++; } if (sol > 2) { return 0; } cout << sol << endl; for (int i = 0; i < sol; i++) { cout << ans[i][0] << " " << ans[i][1] << endl; } return 1; } int trySecondState(vector<vector<char> > &a, int n) { int sol = 0; vector<vector<int> > ans(5, vector<int>(2, 0)); if (a[1][0] != '1') { ans[sol][0] = 2; ans[sol][1] = 1; sol++; } if (a[0][1] != '1') { ans[sol][0] = 1; ans[sol][1] = 2; sol++; } if (a[n - 1][n - 2] != '0') { ans[sol][0] = n; ans[sol][1] = n - 1; sol++; } if (a[n - 2][n - 1] != '0') { ans[sol][0] = n - 1; ans[sol][1] = n; sol++; } if (sol > 2) { return 0; } cout << sol << endl; for (int i = 0; i < sol; i++) { cout << ans[i][0] << " " << ans[i][1] << endl; } return 1; } int main() { int t; cin >> t; int n; while (t--) { cin >> n; vector<vector<char> > a(n, vector<char>(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } int result = tryFirstState(a, n); if (result) { continue; } trySecondState(a, n); } return 0; }
8
CPP
for _ in range(int(input())): n = int(input()) a = [input() for _ in range(n)] for color in range(2): result = [] if int(a[0][1]) != color: result.append((0, 1)) if int(a[1][0]) != color: result.append((1, 0)) if int(a[n - 1][n - 2]) == color: result.append((n - 1, n - 2)) if int(a[n - 2][n - 1]) == color: result.append((n - 2, n - 1)) if len(result) < 3: print(len(result)) for x, y in result: print(x + 1, y + 1) break
8
PYTHON3
for test_var in range(int(input())): n = int(input()) s = ['']*n for i in range(n): s[i] = input().strip() a, b, c, d = s[0][1], s[1][0], s[n-1][n-2], s[n-2][n-1] if a == b == c == d: print("2\n1 2\n2 1") elif a != b and c == d: if a == c: print("1\n1 2") else: print("1\n2 1") elif a == b and c != d: if a == c: print(f"1\n{n} {n-1}") else: print(f"1\n{n-1} {n}") elif a != b and c != d: if a == c: print(f"2\n1 2\n{n-1} {n}") else: print(f"2\n2 1\n{n-1} {n}") else: print(0)
8
PYTHON3
import io import os from collections import Counter, defaultdict, deque def solve(N, grid): vals = [ (x, y, int(grid[x][y])) for x, y in [(0, 1), (1, 0), (N - 1, N - 2), (N - 2, N - 1)] ] patterns = [[0, 0, 1, 1], [1, 1, 0, 0]] for pattern in patterns: flips = [] for p, (x, y, z) in zip(pattern, vals): if p != z: flips.append(str(x + 1) + " " + str(y + 1)) if len(flips) <= 2: return str(len(flips)) + "\n" + "\n".join(flips) assert False if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline TC = int(input()) for tc in range(1, TC + 1): (N,) = [int(x) for x in input().split()] grid = [list(input().decode().rstrip()) for i in range(N)] ans = solve(N, grid) print(ans)
8
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) B = [input() for _ in range(n)] a,b = map(int, (B[0][1],B[1][0])) c,d = map(int, (B[-1][-2],B[-2][-1])) ans = [] if abs(a-1)+abs(b-1) + abs(c-0)+abs(d-0) <= 2: if a == 0: ans.append((1, 2)) if b == 0: ans.append((2, 1)) if c == 1: ans.append((n, n-1)) if d == 1: ans.append((n-1, n)) else: if a == 1: ans.append((1, 2)) if b == 1: ans.append((2, 1)) if c == 0: ans.append((n, n-1)) if d == 0: ans.append((n-1, n)) print(len(ans)) for p in ans: print(*p)
8
PYTHON3
import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = 'x' in file.mode or 'w' in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b'\n') + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' def get_input(a=str): return a(input()) def get_int_input(): return get_input(int) def get_input_arr(a): return list(map(a, input().split())) def get_int_input_arr(): return get_input_arr(int) def solve(): t = get_int_input() for _ in range(t): n = get_int_input() grid = [] for i in range(n): grid.append(get_input()) # print(grid) if grid[0][1] == grid[1][0] and grid[-2][-1] == grid[-1][-2] and grid[-1][-2] != grid[0][1]: cout<<0<<endl elif grid[0][1] == grid[1][0] and grid[-2][-1] == grid[-1][-2] and grid[-1][-2] == grid[0][1]: cout<<2<<endl cout<<1<<" "<<2<<endl cout<<2<<" "<<1<<endl elif grid[0][1] != grid[1][0] and grid[-2][-1] == grid[-1][-2]: if grid[1][0] == grid[-2][-1]: cout<<1<<endl cout<<2<<" "<<1<<endl elif grid[0][1] == grid[-2][-1]: cout<<1<<endl cout<<1<<" "<<2<<endl elif grid[0][1] == grid[1][0] and grid[-2][-1] != grid[-1][-2]: if grid[1][0] == grid[-2][-1]: cout<<1<<endl cout<<n - 1<<" "<<n<<endl elif grid[0][1] == grid[-1][-2]: cout<<1<<endl cout<<n<<" "<<n - 1<<endl elif grid[0][1] != grid[1][0] and grid[-2][-1] != grid[-1][-2]: if grid[0][1] == grid[-2][-1]: cout<<2<<endl cout<<1<<" "<<2<<endl cout<<n<<" "<<n - 1<<endl if grid[0][1] == grid[-1][-2]: cout<<2<<endl cout<<1<<" "<<2<<endl cout<<n - 1<<" "<<n<<endl def main(): solve() if __name__ == "__main__": main()
8
PYTHON3
def main(): n = int(input()) lst = [] for i in range(n): line = input() lst.append(line) top_left = lst[0][1] top_right = lst[1][0] bot_left = lst[n - 1][n - 2] bot_right = lst[n - 2][n - 1] if top_left == top_right: if bot_left == bot_right and bot_left == top_left: print(2) elif bot_left != bot_right: print(1) else: print(0) if bot_left == top_left: print(n, n - 1) if bot_right == top_right: print(n - 1, n) elif bot_left == bot_right: print(1) if bot_left == top_left: print(1, 2) if bot_right == top_right: print(2, 1) else: print(2) print(2, 1) if bot_left == top_left: print(n, n - 1) else: print(n - 1, n) if __name__ == '__main__': t = int(input()) for i in range(t): main()
8
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) matrix = list() for i in range(n): a = list(input()) matrix.append(a) switch = list() count = 0 if matrix[0][1] == matrix[1][0]: common = matrix[0][1] if matrix[n-1][n-2] == common: switch.append(str(n) + " " + str(n-1)) count += 1 if matrix[n-2][n-1] == common: switch.append(str(n-1) + " " + str(n)) count += 1 elif matrix[n-1][n-2] == matrix[n-2][n-1]: common = matrix[n-1][n-2] if matrix[1][0] == common: switch.append(str(2) + " " + str(1)) count += 1 if matrix[0][1] == common: switch.append(str(1) + " " + str(2)) count += 1 else: if matrix[0][1] != '0': switch.append(str(1) + " " + str(2)) count += 1 if matrix[1][0] != '0': switch.append(str(2) + " " + str(1)) count += 1 if matrix[n-1][n-2] != '1': switch.append(str(n) + " " + str(n-1)) count += 1 if matrix[n-2][n-1] != '1': switch.append(str(n-1) + " " + str(n)) count += 1 print(count) print(*switch, sep="\n")
8
PYTHON3
t=int(input()) while t: n=int(input()) maze=[[0]*n]*n for i in range(n): s=input() row=[i for i in s] maze[i]=row s_right=maze[0][1] s_down=maze[1][0] f_up=maze[n-2][n-1] f_left=maze[n-1][n-2] f=[f_up,f_left] if s_right not in f and s_down not in f: print(0) t-=1 continue if s_right!=s_down and f_left!=f_up: if s_right==f_up and s_down==f_left: print(2) print(1,2) print(n,n-1) t-=1 continue else: print(2) print(1,2) print(n-1,n) t-=1 continue if f_up==f_left: num=f_up if s_right!=s_down: if s_right==num: print(1) print(1,2) t-=1 continue if s_down==num: print(1) print(2,1) t-=1 continue if s_right==s_down: num1=s_right if f_up!=f_left: if f_up==num1: print(1) print(n-1,n) t-=1 continue if f_left==num1: print(1) print(n,n-1) t-=1 continue if num==num1: print(2) print(1,2) print(2,1) t-=1 continue t-=1
8
PYTHON3
import sys import bisect as bi import math from collections import defaultdict as dd import heapq import itertools import io import os ##import operator input=sys.stdin.readline ##input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline import random sys.setrecursionlimit(10**7) ##fo=open("output.txt","w") ##fi=open("input2.txt","w") mo=10**9+7 def cin(): return map(int,sin().split()) def ain(): return list(map(int,sin().split())) def sin(): return input() def inin(): return int(input()) for _ in range(inin()): n=inin() mat=[] for i in range(n): l=sin().strip() mat+=[l] ans=[] tem=0 ## print(mat) a,b,c,d=mat[0][1],mat[1][0],mat[-2][-1],mat[-1][-2] p=[[1,2],[2,1],[n-1,n],[n,n-1]] if(a=='1' and b=='1'): if(c=='1'): ans+=[p[2]] tem+=1 if(d=='1'): ans+=[p[3]] tem+=1 elif(a=='0' and b=='0'): if(c=='0'): ans+=[p[2]] tem+=1 if(d=='0'): ans+=[p[3]] tem+=1 else: if(c=='0' and d=='0'): if(a=='0'): ans+=[p[0]] tem+=1 else: ans+=[p[1]] tem+=1 else: if(a=='1'): ans+=[p[0]] tem+=1 if(b=='1'): ans+=[p[1]] tem+=1 if(c=='0'): ans+=[p[2]] tem+=1 if(d=='0'): ans+=[p[3]] tem+=1 print(tem) for i in ans: print(*i) ##def msb(n):n|=n>>1;n|=n>>2;n|=n>>4;n|=n>>8;n|=n>>16;n|=n>>32;n|=n>>64;return n-(n>>1) #2 ki power ##def pref(a,n,f): ## pre=[0]*n ## if(f==0): ##from beginning ## pre[0]=a[0] ## for i in range(1,n): ## pre[i]=a[i]+pre[i-1] ## else: ##from end ## pre[-1]=a[-1] ## for i in range(n-2,-1,-1): ## pre[i]=pre[i+1]+a[i] ## return pre ##maxint=10**24 ##def kadane(a,size): ## max_so_far = -maxint - 1 ## max_ending_here = 0 ## ## for i in range(0, size): ## max_ending_here = max_ending_here + a[i] ## if (max_so_far < max_ending_here): ## max_so_far = max_ending_here ## ## if max_ending_here < 0: ## max_ending_here = 0 ## return max_so_far ##def power(x, y): ## if(y == 0):return 1 ## temp = power(x, int(y / 2))%mo ## if (y % 2 == 0):return (temp * temp)%mo ## else: ## if(y > 0):return (x * temp * temp)%mo ## else:return ((temp * temp)//x )%mo
8
PYTHON3
from functools import reduce import collections import math import sys class Read: @staticmethod def string(): return input() @staticmethod def int(): return int(input()) @staticmethod def list(delimiter=' '): return input().split(delimiter) @staticmethod def list_int(delimiter=' '): return list(map(int, input().split(delimiter))) # infilename = 'input.txt' # sys.stdin = open(infilename, 'r') # outfilename = 'output.txt' # sys.stdout = open(outfilename, 'w') def main(): t = Read.int() for _ in range(t): n = Read.int() a = list() for i in range(n): a.append(list(Read.string())) if (a[0][1] == a[1][0] and a[1][0] == a[n-1][n-1-1] and a[n-1][n-1-1] == a[n-1-1][n-1]): print("2", "1 2", "2 1", sep= '\n') elif (a[0][1] == a[1][0] and a[1][0] != a[n-1][n-1-1] and a[n-1][n-1-1] == a[n-1-1][n-1]): print(0) else: kk = {'1': [], '0': []} if a[0][1] == '1': kk['1'].append("2 1") else: kk['0'].append("2 1") if a[1][0] == '1': kk['1'].append("1 2") else: kk['0'].append("1 2") if a[n-1][n-1-1] == '1': kk['1'].append(str(n-1)+" "+str(n)) else: kk['0'].append(str(n-1)+" "+str(n)) if a[n-1-1][n-1] == '1': kk['1'].append(str(n)+" "+str(n-1)) else: kk['0'].append(str(n)+" "+str(n-1)) # print(kk) if (len(kk['1']) == len(kk['0'])): print(2) print("2 1") if (a[0][1] != a[n-1][n-1-1]): print(str(n-1)+" "+str(n)) else: print(str(n)+" "+str(n-1)) else: print(1) if (len(kk['1']) < len(kk['0'])): print(kk['1'][0]) else: print(kk['0'][0]) if __name__ == '__main__': main()
8
PYTHON3
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline M = mod = 998244353 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split()] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n')] def li3():return [int(i) for i in input().rstrip('\n')] for _ in range(val()): n = val() l = [] for i in range(n):l.append(list(st())) ans = [] if l[0][1] == l[1][0]: if l[-1][-2] == l[0][1]: ans.append([n - 1, n - 2]) if l[-2][-1] == l[0][1]: ans.append([n - 2, n - 1]) else: if l[-1][-2] == l[-2][-1]: if l[-1][-2] == l[0][1]: ans.append([0, 1]) else: ans.append([1, 0]) else: if l[-1][-2] == l[0][1]: ans.append([0, 1]) ans.append([n - 2, n - 1]) else: ans.append([1, 0]) ans.append([n - 2, n - 1]) print(len(ans)) for i in ans: print(i[0] + 1, i[1] + 1)
8
PYTHON3
from collections import defaultdict T = int(input()) ans_c = [0] * T ans_ls = [0] * T for t in range(T): N = int(input()) grid = [list(input()) for i in range(N)] d = defaultdict(int) ls = [] for y,x in [[0,1],[1,0],[N-1,N-2],[N-2,N-1]]: d[grid[y][x]] += 1 same_on_left = True if grid[0][1] != grid[1][0]: same_on_left = False same_on_right = True if grid[-1][-2] != grid[-2][-1]: same_on_right = False if len(d) == 1: ans_c[t] = 2 ans_ls[t] = [[1,2],[2,1]] continue # 二種類 # まず、0と1が2ずつの時 if d['0'] == d['1']: if same_on_left and same_on_right: ans_c[t] = 0 else: ans_c[t] = 2 for y,x in [[0,1],[1,0]]: if grid[y][x] == '0': ls.append([y+1,x+1]) for y,x in [[N-1,N-2],[N-2,N-1]]: if grid[y][x] == '1': ls.append([y+1,x+1]) # 3と1の時 else: ans_c[t] = 1 if same_on_left: right = str(int(grid[0][1])^1) for y,x in [[N-1,N-2],[N-2,N-1]]: if grid[y][x] != right: ls.append([y+1,x+1]) else: left = str(int(grid[N-1][N-2])^1) for y,x in [[0,1],[1,0]]: if grid[y][x] != left: ls.append([y+1,x+1]) ans_ls[t] = ls #print("ans_ls",ans_ls) for t in range(T): print(ans_c[t]) for ans in ans_ls[t]: print(*ans)
8
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) m = [] for _ in range(n): m.append(input()) a, b, c, d = m[1][0], m[0][1], m[-1][-2], m[-2][-1] t = 8*int(a) + 4*int(b) + 2*int(c) + 1*int(d) a2 = 0 if t in [0, 15]: a2 = [(1,2),(2,1)] elif t in [1, 14]: a2 = [(n, n-1)] elif t in [2, 13]: a2 = [(n-1, n)] elif t in [3, 12]: a2 = [] elif t in [4,11]: a2 = [(2,1)] elif t in [5, 10]: a2 = [(2,1),(n-1,n)] elif t in [6, 9]: a2 = [(2,1),(n,n-1)] elif t in [7, 8]: a2 = [(1,2)] print(len(a2)) for e in a2: print(e[0], e[1])
8
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) arr = input() # print(arr) el12 = int(arr[1]) arr = input() # print(arr) el21 = int(arr[0]) if n == 3: el34 = int(arr[n-1]) arr = input() el43 = int(arr[n-2]) else: for _ in range(n-4): arr = input() # print(arr) arr = input() # print(arr) el34 = int(arr[n-1]) arr = input() # print(arr) el43 = int(arr[n-2]) need = 2 - abs((el12+ el21)- (el34+ el43)) print(need) if need == 2: if el12 == el21 == el34== el43: print(1,2) print(2,1) else: if el12 !=1 : print(1,2) if el21 !=1 : print(2,1) if el34 != 0: print(n-1, n) if el43 != 0: print(n, n-1) if need == 1: if el12 == el21: if el34 == el12: print(n-1, n) else: print(n, n-1) else: if el12 == el34: print(1, 2) else: print(2,1) """ End of code 70 lines. """
8
PYTHON3
for _ in range(int(input())): n =int(input()) s = [] for i in range(n): temp = input() s.append([i for i in temp]) cnt1 = 0 ans1 = [] cnt2 = 0 ans2 = [] if(s[0][1] == "0"): pass else: cnt1+=1 ans1.append([1,2]) if(s[1][0] == "0"): pass else: cnt1+=1 ans1.append([2,1]) if(s[-1][-2] == "1"): pass else: cnt1+=1 ans1.append([n,n-1]) if(s[-2][-1] == "1"): pass else: cnt1+=1 ans1.append([n-1,n]) if(s[0][1] == "1"): pass else: cnt2+=1 ans2.append([1,2]) if(s[1][0] == "1"): pass else: cnt2+=1 ans2.append([2,1]) if(s[-1][-2] == "0"): pass else: cnt2+=1 ans2.append([n,n-1]) if(s[-2][-1] == "0"): pass else: cnt2+=1 ans2.append([n-1,n]) if(cnt1<=2): print(cnt1) for i in ans1: print(*i) else: print(cnt2) for i in ans2: print(*i)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; char g[n][n]; bool vis[n][n]; for (int i(0); i < n; i++) { for (int j(0); j < n; j++) { cin >> g[i][j]; vis[i][j] = false; } } if ((g[0][1] == g[1][0])) { cout << ((g[n - 1][n - 2] == g[0][1] ? 1 : 0) + (g[n - 2][n - 1] == g[0][1] ? 1 : 0)) << "\n"; if (g[n - 1][n - 2] == g[0][1]) { cout << n << " " << n - 1 << "\n"; } if (g[n - 2][n - 1] == g[0][1]) { cout << n - 1 << " " << n << "\n"; } continue; } if ((g[n - 1][n - 2] == g[n - 2][n - 1])) { cout << ((g[n - 1][n - 2] == g[0][1] ? 1 : 0) + (g[n - 1][n - 2] == g[1][0] ? 1 : 0)) << "\n"; if (g[n - 1][n - 2] == g[0][1]) { cout << "1 2\n"; } if (g[n - 1][n - 2] == g[1][0]) { cout << "2 1\n"; } continue; } cout << "2\n"; cout << "1 2\n"; if (g[n - 1][n - 2] == g[1][0]) { cout << n << " " << n - 1 << "\n"; } else { cout << n - 1 << " " << n << "\n"; } } return 0; }
8
CPP
t=int(input()) for q in range(0,t): n=int(input()) a=[] for i in range(0,n): a.append(list(input())) ans=[] if a[0][1]==a[1][0] and a[n-1][n-2]==a[n-2][n-1] and a[0][1]!=a[n-1][n-2]: print(0) elif a[0][1]==a[1][0] and a[n-1][n-2]!=a[n-2][n-1]: #print("h") if a[0][1]=='1' and a[n-1][n-2]=='1': print(1) print(n,n-1) elif a[0][1]=='1' and a[n-2][n-1]=='1': print(1) print(n-1,n) elif a[0][1]=='0' and a[n-1][n-2]=='0': print(1) print(n,n-1) elif a[0][1]=='0' and a[n-2][n-1]=='0': print(1) print(n-1,n) elif a[0][1] != a[1][0] and a[n - 1][n - 2] == a[n - 2][n - 1]: if a[0][1] == '1' and a[n - 1][n - 2] == '1': print(1) print(1,2) elif a[0][1] == '0' and a[n - 1][n - 2] == '0': print(1) print(1, 2) elif a[1][0] == '1' and a[n - 1][n - 2] == '1': print(1) print(2, 1) elif a[1][0] == '0' and a[n - 1][n - 2] == '0': print(1) print(2, 1) elif a[0][1]==a[1][0] and a[n-1][n-2]==a[n-2][n-1] and a[0][1]==a[n-1][n-2]: print(2) print(1,2) print(2,1) else: print(2) if a[0][1]!=a[n-1][n-2]: print(1,2) print(n,n-1) elif a[0][1]!=a[n-2][n-1]: print(1,2) print(n-1,n) elif a[1][0]!=a[n-1][n-2]: print(2,1) print(n,n-1) else: print(2,1) print(n-1,n)
8
PYTHON3
for tc in range(int(input())): n = int(input()) mat = [] for i in range(n): mat.append(input()) # print(mat) x = mat[0][1] y = mat[1][0] p = mat[-1][-2] q = mat[-2][-1] if (x == y) and (p == q): if x == p: print('2') print('1 2') print('2 1') else: print('0') elif x == y: print('1') if p != x: print(n-1, n) elif q != x: print(n, n-1) elif p == q: print('1') if x != p: print('2 1') elif y != p: print('1 2') else: print('2') if x == p: print('1 2') print(n-1, n) else: print('1 2') print(n, n-1)
8
PYTHON3
T=int(input()) for _ in range(T): n=int(input()) matrix=[] for i in range(n): s=input() matrix.append(s) #print(matrix) if matrix[0][1]!=matrix[1][0] and matrix[n-2][n-1]!=matrix[n-1][n-2]: lis=[] if matrix[0][1]=='0': lis.append([1,2]) else: lis.append([2,1]) if matrix[n-2][n-1]=='1': lis.append([n-1,n]) else: lis.append([n,n-1]) print(2) for i in lis: print(*i) elif matrix[0][1]==matrix[1][0] and matrix[n-2][n-1]!=matrix[n-1][n-2]: print(1) if matrix[0][1]!=matrix[n-1][n-2]: print(n-1,n) else: print(n,n-1) elif matrix[0][1]!=matrix[1][0] and matrix[n-2][n-1]==matrix[n-1][n-2]: print(1) if matrix[0][1]!=matrix[n-1][n-2]: print(2,1) else: print(1,2) elif matrix[0][1]==matrix[1][0] and matrix[n-2][n-1]==matrix[n-1][n-2] and matrix[0][1]==matrix[n-2][n-1]: print(2) print(1,2) print(2,1) elif matrix[0][1]==matrix[1][0] and matrix[n-2][n-1]==matrix[n-1][n-2] and matrix[0][1]!=matrix[n-2][n-1]: print(0)
8
PYTHON3
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) grid = [[]] for i in range(n): line = ' '+input().strip('\n') grid.append(line) a,b = grid[1][2], grid[2][1] x,y = grid[n][n-1], grid[n-1][n] if a == b and x == y and x!=a: print(0) continue if x == a == b == y: print(2) print(1,2) print(2,1) continue ans = [] if a != b: if x == y: if b != x: a = b ans.append((1,2)) else: b = a ans.append((2,1)) else: a = b ans.append((1,2)) if x == a: ans.append((n,n-1)) elif y == b: ans.append((n-1,n)) print(len(ans)) for i in ans: print(*i)
8
PYTHON3
for _ in range(int(input())): n=int(input()) l=[] for i in range(n): l.append(input()) a=l[1][0];b=l[0][1];c=l[n-2][n-1];d=l[n-1][n-2] if a==b and c==d: if a==c: print(2);print(1,2);print(2,1) else: print(0) elif a==b: if a==c: print(1);print(n-1,n) else: print(1);print(n,n-1) elif c==d: if a==c: print(1);print(2,1) else: print(1);print(1,2) else: if a==c: print(2);print(2,1);print(n,n-1) else: print(2);print(2,1);print(n-1,n)
8
PYTHON3
for _ in range(int(input())): n = int(input()) grid = [[*input()] for _ in range(n)] sr = grid[0][1] sd = grid[1][0] fu = grid[-2][-1] fl = grid[-1][-2] if (sr == sd == '0' and fu == fl == '1') or (sr == sd == '1' and fu == fl == '0'): print(0) elif sr == sd and fu != fl: print(1) if fu == sr: print(n - 1, n) else: print(n, n - 1) elif fu == fl and sr != sd: print(1) if sr == fu: print(1, 2) else: print(2, 1) elif sr == sd == fu == fl: print(2) print(1, 2) print(2, 1) elif sr != sd and fu != fl: print(2) if sr == '1': print(1, 2) else: print(2, 1) if fu == '0': print(n - 1, n) else: print(n, n - 1)
8
PYTHON3
def one_indexed(pair): return [item + 1 for item in pair] t = int(input()) for i in range(t): n = int(input()) grid = [[int(i) if (i != "S" and i != "F") else i for i in list(input())] for j in range(n)] doors = [(0, 1), (1, 0), (n - 2, n - 1), (n - 1, n - 2)] door_vals = [grid[i][j] for i, j in doors] sr, sd, fu, fl = door_vals if sr == sd and fu == fl and sr != fu: print(0) else: if door_vals.count(0) == 1: diff_ind = door_vals.index(0) elif door_vals.count(1) == 1: diff_ind = door_vals.index(1) else: diff_ind = None if not(diff_ind is None): print(1) get_other = { 0: 1, 1: 0, 2: 3, 3: 2, } change_ind = get_other[diff_ind] change = doors[change_ind] print(*one_indexed(change)) else: print(2) if len(set(door_vals)) == 1: for coords in [doors[0], doors[1]]: print(*one_indexed(coords)) elif (sr in (fu, fl)) and (sd in (fu, fl)) and fu != fl: changes = [doors[i] for i in range(4) if (i <= 1 and door_vals[i] == 0) or (i >= 2 and door_vals[i] == 1)] for coords in changes: print(*one_indexed(coords))
8
PYTHON3
for _ in range(int(input())): n = int(input()) arr = [] for i in range(n): arr.append(input()) if arr[1][0] == "0" and arr[0][1] == "0" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "0": print(2) print(1,2) print(2,1) elif arr[1][0] == "0" and arr[0][1] == "0" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "1": print(1) print(n,n-1) elif arr[1][0] == "0" and arr[0][1] == "0" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "0": print(1) print(n-1,n) elif arr[1][0] == "0" and arr[0][1] == "0" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "1": print(0) elif arr[1][0] == "0" and arr[0][1] == "1" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "0": print(1) print(2,1) elif arr[1][0] == "0" and arr[0][1] == "1" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "1": print(2) print(1,2) print(n,n-1) elif arr[1][0] == "0" and arr[0][1] == "1" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "0": print(2) print(2,1) print(n,n-1) elif arr[1][0] == "0" and arr[0][1] == "1" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "1": print(1) print(1,2) elif arr[1][0] == "1" and arr[0][1] == "0" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "0": print(1) print(1,2) elif arr[1][0] == "1" and arr[0][1] == "0" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "1": print(2) print(2,1) print(n,n-1) elif arr[1][0] == "1" and arr[0][1] == "0" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "0": print(2) print(2,1) print(n-1,n) elif arr[1][0] == "1" and arr[0][1] == "0" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "1": print(1) print(2,1) elif arr[1][0] == "1" and arr[0][1] == "1" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "0": print(0) elif arr[1][0] == "1" and arr[0][1] == "1" and arr[n-1][n-2] == "0" and arr[n-2][n-1] == "1": print(1) print(n-1,n) elif arr[1][0] == "1" and arr[0][1] == "1" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "0": print(1) print(n,n-1) elif arr[1][0] == "1" and arr[0][1] == "1" and arr[n-1][n-2] == "1" and arr[n-2][n-1] == "1": print(2) print(1,2) print(2,1)
8
PYTHON3
for _ in range(int(input())): n=int(input()) arr=[] for i in range(n): s=input() arr.append(s) if (arr[0][1]=='0' and arr[1][0]=='0' and arr[n-1][n-2]=='1' and arr[n-2][n-1]=='1') or (arr[0][1]=='1' and arr[1][0]=='1' and arr[n-1][n-2]=='0' and arr[n-2][n-1]=='0'): print('0') else: if arr[0][1]==arr[1][0] and arr[n-1][n-2]==arr[n-2][n-1] and arr[n-1][n-2]==arr[0][1]: print('2') print('1','2') print('2','1') elif arr[0][1]==arr[1][0]: if arr[n-1][n-2]==arr[0][1]: print('1') print(n,n-1) else: print('1') print(n-1,n) elif arr[n-1][n-2]==arr[n-2][n-1]: if arr[n-1][n-2]==arr[0][1]: print('1') print('1','2') else: print('1') print('2','1') else: if arr[1][0]==arr[n-2][n-1]: print('2') print('2','1') print(n,n-1) elif arr[0][1]==arr[n-2][n-1]: print('2') print('1','2') print(n,n-1)
8
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) l=[] for i in range(n): s=str(input()) l.append(s) try: a,b,c,d=l[0][1],l[1][0],l[n-1][n-2],l[n-2][n-1] except: print(l) break arr=[a,b,c,d] temp=["1","1","0","0"] key=["0","0","1","1"] tempc=0 keyc=0 d={} d[0]=[0,1] d[1]=[1,0] d[2]=[n-1,n-2] d[3]=[n-2,n-1] temparr=[] keyarr=[] for i in range(4): if(arr[i]!=temp[i]): tempc+=1 temparr.append(i) if(arr[i]!=key[i]): keyc+=1 keyarr.append(i) #print(temparr,keyarr) if(tempc<keyc): print(len(temparr)) for j in temparr: print(d[j][0]+1,d[j][1]+1) else: print(len(keyarr)) for j in keyarr: print(d[j][0]+1,d[j][1]+1)
8
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) up=[-1,-1] down=[-1,-1] for i in range(n): s=input() if i==0: up[0]=s[1] elif i==1: up[1]=s[0] if i==n-2: down[0]=s[-1] elif i==n-1: down[1]=s[-2] if up[0]==up[1] and down[0]==down[1]: if up[0]==down[0]: print(2) print(1,2) print(2,1) else: print(0) elif up[0]==up[1]: if down[0]==up[0]: print(1) print(n-1,n) else: print(1) print(n,n-1) elif down[0]==down[1]: if down[0]==up[0]: print(1) print(1,2) else: print(1) print(2,1) else: print(2) print(1,2) if up[0]!=down[0]: print(n-1,n) else: print(n,n-1)
8
PYTHON3
# 改行でバグる時 import sys input = lambda: sys.stdin.readline().rstrip() t=int(input()) g=[] for i in range(t): a=int(input()) x=[] for j in range(a): b=list(input()) x.append(b) g.append(x) ans=[] for i in range(t): v=len(g[i]) x1=g[i][0][1] x2=g[i][1][0] y1=g[i][v-2][v-1] y2=g[i][v-1][v-2] if x1=='1' and x2=='1': if y1=='1' and y2=='1': ans.append([2,[v-1,v],[v,v-1]]) elif y1=='1' and y2=='0': ans.append([1,[v-1,v]]) elif y1=='0' and y2=='1': ans.append([1,[v,v-1]]) else: ans.append(0) elif x1=='1' and x2=='0': if y1=='1' and y2=='1': ans.append([1,[1,2]]) elif y1=='1' and y2=='0': ans.append([2,[2,1],[v-1,v]]) elif y1=='0' and y2=='1': ans.append([2,[2,1],[v,v-1]]) else: ans.append([1,[2,1]]) elif x1=='0' and x2=='1': if y1=='1' and y2=='1': ans.append([1,[2,1]]) elif y1=='1' and y2=='0': ans.append([2,[1,2],[v-1,v]]) elif y1=='0' and y2=='1': ans.append([2,[1,2],[v,v-1]]) else: ans.append([1,[1,2]]) else: if y1=='1' and y2=='1': ans.append(0) elif y1=='1' and y2=='0': ans.append([1,[v,v-1]]) elif y1=='0' and y2=='1': ans.append([1,[v-1,v]]) else: ans.append([2,[1,2],[2,1]]) for i in range(t): if ans[i]!=0: x = ans[i][0] print(x) for j in range(x): print(*ans[i][j + 1]) else: print(0)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { int n; cin >> n; vector<string> grid(n); for (auto &it : grid) cin >> it; vector<array<int, 2>> pos = { {0, 1}, {1, 0}, {n - 1, n - 2}, {n - 2, n - 1}}; array<char, 4> an1 = {'0', '0', '1', '1'}, an2 = {'1', '1', '0', '0'}; int cnt = 0; for (int i = 0; i < 4; ++i) if (an1[i] != grid[pos[i][0]][pos[i][1]]) { ++cnt; } if (cnt <= 2) { cout << cnt << '\n'; for (int i = 0; i < 4; ++i) if (an1[i] != grid[pos[i][0]][pos[i][1]]) { cout << pos[i][0] + 1 << ' ' << pos[i][1] + 1 << '\n'; } continue; } cnt = 0; for (int i = 0; i < 4; ++i) if (an2[i] != grid[pos[i][0]][pos[i][1]]) { ++cnt; } if (cnt <= 2) { cout << cnt << '\n'; for (int i = 0; i < 4; ++i) if (an2[i] != grid[pos[i][0]][pos[i][1]]) { cout << pos[i][0] + 1 << ' ' << pos[i][1] + 1 << '\n'; } continue; } } return 0; }
8
CPP
T=int(input()) for Tid in range(T): n=int(input()) a=[] for i in range(n): a.append(input()) a0,a1,a2=[],[],[] def add(x,y,c): if a[x][y]==c: a0.append([x+1,y+1]) else: a1.append([x+1,y+1]) add(0,1,'0') add(1,0,'0') add(n-1,n-2,'1') add(n-2,n-1,'1') if len(a0)<len(a1): a2=a0 else: a2=a1 print(len(a2)) for i in range(len(a2)): print(a2[i][0],a2[i][1])
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { int i, j, k, m, n, l, res = 0, zero = 0, one = 0, a, b, c, d; cin >> n; vector<pair<int, int> > v; string s[n + 1]; for (i = 0; i < n; i++) { cin >> s[i]; } a = s[0][1]; b = s[1][0]; c = s[n - 2][n - 1]; d = s[n - 1][n - 2]; if (a == b) { if (c == d) { if (c != b) cout << 0 << '\n'; else { cout << 2 << '\n'; cout << 1 << ' ' << 2 << '\n'; cout << 2 << ' ' << 1 << '\n'; } } else { cout << 1 << '\n'; if (c == a) cout << n - 1 << ' ' << n << '\n'; else cout << n << ' ' << n - 1 << '\n'; } } else if (c == d) { cout << 1 << '\n'; if (a == c) cout << 1 << ' ' << 2 << '\n'; else cout << 2 << ' ' << 1 << '\n'; } else { cout << 2 << '\n'; cout << 1 << ' ' << 2 << '\n'; if (b == c) cout << n - 1 << ' ' << n << '\n'; else cout << n << ' ' << n - 1 << '\n'; } } }
8
CPP
m = { "0000": (3, 4), "0001": (3,), "0010": (4,), "0011": (), "0100": (1,), "0101": (2, 3), "0110": (2, 4), "0111": (2,), "1000": (2,), "1001": (2, 4), "1010": (2, 3), "1011": (1,), "1100": (), "1101": (4,), "1110": (3,), "1111": (3, 4) } t = int(input()) for _ in range(t): n = int(input()) f = [list(input()) for _ in range(n)] c11 = f[0][1] c12 = f[1][0] c21 = f[-2][-1] c22 = f[-1][-2] s = f"{c11}{c12}{c21}{c22}" r = m[s] print(len(r)) for rr in r: if (rr == 1): print("1 2") elif (rr == 2): print("2 1") elif (rr == 3): print(f"{n - 1} {n}") else: print(f"{n} {n - 1}")
8
PYTHON3
for x in range(int(input())): n = int(input()) l=[] for i in range(n): s=input() l.append(s) if l[n-1][n-2]!=l[n-2][n-1]: if l[1][0]==l[0][1]: print(1) if l[n-1][n-2]==l[1][0]: print(n,n-1) else: print(n-1,n) else: print(2) if l[n-1][n-2]!=l[1][0]: print(n,n-1) print(2,1) else: print(n,n-1) print(1,2) else: if l[1][0]==l[0][1]: if l[n-1][n-2]!=l[1][0]: print(0) else: print(2) print(2,1) print(1,2) else: if l[n-1][n-2]!=l[1][0]: print(1) print(1,2) else: print(1) print(2,1)
8
PYTHON3
from collections import Counter, OrderedDict from itertools import permutations as perm from sys import setrecursionlimit from collections import deque from sys import stdin from bisect import * from heapq import * import threading import math g = lambda : stdin.readline().strip() gl = lambda : g().split() gil = lambda : [int(var) for var in gl()] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] mod = int(1e9)+7 inf = float("inf") t, = gil() for _ in range(t): n, = gil() mat = [] for _ in range(n): mat.append(g()) start, end = [mat[1][0], mat[0][1]], [mat[-1][-2], mat[-2][-1]] start.sort();end.sort() if start == end and start[0] == start[1]:# 00 11 print(2) print(0+1, 1+1) print(1+1, 0+1) elif start == end and start[0] != start[1]: # 01 10 print(2) print(0+1, 1+1) state = mat[0][1] if state != mat[n-1][n-2]: print(n-1+1, n-2+1) else: print(n-2+1, n-1+1) elif start[0] == start[1] and end[0] == end[1] and start[0] != end[0]: # 00/11; print(0) elif start[0] == start[1]: # 00/11 ; 01 10 state = mat[0][1] print(1) if state == mat[n-1][n-2]: print(n-1+1, n-2+1) else: print(n-2+1, n-1+1) elif end[0] == end[1]: state = mat[n-1][n-2] print(1) if state == mat[0][1]: print(0+1, 1+1) else: print(1+1, 0+1) else: print(0)
8
PYTHON3
for _ in range(int(input())): n = int(input()) grid = [] ans = 0 for i in range(n): grid.append([j for j in str(input())]) # print(grid) first_left = [1,2] col_ah = [2,1] last_right = [n,n-1] col_pre = [n-1,n] ans = [] if grid[0][1] == grid[n-1][n-2] == grid[1][0] == grid[n-2][n-1] : ans.append(last_right) ans.append(col_pre) elif grid[0][1] == grid[1][0] and grid[n-1][n-2] == grid[n-2][n-1] and grid[n-2][n-1] != grid[0][1] : pass elif grid[0][1] != grid[1][0] and grid[n-1][n-2] != grid[n-2][n-1] : if grid[0][1] != grid[n-1][n-2] : ans = [first_left, last_right] else : ans = [first_left, col_pre] elif grid[0][1] == grid[1][0] and grid[n-1][n-2] != grid[n-2][n-1] : if grid[0][1] == grid[n-1][n-2] : ans = [last_right] else : ans = [col_pre] elif grid[0][1] != grid[1][0] and grid[n-1][n-2] == grid[n-2][n-1] : if grid[0][1] == grid[n-1][n-2] : ans = [first_left] else : ans = [col_ah] print(len(ans)) for z in ans : print(" ".join([str(b) for b in z]))
8
PYTHON3
import queue t = int(input()) while (t): n = int(input()) grid = [] for i in range(n): grid.append(input()) ans = [] if (grid[0][1] == grid[1][0]): if (grid[-1][-2] == grid[0][1]): ans.append((n,n-1)) if (grid[-2][-1] == grid[0][1]): ans.append((n-1,n)) elif (grid[-1][-2] == grid[-2][-1]): if (grid[0][1] == grid[-1][-2]): ans.append((1,2)) if (grid[1][0] == grid[-1][-2]): ans.append((2,1)) else: if (grid[0][1] != grid[-1][-2]): ans = [(1,2),(n,n-1)] elif (grid[0][1] != grid[-2][-1]): ans = [(1,2),(n-1,n)] elif (grid[1][0] != grid[-1][-2]): ans = [(2,1),(n,n-1)] else: ans = [(2,1),(n-1,n)] print(len(ans)) for a in ans: print(a[0],a[1]) t -= 1
8
PYTHON3
for _ in range(int(input())): n = int(input()) l = [] for i in range(n): l.append(list(input())) e = [l[0][1] , l[1][0] , l[n-2][n-1] , l[n-1][n-2]] pos = [[1,2] , [2,1] , [n-1,n] , [n,n-1]] moves = 0 ans = [] if(e[0] == e[1]): if(e[2] == e[3]): if(e[0] == e[2]): moves = 2 ans.append(pos[2]) ans.append(pos[3]) else: moves = 0 else: moves = 1 if(e[2] == e[0]): ans.append(pos[2]) else: ans.append(pos[3]) else: if(e[2] == e[3]): moves = 1 if(e[0] == e[2]): ans.append(pos[0]) else: ans.append(pos[1]) else: moves = 2 ans.append(pos[1]) if(e[2] == e[0]): ans.append(pos[2]) else: ans.append(pos[3]) print(moves) for i in ans: print(*i)
8
PYTHON3
t=int(input()) i=1 while(i<=t): n = int(input()) mat = [] for j in range(n): mat.append(list(input())) # print(mat) # print(mat[0]) x1 = int(mat[0][1]) x2 = int(mat[1][0]) x3 = int(mat[n-1][n-2]) x4 = int(mat[n-2][n-1]) s1 = x1+x2 s2 = x3+x4 if(s1==0 and s2==1): print(1) if(x3==0): print(n,n-1) if(x4==0): print(n-1,n) elif(s1==1 and s2==1): print(2) if(x1==1): print(1,2) if(x2==1): print(2,1) if(x3==0): print(n,n-1) if(x4==0): print(n-1,n) elif(s1==2 and s2==1): print(1) if(x3==1): print(n,n-1) if(x4==1): print(n-1,n) elif(s1==1 and s2==0): print(1) if(x1==0): print(1,2) if(x2==0): print(2,1) elif(s1==0 and s2==0): print(2) print(1,2) print(2,1) elif(s1==2 and s2==2): print(2) print(1,2) print(2,1) elif(s1==1 and s2==2): print(1) if(x1==1): print(1,2) if(x2==1): print(2,1) else: print(0) i+=1
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) def input(): return sys.stdin.readline().rstrip("\r\n") # n = int(input()) # li = [int(i) for i in input().split()] # s = sorted(li) t = int(input()) for ti in range(t): li = [] n = int(input()) for ni in range(n): s = list(input()) li.append(s) ans = [] if li[0][1] == li[1][0]: if li[1][1] == li[0][2] == li[2][0]: if li[1][1] == li[0][1]: ans.append((0,1)) ans.append((1,0)) else: pass else: for x,y in [(1,1),(0,2),(2,0)]: if li[x][y] == li[0][1]: ans.append((x,y)) else: if li[1][1] == li[0][2] == li[2][0]: if li[1][1] == li[0][1]: ans.append((0,1)) else: ans.append((1,0)) else: d = {} for x,y in [(1,1),(0,2),(2,0)]: d[li[x][y]] = d.get(li[x][y],0) + 1 if d['0'] > d['1']: for x,y in [(1,1),(0,2),(2,0)]: if li[x][y] == '1': ans.append((x,y)) li[x][y] = '0' else: for x,y in [(1,1),(0,2),(2,0)]: if li[x][y] == '0': ans.append((x,y)) li[x][y] = '1' for x,y in [(0,1),(1,0)]: if li[x][y] == li[1][1]: ans.append((x,y)) print(len(ans)) for x,y in ans: print(x+1, y+1)
8
PYTHON3
t = int(input()) for i in range(t): n = list(map(int, input().split()))[0] l = [2, 2, 2, 2] cords = [ (1, 2), (2, 1), (n - 1, n), (n, n - 1) ] for j in range(n): s = input() if j == 0: l[0] = int(s[1]) if j == 1: l[1] = int(s[0]) if j == n - 2: l[2] = int(s[n - 1]) if j == n - 1: l[3] = int(s[n - 2]) sum1 = l[0] + l[1] sum2 = l[2] + l[3] if sum1 == 0: if sum2 == 2: print(0) elif sum2 == 1: if l[2] == 0: print('1\n{} {}'.format(*cords[2])) else: print('1\n{} {}'.format(*cords[3])) else: print('2\n{} {}\n{} {}'.format(*cords[2], *cords[3])) elif sum1 == 2: if sum2 == 0: print(0) elif sum2 == 1: if l[2] == 0: print('1\n{} {}'.format(*cords[3])) else: print('1\n{} {}'.format(*cords[2])) else: print('2\n{} {}\n{} {}'.format(*cords[2], *cords[3])) else: if sum2 == 1: if l[0] == l[2]: print('2\n{} {}\n{} {}'.format(*cords[0], *cords[3])) else: print('2\n{} {}\n{} {}'.format(*cords[0], *cords[2])) elif sum2 == 0: if l[0] == 0: print('1\n{} {}'.format(*cords[0])) else: print('1\n{} {}'.format(*cords[1])) else: if l[0] == 1: print('1\n{} {}'.format(*cords[0])) else: print('1\n{} {}'.format(*cords[1]))
8
PYTHON3
import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools,itertools,operator,bisect,fractions,statistics from collections import deque,defaultdict,OrderedDict,Counter from fractions import Fraction from decimal import Decimal from sys import stdout def main(): starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") for _ in range(ri()): n=ri() z=[] for i in range(n): z.append(list(rs())) a,b,c,d=z[0][1],z[1][0],z[n-1][n-2],z[n-2][n-1] if a==b and c==d and a!=c: print(0) else: no=0 z1=['0','0','1','1'] z2=['1','1','0','0'] abcd=[a,b,c,d] i1=0 i2=0 ind1=[] ind2=[] for i in range(4): if z1[i]!=abcd[i]: i1+=1 ind1.append(i) if z2[i]!=abcd[i]: i2+=1 ind2.append(i) if i1<=2: print(i1) for i in ind1: if i==0: print(1,2) if i==1: print(2,1) if i==2: print(n,n-1) if i==3: print(n-1,n) else: print(i2) for i in ind2: if i==0: print(1,2) if i==1: print(2,1) if i==2: print(n,n-1) if i==3: print(n-1,n) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) 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, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) 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() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main()
8
PYTHON3
# region fastio # from https://codeforces.com/contest/1333/submission/75948789 import sys, io, os BUFSIZE = 8192 class FastIO(io.IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = io.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(io.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() sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #endregion T = int(input()) for _ in range(T): N = int(input()) S = [input() for _ in range(N)] Ans1 = [] Ans2 = [] if S[0][1]=="1": Ans1.append((1, 2)) else: Ans2.append((1, 2)) if S[1][0]=="1": Ans1.append((2, 1)) else: Ans2.append((2, 1)) if S[-2][-1]=="0": Ans1.append((N-1, N)) else: Ans2.append((N-1, N)) if S[-1][-2]=="0": Ans1.append((N, N-1)) else: Ans2.append((N, N-1)) if len(Ans1) <= 2: Ans = Ans1 else: Ans = Ans2 print(len(Ans)) for a, b in Ans: print(a, b)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long INF64 = 1e18; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int chu; cin >> chu; while (chu--) { long long int n; cin >> n; string s[n]; for (long long int i = 0; i < n; i++) cin >> s[i]; if (s[0][1] == s[1][0]) { int ct = 0; vector<pair<int, int> > v; char c = s[0][1]; if (s[n - 1][n - 2] == c) { ct++; v.push_back({n, n - 1}); } if (s[n - 2][n - 1] == c) { ct++; v.push_back({n - 1, n}); } cout << ct << "\n"; for (long long int i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second << "\n"; } else if (s[n - 2][n - 1] == s[n - 1][n - 2]) { int ct = 0; vector<pair<int, int> > v; char c = s[n - 1][n - 2]; if (s[0][1] == c) { ct++; v.push_back({1, 2}); } if (s[1][0] == c) { ct++; v.push_back({2, 1}); } cout << ct << "\n"; for (long long int i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second << "\n"; } else { cout << 2 << "\n"; if (s[0][1] != '0') { cout << 1 << " " << 2 << "\n"; } else { cout << 2 << " " << 1 << "\n"; } if (s[n - 1][n - 2] != '1') { cout << n << " " << n - 1 << "\n"; } else { cout << n - 1 << " " << n << "\n"; } } } return 0; }
8
CPP
import sys import math import bisect from sys import stdin, stdout from math import gcd, floor, sqrt, log2, ceil from collections import defaultdict as dd from bisect import bisect_left as bl, bisect_right as br from bisect import insort from collections import Counter from collections import deque from heapq import heappush,heappop,heapify from itertools import permutations,combinations mod = int(1e9)+7 ip = lambda : int(stdin.readline()) inp = lambda: map(int,stdin.readline().split()) ips = lambda: stdin.readline().rstrip() out = lambda x : stdout.write(str(x)+"\n") t = ip() for _ in range(t): n = ip() arr = [] for i in range(n): arr.append(list(ips())) path = [arr[0][1],arr[1][0],arr[n-1][n-2],arr[n-2][n-1]] one = path.count("1") zero = 4-one ans = [] if one == 0 and zero == 4: ans.append([1,2]) ans.append([2,1]) elif one == 1 and zero == 3: if arr[0][1] == "0" and arr[1][0] == "0": if arr[n-1][n-2] == "0": ans.append([n,n-1]) else: ans.append([n-1,n]) else: if arr[0][1] == "0": ans.append([1,2]) else: ans.append([2,1]) elif one == 2 and zero == 2: if len(set([arr[0][1],arr[1][0]])) == 1: pass else: if arr[0][1] == "1": ans.append([1,2]) if arr[1][0] == "1": ans.append([2,1]) if arr[n-1][n-2] == "0": ans.append([n,n-1]) if arr[n-2][n-1] == "0": ans.append([n-1,n]) elif one == 3 and zero == 1: if arr[0][1] == "1" and arr[1][0] == "1": if arr[n-1][n-2] == "1": ans.append([n,n-1]) else: ans.append([n-1,n]) else: if arr[0][1] == "1": ans.append([1,2]) else: ans.append([2,1]) else: ans.append([1,2]) ans.append([2,1]) print(len(ans)) for i in ans: print(*i)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; char g[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> g[i][j]; } int a = 0, b = 1, c = n - 1, d = n - 2; if (g[a][b] == g[b][a] && g[c][d] == g[d][c]) { if (g[a][b] != g[c][d]) { cout << 0 << endl; } else { cout << 2 << endl; cout << 1 << " " << 2 << endl; cout << 2 << " " << 1 << endl; } } else if (g[a][b] == g[c][d] && g[b][a] == g[d][c]) { cout << 2 << endl; cout << 2 << " " << 1 << endl; cout << n << " " << n - 1 << endl; } else if (g[a][b] == g[d][c] && g[b][a] == g[c][d]) { cout << 2 << endl; cout << 1 << " " << 2 << endl; cout << n << " " << n - 1 << endl; } else if (g[a][b] == g[b][a]) { cout << 1 << endl; if (g[c][d] == g[a][b]) { cout << n << " " << n - 1 << endl; } else if (g[d][c] == g[a][b]) { cout << n - 1 << " " << n << endl; } } else if (g[c][d] = g[d][c]) { cout << 1 << endl; if (g[a][b] == g[c][d]) { cout << 1 << " " << 2 << endl; } else if (g[b][a] == g[c][d]) { cout << 2 << " " << 1 << endl; } } } int main() { int t; cin >> t; while (t--) solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; char a[n][n]; for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) cin >> a[i][j]; } vector<pair<long long, long long>> v; if (a[0][1] == a[1][0]) { if (a[n - 2][n - 1] == a[0][1]) v.push_back({n - 1, n}); if (a[n - 1][n - 2] == a[0][1]) v.push_back({n, n - 1}); } else { if (a[n - 2][n - 1] == a[n - 1][n - 2]) { if (a[n - 2][n - 1] == '0') { if (a[1][0] == '0') v.push_back({2, 1}); else v.push_back({1, 2}); } else { if (a[1][0] == '1') v.push_back({2, 1}); else v.push_back({1, 2}); } } else { if (a[1][0] == '1') v.push_back({2, 1}); else v.push_back({1, 2}); if (a[n - 2][n - 1] == '0') v.push_back({n - 1, n}); else if (a[n - 1][n - 2] == '0') v.push_back({n, n - 1}); } } cout << v.size() << "\n"; for (long long i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); } }
8
CPP
#include <bits/stdc++.h> using namespace std; void solve() { 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]; } char temp = arr[0][1]; vector<pair<int, int>> v; if (arr[1][0] != temp) v.push_back({2, 1}); if (arr[n - 1][n - 2] == temp) v.push_back({n, n - 1}); if (arr[n - 2][n - 1] == temp) v.push_back({n - 1, n}); if (v.size() >= 3) { v.clear(); v.push_back({1, 2}); } cout << v.size() << "\n"; for (auto i : v) cout << i.first << " " << i.second << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); }
8
CPP
# cook your dish here import math; for _ in range(int(input())): n=int(input()) x=[] for i in range(n): x.append(input()) a,b,c,d=x[1][0],x[0][1],x[n-1][n-2],x[n-2][n-1] if(a==b and c==d): if(a==c): print(2) print(1,2) print(2,1) else: print(0) elif(a==b): print(1) if(a=="1"): if(c=="1"): print(n,n-1) else: print(n-1,n) else: if(c=="0"): print(n,n-1) else: print(n-1,n) elif(d==c): print(1) if(c=="1"): if(a=="1"): print(2,1) else: print(1,2) else: if(a=="0"): print(2,1) else: print(1,2) else: print(2) if(a=="0"): print(2,1) else: print(1,2) if(c=="1"): print(n,n-1) else: print(n-1,n)
8
PYTHON3
import sys # sys.stdin = open("E:\\programming\\New folder\\input.txt", "r") # sys.stdout = open("E:\\programming\\New folder\\output.txt", "w") ans = 0 def fun(p, key1, key2): k = 0 global ans ans = 0 for i in range(1, -1, -1): if int(st[k][i]) != key1: ans += 1 p.append((k + 1, i + 1)) k += 1 k = n - 2 for i in range(n-1, n - 3, -1): if int(st[k][i]) != key2: ans += 1 p.append((k + 1, i + 1)) k += 1 t = int(input()) for tc in range(t): # a = list(map(int, input().split())) n = int(input()) st = list() for i in range(n): k = input() st.append(k) p = list() fun(p, 0, 1) if ans < 3: print(ans) for i in p: print(*i) continue p = list() fun(p, 1, 0) print(ans) for i in p: print(*i)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; char a[n][n]; for (long long i = 0; i < n; i++) for (long long j = 0; j < n; j++) cin >> a[i][j]; long long z = 0, o = 0, z2 = 0, o2 = 0; if (a[0][1] == '0') z++; if (a[1][0] == '0') z++; if (a[0][1] == '1') o++; if (a[1][0] == '1') o++; if (a[n - 1][n - 2] == '0') z2++; if (a[n - 2][n - 1] == '0') z2++; if (a[n - 1][n - 2] == '1') o2++; if (a[n - 2][n - 1] == '1') o2++; if (z == 2) { if (z2 == 2) { cout << 2 << endl; cout << "1 2" << endl << "2 1"; } else if (o2 == 2) cout << 0; else { cout << 1 << endl; if (a[n - 1][n - 2] == '0') cout << n << " " << n - 1; else cout << n - 1 << " " << n; } } else if (o == 2) { if (o2 == 2) { cout << 2 << endl; cout << "1 2" << endl << "2 1"; } else if (z2 == 2) cout << 0; else { cout << 1 << endl; if (a[n - 1][n - 2] == '1') cout << n << " " << n - 1; else cout << n - 1 << " " << n; } } else { if (z2 == 2) { cout << 1 << endl; if (a[0][1] == '0') cout << "1 2"; else cout << "2 1"; } else if (o2 == 2) { cout << 1 << endl; if (a[0][1] == '1') cout << "1 2"; else cout << "2 1"; } else { if (a[0][1] == '1') { cout << 2 << endl; cout << "1 2" << endl; if (a[n - 1][n - 2] == '0') cout << n << " " << n - 1; else cout << n - 1 << " " << n; } else { cout << 2 << endl; cout << "2 1" << endl; if (a[n - 1][n - 2] == '0') cout << n << " " << n - 1; else cout << n - 1 << " " << n; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(20); long long t = 1; cin >> t; while (t--) { solve(); cout << endl; } cin.get(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int number = 1000000; int isprime[number + 2] = {0}; long long expo(long long x, long long y, long long m) { long long p = 1; while (y > 0) { if (y % 2 == 1) p = p * x % m; x = x * x % m; y = y / 2; } return (p % m); } void sieve() { isprime[0] = isprime[1] = 1; for (long long i = 2; i < number + 2; i++) { if (isprime[i] == 0) { for (long long j = i * i; j < number + 2; j += i) isprime[j] = 1; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long i, j, l, q, t, m, n, h; long long k, z, p1, h1, x, y; string st = "", st1 = ""; std::cin >> q; for (l = 0; l < q; l++) { std::cin >> n; x = 0; int b[2][2]; long long x1, x2, x3, x4, y1, y2, y3, y4; char a[n][n]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) std::cin >> a[i][j]; x1 = 0; y1 = 1; x2 = 1; y2 = 0; x3 = n - 1; y3 = n - 2; x4 = n - 2; y4 = n - 1; if (a[x1][y1] != a[x2][y2] && a[x3][y3] != a[x4][y4]) { std::cout << 2 << std::endl; if (a[x1][y1] == '0') std::cout << x1 + 1 << " " << y1 + 1 << std::endl; else std::cout << x2 + 1 << " " << y2 + 1 << std::endl; if (a[x3][y3] == '1') std::cout << x3 + 1 << " " << y3 + 1 << std::endl; else std::cout << x4 + 1 << " " << y4 + 1 << std::endl; } else if (a[x1][y1] != a[x2][y2]) { std::cout << 1 << std::endl; if (a[x1][y1] == a[x3][y3]) std::cout << x1 + 1 << " " << y1 + 1 << std::endl; else std::cout << x2 + 1 << " " << y2 + 1 << std::endl; } else if (a[x3][y3] != a[x4][y4]) { std::cout << 1 << std::endl; if (a[x1][y1] == a[x3][y3]) std::cout << x3 + 1 << " " << y3 + 1 << std::endl; else std::cout << x4 + 1 << " " << y4 + 1 << std::endl; } else if (a[x1][y1] == a[x3][y3]) { std::cout << 2 << std::endl; std::cout << x1 + 1 << " " << y1 + 1 << std::endl; std::cout << x2 + 1 << " " << y2 + 1 << std::endl; } else std::cout << 0 << std::endl; } return 0; }
8
CPP
import math test = int(input()) for t in range(test): 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
t = int(input()) for _ in range(t): n = int(input()) grid = [] for i in range(n): grid.append(input()) count = 0 ans = "" if(grid[-1][-2] == grid[-2][-1]): if(grid[0][1] == grid[-2][-1]): ans += "\n1 2" count+=1 if(grid[1][0] == grid[-2][-1]): ans += "\n2 1" count+=1 elif(grid[0][1] == grid[1][0]): if(grid[-1][-2] == grid[0][1]): count += 1 ans +="\n"+str(n)+" " + str(len(grid[0])-1) if(grid[-2][-1] == grid[0][1]): count +=1 ans += "\n"+str(n-1)+" " + str(len(grid[0])) elif(grid[0][1] != grid[-1][-2]): count+=2 ans += "\n1 2" ans += "\n"+str(n)+" " + str(len(grid[0])-1) elif(grid[0][1] != grid[-2][-1]): count+=2 ans += "\n1 2" ans += "\n"+str(n-1)+" " + str(len(grid[0])) print(str(count)+ans)
8
PYTHON3
t=int(input()) for i in range(0,t): n=int(input()) a=[] for j in range(0,n): b=input() q=[b[k] for k in range(0,n)] a.append(q) if(n!=0): c=[a[0][1],a[1][0],a[n-1][n-2],a[n-2][n-1]] x1=int(a[0][1])+int(a[1][0]) x2=int(a[n-1][n-2])+int(a[n-2][n-1]) if(x1==2): if(x2==0): print(0) continue if(x2==2): print(2) print(n,n-1) print(n-1,n) continue if(x2==1): if(c[2]=='1'): print(1) print(n,n-1) continue if(c[3]=='1'): print(1) print(n-1,n) continue if(x1==0): if(x2==2): print(0) continue if(x2==0): print(2) print(n-0,n-1) print(n-1,n-0) continue if(x2==1): if(c[2]=='0'): print(1) print(n-0,n-1) continue if(c[3]=='0'): print(1) print(n-1,n) continue if(x2==0): if(x1==2): print(0) continue if(x1==0): print(2) print(1,2) print(2,1) continue if(x1==1): if(c[0]=='0'): print(1) print(1,2) continue if(c[1]=='0'): print(1) print(2,1) continue if(x2==2): if(x1==0): print(0) continue if(x1==2): print(2) print(1,2) print(2,1) continue if(x1==1): if(c[0]=='1'): print(1) print(1,2) continue if(c[1]=='1'): print(1) print(2,1) continue if(x1==1 and x2==1): print(2) if(c[0]=='1'): print(1,2) if(c[1]=='1'): print(2,1) if(c[2]=='0'): print(n,n-1) if(c[3]=='0'): print(n-1,n-0) else: print(0)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") 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; } void OJ() {} const long long N = 2e5 + 5; long long solve() { long long n; cin >> n; char c; char arr[n][n]; for (long long i = 0; i < n; ++i) for (long long j = 0; j < n; ++j) { cin >> c; arr[i][j] = c; } long long p, q, r, s; p = arr[1][0] - '0'; q = arr[0][1] - '0'; r = arr[n - 1][n - 2] - '0'; s = arr[n - 2][n - 1] - '0'; long long ans[4] = {0}; if (p == q) { if (p != r && p != s) return cout << 0, 0; ; if (p == r && p == s) ans[0] = 1, ans[1] = 1; else if (p == s && p != r) ans[3] = 1; else if (p == r && p != s) ans[2] = 1; } else if (r == s) { if (r != q && r != p) return cout << 0, 0; ; if (r == p && r == q) ans[0] = 1, ans[1] = 1; else if (r == p && r != q) ans[0] = 1; else if (r == q && r != p) ans[1] = 1; } else if (p == s) { ans[0] = 1; ans[2] = 1; } else if (p == r) { ans[1] = 1; ans[2] = 1; } long long count = 0; for (long long i = 0; i < 4; ++i) count += ans[i]; cout << count << '\n'; if (ans[0]) cout << "2 1" << '\n'; if (ans[1]) cout << "1 2\n"; if (ans[2]) cout << n << " " << n - 1 << '\n'; if (ans[3]) cout << n - 1 << " " << n << '\n'; return 0; } signed main() { long long test; cin >> test; while (test--) solve(), cout << '\n'; return 0; }
8
CPP
T = int(input()) M =[ [0 for i in range(300)] for j in range(300)] for t in range(T): n = int(input()) for i in range(n): a = input() for j in range(n): if a[j] != 'S' and a[j] != 'F': M[i][j] = int(a[j]) res1 = [] res0 = [] if M[0][1] == 1: res0.append((1,2)) else: res1.append((1,2)) if M[1][0] == 1: res0.append((2,1)) else: res1.append((2,1)) if M[n-1][n-2] == 1: res1.append((n,n-1)) else: res0.append((n,n-1)) if M[n-2][n-1] == 1: res1.append((n-1,n)) else: res0.append((n-1, n)) if len(res1) <= 2: print(len(res1)) for (a,b) in res1: print(a,b) else: print(len(res0)) for (a,b) in res0: print(a,b)
8
PYTHON3
import sys,bisect,string,math,time,functools,random,fractions from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter from itertools import permutations,combinations,groupby rep=range;R=range def Golf():n,*t=map(int,open(0).read().split()) def I():return int(input()) def S_():return input() def IS():return input().split() def LS():return [i for i in input().split()] def MI():return map(int,input().split()) def LI():return [int(i) for i in input().split()] def LI_():return [int(i)-1 for i in input().split()] def NI(n):return [int(input()) for i in range(n)] def NI_(n):return [int(input())-1 for i in range(n)] def NLI(n):return [[int(i) for i in input().split()] for i in range(n)] def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)] def StoLI():return [ord(i)-97 for i in input()] def ItoS(n):return chr(n+97) def LtoS(ls):return ''.join([chr(i+97) for i in ls]) def RA():return map(int,open(0).read().split()) def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)] def RI(a=1,b=10):return random.randint(a,b) def INP(): N=10;A=10 n=random.randint(1,N) a=[random.randint(1,A) for i in range(n)] return n,a def Rtest(T): case,err=0,0 for i in range(T): inp=INP() a1=naive(*inp) a2=solve(*inp) if a1!=a2: print((a1,a2),inp) err+=1 case+=1 print('Tested',case,'case with',err,'errors') def GI(V,E,ls=None,Directed=False,index=1): org_inp=[];g=[[] for i in range(V)] FromStdin=True if ls==None else False for i in range(E): if FromStdin: inp=LI() org_inp.append(inp) else: inp=ls[i] if len(inp)==2: a,b=inp;c=1 else: a,b,c=inp if index==1:a-=1;b-=1 aa=(a,c);bb=(b,c);g[a].append(bb) if not Directed:g[b].append(aa) return g,org_inp def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1): #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage mp=[boundary]*(w+2);found={} for i in R(h): s=input() for char in search: if char in s: found[char]=((i+1)*(w+2)+s.index(char)+1) mp_def[char]=mp_def[replacement_of_found] mp+=[boundary]+[mp_def[j] for j in s]+[boundary] mp+=[boundary]*(w+2) return h+2,w+2,mp,found def TI(n):return GI(n,n-1) def accum(ls): rt=[0] for i in ls:rt+=[rt[-1]+i] return rt def bit_combination(n,base=2): rt=[] for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s] return rt def gcd(x,y): if y==0:return x if x%y==0:return y while x%y!=0:x,y=y,x%y return y def YN(x):print(['NO','YES'][x]) def Yn(x):print(['No','Yes'][x]) def show(*inp,end='\n'): if show_flg:print(*inp,end=end) mo=10**9+7 #mo=998244353 inf=float('inf') FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb)) l_alp=string.ascii_lowercase #sys.setrecursionlimit(10**9) read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip() show_flg=False #show_flg=True ans=0 for _ in range(I()): ans=0 h=w=I() m=[] for i in range(h): m+=input(), a,b,c,d=m[0][1],m[1][0],m[h-1][w-2],m[h-2][w-1] l=[a,b,c,d] a,b,c,d=map(int,l) l=[a,b,c,d] if a==1: a,b,c,d=map(lambda x:1-x,l) ans=[] y=[(1,2),(2,1),(h,w-1),(h-1,w)] for i in range(1<<4): cnt=sum(i>>j&1 for j in range(4)) if cnt>2: continue x=l[:] for j in range(4): if i>>j&1: x[j]=1-x[j] if x[0]==x[1] and x[2]==x[3] and x[0]!=x[2]: for j in range(4): if i>>j&1: ans+=y[j], break print(cnt) for i in ans: print(*i)
8
PYTHON3
import sys from random import * #from bisect import * #from collections import deque pl=1 #from math import * from copy import * if pl: input=sys.stdin.readline else: sys.stdin=open('input.txt', 'r') sys.stdout=open('outpt.txt','w') def li(): return [int(xxx) for xxx in input().split()] def fi(): return int(input()) def si(): return list(input().rstrip()) def mi(): return map(int,input().split()) t=fi() while t>0: t-=1 n=fi() a=[] for i in range(n): a.append(si()) p=a[0][1] q=a[1][0] r=a[n-1][n-2] s=a[n-2][n-1] ans=[] if len(set([p,q]))==1: if r==p: ans.append([n,n-1]) if s==p: ans.append([n-1,n]) elif len(set([r,s]))==1: if r==p: ans.append([1,2]) if s==q: ans.append([2,1]) else: if p=="0": ans.append([1,2]) if q=="0": ans.append([2,1]) if r=='1': ans.append([n,n-1]) if s=='1': ans.append([n-1,n]) print(len(ans)) for i in ans: print(*i)
8
PYTHON3
def check(l,s,c): if l[0][1]==l[1][0]: x=l[0][1] a=l[n-1][n-2] b=l[n-2][n-1] if a==x: c=c+1 s.append([n,n-1]) if b==x: c=c+1 s.append([n-1,n]) print(c) for i in s: print(*i) return a=l[n-1][n-2] b=l[n-2][n-1] x,y=l[0][1],l[1][0] if a==b: z=a if x==a: c=c+1 s.append([1,2]) else: c=c+1 s.append([2,1]) else: c=c+1 s.append([1,2]) x=1-int(x) if int(a)==x: c=c+1 s.append([n,n-1]) else: c=c+1 s.append([n-1,n]) print(c) for i in s: print(*i) for _ in range(int(input())): n=int(input()) l=[] for i in range(n): k=list(input()) l.append(k) s=[] c=0 check(l,s,c)
8
PYTHON3
def solve(): n = int(input()) grid = [] for _ in range(n): grid.append(input()) g01 = grid[0][1] g10 = grid[1][0] g_12 = grid[n-1][n-2] g_21 = grid[n-2][n-1] if g01 == g10: if g_12 == g_21: if g01 == g_12: print("2") print("1 2") print("2 1") else: print("0") else: if g01 == "0": if g_12 == "0": print("1") print(str(n) + " " + str(n - 1)) else: print("1") print(str(n - 1) + " " + str(n)) else: if g_12 == "0": print("1") print(str(n - 1) + " " + str(n)) else: print("1") print(str(n) + " " + str(n - 1)) else: if g_12 == g_21: if g_12 == "0": if g01 == "0": print("1") print("1 2") else: print("1") print("2 1") else: if g01 == "1": print("1") print("1 2") else: print("1") print("2 1") else: print("2") if g01 == "0": print("2 1") else: print("1 2") if g_12 == "0": print(str(n) + " " + str(n-1)) else: print(str(n-1) + " " + str(n)) def main(): t = int(input()) for _ in range(t): solve() main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void sol() { long long n; cin >> n; char a; long long flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a; if (i == 0 && j == 1) { if (a == '1') flag1++; } if (i == 1 && j == 0) { if (a == '1') flag2++; } if (i == n - 1 && j == n - 2) { if (a == '1') flag3++; } if (i == n - 2 && j == n - 1) { if (a == '1') flag4++; } } } vector<pair<long long, long long>> res; if (flag3 == flag4) { if (flag3 == flag1) { res.push_back(pair<long long, long long>(0, 1)); } if (flag3 == flag2) { res.push_back(pair<long long, long long>(1, 0)); } cout << res.size() << endl; { for (int i = 0; i < res.size(); i++) { cout << res[i].first + 1 << " " << res[i].second + 1 << endl; } } return; } if (flag1 == flag2) { if (flag3 == flag1) { res.push_back(pair<long long, long long>(n - 1, n - 2)); } if (flag4 == flag1) { res.push_back(pair<long long, long long>(n - 2, n - 1)); } cout << res.size() << endl; { for (int i = 0; i < res.size(); i++) { cout << res[i].first + 1 << " " << res[i].second + 1 << endl; } } return; } if (flag1 == 1) { res.push_back(pair<long long, long long>(0, 1)); } else { res.push_back(pair<long long, long long>(1, 0)); } if (flag3 == 1) { res.push_back(pair<long long, long long>(n - 2, n - 1)); } else { res.push_back(pair<long long, long long>(n - 1, n - 2)); } cout << res.size() << endl; { for (int i = 0; i < res.size(); i++) { cout << res[i].first + 1 << " " << res[i].second + 1 << endl; } } } int main() { long long n; cin >> n; for (int i = 0; i < n; i++) { sol(); } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 40; vector<int> used(N, 0); void solve() { int n; cin >> n; char a[n + 2][n + 2]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } int b1; b1 = (a[n - 1][n] == '1') + (a[n][n - 1] == '1'); int bb1; bb1 = (a[n - 2][n] == '0') + (a[n - 1][n - 1] == '0') + (a[n][n - 2] == '0'); if (5 - (b1 + bb1) <= 2) { cout << 5 - ((b1 + bb1)) << '\n'; if (a[n - 1][n] == '0') { cout << n - 1 << " " << n << '\n'; } if (a[n][n - 1] == '0') { cout << n << " " << n - 1 << '\n'; } if (a[n - 2][n] == '1') { cout << n - 2 << " " << n << '\n'; } if (a[n - 1][n - 1] == '1') { cout << n - 1 << " " << n - 1 << '\n'; } if (a[n][n - 2] == '1') { cout << n << " " << n - 2 << '\n'; } } else { cout << (b1 + bb1) << '\n'; if (a[n - 1][n] == '1') { cout << n - 1 << " " << n << '\n'; } if (a[n][n - 1] == '1') { cout << n << " " << n - 1 << '\n'; } if (a[n - 2][n] == '0') { cout << n - 2 << " " << n << '\n'; } if (a[n - 1][n - 1] == '0') { cout << n - 1 << " " << n - 1 << '\n'; } if (a[n][n - 2] == '0') { cout << n << " " << n - 2 << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { solve(); } }
8
CPP
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math from collections import defaultdict for _ in range(ri()): n=ri() l=[] for __ in range(n):l.append(list(input())) ans=[] x=[l[0][1],l[1][0]] y=[l[n-2][n-1],l[n-1][n-2]] z=x.count('0')+y.count('0') if (x.count('1')==0 and y.count('0')==0) or (x.count('0')==0 and y.count('1')==0): wi(0) elif z==4 or z==0: wi(2) print(1,2) print(2,1) elif z==1: if '0' in x: if x[0]=='0': wi(1) print(2,1) else: wi(1) print(1,2) else: if y[0]=='0': wi(1) print(n,n-1) else: wi(1) print(n-1,n) elif z==2: wi(2) if x[0]=='1': print(1,2) else: print(2,1) if y[0]=='0': print(n-1,n) else: print(n,n-1) else: if '1' in x: if x[0]=='1': wi(1) print(2,1) else: wi(1) print(1,2) else: if y[0]=='1': wi(1) print(n,n-1) else: wi(1) print(n-1,n) '''if l[0][1]=='0': ans.append([1,2]) if l[1][0]=='0': ans.append([2,1]) if l[n-2][n-1]=='1': ans.append([n-1,n]) if l[n-1][n-2]=='1': ans.append([n,n-1]) wi(len(ans)) for i in ans: wia(i)'''
8
PYTHON3
for _ in range(int(input())): n=int(input()) data=[] for q in range(n): data.append(input()) start_right=data[0][1] start_down=data[1][0] end_left=data[-1][-2] end_top=data[-2][-1] if(start_right==start_down): if(end_left==end_top): if(start_down==end_left): ans=[[n,n-1],[n-1,n]] else: ans=[] else: if(start_down==end_left): ans=[[n,n-1]] else: ans=[[n-1,n]] else: if(end_left==end_top): if(end_top==start_down): ans=[[2,1]] else: ans=[[1,2]] else: if(start_down==end_top): ans=[[2,1],[n,n-1]] else: ans=[[2,1],[n-1,n]] print(len(ans)) for q in range(len(ans)): print(ans[q][0],ans[q][1])
8
PYTHON3
t = int(input()) for i in range(t): grid = [] n = int(input()) for j in range(n): grid.append(input()) a = grid[0][1] b = grid[1][0] c = grid[n - 2][n - 1] d = grid[n - 1][n - 2] if a == b and c == d and b == c: print(2) print("1 2") print("2 1") elif a == b and c == d: print(0) elif a == b and c != d: print(1) if a == c: print(str(n - 1) + " " + str(n)) else: print(str(n) + " " + str(n - 1)) elif c == d: print(1) if a == c: print("1 2") else: print("2 1") else: print(2) if a == "0": print("2 1") else: print("1 2") if c == "0": print(str(n - 1) + " " + str(n)) else: print(str(n) + " " + str(n - 1))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; char _grid[300][300], grid[300][300]; long long vis[300][300]; long long n, m; long long di[] = {0, 0, -1, 1}; long long dj[] = {-1, 1, 0, 0}; bool isPos(long long i, long long j) { return (i >= 0 and j >= 0 and i < n and j < n and !vis[i][j]); } void dfs(long long i, long long j) { vis[i][j] = true; for (long long k = 0; k < 4; k++) { long long ni = i + di[k], nj = j + dj[k]; if (isPos(ni, nj) and grid[ni][nj] == grid[i][j]) dfs(ni, nj); } } void solve() { cin >> n; for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) cin >> _grid[i][j]; } vector<vector<pair<long long, long long>>> a; pair<long long, long long> p1{1, 0}; pair<long long, long long> p2{0, 1}; pair<long long, long long> p3{n - 2, n - 1}; pair<long long, long long> p4{n - 1, n - 2}; a.push_back({}); a.push_back({p1}); a.push_back({p2}); a.push_back({p3}); a.push_back({p4}); a.push_back({p1, p2}); a.push_back({p1, p3}); a.push_back({p1, p4}); a.push_back({p2, p3}); a.push_back({p2, p4}); a.push_back({p3, p4}); for (auto v : a) { auto change = [&](long long i, long long j) { if (grid[i][j] == '1') grid[i][j] = '0'; else grid[i][j] = '1'; }; memset(vis, 0, sizeof(vis)); for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) grid[i][j] = _grid[i][j]; } for (auto it : v) change(it.first, it.second); dfs(1, 0); dfs(0, 1); if (vis[n - 2][n - 1] or vis[n - 1][n - 2]) continue; cout << (long long)v.size() << endl; for (auto it : v) cout << it.first + 1 << " " << it.second + 1 << endl; return; } } 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--) solve(); }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long t; cin >> t; long long n; long long ans = 0; long long f1 = 0, f2 = 0; while (t--) { f1 = f2 = 0; ans = 0; cin >> n; string s = ""; string t = ""; for (long long i = 0; i < n; i++) { cin >> t; s += t; } if (s[1] == '0' && s[n] == '1') { if (s[n * n - n - 1] == '1' && s[n * n - 2] == '1') { ans++; f1 = 2; } if (s[n * n - n - 1] == '0' && s[n * n - 2] == '0') { ans++; f1 = 1; } if (s[n * n - n - 1] == '0' && s[n * n - 2] == '1') { ans += 2; f1 = 1; f2 = 2; } if (s[n * n - n - 1] == '1' && s[n * n - 2] == '0') { ans += 2; f1 = 1; f2 = 1; } } else if (s[1] == '1' && s[n] == '0') { if (s[n * n - n - 1] == '0' && s[n * n - 2] == '0') { ans++; f1 = 2; } if (s[n * n - n - 1] == '1' && s[n * n - 2] == '1') { ans++; f1 = 1; } if (s[n * n - n - 1] == '1' && s[n * n - 2] == '0') { ans += 2; f1 = 1; f2 = 2; } if (s[n * n - n - 1] == '0' && s[n * n - 2] == '1') { ans += 2; f1 = 1; f2 = 1; } } else if (s[1] == '0' && s[n] == '0') { if (s[n * n - n - 1] == '0' && s[n * n - 2] == '0') { ans += 2; f1 = 3; } if (s[n * n - n - 1] == '0' && s[n * n - 2] == '1') { ans++; f2 = 1; } if (s[n * n - n - 1] == '1' && s[n * n - 2] == '0') { ans++; f2 = 2; } } else if (s[1] == '1' && s[n] == '1') { if (s[n * n - n - 1] == '1' && s[n * n - 2] == '1') { ans += 2; f1 = 3; } if (s[n * n - n - 1] == '0' && s[n * n - 2] == '1') { ans++; f2 = 2; } if (s[n * n - n - 1] == '1' && s[n * n - 2] == '0') { ans++; f2 = 1; } } cout << ans << endl; if (f1 == 1) { cout << 1 << " " << 2 << endl; } if (f1 == 2) { cout << 2 << " " << 1 << endl; } if (f1 == 3) { cout << 1 << " " << 2 << endl; cout << 2 << " " << 1 << endl; } if (f2 == 1) { cout << n - 1 << " " << n << endl; } if (f2 == 2) { cout << n << " " << n - 1 << endl; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char mp[205][205]; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> mp[i][j]; } } queue<pair<int, int>> q; if (mp[2][1] == mp[1][2]) { if (mp[n - 1][n] == mp[2][1]) q.push({n - 1, n}); if (mp[n][n - 1] == mp[2][1]) q.push({n, n - 1}); cout << q.size() << endl; while (!q.empty()) { cout << q.front().first << " " << q.front().second << endl; q.pop(); } } else if (mp[n - 1][n] == mp[n][n - 1]) { if (mp[n - 1][n] == mp[2][1]) q.push({2, 1}); if (mp[n - 1][n] == mp[1][2]) q.push({1, 2}); cout << q.size() << endl; while (!q.empty()) { cout << q.front().first << " " << q.front().second << endl; q.pop(); } } else { if (mp[1][2] == '0') q.push({1, 2}); if (mp[2][1] == '0') q.push({2, 1}); if (mp[n][n - 1] == '1') q.push({n, n - 1}); if (mp[n - 1][n] == '1') q.push({n - 1, n}); cout << q.size() << endl; while (!q.empty()) { cout << q.front().first << " " << q.front().second << endl; q.pop(); } } } }
8
CPP
t = int(input()) import itertools for _ in range(t): n = int(input()) S = [list(str(input())) for _ in range(n)] flag = False s = [] # 0 s.append((0, 0)) visit0 = [[-1]*n for _ in range(n)] while s: i, j = s.pop() for di, dj in (-1, 0), (1, 0), (0, -1), (0, 1): ni,nj = i+di, j+dj if 0 <= ni < n and 0 <= nj < n: if S[ni][nj] == '0' or S[ni][nj] == 'F': if visit0[ni][nj] == -1: visit0[ni][nj] = 0 s.append((ni, nj)) #1 s.append((0, 0)) visit1 = [[-1]*n for _ in range(n)] while s: i, j = s.pop() for di, dj in (-1, 0), (1, 0), (0, -1), (0, 1): ni,nj = i+di, j+dj if 0 <= ni < n and 0 <= nj < n: if S[ni][nj] == '1' or S[ni][nj] == 'F': if visit1[ni][nj] == -1: visit1[ni][nj] = 0 s.append((ni, nj)) if visit0[n-1][n-1] == -1 and visit1[n-1][n-1] == -1: print(0) continue if S[0][1] == '0' and S[1][0] == '0' and S[n-1][n-2] == '0' and S[n-2][n-1] == '0': print(2) print(1, 2) print(2, 1) elif S[0][1] == '0' and S[1][0] == '0' and S[n-1][n-2] == '1' and S[n-2][n-1] == '0': print(1) print(n-1, n) elif S[0][1] == '0' and S[1][0] == '0' and S[n-1][n-2] == '0' and S[n-2][n-1] == '1': print(1) print(n, n-1) elif S[0][1] == '0' and S[1][0] == '0' and S[n-1][n-2] == '1' and S[n-2][n-1] == '1': print(0) elif S[0][1] == '1' and S[1][0] == '0' and S[n-1][n-2] == '0' and S[n-2][n-1] == '0': print(1) print(2, 1) elif S[0][1] == '1' and S[1][0] == '0' and S[n-1][n-2] == '1' and S[n-2][n-1] == '0': print(2) print(1, 2) print(n-1, n) elif S[0][1] == '1' and S[1][0] == '0' and S[n-1][n-2] == '0' and S[n-2][n-1] == '1': print(2) print(1, 2) print(n, n-1) elif S[0][1] == '1' and S[1][0] == '0' and S[n-1][n-2] == '1' and S[n-2][n-1] == '1': print(1) print(1, 2) elif S[0][1] == '0' and S[1][0] == '1' and S[n-1][n-2] == '0' and S[n-2][n-1] == '0': print(1) print(1, 2) elif S[0][1] == '0' and S[1][0] == '1' and S[n-1][n-2] == '1' and S[n-2][n-1] == '0': print(2) print(2, 1) print(n-1, n) elif S[0][1] == '0' and S[1][0] == '1' and S[n-1][n-2] == '0' and S[n-2][n-1] == '1': print(2) print(2, 1) print(n, n-1) elif S[0][1] == '0' and S[1][0] == '1' and S[n-1][n-2] == '1' and S[n-2][n-1] == '1': print(1) print(2, 1) elif S[0][1] == '1' and S[1][0] == '1' and S[n-1][n-2] == '0' and S[n-2][n-1] == '0': print(0) elif S[0][1] == '1' and S[1][0] == '1' and S[n-1][n-2] == '1' and S[n-2][n-1] == '0': print(1) print(n, n-1) elif S[0][1] == '1' and S[1][0] == '1' and S[n-1][n-2] == '0' and S[n-2][n-1] == '1': print(1) print(n-1, n) elif S[0][1] == '1' and S[1][0] == '1' and S[n-1][n-2] == '1' and S[n-2][n-1] == '1': print(2) print(1, 2) print(2, 1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N = 205; char a[N][N]; void Main() { long long n; cin >> n; for (long long i = 1; i <= n; i++) for (long long j = 1; j <= n; j++) { cin >> a[i][j]; } vector<pair<long long, long long> > res; if (a[1][2] == a[2][1]) { char c = a[1][2]; if (a[n - 1][n] == c) { res.push_back(pair<long long, long long>(n - 1, n)); } if (a[n][n - 1] == c) { res.push_back(pair<long long, long long>(n, n - 1)); } cout << res.size() << '\n'; for (auto &i : res) cout << i.first << ' ' << i.second << '\n'; return; } if (a[n - 1][n] == a[n][n - 1]) { char c = a[n - 1][n]; if (a[1][2] == c) { res.push_back(pair<long long, long long>(1, 2)); } if (a[2][1] == c) { res.push_back(pair<long long, long long>(2, 1)); } cout << res.size() << '\n'; for (auto &i : res) cout << i.first << ' ' << i.second << '\n'; return; } char c = a[1][2]; res.push_back(pair<long long, long long>(2, 1)); if (a[n - 1][n] == c) { res.push_back(pair<long long, long long>(n - 1, n)); } if (a[n][n - 1] == c) { res.push_back(pair<long long, long long>(n, n - 1)); } cout << res.size() << '\n'; for (auto &i : res) cout << i.first << ' ' << i.second << '\n'; } signed main() { cin.tie(0)->sync_with_stdio(0); long long T = 1; cin >> T; while (T--) Main(); }
8
CPP
def main(): for _ in range(int(input())): n=int(input()) matx=[] for j in range(n): matx.append([str(x) for x in input()]) x=[int(matx[0][1]),int(matx[1][0])] y=[int(matx[n-1][n-2]),int(matx[n-2][n-1])] if x==[0,0] and y==[1,1 ]: print(0) elif x==[1,1] and y==[0,0]: print(0) elif x==[1,1] and y==[1,1] : print(2) print(1,2) print(2,1) elif x==[0,0] and y==[0,0] : print(2) print(1,2) print(2,1) else: if x==[1,0]: if y==[1,0]: print(2) print(1,2) print(n-1,n) elif y==[0,1]: print(2) print(1,2) print(n,n-1) elif y==[0,0]: print(1) print(2,1) else: print(1) print(1,2) if x == [ 0,1]: if y == [1, 0]: print(2) print(1, 2) print(n, n-1) elif y == [0, 1]: print(2) print(1, 2) print(n - 1, n) elif y == [0, 0]: print(1) print(1,2) else: print(1) print(2,1) if x==[1,1]: if y==[1,0]: print(1) print(n,n-1) elif y==[0,1]: print(1) print(n-1,n) if x==[0,0] : if y==[1,0]: print(1) print(n-1,n) elif y==[0,1]: print(1) print(n,n-1) main()
8
PYTHON3
def solve(n,m): #print(n,m) a = m[0][1] b = m[1][0] c = m[n-1][n-2] d = m[n-2][n-1] res = [] if a ==b: if a == c: res.append([n,n-1]) if a == d: res.append([n-1,n]) elif c == d: if c == a: res.append([1,2]) if b == c: res.append([2,1]) else: #a == b and c == d res.append([2,1]) if a == c: res.append([n,n-1]) if a == d: res.append([n-1,n]) print(len(res)) return res t = int(input()) for _ in range(t): n = int(input()) r = [] c = n while n: x = list(input()) r.append(x) n-=1 k = solve(c,r) for x in k: print(*x)
8
PYTHON3
from math import * import sys from sys import stdin from sys import stdout # input = stdin.readline for __ in range(int(input())): n=int(input()) ar=[] for i in range(n): ar.append(input()) a=ar[0][1] b=ar[1][0] x=ar[n-1][n-2] y=ar[n-2][n-1] if(a==b and x==y and a!=x): print(0) elif(a==b==x==y): print(2) print(1,2) print(2,1) else: if(a!=b and x!=y): print(2) if(a==x): print(1,2) print(n-1,n) else: print(1,2) print(n,n-1) elif(a!=b and x==y): print(1) if(a==x): print(1,2) else: print(2,1) elif(a==b and x!=y): print(1) if(a==x): print(n,n-1) else: print(n-1,n)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> foo(string table[], int n, char character) { vector<pair<int, int>> result(0); char ch = character; if (table[n - 1][n - 3] != ch) result.push_back({n - 1, n - 3}); if (table[n - 2][n - 2] != ch) result.push_back({n - 2, n - 2}); if (table[n - 3][n - 1] != ch) result.push_back({n - 3, n - 1}); if (ch == '1') ch = '0'; else ch = '1'; if (table[n - 1][n - 2] != ch) result.push_back({n - 1, n - 2}); if (table[n - 2][n - 1] != ch) result.push_back({n - 2, n - 1}); return result; } int main() { ios::sync_with_stdio(false); cin.tie(0); vector<pair<int, int>> a, b, c; int tt; cin >> tt; while (tt--) { int n; cin >> n; string table[n]; for (int i = 0; i < n; i++) cin >> table[i]; a = foo(table, n, '1'); b = foo(table, n, '0'); c = a.size() < b.size() ? a : b; cout << c.size() << "\n"; for (int i = 0; i < c.size(); i++) { cout << c[i].first + 1 << " " << c[i].second + 1 << "\n"; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool isprime(int64_t n) { for (int64_t i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } int64_t factorial(int64_t n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } int64_t gcd(int64_t a, int64_t b) { if (a == 0) return b; return gcd(b % a, a); } int64_t lcm(int64_t a, int64_t b) { return (a / gcd(a, b) * b); } int64_t max(int64_t a, int64_t b) { int64_t sol = a > b ? a : b; return sol; } int64_t min(int64_t a, int64_t b) { int64_t sol = a < b ? a : b; return sol; } int64_t power(int64_t x, int64_t y) { int64_t ans = 1; while (y > 0) { if (y & 1) { ans *= x; } y >>= 1LL; x *= x; } return ans; } int64_t inf = 100000000001; inline int64_t solve() { int64_t n; cin >> n; string s[n]; for (int64_t i = 0; i <= n - 1; ++i) { cin >> s[i]; } if (s[0][1] == s[1][0]) { if (s[n - 1][n - 2] == s[n - 2][n - 1]) { if (s[0][1] == s[n - 1][n - 2]) { cout << 2 << "\n"; cout << n << " " << n - 1 << "\n"; cout << n - 1 << " " << n << "\n"; } else { cout << 0 << "\n"; } } else if (s[n - 1][n - 2] == s[0][1]) { cout << 1 << "\n"; cout << n << " " << n - 1 << "\n"; } else if (s[n - 2][n - 1] == s[0][1]) { cout << 1 << "\n"; cout << n - 1 << " " << n << "\n"; } } else { if (s[n - 1][n - 2] == s[n - 2][n - 1]) { if (s[n - 1][n - 2] == s[0][1]) { cout << 1 << "\n"; cout << 1 << " " << 2 << "\n"; } else { cout << 1 << "\n"; cout << 2 << " " << 1 << "\n"; } } else { if ((s[0][1] == s[n - 1][n - 2] && s[1][0] == s[n - 2][n - 1])) { cout << 2 << "\n"; cout << 1 << " " << 2 << "\n"; cout << n - 1 << " " << n << "\n"; } else if ((s[1][0] == s[n - 1][n - 2] && s[0][1] == s[n - 2][n - 1])) { cout << 2 << "\n"; cout << 1 << " " << 2 << "\n"; cout << n << " " << n - 1 << "\n"; } else { cout << 2 << "\n"; cout << 1 << " " << 2 << "\n"; cout << n << " " << n - 1 << "\n"; } } } return 0; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int64_t t; cin >> t; while (t--) { solve(); } }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, n; cin >> t; while (t--) { cin >> n; string s[n]; for (int i = 0; i < n; i++) { cin >> s[i]; } if (s[0][1] == s[1][0]) { if (s[n - 1][n - 2] == s[n - 2][n - 1] && s[n - 1][n - 2] != s[1][0]) { cout << "0\n"; } else if (s[n - 1][n - 2] == s[n - 2][n - 1] && s[n - 1][n - 2] == s[0][1]) { cout << "2\n"; cout << n << " " << n - 1 << "\n" << n - 1 << " " << n << "\n"; } else if (s[n - 1][n - 2] != s[n - 2][n - 1]) { if (s[n - 1][n - 2] == s[0][1]) { cout << "1\n" << n << " " << n - 1 << "\n"; } else { cout << "1\n" << n - 1 << " " << n << "\n"; } } } else { if (s[n - 1][n - 2] == s[n - 2][n - 1]) { if (s[n - 1][n - 2] == s[0][1]) { cout << "1\n1 2\n"; } else { cout << "1\n2 1\n"; } } else if (s[n - 1][n - 2] != s[n - 2][n - 1]) { if (s[n - 1][n - 2] == s[0][1]) { cout << "2\n1 2\n" << n - 1 << " " << n << "\n"; } else if (s[n - 2][n - 1] == s[1][0]) { cout << "2\n2 1\n" << n << " " << n - 1 << "\n"; } else if (s[n - 1][n - 2] == s[1][0]) { cout << "2\n2 1\n" << n - 1 << " " << n << "\n"; } else if (s[n - 2][n - 1] == s[0][1]) { cout << "2\n1 2\n" << n << " " << n - 1 << "\n"; } } } } return 0; }
8
CPP
#include <bits/stdc++.h> const long long INF = 1e18; const long long mod = 1e9 + 7; using namespace std; void solve() { long long n; cin >> n; vector<vector<char>> vec(n, vector<char>(n)); for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { cin >> vec[i][j]; } } long long a = vec[0][1]; long long b = vec[1][0]; long long c = vec[n - 1][n - 2]; long long d = vec[n - 2][n - 1]; vector<pair<long long, long long>> ans; if (c == d) { if (a == c) ans.push_back({0, 1}); if (b == c) ans.push_back({1, 0}); } else if (a == b) { if (a == c) ans.push_back({n - 1, n - 2}); if (a == d) ans.push_back({n - 2, n - 1}); } else { ans.push_back({1, 0}); if (a == c) ans.push_back({n - 1, n - 2}); if (a == d) ans.push_back({n - 2, n - 1}); } cout << ans.size() << endl; for (auto p : ans) { cout << p.first + 1 << " " << p.second + 1 << endl; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tests = 1; cin >> tests; while (tests--) { solve(); } return 0; }
8
CPP
# cook your dish here for _ in range(int(input())): n=int(input()) a=[] for i in range(n): a.append(input()) l=[] if a[0][1]==a[1][0]: if a[-2][-1]==a[0][1]: l.append([n-1,n]) if a[-1][-2]==a[0][1]: l.append([n,n-1]) else: if a[-1][-2]==a[-2][-1]: if a[0][1]==a[-1][-2]: l.append([1,2]) else: l.append([2,1]) else: l.append([1,2]) if a[0][1]==a[-1][-2]: l.append([n-1,n]) else: l.append([n,n-1]) print(len(l)) for x in l: print(*x)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int rd() { int x = 0; bool f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) { x = x * 10 + (c ^ 48); c = getchar(); } return f ? -x : x; } int a[307][307]; inline void work() { int n = rd(); char c = getchar(); while (c != 'S') c = getchar(); for (int i = 2; i <= n; ++i) a[1][i] = getchar() - '0'; c = getchar(); for (int i = 2; i <= n; ++i) { for (int j = 1; j <= n; ++j) a[i][j] = getchar() - '0'; c = getchar(); } int x1 = a[1][2]; int x2 = a[2][1]; int y1 = a[n][n - 1]; int y2 = a[n - 1][n]; if (x1 == x2) { int tot = (y1 == x1) + (y2 == x1); printf("%d\n", tot); if (y1 == x1) printf("%d %d\n", n, n - 1); if (y2 == x1) printf("%d %d\n", n - 1, n); } else if (y1 == y2) { int tot = (x1 == y1) + (x2 == y1); printf("%d\n", tot); if (x1 == y1) printf("%d %d\n", 1, 2); if (x2 == y1) printf("%d %d\n", 2, 1); } else { puts("2"); if (x1 == 0) printf("%d %d\n", 1, 2); if (x2 == 0) printf("%d %d\n", 2, 1); if (y1 == 1) printf("%d %d\n", n, n - 1); if (y2 == 1) printf("%d %d\n", n - 1, n); } } int main() { int t = rd(); while (t--) work(); return 0; }
8
CPP
t = int(input()) for case in range(t): n = int(input()) grid = [] for row in range(n): grid.append([int(q) if q in '01' else q for q in input()]) changes = [] q = sum([grid[0][2], grid[1][1], grid[2][0]]) if q in (0, 3): if q//3 == grid[0][1]: changes.append([1, 2]) if q//3 == grid[1][0]: changes.append([2, 1]) print(len(changes)) for change in changes: print(*change) continue # Otherwise, two of the cells are of one colour # and the other cell is of the other # # Fx1 # y0 # 0 # # You can change (x, y) -> 1 and change 1 -> 0, as long as at least one of x,y is correct # If it isn't, you can change (0, 0) -> 1, 1 and solve assert q in (1, 2) majority = 0 if q == 1 else 1 if grid[0][1] == majority and grid[1][0] == majority: # Floyd will now pick the majority colour. So block # Floyd at the opposite end if grid[n - 1][n - 2] == majority: changes.append([n, n-1]) if grid[n - 2][n - 1] == majority: changes.append([n-1, n]) else: if grid[0][1] == majority: changes.append([1, 2]) if grid[1][0] == majority: changes.append([2, 1]) if grid[0][2] != majority: changes.append([1, 3]) if grid[1][1] != majority: changes.append([2, 2]) if grid[2][0] != majority: changes.append([3, 1]) assert len(changes) <= 2 print(len(changes)) for change in changes: print(*change)
8
PYTHON3
for _ in range(int(input())): n = int(input()) arr = [] for row in range(n): arr.append(input()) n = n+1 if arr[0][1] == arr[1][0] == arr[-2][-1] == arr[-1][-2]: print(2) print(n-2, n-1) print(n-1, n-2) continue elif arr[0][1] == arr[1][0]: if arr[-2][-1] == arr[-1][-2]: print(0) else: print(1) if arr[-2][-1] == arr[0][1]: print(n-2, n-1) else: print(n-1, n-2) elif arr[-2][-1] == arr[-1][-2]: if arr[0][1] == arr[1][0]: print(0) else: print(1) if arr[0][1] == arr[-2][-1]: print(1, 2) else: print(2, 1) else: ref = arr[0][1] print(2) print(2, 1) if arr[-2][-1] == ref: print(n-2, n-1) elif arr[-1][-2] == ref: print(n-1, n-2)
8
PYTHON3
for _ in range(int(input())): n=int(input()) s=[input() for i in range(n)] f1=[int(s[0][1]),int(s[1][0])] f2=[int(s[-1][-2]),int(s[-2][-1])] ans=[] if f1==[0,0]: if f2[0]==0: ans.append([n,n-1]) if f2[1]==0: ans.append([n-1,n]) elif f1==[1,1]: if f2[0]==1: ans.append([n,n-1]) if f2[1]==1: ans.append([n-1,n]) elif f2==[0,0]: if f1[0]==0: ans.append([1,2]) if f1[1]==0: ans.append([2,1]) elif f2==[1,1]: if f1[0]==1: ans.append([1,2]) if f1[1]==1: ans.append([2,1]) elif f1==[0,1] or f1==[1,0]: if f1==[0,1]: ans.append([2,1]) else: ans.append([1,2]) if f2[0]==0: ans.append([n,n-1]) if f2[1]==0: ans.append([n-1,n]) print(len(ans)) for i in range(len(ans)): print(ans[i][0],ans[i][1])
8
PYTHON3
import sys as _sys ZERO = '0' ONE = '1' def main(): t = int(input()) for i_t in range(t): n, = _read_ints() a = tuple(tuple(_read_line()) for i_row in range(n)) result = tuple(find_cells_to_invert(a)) print(len(result)) for x, y in result: if x < 0: x += n if y < 0: y += n x += 1 y += 1 print(y, x) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def find_cells_to_invert(matrix): assert len(matrix) == len(matrix[0]) n = len(matrix) assert n >= 3 get_by_pos = lambda pos: matrix[pos[1]][pos[0]] cell_a, cell_b = (0, 1), (1, 0) cell_c, cell_d = (-1, -2), (-2, -1) value_a, value_b = get_by_pos(cell_a), get_by_pos(cell_b) value_c, value_d = get_by_pos(cell_c), get_by_pos(cell_d) if value_a != value_b and value_c != value_d: if get_by_pos(cell_a) == ONE: yield cell_a else: yield cell_b if get_by_pos(cell_c) == ZERO: yield cell_c else: yield cell_d elif value_a == value_b: value_to_avoid = value_a if value_c == value_to_avoid: yield cell_c if value_d == value_to_avoid: yield cell_d else: assert value_c == value_d value_to_avoid = value_c if value_a == value_to_avoid: yield cell_a if value_b == value_to_avoid: yield cell_b if __name__ == '__main__': main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long i, i1, j, k, k1, t, n, m, res, flag[10], a, b, mt[210][210], f[210]; char x; vector<array<long long, 2>> sl; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { cin >> n; sl.clear(); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { cin >> x; if (x == '1') mt[i][j] = 1; else mt[i][j] = 0; } } mt[1][2] ^= 1; mt[2][1] ^= 1; f[0] = 0; f[1] = 0; long long d1[5] = {1, 2, 1, 2, 3}; long long d2[5] = {2, 1, 3, 2, 1}; for (i = 0; i < 5; i++) { f[mt[d1[i]][d2[i]]]++; } for (i = 0; i < 5; i++) { if (f[mt[d1[i]][d2[i]]] <= 2) sl.push_back({d1[i], d2[i]}); } cout << sl.size() << "\n"; for (auto u : sl) cout << u[0] << ' ' << u[1] << "\n"; } return 0; }
8
CPP
import sys input = sys.stdin.readline import math for _ in range(int(input())): n = int(input()) lis=[] for i in range(n): tmp = list(input()) lis.append(tmp) #print(lis[0][1],lis[1][0],lis[n-2][n-1],lis[n-1][n-2]) if lis[0][1]==lis[1][0]: if lis[n-1][n-2]==lis[n-2][n-1]: if lis[0][1]!=lis[n-1][n-2]: print(0) else: print(2) print(1,2) print(2,1) else: print(1) if lis[n-1][n-2]!=lis[1][0]: print(n-1,n) else: print(n,n-1) else: if lis[n-1][n-2]==lis[n-2][n-1]: print(1) if lis[n-1][n-2]!=lis[1][0]: print(1,2) else: print(2,1) else: print(2) if lis[0][1]=='1': print(1,2) if lis[1][0]=='1': print(2,1) if lis[n-1][n-2]=='0': print(n,n-1) if lis[n-2][n-1]=='0': print(n-1,n)
8
PYTHON3
for _ in range(int(input())): n = int(input()) arr = list() for _ in range(n): arr.append(input()) ss = arr[0][1] sd = arr[1][0] fu = arr[n-2][n-1] fl = arr[n-1][n-2] if ss == sd: if fu == fl: if ss!=fu: print(0) else : print(2) print(1,2) print(2,1) else: if ss==fu: print(1) print(n-1,n) else : print(1) print(n, n-1) else: if fu == fl: print(1) if ss==fu: print(1, 2) else : print(2,1) else: print(2) if(ss==fl): print(2,1) print(n,n-1) if(ss==fu): print(2,1) print(n-1,n)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int inf = 2e9 + 5; double PI = 3.14159265358979323846; void solve() { int n; cin >> n; char c[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> c[i][j]; } } vector<pair<int, int> > ans; if (c[0][1] == c[1][0]) { int x = c[0][1] - '0'; x = 1 - x; if (c[n - 1][n - 2] != '0' + x) { ans.push_back({n - 1, n - 2}); } if (c[n - 2][n - 1] != '0' + x) { ans.push_back({n - 2, n - 1}); } } else if (c[n - 1][n - 2] == c[n - 2][n - 1]) { int x = c[n - 1][n - 2] - '0'; x = 1 - x; if (c[0][1] != '0' + x) { ans.push_back({0, 1}); } if (c[1][0] != '0' + x) { ans.push_back({1, 0}); } } else { int x = c[0][1] - '0'; x = 1 - x; ans.push_back({1, 0}); if (c[n - 1][n - 2] != '0' + x) { ans.push_back({n - 1, n - 2}); } if (c[n - 2][n - 1] != '0' + x) { ans.push_back({n - 2, n - 1}); } } cout << ans.size() << "\n"; for (auto p : ans) { cout << p.first + 1 << " " << p.second + 1 << "\n"; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1}; const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1}; template <typename T_n> void read(T_n &n) { cin >> n; } template <typename T_n, typename T_a> void read(T_a a[], T_n n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <typename T_n, typename T_a> void read(vector<T_a> &a, T_n n) { a.resize(n); for (int i = 0; i < n; ++i) cin >> a[i]; } void _init_() { ios_base::sync_with_stdio(0), cin.tie(0); } const int N = 200 + 9, SEG = 131072; const int Mod_M = 1e8; int n; char a[N][N]; void solve() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s", a[i]); vector<pair<int, int>> ans; if (a[1][0] == a[0][1]) { if (a[n - 1][n - 2] == a[1][0]) ans.push_back({n - 1, n - 2}); if (a[n - 2][n - 1] == a[1][0]) ans.push_back({n - 2, n - 1}); } else if (a[n - 1][n - 2] == a[n - 2][n - 1]) { if (a[1][0] == a[n - 1][n - 2]) ans.push_back({1, 0}); if (a[0][1] == a[n - 1][n - 2]) ans.push_back({0, 1}); } else { (a[1][0] != '0') ? ans.push_back({1, 0}) : ans.push_back({0, 1}); (a[n - 1][n - 2] != '1') ? ans.push_back({n - 1, n - 2}) : ans.push_back({n - 2, n - 1}); } printf("%d\n", (int)ans.size()); for (int i = 0; i < (int)ans.size(); ++i) { printf("%d %d\n", ans[i].first + 1, ans[i].second + 1); } } int main() { _init_(); int t; scanf("%d", &t); while (t--) { solve(); } return 0; }
8
CPP
import sys input=sys.stdin.readline t=int(input()) def f(mat): c = [["0" for i in range(n)]for j in range(n)] for i in range(n): for j in range(n): c[i][j] = mat[i][j] return c from collections import deque def check(mat,l): for i in range(len(l)): if(mat[l[i][0]][l[i][1]] =="0"): mat[l[i][0]][l[i][1]] = "1" else: mat[l[i][0]][l[i][1]] = "0" # print("mmm",mat) q=[(0,0)] q=deque(q) visited = [[0 for i in range(n)]for j in range(n)] visited[0][0]=1 while (len(q)!=0): i,j = q[0] temp1 = [-1,+1,0,0] temp2 = [0,0,-1,+1] for k in range(4): if((i+temp1[k])>=0 and (i+temp1[k])<n and (j+temp2[k])>=0 and (j+temp2[k])<n): if(visited[i+temp1[k]][j+temp2[k]]==0 and (mat[i+temp1[k]][j+temp2[k]]=="0" or mat[i+temp1[k]][j+temp2[k]]=="F")): # print(q[0],i+temp1[k],j+temp2[k],"cccc") visited[i+temp1[k]][j+temp2[k]]=1 # print(visited) q.append((i+temp1[k],j+temp2[k])) q.popleft() # print(visited) if(visited[-1][-1] == 1): # print("lll") return True q=[(0,0)] q=deque(q) visited = [[0 for i in range(n)]for j in range(n)] visited[0][0]=1 while (len(q)!=0): i,j = q[0] temp1 = [-1,+1,0,0] temp2 = [0,0,-1,+1] for k in range(4): if((i+temp1[k])>=0 and (i+temp1[k])<n and (j+temp2[k])>=0 and (j+temp2[k])<n): # print(i+temp1[k],j+temp2[k]) if(visited[i+temp1[k]][j+temp2[k]]==0 and( mat[i+temp1[k]][j+temp2[k]]=="1" or mat[i+temp1[k]][j+temp2[k]]=="F")): visited[i+temp1[k]][j+temp2[k]]=1 q.append((i+temp1[k],j+temp2[k])) q.popleft() # print(visited) if(visited[-1][-1] == 1): return True return False for t1 in range(t): n = int(input()) mat1 = [] for i in range(n): s =input().strip() l = [] for j in range(n): l.append(s[j]) mat1.append(l) l = [(n-1,n-2),(n-2,n-2),(n-2,n-1),(n-1,n-3),(n-3,n-1)] if(check(f(mat1),[])==False): print(0) else: f1 = 0 # print("lll") for i in range(5): if(f1==1): break # print(l[i]) # if(l[i]==(2,3)): # print(l[i]) # print(check(f(mat1),[l[i]])) if(check(f(mat1),[l[i]]) == False): print(1) print(l[i][0]+1, l[i][1]+1) break for j in range(5): c = f(mat1) if(check(f(mat1),[l[i],l[j]]) == False): print(2) print(l[i][0]+1, l[i][1]+1) print(l[j][0]+1, l[j][1]+1) f1 = 1 break
8
PYTHON3
from sys import stdin,stdout from math import gcd,sqrt,factorial,pi,inf from collections import deque,defaultdict input=stdin.readline R=lambda:map(int,input().split()) I=lambda:int(input()) S=lambda:input().rstrip('\n') L=lambda:list(R()) P=lambda x:stdout.write(str(x)+'\n') lcm=lambda x,y:(x*y)//gcd(x,y) hg=lambda x,y:((y+x-1)//x)*x pw=lambda x:1 if x==1 else 1+pw(x//2) chk=lambda x:chk(x//2) if not x%2 else True if x==1 else False sm=lambda x:(x**2+x)//2 N=10**6+6 for _ in range(I()): n=I() a=[S() for i in range(n)] cnt=0 ans=[] if a[0][1]!='0':cnt+=1;ans+=(1,2), if a[1][0]!='0':cnt+=1;ans+=(2,1), if a[-1][-2]!='1':cnt+=1;ans+=(n,n-1), if a[-2][-1]!='1':cnt+=1;ans+=(n-1,n), if cnt<=2: print(cnt) for i in ans:print(*i) continue cnt=0 ans=[] if a[0][1]!='1':cnt+=1;ans+=(1,2), if a[1][0]!='1':cnt+=1;ans+=(2,1), if a[-1][-2]!='0':cnt+=1;ans+=(n,n-1), if a[-2][-1]!='0':cnt+=1;ans+=(n-1,n), print(cnt) for i in ans:print(*i)
8
PYTHON3
from math import * from bisect import * from collections import * from random import * from decimal import * from itertools import * import sys input=sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip('\n') def lis(): return list(map(int,input().split())) def ma(): return map(int,input().split()) t=inp() while(t): t-=1 n=inp() re=[] r=[] for i in range(n): s=st() s=list(s) re.append(s) li=[re[0][1],re[0][2],re[1][1],re[1][0],re[2][0]] for i in range(32): x=bin(i)[2:] x=x.zfill(5) inc=0 for j in range(5): if(li[j]!=x[j]): inc+=1 if(inc<=2 and (x[2]!=x[0] and x[0]!=x[1]) and (x[3]!=x[2] and x[4]!=x[3])): print(inc) for j in range(5): if(li[j]!=x[j]): if(j==0): print(1,2) if(j==1): print(1,3) if(j==2): print(2,2) if(j==3): print(2,1) if(j==4): print(3,1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int mod(long long int x) { if (x >= 0) { return x; } return -x; } vector<int> decToBinary(int n) { vector<int> binaryNum(32, 0); int i = 0; while (n > 0) { binaryNum[i] = n % 2; n = n / 2; i++; } return binaryNum; } void solve(vector<vector<char>> v, long long int n) { char c1 = v[0][1]; char c2 = v[1][0]; char c3 = v[n - 1][n - 2]; char c4 = v[n - 2][n - 1]; if (c1 == c2 && c3 == c4 && c2 != c3) { cout << "0" << endl; return; } if (c1 == c2 && c3 == c4 && c3 == c2) { cout << "2" << endl; cout << "1" << " " << "2" << endl; cout << "2" << " " << "1" << endl; return; } if (c1 == c2 && c3 != c4) { if (c3 == c2) { cout << "1" << endl; cout << n << " " << n - 1 << endl; return; } else { cout << "1" << endl; cout << n - 1 << " " << n << endl; return; } } if (c3 == c4 && c2 != c1) { if (c3 == c2) { cout << "1" << endl; cout << "2" << " " << "1" << endl; return; } else { cout << "1" << endl; cout << "1" << " " << "2" << endl; return; } } cout << "2" << endl; if (c1 == '0') { cout << "1" << " " << "2" << endl; } else { cout << "2" << " " << "1" << endl; } if (c3 == '1') { cout << n << " " << n - 1 << endl; } else { cout << n - 1 << " " << n << endl; } return; } int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; vector<vector<char>> v; for (long long int i = 0; i < n; i++) { vector<char> temp; for (long long int j = 0; j < n; j++) { char c; cin >> c; temp.push_back(c); } v.push_back(temp); } solve(v, n); } }
8
CPP
from collections import * from functools import * import math import sys # sys.stdin = open("Desktop//ip.txt",'r') # sys.stdout = open("Desktop//op.txt",'w') inp = sys.stdin.readline out = sys.stdout.write def mi(): return map(int,inp().split()) def li(): return list(mi()) def ii(): return int(inp()) def main(): for _ in range(ii()): n = ii() grid = [] for _ in range(n): grid.append(input()) ans = [] ; ans1 = [] a = grid[0][1] b = grid[1][0] c = grid[-1][-2] d = grid[-2][-1] if a != '0': ans.append([1,2]) if b != '0': ans.append([2,1]) if c != '1': ans.append([n, n-1]) if d != '1': ans.append([n-1, n]) if a != '1': ans1.append([1,2]) if b != '1': ans1.append([2,1]) if c != '0': ans1.append([n, n-1]) if d != '0': ans1.append([n-1, n]) if len(ans) <= len(ans1): print(len(ans)) for i in ans: print(*i) else: print(len(ans1)) for i in ans1: print(*i) main()
8
PYTHON3