solution set#1
The Very Beautiful Blanket
题意
构造一个
思路
使每个
代码
#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
题意
在
思路
如果弹力球的下一步走到之前经过的状态,那么它将进入死循环。
弹力球总共有4种方向,那么对于整个矩阵有
代码
#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)
题意
一个机器人从
思路
容易想到当水平总的移动方向和垂直总的移动方向等于炸弹位置时,机器人会踩到炸弹。此外,机器人只能向一个方向移动,且炸弹就在该方向上,机器人的移动次数足以到达埋炸弹的地方时,也无法避免。
可以证明存在一种方案,使得相同的方向连续排在一起的。枚举四个方向的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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】