1863D - Two-Colored Dominoes - CodeForces Solution


constructive algorithms graphs greedy

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define F first
#define S second
#define el '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define MAX 2147483647
#define MIN -2147483647
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int mod = 1e9 + 7, N = 1e6;

void solve(){
    int n, m;
    cin >> n >> m;
    vector<vector<char>> grid(n, vector<char>(m));
    bool imp = 0;
    for(int i = 0; i < n; i++){
        int cnt = 0;
        for(int j = 0; j < m; j++){
            cin >> grid[i][j];
            if(grid[i][j] != '.') cnt++;
        }
        if(cnt%2 != 0) imp = 1;
    }
    for(int j = 0; j < m; j++){
        int cnt = 0;
        for(int i = 0; i < n; i++)
            if(grid[i][j] != '.') cnt++;
        if(cnt%2 != 0) imp = 1;
    }
    if(imp){
        cout << -1 << el;
        return;
    }
    //rows
    for(int i = 0; i < n-1; i++){
        bool white = 1;
        for(int j = 0; j < m; j++){
            if(grid[i][j] == 'U'){
                if(white) grid[i][j] = 'W', grid[i+1][j] = 'B';
                else grid[i][j] = 'B', grid[i+1][j] = 'W';
                white = !white;
            }
        }
    }
    //cols
    for(int j = 0; j < m-1; j++){
        bool white = 1;
        for(int i = 0; i < n; i++){
            if(grid[i][j] == 'L'){
                if(white) grid[i][j] = 'W', grid[i][j+1] = 'B';
                else grid[i][j] = 'B', grid[i][j+1] = 'W';
                white = !white;
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++)
            cout << grid[i][j];
        cout << el;
    }
}

int main() {
    FIO
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two