text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
template <typename T>
T min(T x, T y) {
return x < y ? x : y;
}
template <typename T>
T max(T x, T y) {
return x > y ? x : y;
};
const long long INF = 20000000050000;
const long long mod = 998244353;
const long long MAXN = 205;
int N;
char s[MAXN][MAXN];
void invert(char ch) {
if (s[1][3] != ch) printf("1 3\n");
if (s[2][2] != ch) printf("2 2\n");
if (s[3][1] != ch) printf("3 1\n");
}
void solve() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%s", s[i] + 1);
int cnt = 0;
cnt = (s[1][3] == '1') + (s[2][2] == '1') + (s[3][1] == '1');
if (s[1][2] == s[2][1]) {
if ((cnt == 0 || cnt == 3)) {
if (s[1][2] == s[1][3]) {
printf("2\n");
printf("1 2\n");
printf("2 1\n");
} else
printf("0\n");
} else {
if (s[1][2] == '1') {
printf("%d\n", cnt);
invert('0');
} else {
printf("%d\n", 3 - cnt);
invert('1');
}
}
} else {
if ((cnt == 0 || cnt == 3)) {
printf("1\n");
if (s[1][2] == s[1][3])
printf("1 2\n");
else
printf("2 1\n");
} else {
printf("2\n");
if (cnt == 2)
invert('1');
else
invert('0');
char t = (cnt == 2 ? '1' : '0');
if (s[1][2] == t)
printf("1 2\n");
else
printf("2 1\n");
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char ch[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> ch[i][j];
int co = 0;
pair<int, int> p[2];
if (ch[0][1] == ch[1][0]) {
char val;
if (ch[0][1] == '0')
val = '1';
else
val = '0';
if (ch[n - 1][n - 2] != val) {
p[co].first = n - 1;
p[co].second = n - 2;
co++;
}
if (ch[n - 2][n - 1] != val) {
p[co].first = n - 2;
p[co].second = n - 1;
co++;
}
} else if (ch[n - 1][n - 2] == ch[n - 2][n - 1]) {
char val;
if (ch[n - 1][n - 2] == '0')
val = '1';
else
val = '0';
if (ch[0][1] != val) {
p[co].first = 0;
p[co].second = 1;
co++;
}
if (ch[1][0] != val) {
p[co].first = 1;
p[co].second = 0;
co++;
}
} else {
if (ch[0][1] == '0') {
p[co].first = 0;
p[co].second = 1;
co++;
}
if (ch[1][0] == '0') {
p[co].first = 1;
p[co].second = 0;
co++;
}
if (ch[n - 1][n - 2] == '1') {
p[co].first = n - 1;
p[co].second = n - 2;
co++;
}
if (ch[n - 2][n - 1] == '1') {
p[co].first = n - 2;
p[co].second = n - 1;
co++;
}
}
cout << co << "\n";
for (int i = 0; i < co; i++) {
cout << p[i].first + 1 << " " << p[i].second + 1 << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
int a[N][N];
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < s.size(); j++) a[i][j] = s[j] - '0';
}
vector<pair<int, int>> targets;
targets.push_back({0, 1});
targets.push_back({1, 0});
targets.push_back({N - 1, N - 2});
targets.push_back({N - 2, N - 1});
a[N - 1][N - 2] ^= 1;
a[N - 2][N - 1] ^= 1;
int one = 0, zero = 0;
for (int inv = 0; inv <= 2; inv++) {
vector<pair<int, int>> inverts;
for (auto Ti : targets) {
if (a[Ti.first][Ti.second] != inv) inverts.push_back(Ti);
}
if (inverts.size() <= 2) {
cout << inverts.size() << '\n';
for (auto& i : inverts)
cout << i.first + 1 << ' ' << i.second + 1 << '\n';
break;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
char mat[201][201];
int main() {
fast();
int n, t;
bool p, q;
cin >> t;
for (int cas = 0; cas < t; cas++) {
int x = 0;
q = p = false;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> mat[i][j];
if (mat[0][1] == mat[1][0]) p = true;
if (mat[n - 1][n - 2] == mat[n - 2][n - 1]) q = true;
if (q && p) {
if (mat[0][1] == mat[n - 1][n - 2])
cout << 2 << endl << 1 << " " << 2 << endl << 2 << " " << 1 << endl;
else
cout << 0 << endl;
} else if (!p && !q) {
cout << 2 << endl;
if (mat[0][1] == '0')
cout << 2 << " " << 1 << endl;
else
cout << 1 << " " << 2 << endl;
if (mat[n - 1][n - 2] == '0')
cout << n << " " << n - 1 << endl;
else
cout << n - 1 << " " << n << endl;
} else if (p && !q) {
cout << 1 << endl;
if (mat[0][1] == mat[n - 1][n - 2])
cout << n << " " << n - 1 << endl;
else
cout << n - 1 << " " << n << endl;
} else if (!p && q) {
cout << 1 << endl;
if (mat[0][1] == mat[n - 1][n - 2])
cout << 1 << " " << 2 << endl;
else
cout << 2 << " " << 1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
char m[n][n];
for (long long int i = (0); i < (n); i++) {
for (long long int j = (0); j < (n); j++) cin >> m[i][j];
}
if (m[0][1] == '0' and m[1][0] == '0') {
if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << '2' << '\n';
cout << n - 1 << " " << n << '\n';
cout << n << " " << n - 1 << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << "0" << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "1" << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << "1" << '\n';
cout << n - 1 << " " << n << '\n';
}
}
if (m[0][1] == '1' and m[1][0] == '1') {
if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << '2' << '\n';
cout << n - 1 << " " << n << '\n';
cout << n << " " << n - 1 << '\n';
} else if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << "0" << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "1" << '\n';
cout << n - 1 << " " << n << '\n';
} else {
cout << "1" << '\n';
cout << n << " " << n - 1 << '\n';
}
}
if (m[0][1] == '1' and m[1][0] == '0') {
if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << "1" << '\n';
cout << 1 << " " << 2 << '\n';
} else if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "2" << '\n';
cout << 1 << " " << 2 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << "2" << '\n';
cout << 1 << " " << 2 << '\n';
cout << n - 1 << " " << n << '\n';
}
}
if (m[0][1] == '0' and m[1][0] == '1') {
if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '1') {
cout << "1" << '\n';
cout << 2 << " " << 1 << '\n';
} else if (m[n - 2][n - 1] == '0' and m[n - 1][n - 2] == '0') {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
} else if (m[n - 2][n - 1] == '1' and m[n - 1][n - 2] == '0') {
cout << "2" << '\n';
cout << 2 << " " << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << "2" << '\n';
cout << 2 << " " << 1 << '\n';
cout << n - 1 << " " << n << '\n';
}
}
}
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
vector<pair<long long int, long long int>> ans;
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[n - 1][n - 2] == '0') {
if (a[0][1] == '0') {
ans.push_back({1, 2});
}
if (a[1][0] == '0') {
ans.push_back({2, 1});
}
} else {
if (a[0][1] == '1') {
ans.push_back({1, 2});
}
if (a[1][0] == '1') {
ans.push_back({2, 1});
}
}
} else {
if (a[1][0] == '0' && a[0][1] == '0') {
if (a[n - 1][n - 2] == '0') {
ans.push_back({n, n - 1});
}
if (a[n - 2][n - 1] == '0') {
ans.push_back({n - 1, n});
}
} else if (a[1][0] == '1' && a[0][1] == '1') {
if (a[n - 1][n - 2] == '1') {
ans.push_back({n, n - 1});
}
if (a[n - 2][n - 1] == '1') {
ans.push_back({n - 1, n});
}
} else if (a[n - 1][n - 2] == '1') {
ans.push_back({n, n - 1});
if (a[0][1] == '0') {
ans.push_back({1, 2});
}
if (a[1][0] == '0') {
ans.push_back({2, 1});
}
} else {
ans.push_back({n, n - 1});
if (a[0][1] == '1') {
ans.push_back({1, 2});
}
if (a[1][0] == '1') {
ans.push_back({2, 1});
}
}
}
cout << ((int)(ans).size()) << "\n";
for (auto it : ans) {
cout << it.first << " " << it.second << "\n";
}
}
}
|
#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;
}
|
#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;
}
|
#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;
}
|
#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;
}
|
#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';
}
}
}
|
#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;
}
|
#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;
}
|
#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();
}
}
|
#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();
}
|
#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;
}
|
#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;
}
|
#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;
}
|
#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();
}
}
|
#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();
}
}
|
#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();
}
|
#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;
}
|
#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();
}
}
}
}
|
#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();
}
|
#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;
}
|
#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();
}
}
|
#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;
}
|
#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;
}
|
#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;
}
|
#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;
}
|
#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;
}
|
#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;
}
|
#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);
}
}
|
#include <bits/stdc++.h>
using namespace std;
bool db = false;
const long long INF = 1e18 + 100;
const long long MOD = 1e9 + 7;
const long long maxn = 2 * 1e6 + 1;
long long t, n, m, x, k, q;
long long a, b, c, d;
pair<long long, long long> pa, pb, pc, pd;
long long ans = 0;
string s[200];
vector<pair<long long, long long> > v;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
a = (s[1][0] == '1');
b = (s[0][1] == '1');
c = (s[n - 1][n - 2] == '1');
d = (s[n - 2][n - 1] == '1');
if (db) {
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "c: " << c << endl;
cout << "d: " << d << endl;
cout << endl;
}
pa = {1, 0};
pb = {0, 1};
pc = {n - 1, n - 2};
pd = {n - 2, n - 1};
ans = 0;
v.clear();
if (a == 0 and b == 0) {
if (c == 0) v.push_back(pc);
if (d == 0) v.push_back(pd);
} else if (a == 1 and b == 1) {
if (c == 1) v.push_back(pc);
if (d == 1) v.push_back(pd);
} else if (a == 0 and b == 1) {
if (c == 0 and d == 0) {
v.push_back(pa);
} else if (c == 0 and d == 1) {
v.push_back(pb);
v.push_back(pc);
} else if (c == 1 and d == 0) {
v.push_back(pb);
v.push_back(pd);
} else if (c == 1 and d == 1) {
v.push_back(pb);
}
} else if (a == 1 and b == 0) {
if (c == 0 and d == 0) {
v.push_back(pb);
} else if (c == 0 and d == 1) {
v.push_back(pb);
v.push_back(pd);
} else if (c == 1 and d == 0) {
v.push_back(pb);
v.push_back(pc);
} else if (c == 1 and d == 1) {
v.push_back(pa);
}
}
ans = v.size();
cout << ans << endl;
for (auto xy : v) {
cout << xy.first + 1 << " " << xy.second + 1 << endl;
}
if (db) {
cout << endl << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool check(vector<vector<int>> &a, int n, int x) {
deque<pair<int, int>> q;
map<pair<int, int>, bool> was;
q.push_back({1, 1});
was[{1, 1}] = true;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
while (!q.empty()) {
auto u = q.front();
q.pop_front();
for (int i = 0; i < 4; i++) {
int xx = u.first + dx[i];
int yy = u.second + dy[i];
if (xx == n && yy == n) return false;
if (a[xx][yy] == x && !was[{xx, yy}]) {
q.push_back({xx, yy});
was[{xx, yy}] = true;
}
}
}
return true;
}
void v(vector<vector<int>> &a, int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << a[i][j];
}
cout << endl;
}
}
void solve() {
vector<vector<int>> a;
int n;
cin >> n;
a = vector<vector<int>>(n + 2, vector<int>(n + 2, 2));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
char x;
cin >> x;
if (x != 'S' && x != 'F') {
a[i][j] = x - '0';
}
}
}
vector<pair<int, int>> c = {
{n - 1, n - 1}, {n - 1, n}, {n, n - 1}, {n - 2, n}, {n, n - 2}};
if (check(a, n, 1) && check(a, n, 0)) {
cout << 0 << endl;
return;
}
for (int i = 0; i < c.size(); i++) {
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
if (check(a, n, 1) && check(a, n, 0)) {
cout << 1 << '\n';
cout << c[i].first << ' ' << c[i].second << endl;
return;
}
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
}
for (int i = 0; i < c.size(); i++) {
for (int j = i + 1; j < c.size(); j++) {
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
a[c[j].first][c[j].second] = 1 - a[c[j].first][c[j].second];
if (check(a, n, 1) && check(a, n, 0)) {
cout << 2 << '\n';
cout << c[i].first << ' ' << c[i].second << endl;
cout << c[j].first << ' ' << c[j].second << endl;
return;
}
a[c[i].first][c[i].second] = 1 - a[c[i].first][c[i].second];
a[c[j].first][c[j].second] = 1 - a[c[j].first][c[j].second];
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 2e18;
const long long int N = 2e5 + 5;
bool isPrime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
long long int factorial(long long int n) {
long long int ans = 1;
for (long long int i = 1; i <= n; i++) ans = (ans * i) % 1000000007;
return ans;
}
long long int exp(long long int x, long long int n) {
long long int res = 1;
while (n > 0) {
if (n % 2 == 1) res = (res * x) % 1000000007;
x = (x * x) % 1000000007;
n = n / 2;
}
return res;
}
void null() {
long long int n;
cin >> n;
char a[n][n];
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
long long int ans = 0;
if (a[0][1] == a[1][0]) {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[n - 1][n - 2] == a[0][1]) {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
cout << 2 << " " << 1 << '\n';
return;
} else {
cout << 0 << '\n';
return;
}
}
if (a[n - 1][n - 2] != a[n - 2][n - 1]) {
if (a[n - 1][n - 2] == a[0][1]) {
cout << 1 << '\n';
cout << n << " " << n - 1 << '\n';
return;
} else if (a[n - 2][n - 1] == a[0][1]) {
cout << 1 << '\n';
cout << n - 1 << " " << n << '\n';
return;
}
}
} else {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[0][1] == a[n - 1][n - 2]) {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
return;
} else {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
return;
}
} else {
cout << 2 << '\n';
if (a[0][1] == a[n - 1][n - 2]) {
cout << 1 << " " << 2 << '\n';
cout << n - 1 << " " << n << '\n';
return;
} else if (a[0][1] == a[n - 2][n - 1]) {
cout << 1 << " " << 2 << '\n';
cout << n << " " << n - 1 << '\n';
return;
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t = 1;
clock_t start, end;
start = clock();
cin >> t;
while (t--) {
null();
}
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, i, j, c;
char chr;
cin >> t;
bool first_diag, second_diag;
while (t--) {
cin >> n;
string arr[n];
vector<int> v;
c = 0;
for (i = 0; i < n; i++) cin >> arr[i];
first_diag = (arr[0][1] == arr[1][0]);
second_diag = (arr[n - 2][n - 1] == arr[n - 1][n - 2]);
if (first_diag && second_diag) {
if (arr[n - 2][n - 1] == arr[0][1]) {
c = 2;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(1);
}
} else if (first_diag) {
c = 1;
if (arr[n - 1][n - 2] == arr[0][1]) {
v.push_back(n);
v.push_back(n - 1);
} else {
v.push_back(n - 1);
v.push_back(n);
}
} else if (second_diag) {
c = 1;
if (arr[n - 1][n - 2] == arr[0][1]) {
v.push_back(1);
v.push_back(2);
} else {
v.push_back(2);
v.push_back(1);
}
} else {
c = 2;
v.push_back(1);
v.push_back(2);
if (arr[0][1] == arr[n - 2][n - 1]) {
v.push_back(n);
v.push_back(n - 1);
} else {
v.push_back(n - 1);
v.push_back(n);
}
}
cout << c << "\n";
for (i = 0; i < c; i++) cout << v[2 * i] << " " << v[2 * i + 1] << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<string> a;
void input() {
a.clear();
cin >> n;
for (int i = 0; i < n; i++) {
string k;
cin >> k;
a.push_back(k);
}
}
void solve() {
long long int z = 0, o = 0;
if (a[0][1] == '0')
z++;
else
o++;
if (a[1][0] == '0')
z++;
else
o++;
if (a[n - 2][n - 1] == '0')
z++;
else
o++;
if (a[n - 1][n - 2] == '0')
z++;
else
o++;
if (a[0][1] == a[1][0] && a[n - 2][n - 1] == a[n - 1][n - 2] &&
a[0][1] == a[n - 2][n - 1]) {
cout << 2 << endl << "1 2" << endl << "2 1" << endl;
} else if (a[0][1] == a[1][0] && a[n - 2][n - 1] == a[n - 1][n - 2] &&
a[0][1] != a[n - 2][n - 1]) {
cout << 0 << endl;
} else if (z == 1) {
if (a[0][1] == '0')
cout << 1 << endl << "2 1" << endl;
else if (a[1][0] == '0')
cout << 1 << endl << "1 2" << endl;
else if (a[n - 2][n - 1] == '0')
cout << 1 << endl << n << " " << n - 1 << endl;
else if (a[n - 1][n - 2] == '0')
cout << 1 << endl << n - 1 << " " << n << endl;
} else if (o == 1) {
if (a[0][1] == '1')
cout << 1 << endl << "2 1" << endl;
else if (a[1][0] == '1')
cout << 1 << endl << "1 2" << endl;
else if (a[n - 2][n - 1] == '1')
cout << 1 << endl << n << " " << n - 1 << endl;
else if (a[n - 1][n - 2] == '1')
cout << 1 << endl << n - 1 << " " << n << endl;
} else {
cout << 2 << endl;
if (a[0][1] == '1') cout << "2 1" << endl;
if (a[1][0] == '1') cout << "1 2" << endl;
if (a[n - 2][n - 1] == '0') cout << n << " " << n - 1 << endl;
if (a[n - 1][n - 2] == '0') cout << n - 1 << " " << n << endl;
}
}
int main() {
long long int t;
cin >> t;
while (t--) {
input();
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;
vector<pair<int, int>> del{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool dfsf(int n, vector<string>& g) {
vector<vector<bool>> v(n, vector<bool>(n, true));
queue<pair<int, int>> q;
q.push({0, 0});
while (!q.empty()) {
auto tp = q.front();
q.pop();
if (v[tp.first][tp.second]) {
v[tp.first][tp.second] = false;
for (auto x : del) {
int xx = x.first + tp.first, yy = x.second + tp.second;
if (xx >= 0 && yy >= 0 && xx < n && yy < n && v[xx][yy] &&
g[xx][yy] == '.')
q.push({xx, yy});
}
}
}
return !v[n - 1][n - 1];
}
bool f(int n, vector<string> g) {
vector<string> g1(n, string(n, '.')), g2(n, string(n, '.'));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (g[i][j] == '1')
g1[i][j] = '#';
else if (g[i][j] == '0')
g2[i][j] = '#';
return dfsf(n, g1) || dfsf(n, g2);
}
void solve() {
int n;
cin >> n;
vector<string> g(n);
for (int i = 0; i < n; i++) cin >> g[i];
if (!f(n, g)) {
cout << "0\n";
return;
}
vector<pair<int, int>> nodes{{1, 0}, {0, 1}, {n - 1, n - 2}, {n - 2, n - 1}};
for (auto x : nodes) {
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
if (!f(n, g)) {
cout << "1\n";
cout << x.first + 1 << " " << x.second + 1 << "\n";
return;
}
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
}
for (auto x : nodes)
for (auto y : nodes) {
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
g[y.first][y.second] = (g[y.first][y.second] == '1' ? '0' : '1');
if (!f(n, g)) {
cout << "2\n";
cout << x.first + 1 << " " << x.second + 1 << " " << y.first + 1 << " "
<< y.second + 1 << "\n";
return;
}
g[x.first][x.second] = (g[x.first][x.second] == '1' ? '0' : '1');
g[y.first][y.second] = (g[y.first][y.second] == '1' ? '0' : '1');
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long maxi = LLONG_MAX;
long long mini = LLONG_MIN;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int main() {
fast();
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
char a[n][n];
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
cin >> a[i][j];
}
}
vector<pair<long long, long long> > ans;
if (a[0][1] + a[1][0] == '0' + '0') {
if (a[n - 1][n - 2] == '0') ans.push_back({n, n - 1});
if (a[n - 2][n - 1] == '0') ans.push_back({n - 1, n});
} else if (a[0][1] + a[1][0] == '1' + '1') {
if (a[n - 1][n - 2] == '1') ans.push_back({n, n - 1});
if (a[n - 2][n - 1] == '1') ans.push_back({n - 1, n});
} else if (a[n - 1][n - 2] + a[n - 2][n - 1] == '1' + '1') {
if (a[0][1] == '1') ans.push_back({1, 2});
if (a[1][0] == '1') ans.push_back({2, 1});
} else if (a[n - 1][n - 2] + a[n - 2][n - 1] == '0' + '0') {
if (a[0][1] == '0') ans.push_back({1, 2});
if (a[1][0] == '0') ans.push_back({2, 1});
} else if ((a[0][1] + a[1][0] == '1' + '0') &&
(a[n - 1][n - 2] + a[n - 2][n - 1] == '1' + '0')) {
if (a[n - 1][n - 2] == '0') ans.push_back({n, n - 1});
if (a[n - 2][n - 1] == '0') ans.push_back({n - 1, n});
if (a[0][1] == '1') ans.push_back({1, 2});
if (a[1][0] == '1') ans.push_back({2, 1});
}
cout << ans.size() << endl;
for (auto p : ans) {
cout << p.first << ' ' << p.second << "\n";
;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e18;
const long long minN = -1e18;
const int INF = 2e9;
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const long double PI = acos(-1);
const long long limit = 2e5 + 5;
const long long limit1 = 1e6 + 5;
const long long limit2 = 1e3 + 5;
pair<int, int> dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
pair<int, int> NON = {-1, -1};
int t, n;
char c[250][250];
void solveProblem() {
if (c[1][2] == c[2][1]) {
if (c[n - 1][n] == c[n][n - 1]) {
if (c[1][2] == c[n - 1][n]) {
cout << 2 << '\n' << 1 << " " << 2 << '\n' << 2 << " " << 1 << '\n';
}
if (c[1][2] != c[n - 1][n]) {
cout << 0 << '\n';
}
} else {
if (c[1][2] == c[n - 1][n]) {
cout << 1 << '\n' << n - 1 << " " << n << '\n';
} else
cout << 1 << '\n' << n << " " << n - 1 << '\n';
}
} else {
if (c[n][n - 1] == c[n - 1][n]) {
if (c[n][n - 1] == c[1][2]) {
cout << 1 << '\n' << 1 << " " << 2 << '\n';
} else {
cout << 1 << '\n' << 2 << " " << 1 << '\n';
}
} else {
if (c[1][2] == c[n][n - 1]) {
cout << 2 << '\n'
<< 1 << " " << 2 << '\n'
<< " " << n - 1 << " " << n << '\n';
} else {
cout << 2 << '\n'
<< 1 << " " << 2 << '\n'
<< " " << n << " " << n - 1 << '\n';
}
}
}
}
void fastInput() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void readInput() {
cin >> t;
while (t--) {
cin >> n;
for (int i = (1); i <= (n); ++i) {
for (int j = (1); j <= (n); ++j) {
cin >> c[i][j];
}
}
solveProblem();
}
}
int main() {
fastInput();
readInput();
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n;
char s[207][207];
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> s[i][j];
}
}
int cnt0 = 0, cnt1 = 0;
if (s[1][2] == '0') {
cnt0++;
} else {
cnt1++;
}
if (s[2][1] == '0') {
cnt0++;
} else {
cnt1++;
}
if (s[n - 1][n] == '0') {
cnt0++;
} else {
cnt1++;
}
if (s[n][n - 1] == '0') {
cnt0++;
} else {
cnt1++;
}
if (cnt0 == 4 || cnt1 == 4) {
cout << "2" << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (cnt0 > cnt1) {
if (s[1][2] == '1') {
cout << "1" << endl;
cout << "2 1" << endl;
}
if (s[2][1] == '1') {
cout << "1" << endl;
cout << "1 2" << endl;
}
if (s[n - 1][n] == '1') {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
}
if (s[n][n - 1] == '1') {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
}
} else if (cnt0 < cnt1) {
if (s[1][2] == '0') {
cout << "1" << endl;
cout << "2 1" << endl;
}
if (s[2][1] == '0') {
cout << "1" << endl;
cout << "1 2" << endl;
}
if (s[n - 1][n] == '0') {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
}
if (s[n][n - 1] == '0') {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
}
} else {
if (s[1][2] == s[2][1]) {
cout << "0" << endl;
} else {
cout << "2" << endl;
if (s[1][2] == '0') {
cout << "1 2" << endl;
}
if (s[2][1] == '0') {
cout << "2 1" << endl;
}
if (s[n - 1][n] == '1') {
cout << n - 1 << " " << n << endl;
}
if (s[n][n - 1] == '1') {
cout << n << " " << n - 1 << endl;
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
char s[205][205];
int main() {
long long a, b, t, n, m, i, j;
cin >> t;
while (t--) {
cin >> n;
cin.get();
for (i = 0; i <= n; i++)
for (j = 0; j <= n; j++) s[i][j] = '0';
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) cin >> s[i][j];
if (s[n][n - 1] == s[n - 1][n]) {
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[1][2]) {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
cout << 2 << " " << 1 << '\n';
} else
cout << 0 << '\n';
} else {
if (s[1][2] == s[n][n - 1]) {
cout << 1 << '\n';
cout << 1 << " " << 2 << '\n';
} else {
cout << 1 << '\n';
cout << 2 << " " << 1 << '\n';
}
}
} else {
if (s[1][2] == s[2][1]) {
if (s[1][2] == s[n][n - 1]) {
cout << 1 << '\n';
cout << n << " " << n - 1 << '\n';
} else {
cout << 1 << '\n';
cout << n - 1 << " " << n << '\n';
}
} else {
if (s[1][2] == s[n][n - 1]) {
cout << 2 << '\n';
cout << 1 << " " << 2 << '\n';
cout << n - 1 << " " << n << '\n';
} else {
cout << 2 << '\n';
cout << 2 << " " << 1 << '\n';
cout << n - 1 << " " << n << '\n';
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class A>
void read(vector<A>& v);
template <class T>
void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d = stod(t);
}
void read(long double& d) {
string t;
read(t);
d = stold(t);
}
template <class H, class... T>
void read(H& h, T&... t) {
read(h);
read(t...);
}
template <class A>
void read(vector<A>& x) {
for (auto& a : x) read(a);
}
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return string(s); }
string to_string(vector<bool> v) {
string res;
for (int i = 0; i < (int)(v).size(); i++) {
res += char('0' + v[i]);
}
return res;
}
template <class T>
string to_string(T v) {
bool f = 1;
string res;
for (auto& x : v) {
if (!f) res += ' ';
f = 0;
res += to_string(x);
}
return res;
}
template <class A>
void write(A x) {
cout << to_string(x);
}
template <class H, class... T>
void write(const H& h, const T&... t) {
write(h);
write(t...);
}
void print() { write("\n"); }
template <class H, class... T>
void print(const H& h, const T&... t) {
write(h);
if (sizeof...(t)) write(' ');
print(t...);
}
void pre() {}
void solve() {
int n;
read(n);
vector<string> s(n);
for (int i = 0; i < n; i++) {
read(s[i]);
}
vector<pair<int, int>> ans;
int tot = 0;
if (s[0][1] == '1') {
tot++;
ans.push_back({0, 1});
}
if (s[1][0] == '1') {
tot++;
ans.push_back({1, 0});
}
if (s[2][0] == '0') {
tot++;
ans.push_back({2, 0});
}
if (s[1][1] == '0') {
tot++;
ans.push_back({1, 1});
}
if (s[0][2] == '0') {
tot++;
ans.push_back({0, 2});
}
if (tot <= 2) {
print(tot);
for (auto itr : ans) {
cout << itr.first + 1 << " " << itr.second + 1 << "\n";
}
return;
}
tot = 0;
ans.clear();
if (s[0][1] == '0') {
tot++;
ans.push_back({0, 1});
}
if (s[1][0] == '0') {
tot++;
ans.push_back({1, 0});
}
if (s[2][0] == '1') {
tot++;
ans.push_back({2, 0});
}
if (s[1][1] == '1') {
tot++;
ans.push_back({1, 1});
}
if (s[0][2] == '1') {
tot++;
ans.push_back({0, 2});
}
if (tot <= 2) {
print(tot);
for (auto itr : ans) {
cout << itr.first + 1 << " " << itr.second + 1 << "\n";
}
return;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pre();
int t = 1;
read(t);
for (int i = 0; i < t; i++) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100001;
const int MAX_D = 17;
const long long MOD = 1000000007;
const int INF32 = 0x3f3f3f3f;
const long long INF = 1e18;
void solve() {
int n;
cin >> n;
char grid[n][n];
for (int i = (0); i < (n); i++)
for (int j = (0); j < (n); j++) cin >> grid[i][j];
int a = grid[1][0] + grid[0][1] - 2 * '0',
b = grid[n - 1][n - 2] + grid[n - 2][n - 1] - 2 * '0';
if (a == 2) {
cout << b << endl;
if (grid[n - 1][n - 2] == '1') cout << n << ' ' << n - 1 << endl;
if (grid[n - 2][n - 1] == '1') cout << n - 1 << ' ' << n << endl;
}
if (!a) {
cout << 2 - b << endl;
if (grid[n - 1][n - 2] == '0') cout << n << ' ' << n - 1 << endl;
if (grid[n - 2][n - 1] == '0') cout << n - 1 << ' ' << n << endl;
}
if (a == 1) {
if (b == 1) {
cout << 2 << endl;
if (grid[0][1] == '0') cout << "1 2\n";
if (grid[1][0] == '0') cout << "2 1\n";
if (grid[n - 1][n - 2] == '1') cout << n << ' ' << n - 1 << endl;
if (grid[n - 2][n - 1] == '1') cout << n - 1 << ' ' << n << endl;
}
if (b == 2) {
cout << 1 << endl;
if (grid[0][1] == '1') cout << "1 2\n";
if (grid[1][0] == '1') cout << "2 1\n";
}
if (!b) {
cout << 1 << endl;
if (grid[0][1] == '0') cout << "1 2\n";
if (grid[1][0] == '0') cout << "2 1\n";
}
}
return;
}
int main(int argc, const char* argv[]) {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
inline void solve() {
int n;
cin >> n;
char A[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
}
}
bool top = false;
bool bottom = false;
if (A[1][0] == A[0][1]) {
top = true;
}
if (A[n - 1][n - 2] == A[n - 2][n - 1]) {
bottom = true;
}
if (top && bottom) {
if (A[1][0] != A[n - 1][n - 2])
cout << 0 << "\n";
else {
cout << 2 << "\n";
cout << n << " " << n - 1 << "\n";
cout << n - 1 << " " << n << "\n";
}
} else if (top) {
cout << 1 << "\n";
if (A[n - 1][n - 2] == A[1][0]) {
cout << n << " " << n - 1 << "\n";
} else {
cout << n - 1 << " " << n << "\n";
}
} else if (bottom) {
cout << 1 << "\n";
if (A[n - 1][n - 2] == A[1][0]) {
cout << 2 << " " << 1 << "\n";
} else {
cout << 1 << " " << 2 << "\n";
}
} else {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
if (A[n - 1][n - 2] == A[1][0]) {
cout << n << " " << n - 1 << "\n";
} else {
cout << n - 1 << " " << n << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char s[n + 1][n + 1];
for (int i = 1; i <= n; ++i) cin >> s[i] + 1;
vector<pair<int, int>> ans;
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[1][2]) ans.push_back(pair<int, int>(n, n - 1));
if (s[n - 1][n] == s[1][2]) ans.push_back(pair<int, int>(n - 1, n));
} else if (s[n][n - 1] == s[n - 1][n]) {
if (s[n][n - 1] == s[1][2]) ans.push_back(pair<int, int>(1, 2));
if (s[n - 1][n] == s[2][1]) ans.push_back(pair<int, int>(2, 1));
} else {
if (s[1][2] == '1')
ans.push_back(pair<int, int>(1, 2));
else
ans.push_back(pair<int, int>(2, 1));
if (s[n][n - 1] == '0')
ans.push_back(pair<int, int>(n, n - 1));
else
ans.push_back(pair<int, int>(n - 1, n));
}
cout << ans.size() << '\n';
for (auto x : ans) cout << x.first << " " << x.second << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char fi[n][n];
for (int i = 0; i < (n); ++i) {
for (int j = 0; j < (n); ++j) {
cin >> fi[i][j];
}
}
int a = fi[0][1] - '0';
int b = fi[1][0] - '0';
int c = fi[n - 2][n - 1] - '0';
int d = fi[n - 1][n - 2] - '0';
if (a == 0 && b == 0 && c == 0 && d == 0) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
} else if (a == 0 && b == 0 && c == 0 && d == 1) {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 0 && b == 0 && c == 1 && d == 0) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 0 && b == 0 && c == 1 && d == 1) {
cout << 0 << endl;
} else if (a == 0 && b == 1 && c == 0 && d == 0) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else if (a == 0 && b == 1 && c == 0 && d == 1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 0 && b == 1 && c == 1 && d == 0) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 0 && b == 1 && c == 1 && d == 1) {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
} else if (a == 1 && b == 0 && c == 0 && d == 0) {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
} else if (a == 1 && b == 0 && c == 0 && d == 1) {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 1 && b == 0 && c == 1 && d == 0) {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 1 && b == 0 && c == 1 && d == 1) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else if (a == 1 && b == 1 && c == 0 && d == 0) {
cout << 0 << endl;
} else if (a == 1 && b == 1 && c == 0 && d == 1) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if (a == 1 && b == 1 && c == 1 && d == 0) {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if (a == 1 && b == 1 && c == 1 && d == 1) {
cout << 2 << endl;
cout << n - 1 << " " << n << endl;
cout << n << " " << n - 1 << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long T = 1;
cin >> T;
while (T--) {
long long n;
cin >> n;
char a[n][n];
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++) cin >> a[i][j];
vector<pair<long long, long long> > v;
if (a[1][0] == '0' && a[0][1] == '0') {
if (a[n - 1][n - 2] != '1') v.push_back({n - 1, n - 2});
if (a[n - 2][n - 1] != '1') v.push_back({n - 2, n - 1});
} else if (a[1][0] == '0' && a[0][1] == '1') {
if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '0') {
v.push_back({1, 0});
} else if (a[n - 1][n - 2] == '1' && a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n - 2});
v.push_back({1, 0});
} else if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '1') {
v.push_back({n - 2, n - 1});
v.push_back({1, 0});
} else {
v.push_back({0, 1});
}
} else if (a[1][0] == '1' && a[0][1] == '1') {
if (a[n - 1][n - 2] != '0') v.push_back({n - 1, n - 2});
if (a[n - 2][n - 1] != '0') v.push_back({n - 2, n - 1});
} else if (a[1][0] == '1' && a[0][1] == '0') {
if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '0') {
v.push_back({0, 1});
} else if (a[n - 1][n - 2] == '1' && a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n - 2});
v.push_back({0, 1});
} else if (a[n - 1][n - 2] == '0' && a[n - 2][n - 1] == '1') {
v.push_back({n - 2, n - 1});
v.push_back({0, 1});
} else {
v.push_back({1, 0});
}
}
cout << v.size() << "\n";
for (auto it = (v).begin(); it != (v).end(); it++)
cout << it->first + 1 << " " << it->second + 1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n = 0;
cin >> n;
vector<vector<long long>> arr(n, vector<long long>(n));
for (long long i = 0; i < n; i++) {
string s;
cin >> s;
for (long long j = 0; j < n; j++) {
for (long long j = 0; j < n; j++) arr[i][j] = s[j] - '0';
}
}
long long n1 = arr[0][1];
long long n2 = arr[1][0];
long long n3 = arr[n - 1][n - 2];
long long n4 = arr[n - 2][n - 1];
if (n1 == n2 && n2 == n3 && n3 == n4) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << 2 << " " << 1 << "\n";
} else if (n1 == n2 && n3 == n4) {
cout << 0 << "\n";
} else if (n1 == n2) {
cout << 1 << "\n";
if (n2 == n3)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
} else if (n3 == n4) {
cout << 1 << "\n";
if (n1 == n3)
cout << 1 << " " << 2 << "\n";
else
cout << 2 << " " << 1 << "\n";
} else if (n1 == n3) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << n - 1 << " " << n << "\n";
} else {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << n << " " << n - 1 << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char arr[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
vector<pair<int, int>> vec;
if (arr[0][1] == arr[1][0]) {
if (arr[0][1] == '1') {
if (arr[n - 2][n - 1] != '0') {
vec.push_back({n - 1, n});
}
if (arr[n - 1][n - 2] != '0') {
vec.push_back({n, n - 1});
}
} else {
if (arr[0][1] == '0') {
if (arr[n - 2][n - 1] != '1') {
vec.push_back({n - 1, n});
}
if (arr[n - 1][n - 2] != '1') {
vec.push_back({n, n - 1});
}
}
}
} else if (arr[n - 1][n - 2] == arr[n - 2][n - 1]) {
if (arr[n - 1][n - 2] == '1') {
if (arr[0][1] != '0') {
vec.push_back({1, 2});
}
if (arr[1][0] != '0') {
vec.push_back({2, 1});
}
} else {
if (arr[n - 1][n - 2] == '0') {
if (arr[1][0] != '1') {
vec.push_back({2, 1});
}
if (arr[0][1] != '1') {
vec.push_back({1, 2});
}
}
}
} else {
if (arr[n - 1][n - 2] != '1') {
vec.push_back({n, n - 1});
}
if (arr[n - 2][n - 1] != '1') {
vec.push_back({n - 1, n});
}
if (arr[0][1] != '0') {
vec.push_back({1, 2});
}
if (arr[1][0] != '0') {
vec.push_back({2, 1});
}
}
cout << vec.size() << endl;
for (auto x : vec) {
cout << x.first << " " << x.second << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int T;
int n, i, j;
char num[300][300];
cin >> T;
while (T--) {
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cin >> num[i][j];
}
}
int cnt = 0;
cnt = num[2][1] + num[1][2] + num[n - 1][n] + num[n][n - 1] - 4 * '0';
int x, y, t;
if (cnt == 0 || cnt == 4) {
printf("2\n1 2\n2 1\n");
} else if (cnt == 1 || cnt == 3) {
printf("1\n");
if (cnt == 1)
t = '0';
else
t = '1';
if (num[2][1] != num[1][2]) {
if (t == num[2][1])
x = 2, y = 1;
else
x = 1, y = 2;
} else {
if (t == num[n][n - 1])
x = n, y = n - 1;
else
x = n - 1, y = n;
}
printf("%d %d\n", x, y);
} else {
if (num[1][2] == num[2][1]) {
printf("0\n");
} else {
printf("2\n1 2\n");
if (num[n - 1][n] == num[1][2])
printf("%d %d\n", n, n - 1);
else
printf("%d %d\n", n - 1, n);
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000002;
const int MOD2 = 998244353;
const int MOD = 1000000007;
const int INF = 1e8;
const long double EPS = 1e-7;
long long int mul(long long int x, long long int y) {
return (x * 1ll * y) % MOD;
}
long long int fastpow(long long int x, long long int y) {
long long int z = 1;
while (y) {
if (y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
long long int modinv(long long int n, long long int p) {
return fastpow(n, p - 2);
}
struct Comp {
bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b) {
if (a.first != b.first) {
return a.first > b.first;
}
return a.second > b.second;
}
};
int main(int argc, char** argv) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cout << setprecision(10);
int tt;
cin >> tt;
while (tt--) {
long long int n;
cin >> n;
string s[n];
for (int i = 0; i < (int)n; i++) cin >> s[i];
long long int c = (s[n - 2][n - 1] - '0'), d = (s[n - 1][n - 2] - '0');
long long int a = (s[0][1] - '0'), b = (s[1][0] - '0');
if (a + b + c + d == 4 || a + b + c + d == 0) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << 2 << " " << 1 << "\n";
continue;
} else if (a + b + c + d == 1) {
cout << 1 << "\n";
if (a == 1) {
cout << 2 << " " << 1 << "\n";
} else if (b == 1) {
cout << 1 << " " << 2 << "\n";
} else if (c == 1)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
continue;
} else if (a + b + c + d == 3) {
cout << 1 << "\n";
if (a == 0) {
cout << 2 << " " << 1 << "\n";
} else if (b == 0) {
cout << 1 << " " << 2 << "\n";
} else if (c == 0)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
} else {
if (a == b) {
cout << 0 << "\n";
} else {
if (a == c) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << n << " " << n - 1 << "\n";
} else {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << n - 1 << " " << n << "\n";
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
int n, m, i, x = 0, y, z;
cin >> n;
string s[n + 1];
for (int i = 0; i < n; i++) cin >> s[i];
vector<pair<int, int>> v;
if (s[0][1] == s[1][0]) {
if (s[n - 1][n - 2] == s[0][1]) {
v.push_back({n, n - 1});
}
if (s[n - 2][n - 1] == s[0][1]) {
v.push_back({n - 1, n});
}
} else if (s[n - 1][n - 2] == s[n - 2][n - 1]) {
if (s[n - 1][n - 2] == s[0][1]) {
v.push_back({1, 2});
}
if (s[n - 2][n - 1] == s[1][0]) {
v.push_back({2, 1});
}
} else {
if (s[0][1] != '0') {
v.push_back({1, 2});
}
if (s[1][0] != '0') {
v.push_back({2, 1});
}
if (s[n - 2][n - 1] != '1') {
v.push_back({n - 1, n});
}
if (s[n - 1][n - 2] != '1') {
v.push_back({n, n - 1});
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
ll fac[1000001];
void pre() {
fac[0] = fac[1] = 1;
for (int i = 2; i <= 1000001; i++) {
fac[i] = (fac[i - 1] * 1LL * i) % mod;
}
}
ll binpower(ll a, ll n) {
ll res = 1;
while (n) {
if (n % 2) res = (res * 1LL * a) % mod;
n /= 2;
a = (a * 1LL * a) % mod;
}
return res;
}
ll nCrmod(ll n, ll r) {
ll res = fac[n];
res = (res * 1LL * binpower(fac[r], mod - 2)) % mod;
res = (res * 1LL * binpower(fac[n - r], mod - 2)) % mod;
return res;
}
long long ncr(int n, int r) {
if (r > n - r) r = n - r;
long long ans = 1;
int i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
ll modexp(ll a, ll b, ll m) {
if (b == 0) return 1;
if (b % 2 == 0) {
ll y = modexp(a, b / 2, m);
return (y * y) % m;
} else {
return ((a % m) * modexp(a, b - 1, m)) % m;
}
}
ll modinv(ll a, ll m) { return modexp(a, m - 2, m); }
void SieveOfEratosthenes(ll n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (ll p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (ll i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (ll p = 2; p <= n; p++)
if (prime[p]) cout << p << " ";
}
vector<int> Centroid(vector<int> g[], int n) {
vector<int> centroid;
vector<int> sz(n + 1);
function<void(int, int)> dfs = [&](int u, int prev) {
sz[u] = 1;
bool is_centroid = true;
for (auto v : g[u])
if (v != prev) {
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > n / 2) is_centroid = false;
}
if (n - sz[u] > n / 2) is_centroid = false;
if (is_centroid) centroid.push_back(u);
};
dfs(1, 0);
return centroid;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char grid[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 1; j <= n; j++) {
grid[i][j] = s[j - 1];
}
}
char a = grid[1][2], b = grid[2][1], c = grid[n][n - 1], d = grid[n - 1][n];
if (a == b) {
int ans = 0;
vector<pair<int, int>> v;
if (a == '0') {
if (c == '0') {
ans++;
v.push_back({n, n - 1});
}
if (d == '0') {
ans++;
v.push_back({n - 1, n});
}
}
if (a == '1') {
if (c == '1') {
ans++;
v.push_back({n, n - 1});
}
if (d == '1') {
ans++;
v.push_back({n - 1, n});
}
}
cout << ans << "\n";
for (auto x : v) {
cout << x.first << " " << x.second << "\n";
}
} else {
int ans = 0;
vector<pair<int, int>> v;
if (c == d) {
if (c == '0') {
if (a == '0') {
ans++;
v.push_back({1, 2});
}
if (b == '0') {
ans++;
v.push_back({2, 1});
}
} else {
if (a == '1') {
ans++;
v.push_back({1, 2});
}
if (b == '1') {
ans++;
v.push_back({2, 1});
}
}
} else {
if (c == '1') {
ans++;
v.push_back({n, n - 1});
if (a == '0') {
ans++;
v.push_back({1, 2});
}
if (b == '0') {
ans++;
v.push_back({2, 1});
}
} else {
ans++;
v.push_back({n, n - 1});
if (a == '1') {
ans++;
v.push_back({1, 2});
}
if (b == '1') {
ans++;
v.push_back({2, 1});
}
}
}
cout << ans << "\n";
for (auto x : v) {
cout << x.first << " " << x.second << "\n";
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2e5 + 7, mod = 1e9 + 7;
const ll inf = 2e18;
int n;
string s[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
int a = s[0][1] + s[1][0] - 2 * '0',
b = s[n - 1][n - 2] + s[n - 2][n - 1] - 2 * '0';
vector<pair<int, int> > ans;
if (a == 0) {
if (s[n - 1][n - 2] == '0') ans.push_back({n, n - 1});
if (s[n - 2][n - 1] == '0') ans.push_back({n - 1, n});
} else if (a == 1) {
if (b == 1) {
if (s[0][1] == '0') ans.push_back({1, 2});
if (s[1][0] == '0') ans.push_back({2, 1});
if (s[n - 1][n - 2] == '1') ans.push_back({n, n - 1});
if (s[n - 2][n - 1] == '1') ans.push_back({n - 1, n});
} else if (b == 0) {
if (s[0][1] == '0') ans.push_back({1, 2});
if (s[1][0] == '0') ans.push_back({2, 1});
} else {
if (s[0][1] == '1') ans.push_back({1, 2});
if (s[1][0] == '1') ans.push_back({2, 1});
}
} else {
if (s[n - 1][n - 2] == '1') ans.push_back({n, n - 1});
if (s[n - 2][n - 1] == '1') ans.push_back({n - 1, n});
}
cout << (int)ans.size() << "\n";
for (auto u : ans) cout << u.first << " " << u.second << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t, n, i, j, y, ans;
string s;
char ch;
cin >> t;
while (t--) {
cin >> n;
vector<int> a(4);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cin >> ch;
if (i == 0 && j == 1) {
a[0] = ch - '0';
}
if (i == 1 && j == 0) {
a[1] = ch - '0';
}
if (i == n - 2 && j == n - 1) {
a[2] = ch - '0';
}
if (i == n - 1 && j == n - 2) {
a[3] = ch - '0';
}
}
}
if (a[0] != a[1]) {
if (a[2] == a[3]) {
if (a[0] != a[2]) {
cout << 1 << "\n"
<< "2 1"
<< "\n";
} else {
cout << 1 << "\n"
<< "1 2"
<< "\n";
}
} else {
if (a[0] == a[2]) {
cout << 2 << "\n"
<< "2 1"
<< "\n"
<< (n - 1) << " " << n << "\n";
} else {
cout << 2 << "\n"
<< "2 1"
<< "\n"
<< (n) << " " << (n - 1) << "\n";
}
}
} else {
if (a[2] == a[3]) {
if (a[0] != a[2]) {
cout << 0 << "\n";
} else {
cout << 2 << "\n"
<< n - 1 << " " << n << "\n"
<< n << " " << n - 1 << "\n";
}
} else {
if (a[0] == a[2]) {
cout << 1 << "\n" << n - 1 << " " << n << "\n";
} else {
cout << 1 << "\n" << n << " " << n - 1 << "\n";
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n, p, i, c, d, e, f, q = 0, r = 0, l = 0, m = 0;
cin >> n;
p = n * n;
for (i = 0; i < p; i++) {
char s;
cin >> s;
if (i == 1) {
c = s - '0';
} else if (i == n) {
d = s - '0';
} else if (i == p - n - 1) {
e = s - '0';
} else if (i == p - 2) {
f = s - '0';
}
}
if (c == d) {
if (e == c || e == d) {
q = 1;
r++;
l = 1;
}
if (f == c || f == d) {
q = 1;
r++;
m = 1;
}
if (l == 1 && r == 1) {
cout << r << endl;
cout << (p - n) / n << " " << n << endl;
} else if (m == 1 && r == 1) {
cout << r << endl;
cout << n << " " << n - 1 << endl;
} else if (r == 2) {
cout << r << endl;
cout << (p - n) / n << " " << n << endl;
cout << n << " " << n - 1 << endl;
}
} else if (e == f) {
if (c == e || c == f) {
q = 1;
l = 1;
r++;
}
if (d == e || d == f) {
q = 1;
r++;
m = 1;
}
if (l == 1 && r == 1) {
cout << r << endl;
cout << 1 << " " << 2 << endl;
} else if (m == 1 && r == 1) {
cout << r << endl;
cout << 2 << " " << 1 << endl;
} else if (r == 2) {
cout << r << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
}
} else if (c != d && e != f) {
if (c == 1) {
q = 1;
r++;
l = 1;
} else if (d == 1) {
q = 1;
r++;
l = 1;
}
if (e == 0) {
q = 1;
r++;
m = 1;
} else if (f == 0) {
q = 1;
r++;
m = 1;
}
cout << r << endl;
if (l == 1) {
if (c == 1) {
cout << 1 << " " << 2 << endl;
} else if (d == 1) {
cout << 2 << " " << 1 << endl;
}
}
if (m == 1) {
if (e == 0) {
cout << (p - n) / n << " " << n << endl;
} else if (f == 0) {
cout << n << " " << n - 1 << endl;
}
}
}
if (q == 0) {
cout << 0 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tests;
cin >> tests;
while (tests--) {
int n;
cin >> n;
vector<vector<char>> t(n);
char c;
for (auto& i : t) {
for (int j = 0; j < n; ++j) {
cin >> c;
i.push_back(c);
}
}
vector<pair<int, int>> ans;
char right_s = t[0][1];
char down_s = t[1][0];
char left_f = t[n - 1][n - 2];
char up_f = t[n - 2][n - 1];
if (right_s == down_s) {
if (right_s == '0')
c = '1';
else
c = '0';
if (left_f != c) ans.push_back(make_pair(n, n - 1));
if (up_f != c) ans.push_back(make_pair(n - 1, n));
} else if (left_f == up_f) {
if (left_f == '0')
c = '1';
else
c = '0';
if (right_s != c) ans.push_back(make_pair(1, 2));
if (down_s != c) ans.push_back(make_pair(2, 1));
} else {
c = right_s;
ans.push_back(make_pair(1, 2));
if (left_f != c) ans.push_back(make_pair(n, n - 1));
if (up_f != c) ans.push_back(make_pair(n - 1, n));
}
cout << ans.size() << '\n';
for (auto i : ans) {
cout << i.first << ' ' << i.second << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int testcase;
cin >> testcase;
for (int i = 0; i < testcase; i++) {
int size;
cin >> size;
char map[size][size];
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
cin >> map[j][k];
}
}
char start1 = map[0][1], start2 = map[1][0], end1 = map[size - 1][size - 2],
end2 = map[size - 2][size - 1];
if (start1 == start2) {
if (end1 == end2 && start1 != end1) {
cout << "0" << endl;
continue;
} else if (end1 == end2) {
cout << "2" << endl;
cout << size << " " << size - 1 << endl;
cout << size - 1 << " " << size << endl;
continue;
} else if (end1 != end2) {
if (end1 == start1) {
cout << "1" << endl;
cout << size << " " << size - 1 << endl;
continue;
} else if (end2 == start1) {
cout << "1" << endl;
cout << size - 1 << " " << size << endl;
continue;
}
}
}
if (end1 == end2) {
if (start1 == start2 && start1 != end1) {
cout << "0" << endl;
continue;
} else if (start1 == start2) {
cout << "2" << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
continue;
} else if (start1 != start2) {
if (end1 == start1) {
cout << "1" << endl;
cout << "1 2" << endl;
continue;
} else if (end1 == start2) {
cout << "1" << endl;
cout << "2 1" << endl;
continue;
}
}
}
if (end1 != end2 && start1 != start2) {
cout << "2" << endl;
if (start1 == '0') {
cout << "1 2" << endl;
}
if (start2 == '0') {
cout << "2 1" << endl;
}
if (end1 == '1') {
cout << size << " " << size - 1 << endl;
}
if (end2 == '1') {
cout << size - 1 << " " << size << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 9;
const int INF = (int)1e9 + 7;
int mod = (int)1e9 + 7;
string tab[209];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> tab[i];
char a0 = tab[0][1], a1 = tab[1][0];
char b0 = tab[n - 1][n - 2], b1 = tab[n - 2][n - 1];
if (0)
cout << "a0"
<< " = " << a0 << " "
<< "a1"
<< " = " << a1 << " "
<< "b0"
<< " = " << b0 << " "
<< "b1"
<< " = " << b1 << " " << endl;
if (a0 == a1) {
if (b0 == b1) {
if (b0 == a0)
cout << "2\n1 2\n2 1\n";
else
cout << "0\n";
} else {
if (b0 == a0)
cout << "1\n" << n << " " << n - 1 << "\n";
else
cout << "1\n" << n - 1 << " " << n << "\n";
}
} else {
if (b0 != b1) {
if (a0 == b0)
cout << "2\n1 2\n" << n - 1 << " " << n << "\n";
else
cout << "2\n1 2\n" << n << " " << n - 1 << "\n";
} else {
if (a0 == b0)
cout << "1\n1 2\n";
else
cout << "1\n2 1\n";
}
}
}
int main(int argc, char* argv[]) {
ios::sync_with_stdio(false);
if (0)
;
else
cin.tie(NULL), cout.tie(NULL);
int cases;
cin >> cases;
while (cases--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s[n];
for (long long i = 0; i < n; i++) {
cin >> s[i];
}
vector<pair<long long, long long> > arr;
if (s[0][1] == '0' || s[1][0] == '0') {
if (s[n - 1][n - 2] == '1' || s[n - 2][n - 1] == '1') {
if (s[0][1] == '1') {
arr.push_back(make_pair(1, 2));
}
if (s[1][0] == '1') {
arr.push_back(make_pair(2, 1));
}
if (s[n - 1][n - 2] == '0') {
arr.push_back(make_pair(n, n - 1));
}
if (s[n - 2][n - 1] == '0') {
arr.push_back(make_pair(n - 1, n));
}
} else {
if (s[0][1] == '0') {
arr.push_back(make_pair(1, 2));
}
if (s[1][0] == '0') {
arr.push_back(make_pair(2, 1));
}
}
} else {
if (s[n - 1][n - 2] == '1') {
arr.push_back(make_pair(n, n - 1));
}
if (s[n - 2][n - 1] == '1') {
arr.push_back(make_pair(n - 1, n));
}
}
cout << arr.size() << endl;
for (long long i = 0; i < arr.size(); i++) {
cout << arr[i].first << " " << arr[i].second << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char c = getchar();
long long x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
const long long M = 210;
long long n, a[M][M];
void check(long long x, long long y, long long p) {
if (a[x][y] != p) cout << x << ' ' << y << endl;
}
signed main() {
long long T = read();
while (T--) {
long long n = read();
for (long long i = 1; i <= n; i++) {
string s;
cin >> s;
for (long long j = 1; j <= n; j++) a[i][j] = s[j - 1] == '0' ? 0 : 1;
}
long long cnt = (a[n][n - 1] ^ 1) + (a[n - 1][n] ^ 1) + a[n - 1][n - 1] +
a[n][n - 2] + a[n - 2][n];
if (cnt <= 2) {
cout << cnt << endl;
check(n, n - 1, 1), check(n - 1, n, 1), check(n - 1, n - 1, 0);
check(n - 2, n, 0), check(n, n - 2, 0);
} else {
cout << 5 - cnt << endl;
check(n, n - 1, 0), check(n - 1, n, 0), check(n - 1, n - 1, 1);
check(n - 2, n, 1), check(n, n - 2, 1);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cin >> a[i][j];
}
int w, x, y, z, cnt = 0, ans[4];
w = a[0][1];
x = a[1][0];
y = a[n - 1][n - 2];
z = a[n - 2][n - 1];
if (w == x) {
if (y == z) {
if (w == y) {
cnt = 2;
ans[0] = 0;
ans[1] = 1;
ans[2] = 1;
ans[3] = 0;
}
} else {
if (w == y) {
cnt++;
ans[0] = n - 1;
ans[1] = n - 2;
} else if (w == z) {
cnt++;
ans[0] = n - 2;
ans[1] = n - 1;
}
}
} else {
if (y == z) {
if (w == y) {
cnt++;
ans[0] = 0;
ans[1] = 1;
} else if (x == y) {
cnt++;
ans[0] = 1;
ans[1] = 0;
}
} else {
cnt++;
ans[0] = 1;
ans[1] = 0;
x = w;
if (x == y) {
cnt++;
ans[2] = n - 1;
ans[3] = n - 2;
} else if (x == z) {
cnt++;
ans[2] = n - 2;
ans[3] = n - 1;
}
}
}
cout << cnt << endl;
cnt *= 2;
for (int i = 0; i < cnt; i += 2) {
cout << ans[i] + 1 << " " << ans[i + 1] + 1;
cout << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 205;
int n, ans, T;
char a[maxn][maxn];
int main() {
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cin >> a[i][j];
}
int p1 = a[1][2] - '0', p2 = a[2][1] - '0', q1 = a[n][n - 1] - '0',
q2 = a[n - 1][n] - '0';
int cnt = p1 + p2 + q1 + q2;
if (cnt == 0 || cnt == 4) {
cout << 2 << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (cnt == 1 || cnt == 3) {
cout << 1 << endl;
int f = 1;
if (cnt == 3) f = 0;
if (p1 == f) cout << "2 1" << endl;
if (p2 == f) cout << "1 2" << endl;
if (q1 == f) cout << n - 1 << ' ' << n << endl;
if (q2 == f) cout << n << ' ' << n - 1 << endl;
} else {
if (p1 == p2)
cout << 0 << endl;
else {
cout << 2 << endl;
if (p1 == 0) cout << "1 2" << endl;
if (p2 == 0) cout << "2 1" << endl;
if (q1 == 1) cout << n << ' ' << n - 1 << endl;
if (q2 == 1) cout << n - 1 << ' ' << n << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T a) {
return a * a;
}
template <class T>
T abs(T x) {
if (x < 0) return -x;
return x;
}
const double eps = 1e-8;
const double pi = acos(-1.0);
struct node {
int x, y, val;
};
char str[210][210];
vector<node> vec;
int main() {
int tks, ks = 1;
scanf("%d", &tks);
while (tks--) {
vec.clear();
node a, b, c, d;
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%s", str[i]);
a.x = 0;
a.y = 1;
a.val = str[a.x][a.y] - '0';
b.x = 1;
b.y = 0;
b.val = str[b.x][b.y] - '0';
c.x = n - 1;
c.y = n - 2;
c.val = str[c.x][c.y] - '0';
d.x = n - 2;
d.y = n - 1;
d.val = str[d.x][d.y] - '0';
if (a.val == b.val) {
if (a.val == c.val) vec.push_back(c);
if (a.val == d.val) vec.push_back(d);
} else if (c.val == d.val) {
if (a.val == c.val) vec.push_back(a);
if (b.val == c.val) vec.push_back(b);
} else {
if (a.val == 1)
vec.push_back(b);
else
vec.push_back(a);
if (c.val == 0)
vec.push_back(d);
else
vec.push_back(c);
}
printf("%d\n", vec.size());
for (i = 0; i < vec.size(); i++)
printf("%d %d\n", vec[i].x + 1, vec[i].y + 1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<string> arr(n);
for (long long i = 0; i < n; i++) cin >> arr[i];
char sa = arr[0][1], sb = arr[1][0], fa = arr[n - 1][n - 2],
fb = arr[n - 2][n - 1];
vector<vector<long long>> res;
if (sa == sb) {
if (fa == sa) res.push_back({n - 1, n - 2});
if (fb == sa) res.push_back({n - 2, n - 1});
} else if (fa == fb) {
if (fa == sa) res.push_back({0, 1});
if (fa == sb) res.push_back({1, 0});
} else {
if (sa == '0') res.push_back({0, 1});
if (sb == '0') res.push_back({1, 0});
if (fa == '1') res.push_back({n - 1, n - 2});
if (fb == '1') res.push_back({n - 2, n - 1});
}
cout << res.size() << endl;
for (auto v : res) cout << v[0] + 1 << " " << v[1] + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int solve() {
int n;
cin >> n;
vector<vector<char>> gr(n, vector<char>(n));
vector<vector<int>> g(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> gr[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == 0 && j == 0) || (i == n - 1 && j == n - 1))
g[i][j] = 2;
else
g[i][j] = gr[i][j] - '0';
}
}
if (g[0][1] == 0 && g[1][0] == 1) {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << "2 1" << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n - 1 << " " << n << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << "1" << endl;
cout << "1 2" << endl;
}
} else if (g[0][1] == 1 && g[1][0] == 1) {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "2" << endl;
cout << n - 1 << " " << n << endl;
cout << n << " " << n - 1 << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << "0" << endl;
}
} else if (g[0][1] == 1 && g[1][0] == 0) {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << "1 2" << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n << " " << n - 1 << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "2" << endl;
cout << "2 1" << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << "1" << endl;
cout << "2 1" << endl;
}
} else {
if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 1) {
cout << "0" << endl;
} else if (g[n - 1][n - 2] == 1 && g[n - 2][n - 1] == 0) {
cout << "1" << endl;
cout << n - 1 << " " << n << endl;
} else if (g[n - 1][n - 2] == 0 && g[n - 2][n - 1] == 1) {
cout << "1" << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << "2" << endl;
cout << n - 1 << " " << n << endl;
cout << n << " " << n - 1 << endl;
}
}
return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n, i, j, count = 0;
cin >> n;
char ch[n + 1][n + 1];
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cin >> ch[i][j];
}
}
long long int b[5][5];
b[1][2] = b[2][1] = 0;
b[3][1] = b[2][2] = b[1][3] = 1;
long long int c[5][5];
c[1][2] = c[2][1] = 1;
c[3][1] = c[2][2] = c[1][3] = 0;
count = 0;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != b[i][j]) {
count++;
}
}
}
}
if (count <= 2) {
cout << count << '\n';
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != b[i][j]) {
cout << i << " " << j << '\n';
}
}
}
}
} else {
count = 0;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != c[i][j]) {
count++;
}
}
}
}
cout << count << '\n';
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 1 && j == 1) {
continue;
}
if (i + j <= 4) {
if (ch[i][j] - '0' != c[i][j]) {
cout << i << " " << j << '\n';
}
}
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long int ceil(long long int a, long long int b) {
if ((a >= 0 && b >= 0) || (a < 0 && b < 0)) return (a / b) + ((a % b) != 0);
return a / b;
}
long long int floor(long long int a, long long int b) {
if ((a >= 0 && b >= 0) || (a < 0 && b < 0)) return a / b;
return a / b - ((a % b) != 0);
}
void IO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void solve(vector<string>& grid, int n) {
char a = grid[1][0];
char b = grid[0][1];
char x = grid[n - 1][n - 2];
char y = grid[n - 2][n - 1];
int num0 = 0;
if (a == '0') num0++;
if (b == '0') num0++;
if (x == '0') num0++;
if (y == '0') num0++;
if (num0 == 0 || num0 == 4) {
cout << 2 << "\n";
cout << "2 1\n1 2\n";
} else if (num0 == 1 || num0 == 3) {
cout << 1 << "\n";
if (a == b) {
if (x == a) cout << n << " " << n - 1 << "\n";
if (y == a) cout << n - 1 << " " << n << "\n";
}
if (x == y) {
if (a == x) cout << "2 1\n";
if (b == x) cout << "1 2\n";
}
} else {
if (a == b) {
cout << "0\n";
return;
}
cout << 2 << "\n";
if (a == '1') cout << "2 1\n";
if (b == '1') cout << "1 2\n";
if (x == '0') cout << n << " " << n - 1 << "\n";
if (y == '0') cout << n - 1 << " " << n << "\n";
}
}
int main() {
IO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<string> grid(n);
for (int i = 0; i < n; i++) cin >> grid[i];
solve(grid, n);
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
bool isPowerOfTwo(long long int x) { return x && (!(x & (x - 1))); }
long long int Modular_Exponentiation(long long int x, long long int n,
long long int M) {
if (n == 0) return 1;
if (n % 2)
return (x * Modular_Exponentiation((x * x) % M, n / 2, M)) % M;
else
return (Modular_Exponentiation((x * x) % M, n / 2, M)) % M;
}
bool isPrime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 5; i * i <= n; i += 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
void solve() {
long long int n;
cin >> n;
string str[n];
for (long long int i = 0; i < n; ++i) cin >> str[i];
long long int a, b, c, d, one = 0, zero = 0;
a = str[0][1] - '0';
b = str[1][0] - '0';
c = str[n - 2][n - 1] - '0';
d = str[n - 1][n - 2] - '0';
if (a == 0)
zero++;
else
one++;
if (b == 0)
zero++;
else
one++;
if (c == 0)
zero++;
else
one++;
if (d == 0)
zero++;
else
one++;
if (zero < one) {
if (zero == 1) {
cout << "1\n";
if (a == 0)
cout << "2 1\n";
else if (b == 0)
cout << "1 2\n";
else if (c == 0)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
return;
} else {
cout << "2\n";
cout << "1 2\n2 1\n";
return;
}
} else if (one < zero) {
if (one == 1) {
cout << "1\n";
if (a == 1)
cout << "2 1\n";
else if (b == 1)
cout << "1 2\n";
else if (c == 1)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
return;
} else {
cout << "2\n";
cout << "1 2\n2 1\n";
return;
}
} else {
if (a != b) {
cout << "2\n";
cout << "1 2\n";
if (a == c)
cout << n << " " << n - 1 << "\n";
else
cout << n - 1 << " " << n << "\n";
return;
} else
cout << "0\n";
}
}
int32_t main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
while (T--) solve();
cerr << "Time taken : " << fixed << setprecision(5)
<< ((float)clock() / CLOCKS_PER_SEC) * 1000 << " ms"
<< "\n";
cerr << "My CLOCKS_PER_SEC= " << CLOCKS_PER_SEC << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
for (int p = 1; p <= t; p++) {
int n, z;
cin >> n;
vector<vector<int>> grid(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
char c;
cin >> c;
if ((i == 0 && j == 0) || (i == n - 1 && j == n - 1)) {
continue;
}
grid[i][j] = c - 48;
}
}
int ans = 0, p1x = -1, p1y = -1, p2x = -1, p2y = -1, x1 = grid[0][1],
x2 = grid[1][0], x3 = grid[n - 2][n - 1], x4 = grid[n - 1][n - 2];
if (x1 == 0 && x2 == 0) {
if (x3 == 0 && x4 == 0) {
ans = 2;
p1x = 0, p1y = 1, p2x = 1, p2y = 0;
} else if (x3 == 1 && x4 == 1) {
ans = 0;
} else if (x3 == 1 && x4 == 0) {
ans = 1;
p1x = n - 1, p1y = n - 2;
} else if (x3 == 0 && x4 == 1) {
ans = 1;
p1x = n - 2, p1y = n - 1;
}
} else if (x1 == 1 && x2 == 1) {
if (x3 == 0 && x4 == 0) {
ans = 0;
} else if (x3 == 1 && x4 == 1) {
ans = 2;
p1x = 0, p1y = 1, p2x = 1, p2y = 0;
} else if (x3 == 1 && x4 == 0) {
ans = 1;
p1x = n - 2, p1y = n - 1;
} else if (x3 == 0 && x4 == 1) {
ans = 1;
p1x = n - 1, p1y = n - 2;
}
} else if (x1 == 1 && x2 == 0) {
if (x3 == 0 && x4 == 0) {
ans = 1;
p1x = 1, p1y = 0;
} else if (x3 == 1 && x4 == 1) {
ans = 1;
p1x = 0, p1y = 1;
} else if (x3 == 1 && x4 == 0) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 1, p2y = n - 2;
} else if (x3 == 0 && x4 == 1) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 2, p2y = n - 1;
}
} else if (x1 == 0 && x2 == 1) {
if (x3 == 0 && x4 == 0) {
ans = 1;
p1x = 0, p1y = 1;
} else if (x3 == 1 && x4 == 1) {
ans = 1;
p1x = 1, p1y = 0;
} else if (x3 == 1 && x4 == 0) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 2, p2y = n - 1;
} else if (x3 == 0 && x4 == 1) {
ans = 2;
p1x = 0, p1y = 1, p2x = n - 1, p2y = n - 2;
}
}
cout << ans << endl;
if (ans == 0)
;
else if (ans == 1) {
cout << p1x + 1 << " " << p1y + 1 << endl;
} else {
cout << p1x + 1 << " " << p1y + 1 << endl
<< p2x + 1 << " " << p2y + 1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string arr[n];
for (long long int i = 0; i < n; i++) cin >> arr[i];
char s1 = arr[0][1];
char s2 = arr[1][0];
char e1 = arr[n - 2][n - 1];
char e2 = arr[n - 1][n - 2];
if ((s1 == s2) && (e1 == e2) && s1 == e1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
} else if ((s1 == s2) && (e1 == e2) && s1 != e1)
cout << 0 << endl;
else if ((s1 != s2) && (e1 != e2) && s1 == e1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n << " " << n - 1 << endl;
} else if ((s1 != s2) && (e1 != e2) && s1 != e1) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n - 1 << " " << n << endl;
} else if ((s1 == s2) && (e1 != e2) && s1 == e1) {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
} else if ((s1 == s2) && (e1 != e2) && s1 == e2) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else if ((s1 != s2) && (e1 == e2) && s1 == e1) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else if ((s1 != s2) && (e1 == e2) && s2 == e1) {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
if (0 <= second && second < n && 0 <= first && first < m) return true;
return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
string a[222];
int dp[222][222], n;
bool can(int second, int first) {
if (second == n - 1 && first == n - 1) return true;
dp[second][first] = 1;
for (int e = 0; e < 4; e++) {
int ny = second + dy[e];
int nx = first + dx[e];
if (isrange(ny, nx, n, n) && dp[ny][nx] == 0) {
if (a[ny][nx] == 'F' || a[ny][nx] == a[second][first]) {
if (can(ny, nx)) return true;
}
}
}
return false;
}
void init() {
for (int e = 0; e < n; e++)
for (int p = 0; p < n; p++) dp[e][p] = 0;
dp[0][0] = 1;
}
bool isit() {
init();
bool suc1 = can(0, 1);
init();
bool suc2 = can(1, 0);
if (!suc1 && !suc2) return false;
return true;
}
void inverse(int second, int first) {
if (a[second][first] == '0')
a[second][first] = '1';
else
a[second][first] = '0';
}
vector<pair<int, int> > vv;
int main(void) {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
vv.clear();
vv.push_back(make_pair(0, 1));
vv.push_back(make_pair(1, 0));
vv.push_back(make_pair(1, 1));
vv.push_back(make_pair(n - 2, n - 1));
vv.push_back(make_pair(n - 1, n - 2));
if (n != 3) vv.push_back(make_pair(n - 2, n - 2));
for (int e = 0; e < n; e++) cin >> a[e];
bool suc = false;
if (!isit()) {
printf("0\n");
suc = true;
}
if (!suc) {
for (int e = 0; e < (int)vv.size(); e++) {
inverse(vv[e].first, vv[e].second);
if (!isit()) {
printf("1\n%d %d\n", vv[e].first + 1, vv[e].second + 1);
suc = true;
break;
}
inverse(vv[e].first, vv[e].second);
}
}
if (!suc) {
for (int e = 0; e < (int)vv.size(); e++) {
for (int p = e + 1; p < (int)vv.size(); p++) {
inverse(vv[e].first, vv[e].second);
inverse(vv[p].first, vv[p].second);
if (!isit()) {
printf("2\n");
printf("%d %d\n", vv[e].first + 1, vv[e].second + 1);
printf("%d %d\n", vv[p].first + 1, vv[p].second + 1);
suc = true;
break;
}
inverse(vv[e].first, vv[e].second);
inverse(vv[p].first, vv[p].second);
}
if (suc) break;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int i, n, t, j = 0, p, x, a = 0, b, l = 0, r = 0, y, k, c, sum = 0,
m, d, cnts = 0;
cin >> t;
while (t--) {
cin >> n;
char s[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cin >> s[i][j];
}
}
if (s[0][1] == '1' && s[1][0] == '1') {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 0 << endl;
} else if (s[n - 1][n - 2] == '1') {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else if (s[0][1] == '0' && s[1][0] == '0') {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 0 << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0') {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else if (s[0][1] == '1') {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 1 << endl;
cout << "1 2" << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 1 << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n - 1 << " " << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1' && s[n - 2][n - 1] == '1') {
cout << 1 << endl;
cout << "2 1" << endl;
} else if (s[n - 1][n - 2] == '0' && s[n - 2][n - 1] == '0') {
cout << 1 << endl;
cout << "1 2" << endl;
} else if (s[n - 1][n - 2] == '0') {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << 2 << endl;
cout << "1 2" << endl;
cout << n << " " << n - 1 << endl;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void testCase() {
long long n;
cin >> n;
vector<string> grid(n);
vector<pair<long long, long long>> ans;
for (string &s : grid) cin >> s;
if (grid[0][1] == grid[1][0]) {
if (grid[n - 1][n - 2] == grid[0][1]) ans.push_back({n - 1, n - 2});
if (grid[n - 2][n - 1] == grid[0][1]) ans.push_back({n - 2, n - 1});
} else if (grid[n - 1][n - 2] == grid[n - 2][n - 1]) {
if (grid[n - 1][n - 2] == grid[0][1]) ans.push_back({0, 1});
if (grid[n - 1][n - 2] == grid[1][0]) ans.push_back({1, 0});
} else {
if (grid[0][1] == '1') ans.push_back({0, 1});
if (grid[1][0] == '1') ans.push_back({1, 0});
if (grid[n - 1][n - 2] == '0') ans.push_back({n - 1, n - 2});
if (grid[n - 2][n - 1] == '0') ans.push_back({n - 2, n - 1});
}
cout << (long long)(ans.size()) << endl;
for (auto &it : ans) cout << it.first + 1 << " " << it.second + 1 << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cin.tie(nullptr);
long long t_c = 1;
cin >> t_c;
while (t_c--) testCase();
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
if (b < a) a = b;
return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 0;
void solve() {
long long n;
cin >> n;
string s[n];
for (long long i = 0; i < n; i++) {
cin >> s[i];
}
long long a = s[0][1], b = s[1][0];
long long c = s[n - 1][n - 2], d = s[n - 2][n - 1];
vector<pair<long long, long long> > pr;
if (a == b) {
if (c == a) pr.push_back({n, n - 1});
if (d == a) pr.push_back({n - 1, n});
} else if (c == d) {
if (a == c) pr.push_back({1, 2});
if (b == c) pr.push_back({2, 1});
} else {
pr.push_back({2, 1});
if (c == a) pr.push_back({n, n - 1});
if (d == a) pr.push_back({n - 1, n});
}
cout << (long long)((pr).size()) << "\n";
for (auto x : pr) {
cout << x << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long ll_scan() {
long long x;
cin >> x;
return x;
}
string str_scan() {
string x;
cin >> x;
return x;
}
int main() {
long long t = 1, T = 1;
t = ll_scan(), T = t;
while (t--) {
long long n = ll_scan();
vector<string> s(n);
for (long long i = 0; i < n; i++) cin >> s[i];
if (s[0][1] == '1') {
if (s[1][0] == '1') {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "2" << endl;
cout << n - 1 << ' ' << n << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "1" << endl;
cout << n - 1 << ' ' << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "0" << endl;
}
}
} else {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << 1 << ' ' << 2 << endl;
} else {
cout << "2" << endl;
cout << 2 << ' ' << 1 << endl;
cout << n - 1 << ' ' << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "2" << endl;
cout << 2 << ' ' << 1 << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "1" << endl;
cout << 2 << ' ' << 1 << endl;
}
}
}
} else {
if (s[1][0] == '1') {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << 2 << ' ' << 1 << endl;
} else {
cout << "2" << endl;
cout << 1 << ' ' << 2 << endl;
cout << n - 1 << ' ' << n << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "2" << endl;
cout << 1 << ' ' << 2 << endl;
cout << n << ' ' << n - 1 << endl;
} else {
cout << "1" << endl;
cout << 1 << ' ' << 2 << endl;
}
}
} else {
if (s[n - 2][n - 1] == '1') {
if (s[n - 1][n - 2] == '1') {
cout << "0" << endl;
} else {
cout << "1" << endl;
cout << n << ' ' << n - 1 << endl;
}
} else {
if (s[n - 1][n - 2] == '1') {
cout << "1" << endl;
cout << n - 1 << ' ' << n << endl;
} else {
cout << "2" << endl;
cout << n - 1 << ' ' << n << endl;
cout << n << ' ' << n - 1 << endl;
}
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void testcase() {
int n, chng = 0;
cin >> n;
vector<string> a(n);
vector<pair<int, int>> b;
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0][1] == a[1][0]) {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[n - 1][n - 2] != a[0][1]) {
cout << 0 << endl;
return;
} else {
cout << 2 << endl;
cout << n << " " << n - 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else {
if (a[n - 1][n - 2] == a[0][1]) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
}
} else {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[0][1] == a[n - 1][n - 2]) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
}
} else {
cout << 2 << endl;
if (a[0][1] == '1')
cout << 1 << " " << 2 << endl;
else {
cout << 2 << " " << 1 << endl;
}
if (a[n - 1][n - 2] == '0')
cout << n << " " << n - 1 << endl;
else {
cout << n - 1 << " " << n << endl;
}
}
}
}
int main() {
long long t;
cin >> t;
while (t--) {
testcase();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
vector<vector<long long> > grid;
long long n;
void doTest() {
cin >> n;
grid = vector<vector<long long> >(n, vector<long long>(n, 0));
for (long long i = (0); i < (n); i++) {
string s;
cin >> s;
for (long long j = (0); j < (n); j++) {
if (s[j] == '0') grid[i][j] = 0;
if (s[j] == '1') grid[i][j] = 1;
if (s[j] == 'S') grid[i][j] = -1;
if (s[j] == 'F') grid[i][j] = -1;
}
}
vector<pair<long long, long long> > change =
vector<pair<long long, long long> >();
if (grid[1][0] == grid[0][1]) {
if (grid[n - 2][n - 1] == grid[1][0]) {
change.push_back(make_pair(n - 1, n));
}
if (grid[n - 1][n - 2] == grid[1][0]) {
change.push_back(make_pair(n, n - 1));
}
} else if (grid[n - 2][n - 1] == grid[n - 1][n - 2]) {
if (grid[n - 2][n - 1] == grid[1][0]) {
change.push_back(make_pair(2, 1));
}
if (grid[n - 2][n - 1] == grid[0][1]) {
change.push_back(make_pair(1, 2));
}
} else {
if (grid[1][0] != grid[n - 2][n - 1]) {
change.push_back(make_pair(2, 1));
change.push_back(make_pair(n - 1, n));
} else {
change.push_back(make_pair(2, 1));
change.push_back(make_pair(n, n - 1));
}
}
cout << change.size() << endl;
for (long long i = (0); i < (change.size()); i++) {
cout << change[i].first << ' ' << change[i].second << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
for (long long i = (0); i < (t); i++) doTest();
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 208;
int n, mat[MAXN][MAXN];
vector<pair<int, int> > changes;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
cin >> n;
changes.clear();
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 1; j <= n; j++) {
mat[i][j] = s[j - 1] - '0';
}
}
if (mat[1][2] == 1 && mat[2][1] == 1) {
if (mat[n][n - 1] == 1) changes.push_back(make_pair(n, n - 1));
if (mat[n - 1][n] == 1) changes.push_back(make_pair(n - 1, n));
} else {
if (mat[n][n - 1] == 0 && mat[n - 1][n] == 0) {
if (mat[1][2] == 0) changes.push_back(make_pair(1, 2));
if (mat[2][1] == 0) changes.push_back(make_pair(2, 1));
} else {
if (mat[1][2] == 1) changes.push_back(make_pair(1, 2));
if (mat[2][1] == 1) changes.push_back(make_pair(2, 1));
if (mat[n][n - 1] == 0) changes.push_back(make_pair(n, n - 1));
if (mat[n - 1][n] == 0) changes.push_back(make_pair(n - 1, n));
}
}
cout << (int)changes.size() << endl;
for (pair<int, int> par : changes)
cout << par.first << " " << par.second << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n;
vector<pair<int, int> > ho1, ho2;
char a[207][207];
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] + 1;
}
ho1.clear();
ho2.clear();
if (a[1][2] == '0') {
ho1.push_back({1, 2});
} else
ho2.push_back({1, 2});
if (a[2][1] == '0') {
ho1.push_back({2, 1});
} else
ho2.push_back({2, 1});
if (a[n][n - 1] == '1') {
ho1.push_back({n, n - 1});
} else
ho2.push_back({n, n - 1});
if (a[n - 1][n] == '1') {
ho1.push_back({n - 1, n});
} else
ho2.push_back({n - 1, n});
if (ho1.size() > ho2.size()) {
printf("%d\n", ho2.size());
for (int i = 0; i < ho2.size(); i++) {
printf("%d %d\n", ho2[i].first, ho2[i].second);
}
} else {
printf("%d\n", ho1.size());
for (int i = 0; i < ho1.size(); i++) {
printf("%d %d\n", ho1[i].first, ho1[i].second);
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
using TMatrix = vector<vector<T>>;
template <class T>
using TVector = vector<T>;
using TString = string;
template <class T1, class T2>
using THashMap = unordered_map<T1, T2>;
void read(int& x) { scanf("%i", &x); }
void write(const int x) { printf("%i", x); }
void read(long long& x) { scanf("%lli", &x); }
void write(const long long& x) { printf("%lli", x); }
void read(double& x) { scanf("%lf", &x); }
void write(const double& x) { printf("%lf", x); }
void read(char& c, bool whiteSpaces = false) {
while (1) {
c = getchar();
if (whiteSpaces || !isspace(c)) {
break;
}
}
}
void write(const char c) { printf("%c", c); }
void read(TString& result, bool untilEol = false) {
result.clear();
char c;
if (!untilEol) {
while (1) {
c = getchar();
if (!isspace(c) || c == EOF) break;
}
result.push_back(c);
}
while (1) {
c = getchar();
if (c == EOF || c == '\n' || (!untilEol && isspace(c))) break;
result.push_back(c);
}
}
void write(const TString& s) { printf("%s", s.c_str()); }
void writeYES(const bool condition) {
printf("%s\n", condition ? "YES" : "NO");
}
void writeYes(const bool condition) {
printf("%s\n", condition ? "Yes" : "No");
}
template <class T>
void writeIf(const bool condition, const T& forTrue, const T& forFalse) {
cout << (condition ? forTrue : forFalse) << endl;
}
template <class T1, class T2>
void read(pair<T1, T2>& x) {
read(x.first);
read(x.second);
}
template <class T1, class T2>
void write(pair<T1, T2>& x) {
write(x.first);
write(' ');
write(x.second);
}
template <class T>
void writeln(const T& x) {
write(x);
write('\n');
}
template <class T1, class T2>
void read(T1& x1, T2& x2) {
read(x1);
read(x2);
}
template <class T1, class T2, class T3>
void read(T1& x1, T2& x2, T3& x3) {
read(x1);
read(x2);
read(x3);
}
template <class T1, class T2, class T3, class T4>
void read(T1& x1, T2& x2, T3& x3, T4& x4) {
read(x1);
read(x2);
read(x3);
read(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
void read(T1& x1, T2& x2, T3& x3, T4& x4, T5& x5) {
read(x1);
read(x2);
read(x3);
read(x4);
read(x5);
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void read(T1& x1, T2& x2, T3& x3, T4& x4, T5& x5, T6& x6) {
read(x1);
read(x2);
read(x3);
read(x4);
read(x5);
read(x6);
}
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
void read(T1& x1, T2& x2, T3& x3, T4& x4, T5& x5, T6& x6, T7& x7) {
read(x1);
read(x2);
read(x3);
read(x4);
read(x5);
read(x6);
read(x7);
}
template <class T1, class T2>
void write(const T1& x1, const T2& x2) {
write(x1);
write(' ');
write(x2);
}
template <class T1, class T2>
void writeln(const T1& x1, const T2& x2) {
write(x1, x2);
write('\n');
}
template <class T1, class T2, class T3>
void write(const T1& x1, const T2& x2, const T3& x3) {
write(x1, x2);
write(' ');
write(x3);
}
template <class T1, class T2, class T3>
void writeln(const T1& x1, const T2& x2, const T3& x3) {
write(x1, x2, x3);
write('\n');
}
template <class T1, class T2, class T3, class T4>
void write(const T1& x1, const T2& x2, const T3& x3, const T4& x4) {
write(x1, x2, x3);
write(' ');
write(x4);
}
template <class T1, class T2, class T3, class T4>
void writeln(const T1& x1, const T2& x2, const T3& x3, const T4& x4) {
write(x1, x2, x3, x4);
write('\n');
}
namespace NVector {
template <class T>
void Read(TVector<T>& v, int length = -1) {
if (length == -1) {
read(length);
}
v.resize(length);
for (auto& elem : v) {
read(elem);
}
}
template <class T>
void Write(const TVector<T>& v, TString del = " ", bool needEndl = true) {
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i < v.size() - 1) {
cout << del;
}
}
if (needEndl) {
cout << endl;
}
}
template <class T>
TVector<T> Filter(const TVector<T>& v, std::function<bool(T)> filter) {
TVector<T> result;
for (const auto& elem : v) {
if (filter(elem)) {
result.push_back(elem);
}
}
return move(result);
}
template <class T>
T Max(const TVector<T>& v) {
return *max_element(v.begin(), v.end());
}
template <class T>
void Sort(TVector<T>& v) {
sort(v.begin(), v.end());
}
template <class T>
void SortR(TVector<T>& v) {
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
}
template <class T>
void Reverse(TVector<T>& v) {
reverse(v.begin(), v.end());
}
template <class T>
T Min(const TVector<T>& v) {
return *min_element(v.begin(), v.end());
}
template <class T>
T Sum(const TVector<T>& v) {
T result = 0;
for (const auto& elem : v) {
result += elem;
}
return result;
}
template <class T>
T Mult(const TVector<T>& v) {
T result = 1;
for (const auto& elem : v) {
result *= elem;
}
return result;
}
template <class T>
TVector<int> FromInt(T number) {
TVector<int> v;
while (number) {
v.push_back(number % 10);
number /= 10;
}
reverse(v.begin(), v.end());
return move(v);
}
template <class T>
T ToInt(const TVector<int>& v) {
T ans = 0;
for (const auto e : v) {
ans = ans * 10 + e;
}
return ans;
}
template <class T>
T Mex(const TVector<T>& v) {
unordered_set<T> s(v.begin(), v.end());
T value;
for (value = 1; s.find(value) != s.end(); value += 1) {
}
return value;
}
template <class T>
bool Contains(const TVector<T>& v, const T& searchedElem) {
for (const auto& elem : v) {
if (elem == searchedElem) {
return true;
}
}
return false;
}
} // namespace NVector
namespace NMath {
TVector<bool> SieveOfEratosthenes;
template <class T>
T Gcd(T x, T y) {
while (x) {
y %= x;
swap(x, y);
}
return y;
}
template <class T>
T Lcm(T x, T y) {
return (x / Gcd(x, y)) * y;
}
template <class T>
T Gcd(const vector<T>& v) {
T ans = v.front();
for (const auto& elem : v) {
ans = Gcd(ans, elem);
}
return ans;
}
template <class T>
T Lcm(const vector<T>& v) {
T ans = v.front();
for (const auto& elem : v) {
ans = Lcm(ans, elem);
}
return ans;
}
int GcdEx(const int a, const int b, int& x, int& y) {
if (!a) {
x = 0;
y = 1;
return b;
}
int x1, y1;
int d = GcdEx(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
int CalcInverseNumber(const int number, const int mod) {
int x, y;
int g = GcdEx(number, mod, x, y);
if (g != 1) {
return -1;
}
return x = (x % mod + mod) % mod;
}
template <class T>
T Factorial(T n) {
T result = 1;
while (n > 0) {
result *= n--;
}
return result;
}
template <class T>
T FactorialWithMod(T n, T mod) {
if (n >= mod) {
return 0;
}
T result = 1;
while (n > 0) {
result = (result * n) % mod;
--n;
}
return result;
}
template <class T1, class T2>
T1 BinPow(T1 value, T2 extent) {
T1 res = 1;
while (extent > 0) {
if (extent & 1) {
res *= value;
}
extent >>= 1;
value *= value;
}
return res;
}
template <class T1, class T2>
T1 BinPowWithMod(T1 value, T2 extent, T1 mod) {
T1 res = 1;
value %= mod;
while (extent > 0) {
if (extent & 1) {
res = (value * res) % mod;
}
extent >>= 1;
value = (value * value) % mod;
}
return res;
}
void PrecalcPrimes(const int length) {
SieveOfEratosthenes.resize(length + 1, true);
SieveOfEratosthenes[0] = SieveOfEratosthenes[1] = false;
for (int i = 2; i * i <= length; ++i) {
if (SieveOfEratosthenes[i]) {
for (int j = i * i; j <= length; j += i) {
SieveOfEratosthenes[j] = false;
}
}
}
}
template <class T>
bool IsPrime(T value) {
if (value < SieveOfEratosthenes.size() - 1) return SieveOfEratosthenes[value];
for (T i = 2; i * i <= value; ++i) {
if (value % i == 0) {
return false;
}
}
return true;
}
template <class T>
T NMemberOfArithmeticProgression(const T& a1, const T& n, const T& d) {
return a1 + d * (n - 1);
}
template <class T>
T SumOfArithmeticProgression(const T& a1, const T& n, const T& d) {
return (a1 + NMemberOfArithmeticProgression(a1, n, d)) * n / 2;
}
template <class T>
T Min(const T& a, const T& b) {
return a < b ? a : b;
}
template <class T>
T Max(const T& a, const T& b) {
return a > b ? a : b;
}
template <class T>
T Abs(const T& value) {
return value >= 0 ? value : -value;
}
template <class T>
TVector<T> Factorize(T value) {
TVector<T> res;
for (T i = 2; i * i <= value; i++) {
while (value % i == 0) {
res.push_back(i);
value /= i;
}
}
if (value > 1) {
res.push_back(value);
}
return res;
}
template <class T>
TVector<T> GetDivisors(T value) {
TVector<T> res;
for (T i = 1; i * i <= value; i++) {
if (value % i == 0) {
res.push_back(i);
if (i * i != value) res.push_back(value / i);
}
}
sort(res.begin(), res.end());
return res;
}
double Log(const double a, const double b) { return log(b) / log(a); }
} // namespace NMath
int solve();
int main(int argc, char* argv[]) {
return solve();
return 0;
}
template <class T>
void Read(TMatrix<T>& matrix, int n = -1, int m = -1) {
if (n == -1) {
read(n, m);
} else if (m == -1) {
m = n;
}
matrix.clear();
matrix.resize(n, TVector<T>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
read(matrix[i][j]);
}
}
}
int solve() {
int t;
read(t);
while (t--) {
TMatrix<char> mat;
int n;
read(n);
Read<char>(mat, n);
TVector<pair<int, int>> ans;
if (mat[0][1] == mat[1][0]) {
if (mat[n - 1][n - 2] == mat[1][0]) {
ans.push_back({n, n - 1});
}
if (mat[n - 2][n - 1] == mat[1][0]) {
ans.push_back({n - 1, n});
}
} else if (mat[n - 1][n - 2] == mat[n - 2][n - 1]) {
if (mat[n - 1][n - 2] == mat[1][0]) {
ans.push_back({2, 1});
}
if (mat[n - 2][n - 1] == mat[0][1]) {
ans.push_back({1, 2});
}
} else {
ans.push_back({1, 2});
if (mat[n - 1][n - 2] == mat[1][0]) {
ans.push_back({n, n - 1});
}
if (mat[n - 2][n - 1] == mat[1][0]) {
ans.push_back({n - 1, n});
}
}
writeln<int>((int)ans.size());
for (int i = 0; i < ans.size(); i++) {
writeln(ans[i].first, ans[i].second);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
T data = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
data = (data << 3) + (data << 1) + ch - '0';
ch = getchar();
}
x = f * data;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
read(t);
read(args...);
}
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e5 + 9;
char maze[209][209];
signed main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", maze[i] + 1);
}
if (maze[1][2] == maze[2][1] && maze[n][n - 1] == maze[n - 1][n]) {
if (maze[1][2] == maze[n][n - 1])
printf("2\n1 2\n2 1\n");
else
printf("0\n");
} else if (maze[1][2] == maze[2][1]) {
if (maze[1][2] == maze[n][n - 1])
printf("1\n%d %d\n", n, n - 1);
else
printf("1\n%d %d\n", n - 1, n);
} else if (maze[n][n - 1] == maze[n - 1][n]) {
if (maze[1][2] == maze[n][n - 1])
printf("1\n1 2\n");
else
printf("1\n2 1\n");
} else {
printf("2\n2 1\n");
if (maze[1][2] == maze[n][n - 1])
printf("%d %d\n", n, n - 1);
else
printf("%d %d\n", n - 1, n);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a;
cin >> a;
for (long long i = 0; i < a; i++) {
long long b;
cin >> b;
string s[b + 1];
for (long long j = 0; j < b; j++) {
cin >> s[j];
}
vector<pair<long long, long long>> ans;
if (s[0][1] == s[1][0]) {
if (s[b - 1][b - 2] == s[0][1]) {
ans.push_back({b - 1, b - 2});
}
if (s[b - 2][b - 1] == s[0][1]) {
ans.push_back({b - 2, b - 1});
}
} else {
if (s[b - 1][b - 2] == s[b - 2][b - 1]) {
if (s[0][1] == s[b - 1][b - 2]) {
ans.push_back({0, 1});
}
if (s[b - 2][b - 1] == s[1][0]) {
ans.push_back({1, 0});
}
} else {
if (s[0][1] == '1') {
ans.push_back({0, 1});
} else {
ans.push_back({1, 0});
}
if (s[b - 1][b - 2] == '0') {
ans.push_back({b - 1, b - 2});
} else {
ans.push_back({b - 2, b - 1});
}
}
}
cout << ans.size() << "\n";
for (long long j = 0; j < ans.size(); j++) {
cout << ans[j].first + 1 << " " << ans[j].second + 1 << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void speed() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
}
int main() {
long long int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
vector<pair<int, int>> ans;
char x;
if (a[0][1] == a[1][0]) {
x = a[0][1];
if (x == a[n - 1][n - 2]) {
ans.push_back(make_pair(n, n - 1));
}
if (x == a[n - 2][n - 1]) {
ans.push_back(make_pair(n - 1, n));
}
} else if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
x = a[n - 1][n - 2];
if (x == a[0][1]) {
ans.push_back(make_pair(1, 2));
}
if (x == a[1][0]) {
ans.push_back(make_pair(2, 1));
}
} else {
x = a[0][1];
ans.push_back(make_pair(2, 1));
if (x == a[n - 1][n - 2]) {
ans.push_back(make_pair(n, n - 1));
}
if (x == a[n - 2][n - 1]) {
ans.push_back(make_pair(n - 1, n));
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> pos[10] = {make_pair(1, 2), make_pair(2, 1), make_pair(-1, 0),
make_pair(0, -1)};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char c;
int up = 0, down = 0;
vector<int> v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> c;
if ((i == 1 && j == 2) || (i == 2 && j == 1)) {
up += (c - '0');
v.push_back(c - '0');
}
if ((i == n - 1 && j == n) || (i == n && j == n - 1)) {
down += (c - '0');
v.push_back(c - '0');
}
}
}
vector<int> afis;
int myPosition = 0;
if (v[1] != v[0] && (v[0] != v[2] || v[0] != v[3])) {
afis.push_back(1);
myPosition = 0;
} else if (v[1] != v[0] && (v[1] != v[2] || v[1] != v[3])) {
afis.push_back(0);
myPosition = 1;
}
for (int i = 2; i < 4; i++)
if (v[i] == v[myPosition]) afis.push_back(i);
cout << afis.size() << "\n";
for (int i = 0; i < afis.size(); i++) {
if (afis[i] < 2)
cout << pos[afis[i]].first << " " << pos[afis[i]].second << "\n";
else
cout << n + pos[afis[i]].first << " " << n + pos[afis[i]].second
<< "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
cin >> n;
vector<vector<char>> a(n, vector<char>(n));
vector<int> ans1;
vector<int> ans2;
int res = 0;
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++) cin >> a[x][y];
int mia, mib;
mia = ((a[0][1] == '0') + (a[1][0] == '0'));
mib = ((a[n - 1][n - 2] == '0') + (a[n - 2][n - 1] == '0'));
if (mia >= mib) {
if (a[0][1] != '0') {
res++;
ans1.push_back(1);
ans2.push_back(2);
}
if (a[1][0] != '0') {
res++;
ans1.push_back(2);
ans2.push_back(1);
}
if (a[n - 1][n - 2] != '1') {
res++;
ans1.push_back(n);
ans2.push_back(n - 1);
}
if (a[n - 2][n - 1] != '1') {
res++;
ans1.push_back(n - 1);
ans2.push_back(n);
}
} else {
if (a[0][1] != '1') {
res++;
ans1.push_back(1);
ans2.push_back(2);
}
if (a[1][0] != '1') {
res++;
ans1.push_back(2);
ans2.push_back(1);
}
if (a[n - 1][n - 2] != '0') {
res++;
ans1.push_back(n);
ans2.push_back(n - 1);
}
if (a[n - 2][n - 1] != '0') {
res++;
ans1.push_back(n - 1);
ans2.push_back(n);
}
}
cout << res << endl;
for (int f = 0; f < res; f++) {
cout << ans1[f] << " " << ans2[f] << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, int> &left, const pair<int, int> &right) {
return left.first > right.first ||
(left.first == right.first && left.second < right.second);
}
int main() {
int t, cs = 1;
cin >> t;
while (t--) {
int n, m, a, b, c, i, j, k, mx = 0, mn = 1e9;
string ar[2000];
int bs[2000] = {0};
cin >> n;
for (i = 0; i < n; i++) cin >> ar[i];
if (ar[0][1] == ar[1][0] && ar[n - 1][n - 2] == ar[n - 2][n - 1]) {
if (ar[0][1] != ar[n - 1][n - 2])
cout << 0 << endl;
else {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
}
} else if (ar[0][1] == ar[1][0] && ar[n - 1][n - 2] != ar[n - 2][n - 1]) {
if (ar[0][1] == ar[n - 1][n - 2]) {
cout << 1 << endl;
cout << n << " " << n - 1 << endl;
} else {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
} else if (ar[0][1] != ar[1][0] && ar[n - 1][n - 2] == ar[n - 2][n - 1]) {
if (ar[0][1] == ar[n - 1][n - 2]) {
cout << 1 << endl;
cout << 1 << " " << 2 << endl;
} else {
cout << 1 << endl;
cout << 2 << " " << 1 << endl;
}
} else {
if (ar[0][1] == ar[n - 1][n - 2]) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << n - 1 << " " << n << endl;
} else {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
cout << n - 1 << " " << n << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T, n, res, a1, a2, a3, a4;
char s[210][210];
bool check(char x, char y) {
res =
(s[1][2] == y) + (s[2][1] == y) + (s[n][n - 1] == x) + (s[n - 1][n] == x);
if (res <= 2) {
printf("%d\n", res);
if (s[1][2] == y) printf("1 2\n");
if (s[2][1] == y) printf("2 1\n");
if (s[n][n - 1] == x) printf("%d %d\n", n, n - 1);
if (s[n - 1][n] == x) printf("%d %d\n", n - 1, n);
return 1;
}
return 0;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
if (check('0', '1'))
;
else
check('1', '0');
}
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int t = 1;
cin >> t;
while (t--) {
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
int cnt = 0;
int c = 0;
vector<pair<int, int>> v;
int ans = 0;
if (a[0][1] == '1') {
cnt++;
}
if (a[1][0] == '1') {
cnt++;
}
if (a[n - 1][n - 2] == '1') {
c++;
}
if (a[n - 2][n - 1] == '1') {
c++;
}
if (cnt == 2) {
if (a[n - 2][n - 1] == '1') {
v.push_back({n - 1, n});
ans++;
}
if (a[n - 1][n - 2] == '1') {
v.push_back({n, n - 1});
ans++;
}
} else if (c == 2) {
if (a[1][0] == '1') {
v.push_back({2, 1});
ans++;
}
if (a[0][1] == '1') {
v.push_back({1, 2});
ans++;
}
} else if (cnt == 0) {
if (a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n});
ans++;
}
if (a[n - 1][n - 2] == '0') {
v.push_back({n, n - 1});
ans++;
}
} else if (c == 0) {
if (a[1][0] == '0') {
v.push_back({2, 1});
ans++;
}
if (a[0][1] == '0') {
v.push_back({1, 2});
ans++;
}
} else if (cnt == 1 && c == 1) {
if (a[1][0] == '1') {
v.push_back({2, 1});
ans++;
}
if (a[0][1] == '1') {
v.push_back({1, 2});
ans++;
}
if (a[n - 2][n - 1] == '0') {
v.push_back({n - 1, n});
ans++;
}
if (a[n - 1][n - 2] == '0') {
v.push_back({n, n - 1});
ans++;
}
}
cout << ans << "\n";
for (auto u : v) {
cout << u.first << " " << u.second << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string m[n];
for (int i = 0; i < n; i++) cin >> m[i];
char a = m[1][0], b = m[0][1], c = m[n - 1][n - 2], d = m[n - 2][n - 1];
if (a == b && c == d) {
if (a == c)
cout << "2\n2 1\n1 2\n";
else
cout << 0 << endl;
} else if (a == b) {
if (c == a)
cout << "1\n" << n << ' ' << n - 1 << endl;
else
cout << "1\n" << n - 1 << ' ' << n << endl;
} else if (c == d) {
if (c == a)
cout << "1\n2 1\n";
else
cout << "1\n1 2\n";
} else {
if (a == c)
cout << "2\n2 1\n" << n - 1 << ' ' << n << endl;
else
cout << "2\n2 1\n" << n << ' ' << n - 1 << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long b) {
long long res = 1;
while (b != 0) {
if (1 & b) res *= a;
b >>= 1;
a *= a;
}
return res;
}
void solve() {
long long n, m;
cin >> n;
long long a, b, c, d, temp;
string s;
for (long long i = 1; i < n + 1; i++) {
cin >> s;
if (i == 1) {
a = s[1];
}
if (i == 2) {
b = s[0];
}
if (i == n - 1) {
c = s[n - 1];
}
if (i == n) {
d = s[n - 2];
}
}
if (a == b && c == d && a != c) {
cout << "0\n";
} else if (a == b && c == d && a == b) {
cout << "2\n1 2\n";
cout << "2 1\n";
} else if (a == b && c != d) {
if (c == a) {
cout << "1\n" << n - 1 << " " << n << "\n";
}
if (d == a) {
cout << "1\n" << n << " " << n - 1 << "\n";
}
} else if (a != b && c == d) {
if (c == a) {
cout << "1\n1 2\n";
}
if (c == b) {
cout << "1\n2 1\n";
}
} else {
if (a == c) {
cout << "2\n1 2\n" << n << " " << n - 1 << "\n";
}
if (a == d) {
cout << "2\n1 2\n" << n - 1 << " " << n << "\n";
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tt;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
char arr[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cin >> arr[i][j];
}
vector<pair<int, int>> ans;
int a, b, c, d;
a = (int)arr[0][1] - '0';
b = (int)arr[1][0] - '0';
c = (int)arr[n - 2][n - 1] - '0';
d = (int)arr[n - 1][n - 2] - '0';
int sum = 0;
sum += (a != 0) + (b != 0) + (c != 1) + (d != 1);
if (sum <= 2) {
if (a != 0) ans.push_back({1, 2});
if (b != 0) ans.push_back({2, 1});
if (c != 1) ans.push_back({n - 1, n});
if (d != 1) ans.push_back({n, n - 1});
} else {
if (a != 1) ans.push_back({1, 2});
if (b != 1) ans.push_back({2, 1});
if (c != 0) ans.push_back({n - 1, n});
if (d != 0) ans.push_back({n, n - 1});
}
cout << ans.size() << "\n";
for (auto x : ans) cout << x.first << " " << x.second << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace {
template <typename T>
inline void read(T &x) {
x = 0;
T f = 1;
char s = getchar();
for (; !isdigit(s); s = getchar())
if (s == '-') f = -1;
for (; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
x *= f;
}
} // namespace
const int N = 205;
char s[N][N];
int main() {
int t;
read(t);
while (t--) {
int n;
read(n);
for (int i = 1; i <= n; i++) {
scanf("%s", s[i] + 1);
}
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[n - 1][n] && s[1][2] != s[n][n - 1])
puts("0");
else if (s[n][n - 1] == s[n - 1][n]) {
printf("2\n%d %d\n%d %d\n", n, n - 1, n - 1, n);
} else {
printf("1\n");
if (s[1][2] == s[n][n - 1]) {
printf("%d %d\n", n, n - 1);
} else
printf("%d %d\n", n - 1, n);
}
} else {
if (s[n][n - 1] != s[n - 1][n]) {
printf("2\n");
if (s[1][2] == s[n][n - 1])
printf("1 2\n%d %d\n", n - 1, n);
else
printf("1 2\n%d %d\n", n, n - 1);
} else {
printf("1\n");
if (s[1][2] == s[n][n - 1])
printf("1 2\n");
else
printf("2 1\n");
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int tc;
cin >> tc;
while (tc--) {
long long int n;
cin >> n;
char ar[n + 1][n + 1];
for (long long int i = 0; i < n; i++)
for (long long int j = 0; j < n; j++) cin >> ar[i][j];
long long int a, b, c, d;
a = ar[0][1];
b = ar[1][0];
c = ar[n - 2][n - 1];
d = ar[n - 1][n - 2];
if (a == b and c == d and c == a) {
cout << 2 << endl;
cout << n - 1 << " " << n << endl << n << " " << n - 1 << endl;
} else if (a != b and c != d) {
cout << 2 << endl;
if (a != c)
cout << 1 << " " << 2 << endl << n - 1 << " " << n << endl;
else if (a != d)
cout << 1 << " " << 2 << endl << n << " " << n - 1 << endl;
} else if (a == b and c != d) {
cout << 1 << endl;
if (c == a)
cout << n - 1 << " " << n << endl;
else if (d == a)
cout << n << " " << n - 1 << endl;
} else if (a != b and c == d) {
cout << 1 << endl;
if (a == c)
cout << 1 << " " << 2 << endl;
else if (b == c)
cout << 2 << " " << 1 << endl;
} else if (((a == b) and (c == d)) and c != a)
cout << 0 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
scanf("%d", &n);
char ch[n + 1][n + 1], a, b, c, d, x;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; ++j) cin >> ch[i][j];
}
a = ch[0][1];
b = ch[1][0];
c = ch[n - 1][n - 2];
d = ch[n - 2][n - 1];
if (a == b && c != d) {
cout << 1 << "\n";
if (a == c) {
cout << n << " " << n - 1 << "\n";
} else {
cout << n - 1 << " " << n << "\n";
}
} else if (a == b and c == d) {
if (a == c) {
cout << 2 << "\n";
cout << 1 << " " << 2 << "\n";
cout << 2 << " " << 1 << "\n";
} else
cout << 0 << "\n";
} else if (c == d and a != b) {
cout << 1 << "\n";
if (a == c) {
cout << 1 << " " << 2 << "\n";
} else {
cout << 2 << " " << 1 << "\n";
}
} else if (a != b and c != d) {
cout << "2\n";
if (a == c) {
cout << "1 2"
<< "\n";
cout << n - 1 << " " << n << "\n";
} else {
cout << "1 2"
<< "\n";
cout << n << " " << n - 1 << "\n";
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int vis[301][300];
char str[300][300];
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, k = 1, sum = 0;
bool sign = false;
memset(vis, 0, sizeof(vis));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> str[i][j];
}
}
if (str[1][2] != '0') {
sum++;
vis[1][2] = 1;
}
if (str[2][1] != '0') {
sum++;
vis[2][1] = 1;
}
if (str[n - 1][n] != '1') {
sum++;
vis[n - 1][n] = 1;
}
if (str[n][n - 1] != '1') {
sum++;
vis[n][n - 1] = 1;
}
if (sum > 2) {
memset(vis, 0, sizeof(vis));
sum = 0;
if (str[1][2] != '1') {
sum++;
vis[1][2] = 1;
}
if (str[2][1] != '1') {
sum++;
vis[2][1] = 1;
}
if (str[n - 1][n] != '0') {
sum++;
vis[n - 1][n] = 1;
}
if (str[n][n - 1] != '0') {
sum++;
vis[n][n - 1] = 1;
}
}
printf("%d\n", sum);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (vis[i][j]) {
printf("%d %d\n", i, j);
}
}
}
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
char mat[201][201];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> mat[i][j];
int a = mat[0][1] - '0';
int b = mat[1][0] - '0';
int c = mat[n - 1][n - 2] - '0';
int d = mat[n - 2][n - 1] - '0';
int cnt = 0;
vector<pair<int, int>> ans;
if (a == b) {
if (c == a) {
cnt++;
ans.push_back({n - 1, n - 2});
}
if (d == a) {
cnt++;
ans.push_back({n - 2, n - 1});
}
} else {
int cnt0 = 0, cnt1 = 0;
if (a == 0)
cnt0++;
else
cnt1++;
if (b == 0)
cnt0++;
else
cnt1++;
if (c == 0)
cnt0++;
else
cnt1++;
if (d == 0)
cnt0++;
else
cnt1++;
if (cnt0 == cnt1) {
if (a == 0) {
cnt++;
ans.push_back({1, 0});
} else {
cnt++;
ans.push_back({0, 1});
}
if (c == 1) {
cnt++;
ans.push_back({n - 2, n - 1});
} else {
cnt++;
ans.push_back({n - 1, n - 2});
}
} else {
if (cnt0 == 1) {
if (a == 0) {
cnt++;
ans.push_back({1, 0});
} else {
cnt++;
ans.push_back({0, 1});
}
} else {
if (a == 1) {
cnt++;
ans.push_back({1, 0});
} else {
cnt++;
ans.push_back({0, 1});
}
}
}
}
cout << cnt << "\n";
for (auto x : ans) cout << x.first + 1 << " " << x.second + 1 << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize "trapv"
using namespace std;
const int N = 1e5 + 5;
const unsigned int M = 1000000007;
long long a[N], b[N];
char invert(char x) {
if (x == '0')
return '1';
else
return '0';
}
void solve() {
int n;
cin >> n;
char a[n][n];
vector<pair<int, int>> ans;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> a[i][j];
if (a[0][1] == a[1][0]) {
if (a[n - 1][n - 2] == a[0][1]) {
a[n - 1][n - 2] = invert(a[n - 1][n - 2]);
ans.push_back(make_pair(n, n - 1));
}
if (a[n - 2][n - 1] == a[0][1]) {
a[n - 2][n - 1] = invert(a[n - 2][n - 1]);
ans.push_back(make_pair(n - 1, n));
}
} else {
if (a[n - 1][n - 2] == a[n - 2][n - 1]) {
if (a[0][1] == a[n - 1][n - 2])
ans.push_back(make_pair(1, 2));
else
ans.push_back(make_pair(2, 1));
} else {
if (a[0][1] != a[n - 1][n - 2]) {
ans.push_back(make_pair(1, 2));
ans.push_back(make_pair(n, n - 1));
} else {
ans.push_back(make_pair(1, 2));
ans.push_back(make_pair(n - 1, n));
}
}
}
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << ' ' << ans[i].second << "\n";
}
}
int32_t main() {
;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.