P1958 上学路线
解题思路
题目中的交叉路口,我们可以用数组标记,并在搜索时写成条件
参考代码
#include <iostream>
using namespace std;
int a, b, n, x, y, dx[2] = {0, 1}, dy[2] = {1, 0}, cnt;
bool f[20][20], ff[20][20];
void dfs(int x, int y)
{
if (x == a && y == b) {cnt ++; return;}
for (int i = 0; i < 2; i ++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || nx > a || ny < 1 || ny > b || f[nx][ny] || ff[nx][ny]) continue;
f[nx][ny] = 1;
dfs(nx, ny);
f[nx][ny] = 0;
}
}
int main()
{
cin >> a >> b;
cin >> n;
for (int i = 1; i <= n; i ++) cin >> x >> y, ff[x][y] = 1;
if (ff[1][1]) {cout << 0; return 0;}
f[1][1] = 1;
dfs(1, 1);
cout << cnt;
return 0;
}D3432 【基础】迷宫的第一条出路
解题思路
我们要想记录路线, 就可以先用 (前置数组)记录上一个点,最后递归遍历,退出结果
:输出格式记得要按题目
参考代码
#include <iostream>
#include <string>
using namespace std;
struct node
{
int x, y;
} pre[25][25];
int n, dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
char a[25][25];
bool f[25][25], ff;
void dfs(int x, int y)
{
if (ff) return;
if (x == n && y == n) {ff = 1; return;}
for (int i = 0; i < 4; i ++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > n || f[nx][ny] || a[nx][ny] == '1') continue;
f[nx][ny] = 1;
pre[nx][ny].x = x, pre[nx][ny].y = y;
dfs(nx, ny);
if (ff) return;
f[nx][ny] = 0;
}
}
void print(int x, int y)
{
if (x == 1 && y == 1) {cout << "(1,1)"; return;}
print(pre[x][y].x, pre[x][y].y);
cout << "->(" << x << "," << y << ")";
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++) cin >> a[i][j];
}
f[1][1] = 1;
dfs(1, 1);
print(n, n);
return 0;
}D3361【基础】卒的遍历
解题思路
因为我们需要寻找不同方案往下走的步骤,所以我们会直接在 里面输出,先用数组记录步骤的位置,相当于直接把步骤在遍历的途中记录,如果用上一道题目的方法,很明显,会复杂很多
参考代码
#include <iostream>
#include <string>
using namespace std;
int n, m, dx[2] = {1, 0}, dy[2] = {0, 1}, cnt, pre[20][5];
bool f[10][10];
void dfs(int x, int y, int step)
{
pre[step][0] = x;
pre[step][1] = y;
if (x == n && y == m)
{
cnt ++;
cout << cnt << ":";
for (int i = 0; i <= step; i ++)
{
cout << pre[i][0] << "," << pre[i][1];
if (i < step) cout << "->";
}
cout << '\n';
return;
}
for (int i = 0; i < 2; i ++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx > n || ny > m || f[nx][ny]) continue;
f[nx][ny] = 1;
dfs(nx, ny, step + 1);
f[nx][ny] = 0;
}
}
int main()
{
cin >> n >> m;
f[1][1] = 1;
dfs(1, 1, 0);
return 0;
}P1238 走迷宫
解题思路
跟上道题一模一样,只是要套上题目给的地图(迷宫)
参考代码
#include <iostream>
#include <string>
using namespace std;
int n, m, dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0}, pre[225][5], sx, sy, tx, ty;
int a[20][20];
bool f[20][20], ff;
void dfs(int x, int y, int step)
{
pre[step][0] = x;
pre[step][1] = y;
if (x == tx && y == ty)
{
ff = 1;
for (int i = 0; i <= step; i ++)
{
cout << '(' << pre[i][0] << "," << pre[i][1] << ')';
if (i < step) cout << "->";
}
cout << '\n';
return;
}
for (int i = 0; i < 4; i ++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || ny < 1 || nx > m || ny > n || f[nx][ny] || !a[nx][ny]) continue;
f[nx][ny] = 1;
dfs(nx, ny, step + 1);
f[nx][ny] = 0;
}
}
int main()
{
cin >> m >> n;
for (int i = 1; i <= m; i ++)
{
for (int j = 1; j <= n; j ++) cin >> a[i][j];
}
cin >> sx >> sy >> tx >> ty;
f[sx][sy] = 1;
dfs(sx, sy, 0);
if (!ff) cout << -1;
return 0;
}P1644 跳马问题
解题思路
简简单单,然后记得题目只让我们往右跳,所以方向数组要修改,只有四个才行(方向数组)
参考代码
#include <iostream>
using namespace std;
int n, m, dx[4] = {1, 1, 2, 2}, dy[4] = {-2, 2, -1, 1}, cnt;
void dfs(int x, int y)
{
if (x == m && y == n) {cnt ++; return;}
for (int i = 0; i < 4; i ++)
{
int nx = x + dx[i], ny = y + dy[i];
if (ny < 0 || nx > m || ny > n) continue;
dfs(nx, ny);
}
}
int main()
{
cin >> n >> m;
dfs(0, 0);
cout << cnt;
return 0;
}P1101 单词方阵
解题思路
写代码里了
参考代码(带注释)
#include <iostream>
#include <string>
using namespace std;
const int N = 105;
string s = "yizhong";
int n, dx[8] = {0, -1, 1, 0, -1, 1, -1, 1}, dy[8] = {-1, -1, -1, 1, 1, 1, 0, 0};
char a[N][N], ans[N][N];
bool f;
void dfs(int x, int y)
{
for (int i = 0; i < 8; i ++)
{
f = 1;
for (int j = 1; j <= 6; j ++) //比较 "yizhong"
{
int nx = x + j * dx[i], ny = y + j * dy[i]; //收集单词,以便以后比较
if (nx < 1 || nx > n || ny < 1 || ny > n)
{
f = 0; //如果这就不行了,那么以后也不行
break;
}
if (s[j] != a[nx][ny]) //当前为是否符合
{
f = 0; //同上次f的解释
break;
}
}
if (f == 0) continue; //如果经过筛后不行,就跳过
for (int j = 0; j <= 6; j ++) //将符合的记录
{
int nx = x + j * dx[i], ny = y + j * dy[i];
ans[nx][ny] = a[nx][ny];
}
}
return;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++) cin >> a[i][j];
}
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++) if (a[i][j] == 'y') dfs(i, j); //如果在(i, j)碰到y,那么就看看附近有没有一整个yizhong(搜索)
}
for (int i = 1; i <= n; i ++) //按照题目输出
{
for (int j = 1; j <= n; j ++)
{
if (ans[i][j] == 0) cout << '*';
else cout << ans[i][j];
}
cout << '\n';
}
return 0;
}P12266[蓝桥杯 2024 国 Python B]不同的总分值
解题思路
用枚举,符合就记录,有两种情况,就用枚举就行了
参考代码
#include <iostream>
using namespace std;
int a[15] = {0, 5, 5, 10, 10, 15, 15, 20, 20, 25, 25}, cnt;
bool x[200];
void dfs(int v, int s)
{
if (v > 10) {x[s] = 1; return;}
dfs(v + 1, s);
dfs(v + 1, s + a[v]);
}
int main()
{
dfs(1, 0);
for (int i = 0; i <= 150; i ++) cnt += x[i];
cout << cnt;
return 0;
}