538D - Weird Chess - CodeForces Solution


brute force constructive algorithms implementation *1800

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <unordered_map>
#include <set>
#include <vector>
#include <stack>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdio>
//#include <nits/stdc++.h>
#define debug cout << "***";
#define inf 1000000000000000005
#define threesum cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);

using namespace std;

int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int lcm(int a, int b) {
    return ((a * b) / (gcd(a, b)));
}

bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
    return true;
}

//printf("%.10f\n", (double)sum / n);

char board[55][55], ans[105][105];

int main() {
    threesum
    int n;cin >> n;
    vector<pair<int, int>> mohre, attacked;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> board[i][j];
            if (board[i][j] == 'o') mohre.emplace_back(i, j);
            if (board[i][j] == 'x') attacked.emplace_back(i, j);
        }
    }
    if (n == 1) {
        cout << "YES\no";
    }
    else {
        if (attacked.size() == 0) {
            cout << "YES\n";
            const int len = 2 * n - 1, mid = len / 2;
            for (int i = 0; i < len; ++i) {
                for (int j = 0; j < len; ++j) {
                    if (i == mid && j == mid)cout << 'o';
                    else cout << '.';
                }
                cout << "\n";
            }
        }
        else {
            const int len = 2 * n - 1, mid = len / 2;
            for (int i = 0; i < len; ++i) {
                for (int j = 0; j < len; ++j) {
                    ans[i][j] = 'x';
                }
            }
            ans[mid][mid] = 'o';
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (board[i][j] != '.') continue;
                    for (int k = 0; k < mohre.size(); ++k) {
                        pair<int, int> d{ mid + (i - mohre[k].first), mid + (j - mohre[k].second) };
                        if (d.first >= 0 && d.first < len && d.second >= 0 && d.second < len) {
                            ans[d.first][d.second] = '.';
                        }
                    }
                }
            }
            bool ok = false;
            for (int i = 0; i < attacked.size(); ++i) {
                ok = false;
                for (int j = 0; j < mohre.size(); ++j) {
                    if (ans[mid + attacked[i].first - mohre[j].first][mid + attacked[i].second - mohre[j].second] == 'x') ok = true;
                }
                if (!ok) break;
            }
            if (!ok) cout << "NO\n";
            else {
                cout << "YES\n";
                for (int i = 0; i < len; ++i) {
                    for (int j = 0; j < len; ++j) {
                        cout << ans[i][j];
                    }
                    cout << "\n";
                }
            }
        }
    }
}


Comments

Submit
0 Comments
More Questions

672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element