整体情况

提交记录

  • A题

    • 提交 3 发通过
    • 耗时 30 min
    • 反思:考虑问题不够全面,代码能力有待加强
  • B题

    • 提交 1 发过
    • 耗时 1h
    • 反思:题目记得看清楚,别没看清题就开始做
  • C题

    • 提交 1 发过
    • 耗时 40 min
    • 反思:变量记得别写错
  • D题

    • 提交 2 发过
    • 耗时 40 min
    • 反思:搜索记得加减枝
  • E题

    • 提交 1 发过
    • 耗时 1.5h
    • 反思:想清楚思路后再做,慢慢来

A

题目大意

给定一个 $n \times m$ 的迷宫,每个格子为 $.$(空地)或 $#$(障碍)。你从左上角 $(1, 1)$ 出发,目标是到达右下角 $(n, m)$,每步可以向上、下、左、右移动一格,不能移动到迷宫外部,也不能移动到障碍格子上。

你可以选择至多一次服用一种药剂,在服药后的连续$k(k \ge 0)$步中,你可以将障碍视为可以通行的空地。

请你计算从起点到终点可达的前提下,所需的最小 $k$ 值是多少。

数据范围

  • $1 \le T \le 2.5 \times 10^5$

  • $2 \le n, m \le 1000$

  • $\sum nm \le 10^6$

思路

答案 $k$ 值很明显具有单调性,于是我们可以二分答案这个 $k$,然后用 bfs 来验证这个 $k$ 是否可以,时间复杂度 $\mathcal O (nm \times 常数)$。

那能不能不二分呢?

当然可以!我们可以用 bfs 先搜索一遍起点最多能到达那些墙,终点可以到达那些墙,然后用一个多源 bfs 来索索起点到达的墙的位置到终点能到的墙需要走多少步。搜索出来的最小值就是 $k$ 这个答案。

需要注意的是,喝了药剂不代表只能走墙,也可以走 $.$ 的位置。如果可以不通过走墙就可以到达终点,那么就不要走墙,这个需要特殊处理。

时间复杂度:$\mathcal O (nm)$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <bits/stdc++.h>

using namespace std;

const int N = 1010;

int T, n, m, flag;
int vis[N][N];
char a[N][N];
vector<pair<int, int>> start, ended;
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};

void init() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
vis[i][j] = 0;
}
}
}

void bfs1(int sx, int sy) {
init();
queue<tuple<int, int>> q;
q.push({sx, sy});
vis[sx][sy] = 1;
while (!q.empty()) {
auto [x, y] = q.front();
if (x == n && y == m) {
flag = 1;
return;
}
q.pop();
for (int i = 1; i <= 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty]) {
if (!vis[tx][ty] && a[tx][ty] != '#') {
vis[tx][ty] = 1;
q.push({tx, ty});
}
if (a[tx][ty] == '#') {
start.push_back({tx, ty});
}
}
}
}
}

void bfs2(int sx, int sy) {
init();
queue<tuple<int, int>> q;
q.push({sx, sy});
vis[sx][sy] = 1;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (int i = 1; i <= 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
if (!vis[tx][ty] && a[tx][ty] != '#') {
vis[tx][ty] = 1;
q.push({tx, ty});
}
if (a[tx][ty] == '#') {
ended.push_back({tx, ty});
}
}
}
}
}

void bfs3() {
init();
queue<tuple<int, int, int>> q;
for (auto [x, y] : start)
q.push({x, y, 1});
while (!q.empty()) {
auto [x, y, step] = q.front();
q.pop();
if (vis[x][y])
continue;
vis[x][y] = step;
for (int i = 1; i <= 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
q.push({tx, ty, step + 1});
}
}
}
int ans = 1e9;
for (auto [x, y] : ended)
ans = min(ans, vis[x][y]);
cout << ans << '\n';
}

void solve() {
start.clear(), ended.clear();
flag = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
bfs1(1, 1);
if (flag) {
cout << "0\n";
return;
}
bfs2(n, m);
if (start.empty()) {
cout << "0\n";
return;
}
bfs3();
}

int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> T;
while (T--) {
solve();
}
return 0;
}

B

题目大意

给定一个 $n \times m$ 的字母矩阵,判断是否存在由相同字母构成的环。环需要满足:

  • 所有字母相同

  • 长度 $\ge 4$

  • 首尾相连且不自交

如果存在输出 Yes,否则输出 No。

数据范围

  • $2 \le n, m \le 50$

思路

直接暴搜就完事了。

时间复杂度:$\mathcal O (nm)$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>

using namespace std;

int n, m;
map<char, int> mp;
bool vis[60][60];
char a[60][60];
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};

bool dfs(int x, int y, char k, int prex, int prey) {
vis[x][y] = 1;
for (int i = 1; i <= 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx == prex && ty == prey) continue;
if (vis[tx][ty] && a[tx][ty] == k) return true;
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty] && a[tx][ty] == k) {
if (dfs(tx, ty, k, x, y))
return true;
}
}
return false;
}

int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j], mp[a[i][j]]++;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (vis[i][j]) continue;
if (mp[a[i][j]] < 4) continue;
if (dfs(i, j, a[i][j], -1, -1)) {
cout << "Yes";
return 0;
}
}
}
cout << "No";
return 0;
}

C

题目大意

现在有一个英雄有如下 4 个技能:

  1. 对一名敌人造成 $d_i$ 点伤害

  2. 积攒 $a$ 点怒气值

  3. 对一名敌人造成你积攒的怒气值的伤害,怒气值不清空

  4. 下一回合对敌人早成的伤害翻倍

他打 $n$ 个回合,每次释放一次技能就需要冷却 2 个回合,你需要求出 $n$ 个回合后最大能对敌人造成多少伤害。

数据范围

  • $1 \le n \le 18$

  • $1 \le a, d_i \le 10^5$

思路

我们发现 $n$ 只有 $18$,所以我们可以直接暴力模拟搜索即可。

注意题目要读清楚,别模拟错了。

时间复杂度:$\mathcal O (4^{n})$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>

using namespace std;

int n, a, d[20], ans;

void dfs(int x, int c1, int c2, int c3, int c4, int sum, int cnt, int f) {
if (x == n + 1) {
ans = max(ans, sum);
return;
}
if (c1 == 0)
dfs(x + 1, 2 , max(c2 - 1, 0), max(c3 - 1, 0), max(c4 - 1, 0), sum + d[x] * f, cnt , 1);
if (c2 == 0)
dfs(x + 1, max(c1 - 1, 0), 2 , max(c3 - 1, 0), max(c4 - 1, 0), sum , cnt + a, 1);
if (c3 == 0)
dfs(x + 1, max(c1 - 1, 0), max(c2 - 1, 0), 2 , max(c4 - 1, 0), sum + cnt * f , cnt , 1);
if (c4 == 0)
dfs(x + 1, max(c1 - 1, 0), max(c2 - 1, 0), max(c3 - 1, 0), 2 , sum , cnt , 2);
}

int main() {
cin >> n >> a;
for (int i = 1; i <= n; i++)
cin >> d[i];
dfs(1, 0, 0, 0, 0, 0, 0, 1);
cout << ans;
return 0;
}

D

题目大意

给定两个整数 $a$ 和 $b$,你可以在每一轮中执行以下两种操作之一:

  • 如果 $a>0$,则将 $a$ 的值减少 $gcd⁡(a,b)$ 。

  • 如果 $b>0$,则将 $b$ 的值减少 $gcd⁡(a,b)$ 。

你需要求出将 $a, b$ 都变成 $0$ 的最小操作次数。

数据范围

  • $1 \le T \le 1000$

  • $1 \le a \le b$

  • $a \le 5000$

  • $b \le 10^{18}$

思路

这道题有一个特大难点就是这玩意最多 $26$ 步就可以完成,所以我们可以限制搜索层数。

那为只要 $26$ 呢?

我们知道,如果 $a, b$ 中的任意一个数为 $0$,那么下一次也就可以让另一个书变为 $0$。

考虑 $a$ 和 $b$ 的二进制最低位:

  • 如果都是 $0$ ,则此后的 GCD 中必然包含 $2$ 这个因子,此时将 $a$ 和 $b$ 除以 $2$ 不会影响答 案。

  • 如果某一个数的二进制最低位不是 $0$ ,则 GCD 必然是一个奇数。此时将最低位不是 $0$ 的 那个数减去 GCD ,最低位会变为 $0$ 。 于是,只需要至多 $24$ 步,就能将 $a$ 除以 $4096$ ,而 $a$ 的最大值只有 $5000$ 。此时 $a$ 最大只能 为 $1$ ,再操作一次即可使 $a$ 归零。

得到这个结论后就可以直接模拟了。

这里需要注意要加个剪枝,如果当前修改步数已经超过当前最有答案了就没必要再继续搜索了。

时间复杂度:$\mathcal O (2^{26})$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
#define int long long

using namespace std;

int T, ans;

int gcd(int a, int b) {
return !b ? a : gcd(b, a % b);
}

void dfs(int a, int b, int k) {
if (k > 26)
return;
if (k >= ans)
return;
if (a == 0 && b == 0) {
ans = min(ans, k);
return;
}
if (a > 0)
dfs(a - gcd(a, b), b, k + 1);
if (b > 0)
dfs(a, b - gcd(a, b), k + 1);
}

void solve() {
ans = 30;
int a, b;
cin >> a >> b;
dfs(a, b, 0);
cout << ans << '\n';
}

signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> T;
while (T--) {
solve();
}
return 0;
}

E

题目大意

给定一个 $3$ 行 $3$ 列的网格,每个格子包含一个整数。网格中的数字 $1$ 到 $9$(含两端)每个数字恰好出现一次。

你可以对网格执行以下三种操作:

img

现在给定两个这样的网格,计算将第一个网格转换为第二个网格所需的最少操作次数。

数据范围

  • $1 \le T \le 2 \times 10^5$

思路

如果每次询问都去重新搜索一遍的话,那么肯定会 TLE,于是我们要将一些其他情况变成一种情况。

具体咋变可以看代码~~(不太会写)~~。

变成一种情况后,我们就可以预处理一下这种情况可以变成那些情况,然后输出即可。

一共有 $181441$ 中状态。

时间复杂度:$\mathcal O (181441)$

代码

有点丑…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>

using namespace std;

int T, a[20], tt;
map<string, int> vis;

int main() {
cin.tie(0)->sync_with_stdio(0);
queue<pair<string, int>> q;
q.push({"123456789", 0});
while (!q.empty()) {
auto [x, step] = q.front();
q.pop();
if (vis[x] != 0)
continue;
tt++;
vis[x] = step;
string tmp;
tmp = x; swap(tmp[8], tmp[7]); swap(tmp[7], tmp[6]); q.push({tmp, step + 1});
tmp = x; swap(tmp[5], tmp[4]); swap(tmp[4], tmp[3]); q.push({tmp, step + 1});
tmp = x; swap(tmp[2], tmp[1]); swap(tmp[1], tmp[0]); q.push({tmp, step + 1});

tmp = x; swap(tmp[6], tmp[3]); swap(tmp[3], tmp[0]); q.push({tmp, step + 1});
tmp = x; swap(tmp[7], tmp[4]); swap(tmp[4], tmp[1]); q.push({tmp, step + 1});
tmp = x; swap(tmp[8], tmp[5]); swap(tmp[5], tmp[2]); q.push({tmp, step + 1});

tmp = x;
swap(tmp[0], tmp[2]);
swap(tmp[0], tmp[8]);
swap(tmp[0], tmp[6]);
swap(tmp[1], tmp[5]);
swap(tmp[1], tmp[7]);
swap(tmp[1], tmp[3]);
q.push({tmp, step + 1});
}
cout << tt << '\n';
cin >> T;
while (T--) {
char x;
string s, ans;
for (int i = 1; i <= 9; i++) {
cin >> x;
a[x - '0'] = i;
s += i + '0';
}
for (int i = 1; i <= 9; i++) {
cin >> x;
ans += a[x - '0'] + '0';
}
if (s == ans) cout << "0\n";
else if (vis[ans] == 0) cout << "-1\n";
else cout << vis[ans] << '\n';
}
return 0;
}