CF-Codeforces Round #676 (Div. 2)-B. Putting Bricks in the Wall【思维】

it2024-12-08  13

题目链接 题意:给定一个 n ∗ n n*n nn的01矩阵,起点是 ( 1 , 1 ) (1,1) (1,1),终点是 ( n , n ) (n,n) (n,n),从始至终只可以走相同的数字,为了使可行路线不存在,可以转换除起点和终点之外的其他点(每次转换可以把1变成0或者把0变成1),问能否在两次转换之内实现. 思路:显然只要把起点或终点封住就行,也就是让起点旁边的两个数字和终点旁边的两个数字不一样。 AC代码:

#include <bits/stdc++.h> #define pcc pair<char, char> #define pii pair<int, int> #define vi vector<int> #define vl vector<ll> #define rep(i, x, y) for (int i = x; i < y; i++) #define per(i, x, y) for (int i = x; i >= y; i--) #define rep0(i, n) for (int i = 0; i < (n); i++) #define per0(i, n) for (int i = (n)-1; i >= 0; i--) #define mp make_pair #define pb push_back #define F first #define S second #define sz(x) (x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ull unsigned long long #define db double #define ld long double using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } /***************main****************/ ll T = 1; ll m, n; int s[4]; int main() { T = read(); while (T--) { cin >> n; vector<string> a(n + 1); rep(i, 0, n) cin >> a[i]; int num = 0; s[0] = a[0][1] - '0', s[1] = a[1][0] - '0', s[2] = a[n - 1][n - 2] - '0', s[3] = a[n - 2][n - 1] - '0'; rep(i, 0, 4) num += s[i]; if (num == 0 || num == 4) { cout << 2 << endl; cout << 1 << ' ' << 2 << endl; cout << 2 << ' ' << 1 << endl; } else if (num == 1) { cout << 1 << endl; if (s[0] == 1) cout << 2 << ' ' << 1 << endl; else if (s[1] == 1) cout << 1 << ' ' << 2 << endl; else if (s[2] == 1) cout << n - 1 << ' ' << n << endl; else if (s[3] == 1) cout << n << ' ' << n - 1 << endl; } else if (num == 2) { if (s[0] + s[1] == 0 || s[2] + s[3] == 0) cout << 0 << endl; else { cout << 2 << endl; // 1 if (s[0] == 1) cout << 2 << ' ' << 1 << endl; else cout << 1 << ' ' << 2 << endl; if (s[2] == 1) cout << n << ' ' << n - 1 << endl; else cout << n - 1 << ' ' << n << endl; } } else if (num == 3) { cout << 1 << endl; if (s[0] + s[1] == 1) { if (s[0]) cout << 1 << ' ' << 2 << endl; else cout << 2 << ' ' << 1 << endl; } else { if (s[2]) cout << n << ' ' << n - 1 << endl; else cout << n - 1 << ' ' << n << endl; } } } return 0; }
最新回复(0)