上学路线
从起点开始用DFS寻找路径,遇到障碍或出界return,到达终点记录当前路径数量即可。
#include <bits/stdc++.h>
using namespace std;
int ans = 0, vis[16 + 9][16 + 9], a, b;
void DFS(int x, int y)
{
if (x < 1 || x > a || y < 1 || y > b)//判断出界问题
{
return;
}
if (vis[x][y] == true)//(x, y)有障碍
{
return;
}
if (x == a && y == b)//到达(a, b)终点
{
ans ++;//新答案
return;
}
DFS(x + 1, y);//向下走
DFS(x, y + 1);//向右走
}
int main()
{
int n;
cin >> a >> b >> n;
while (n --)
{
int x, y;
cin >> x >> y;
vis[x][y] = true;//记录(x, y)有障碍
}
DFS(1, 1);//起点开始
cout << ans;
return 0;
}【基础】迷宫的第一条出路
正常进行有障碍的搜索。用pre记录下已经走过的路线坐标,首次到达终点时考虑输出答案即可。
#include <bits/stdc++.h>
using namespace std;
int ans = 0, vis[16 + 9][16 + 9], a, mp[40][40];
struct node
{
int x, y;
}pre[16 + 9][16 + 9];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 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);//继续检查已存入的pre坐标信息
cout << "->(" << x << "," << y << ")";
}
void DFS(int x, int y)
{
if (x==a && y==a)//首次到达终点(a, a)
{
print(x, y);
return;
}
for (int i = 0; i < 4; i ++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= a && ny >= 1 && ny <= a && vis[nx][ny] == false && mp[nx][ny]==0)//可以继续走到(nx, ny)的位置
{
vis[nx][ny] = 1;//标记已走
pre[nx][ny] = {x, y};//记录坐标
DFS(nx, ny);
}
}
return;
}
int main()
{
cin >> a;
for (int i = 1; i <= a; i ++)
{
for (int j = 1; j <= a; j ++)
{
char c;
cin >> c;
if (c == '1')
{
mp[i][j] = 1;
}
else
{
mp[i][j] = 0;
}
}
}
vis[1][1] = 1;//标记起点
DFS(1, 1);
return 0;
}【基础】卒的遍历
同【基础】迷宫的第一条出路的思路,只要找到终点时不停止搜索,回溯即可。
#include <bits/stdc++.h>
using namespace std;
int ans = 0, vis[16 + 9][16 + 9], a, b, sum = 0;
struct node
{
int x, y;
}pre[16 + 9][16 + 9];
int dx[] = {1, 0};//只有向下走和向右走两个方向
int dy[] = {0, 1};
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;
}
void DFS(int x, int y)
{
if (x == a && y == b)
{
sum ++;//统计方案数量
cout << sum << ":";
print(x, y);//照常输出
cout << "\n";
return;
}
for (int i = 0; i < 2; i ++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= a && ny >= 1 && ny <= b && vis[nx][ny] == false)
{
vis[nx][ny] = true;
pre[nx][ny] = {x, y};
DFS(nx, ny);
vis[nx][ny] = false;
}
}
return;
}
int main()
{
cin >> a >> b;
vis[1][1] = true;
DFS(1, 1);
return 0;
}走迷宫
和【基础】卒的遍历同理,只是需要判断障碍和修改起点,终点的位置。
#include <bits/stdc++.h>
using namespace std;
int ans = 0, vis[16 + 9][16 + 9], a, b, v1, v2, v3, v4, l = false;
struct node
{
int x, y;
}pre[16 + 9][16 + 9];
int dx[] = {0, -1, 0, 1};//上下左右都能走
int dy[] = {-1, 0, 1, 0};
void print(int x, int y)
{
if (x == v1 && y == v2)
{
cout << "(" << v1 << "," << v2 << ")";//第一个输出的是起点,不一定是(1, 1)
return;
}
print(pre[x][y].x, pre[x][y].y);
cout << "->(" << x << "," << y << ")";
}
void DFS(int x, int y)
{
if (x == v3 && y == v4)//判断是否到达终点
{
l = true;
print(x, y);
cout << "\n";
return;
}
for (int i = 0; i < 4; i ++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= a && ny >= 1 && ny <= b && vis[nx][ny] == false)
{
vis[nx][ny] = true;
pre[nx][ny] = {x, y};
DFS(nx, ny);
vis[nx][ny] = false;
}
}
return;
}
int main()
{
cin >> a >> b;
for (int i = 1; i <= a; i ++)
{
for (int j = 1; j <= b; j ++)
{
char c;
cin >> c;
if (c == '1')//反过来了
{
vis[i][j] = false;
}
else
{
vis[i][j] = true;
}
}
}
cin >> v1 >> v2 >> v3 >> v4;
vis[v1][v2] = true;
DFS(v1, v2);
if (! l)//没有通向终点的路径
{
cout << -1;
}
return 0;
}P1644 跳马问题
使用dp数组来路=末路的原理,计算马的走法。
#include <bits/stdc++.h>
#define int long long
using namespace std;
int dp[18 + 9][18 + 9];
signed main()
{
int n, m;
cin >> n >> m;
for (int j = 0; j <= m; j ++)
{
for (int i = 0; i <= n; i ++)
{
if (i == 0 && j == 0)
{
dp[0][0] = 1;//起点走过了
continue;
}
if (i - 2 >= 0 && j - 1 >= 0)//判断马是否能走
{
dp[i][j] += dp[i - 2][j - 1];//加上前面的路
}
if (i + 2 <= n && j - 1 >= 0)//判断马是否能走
{
dp[i][j] += dp[i + 2][j - 1];//加上前面的路
}
if (i + 1 <= n && j - 2 >= 0)//判断马是否能走
{
dp[i][j] += dp[i + 1][j - 2];//加上前面的路
}
if (i - 1 >= 0 && j - 2 >= 0)//判断马是否能走
{
dp[i][j] += dp[i - 1][j - 2];//加上前面的路
}
}
}
cout << dp[n][m];
return 0;
}P1101 单词方阵
枚举每个点,判断8个延申方向是否有yizhong即可。
#include <bits/stdc++.h>
using namespace std;
char a[100 + 9][100 + 9];
char t[100 + 9][100 + 9];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
cin >> a[i][j];
t[i][j] = '*';//一开始没有yizhong
}
}
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{//枚举每个点,判断yizhong
if (a[i][j] == 'y' && a[i][j + 1] == 'i' && a[i][j + 2] == 'z' && a[i][j + 3] == 'h' && a[i][j + 4] == 'o' && a[i][j + 5] == 'n' && a[i][j + 6] == 'g')
{
t[i][j] = 'y';
t[i][j + 1] = 'i';
t[i][j + 2] = 'z';
t[i][j + 3] = 'h';
t[i][j + 4] = 'o';
t[i][j + 5] = 'n';
t[i][j + 6] = 'g';//记录答案
}
if (a[i][j] == 'y' && a[i][j - 1] == 'i' && a[i][j - 2] == 'z' && a[i][j - 3] == 'h' && a[i][j - 4] == 'o' && a[i][j - 5] == 'n' && a[i][j - 6] == 'g')
{
t[i][j] = 'y';
t[i][j - 1] = 'i';
t[i][j - 2] = 'z';
t[i][j - 3] = 'h';
t[i][j - 4] = 'o';
t[i][j - 5] = 'n';
t[i][j - 6] = 'g';//记录答案
}
if (a[i][j] == 'y' && a[i + 1][j] == 'i' && a[i + 2][j] == 'z' && a[i + 3][j] == 'h' && a[i + 4][j] == 'o' && a[i + 5][j] == 'n' && a[i + 6][j] == 'g')
{
t[i][j] = 'y';
t[i + 1][j] = 'i';
t[i + 2][j] = 'z';
t[i + 3][j] = 'h';
t[i + 4][j] = 'o';
t[i + 5][j] = 'n';
t[i + 6][j] = 'g';//记录答案
}
if (a[i][j] == 'y' && a[i - 1][j] == 'i' && a[i - 2][j] == 'z' && a[i - 3][j] == 'h' && a[i - 4][j] == 'o' && a[i - 5][j] == 'n' && a[i - 6][j] == 'g')
{
t[i][j] = 'y';
t[i - 1][j] = 'i';
t[i - 2][j] = 'z';
t[i - 3][j] = 'h';
t[i - 4][j] = 'o';
t[i - 5][j] = 'n';
t[i - 6][j] = 'g';//记录答案
}
if (a[i][j] == 'y' && a[i + 1][j + 1] == 'i' && a[i + 2][j + 2] == 'z' && a[i + 3][j + 3] == 'h' && a[i + 4][j + 4] == 'o' && a[i + 5][j + 5] == 'n' && a[i + 6][j + 6] == 'g')
{
t[i][j] = 'y';
t[i + 1][j + 1] = 'i';
t[i + 2][j + 2] = 'z';
t[i + 3][j + 3] = 'h';
t[i + 4][j + 4] = 'o';
t[i + 5][j + 5] = 'n';
t[i + 6][j + 6] = 'g';//记录答案
}
if (a[i][j] == 'y' && a[i - 1][j + 1] == 'i' && a[i - 2][j + 2] == 'z' && a[i - 3][j + 3] == 'h' && a[i - 4][j + 4] == 'o' && a[i - 5][j + 5] == 'n' && a[i - 6][j + 6] == 'g')
{
t[i][j] = 'y';
t[i - 1][j + 1] = 'i';
t[i - 2][j + 2] = 'z';
t[i - 3][j + 3] = 'h';
t[i - 4][j + 4] = 'o';
t[i - 5][j + 5] = 'n';
t[i - 6][j + 6] = 'g';//记录答案
}
if (a[i][j] == 'y' && a[i + 1][j - 1] == 'i' && a[i + 2][j - 2] == 'z' && a[i + 3][j - 3] == 'h' && a[i + 4][j - 4] == 'o' && a[i + 5][j - 5] == 'n' && a[i + 6][j - 6] == 'g')
{
t[i][j] = 'y';
t[i + 1][j - 1] = 'i';
t[i + 2][j - 2] = 'z';
t[i + 3][j - 3] = 'h';
t[i + 4][j - 4] = 'o';
t[i + 5][j - 5] = 'n';
t[i + 6][j - 6] = 'g';//记录答案
}
if (a[i][j] == 'y' && a[i - 1][j - 1] == 'i' && a[i - 2][j - 2] == 'z' && a[i - 3][j - 3] == 'h' && a[i - 4][j - 4] == 'o' && a[i - 5][j - 5] == 'n' && a[i - 6][j - 6] == 'g')
{
t[i][j] = 'y';
t[i - 1][j - 1] = 'i';
t[i - 2][j - 2] = 'z';
t[i - 3][j - 3] = 'h';
t[i - 4][j - 4] = 'o';
t[i - 5][j - 5] = 'n';
t[i - 6][j - 6] = 'g';//记录答案
}
}
}
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
cout << t[i][j];//输出答案
}
cout << endl;
}
return 0;
}P12266 [蓝桥杯 2024 国 Python B] 不同的总分值
分数都是5的倍数,那0 ~ 150有31种5的倍数的情况,而所有5倍数的分数都有,一定组的出来。所以有31种。
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << 31;
return 0;
}