solution set#1

The Very Beautiful Blanket

Problem - A - Codeforces

题意

构造一个n×m的矩阵,使其中每个4×4子矩阵中,右上角的2×2异或和与左下角的2×2异或和相等,左上角和右下角也是一样。

思路

使每个2×2子矩阵的异或和为0即可。

代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void solve() {
    int n, m;
    cin >> n >> m;

    cout << n * m << "\n";
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cout << (i << 10) + j << " \n"[j == m - 1];
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while(t--) {
        solve();
    }

    return 0;
}

Bouncy Ball

Problem - F - Codeforces

题意

n×m的矩阵上,一个弹力球从某个点朝某个方向发射,遇到边界后改变方向。求弹力求经过几次反弹后到达指定点,如果弹力求永远无法到达指定点,输出-1。

思路

如果弹力球的下一步走到之前经过的状态,那么它将进入死循环。

弹力球总共有4种方向,那么对于整个矩阵有4nm种状态,复杂度可行,所以直接模拟即可。

代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void solve() {
    int n, m, i, j, x, y;
    string d;
    cin >> n >> m >> i >> j >> x >> y >> d;

    int dx = (d[0] == 'D' ? 1 : -1);
    int dy = (d[1] == 'R' ? 1 : -1);
    int ans = 0;
    for(int k = 0; k < 4 * n * m; k++) {
        if(i == x && j == y) {
            cout << ans << "\n";
            return;
        }
        int f = 0;
        if(i + dx > n || i + dx < 1) {
            f = 1;
            dx *= -1;
        }
        if(j + dy > m || j + dy < 1) {
            f = 1;
            dy *= -1;
        }
        i += dx, j += dy;
        ans += f;
    }

    cout << -1 << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while(t--) {
        solve();
    }

    return 0;
}

Evil Coordinate

Evil Coordinate (nowcoder.com)

题意

一个机器人从(0,0)开始,按指令移动,在(x,y)埋有一个炸弹,为防止机器人踩到炸弹,可以改变指令的顺序。如果无论如何都无法阻止机器人踩到炸弹,输出"Impossible"。

思路

容易想到当水平总的移动方向和垂直总的移动方向等于炸弹位置时,机器人会踩到炸弹。此外,机器人只能向一个方向移动,且炸弹就在该方向上,机器人的移动次数足以到达埋炸弹的地方时,也无法避免。

可以证明存在一种方案,使得相同的方向连续排在一起的。枚举四个方向的24种排列一一验证即可。

代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
string str = "URDL";

void solve() {
    int mx, my;
    string s;
    cin >> mx >> my >> s;

    array<int, 4> d = {0, 0, 0, 0};
    for(auto c : s) {
        d[0] += (c == 'U');
        d[1] += (c == 'R');
        d[2] += (c == 'D');
        d[3] += (c == 'L');
    }

    int tx = d[1] - d[3], ty = d[0] - d[2];
    if((tx == mx && ty == my) || (mx == 0 && my == 0)) {
        cout << "Impossible\n";
        return;
    }

    array<int, 4> p = {0, 1, 2, 3};
    int n = s.size();
    do {
        int x = 0, y = 0, ok = 0;
        string ans(n, '0');
        for(int i = 0, cnt = 0; i < 4; i++) {
            for(int j = 0; j < d[p[i]]; j++) {
                x += dx[p[i]];
                y += dy[p[i]];
                if(x == mx && y == my) {
                    ok = 1;
                    break;
                }
                ans[cnt++] = str[p[i]];
            }
            if(ok) break;
        }
        if(ok) continue;
        cout << ans << "\n";
        return;
    } while(next_permutation(p.begin(), p.end()));

    cout << "Impossible\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while(t--) {
        solve();
    }

    return 0;
}
posted @   wuyoudexian  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示