Problem link : https://codeforces.com/problemset/problem/1966/B
I am not able to solve this problem. Can anyone help me?
Try:
I tried to do brute force for all squares with a time complexity of O(n^4).
I am expecting an optimized code. Can anyone please provide code with concept.
>Solution :
In that problem, you have to check 1st row & column and the last row & column if ‘B’ or ‘W’ exists. If you get the same color on all 4 sides, it’s possible to get such a grid. Because we can chose (x,y) where min(x1,x2) ≤ x ≤ max(x1,x2) and min(y1,y2) ≤ y ≤ max(y1,y2) and change colors.
Recently I solved this problem. So, I can share my code.
Code in C++:
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ll long long int
int main () {
fast
unsigned test; cin >> test; // Non-negative
while (test--) {
int row, column;
cin >> row >> column;
vector<vector<char>> v(row);
for (int i = 0; i < row; i++) {
string s;
cin >> s;
for (auto c : s) {
v[i].push_back(c);
}
}
int count_W = 0, count_B = 0;
for (int i = 0; i < column; i++) {
if (v[0][i] == 'W' && count_W == 0) {
count_W++;
}
if (v[0][i] == 'B' && count_B == 0) {
count_B++;
}
}
for (int i = 0; i < column; i++) {
if (v[row - 1][i] == 'W' && count_W == 1) {
count_W++;
}
if (v[row - 1][i] == 'B' && count_B == 1) {
count_B++;
}
}
for (int i = 0; i < row; i++) {
if (v[i][0] == 'W' && count_W == 2) {
count_W++;
}
if (v[i][0] == 'B' && count_B == 2) {
count_B++;
}
}
for (int i = 0; i < row; i++) {
if (v[i][column - 1] == 'W' && count_W == 3) {
count_W++;
}
if (v[i][column - 1] == 'B' && count_B == 3) {
count_B++;
}
}
cout << ((count_W == 4 || count_B == 4) ? "YES" : "NO") << endl;
}
return 0;
}