跳到正文

个人记录

7.5-401-深度优先搜索-路径

AKCoder约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月28日
篇幅
约 15 分钟

版本

  1. v1当前

T1 P1958 上学路线

C++
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const int MAXH = 50;
const int mod = 1e9 + 7;
int a,b;
int n;
bool s[MAXH][MAXH],vis[MAXH][MAXH];//vis标记数组
const int dx[] = {0,1},dy[] = {1,0};//两个变化向量
ll ans = 0;
inline void dfs(int x,int y){
	if(x == a && y == b){
		ans++;
		return ;
	}//dfs时到达了终点,统计答案
	for(int i = 0;i < 2;i++){
		int xx = x + dx[i];
		int yy = y + dy[i];
		if(xx < 1 || xx > a || yy < 1 || yy > b)//判断边界
			continue;
		if(!vis[xx][yy] && !s[xx][yy]){//判断(xx,yy)是否访问过,没有,则访问之
			vis[xx][yy] = 1;
			dfs(xx,yy);
			vis[xx][yy] = 0;//回溯
		}
	}
}
signed main()
{
	scanf("%d%d",&a,&b);
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		s[x][y] = 1;//代表有障碍物
	}
	dfs(1,1);
	printf("%lld\n",ans);
	return 0;
}

T2 D3432 【基础】迷宫的第一条出路

C++
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const int MAXH = 30;
const int mod = 1e9 + 7;

int n;
char s[MAXH][MAXH];
const int dx[] = {0,-1,0,1},dy[] = {-1,0,1,0};
vector<pair<int,int>>p;
inline void print(){
	printf("(%d,%d)",1,1);//第一个数可以特判一下,因为没有箭头"->"
	for(int i = 1;i < p.size();i++){
		printf("->");
		printf("(%d,%d)",p[i].first,p[i].second);
	}
}
bool vis[MAXH][MAXH];
inline void dfs(int x,int y){
	if(x == n && y == n){
		print();
		exit(0);//退出程序
	}
	for(int i = 0;i < 4;i++){
		int xx = x + dx[i];
		int yy = y + dy[i];
		if(xx < 1 || xx > n || yy < 1 || yy > n)//判断边界
			continue;
		if(!vis[xx][yy] && s[xx][yy] == '0'){
			vis[xx][yy] = 1;
			p.push_back(make_pair(xx,yy));
			dfs(xx,yy);
			vis[xx][yy] = 0;
			p.pop_back();//回溯
		}
	}
}
signed main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		scanf("%s",s[i]+1);
	}
	p.push_back(make_pair(1,1));
	vis[1][1] = 1;//易错点:开始前一定要把vis[sx][sy] = 1;
	dfs(1,1);
	return 0;
}
//重要的事情说三遍!初始化!初始化!初始化!

T3 D3361 【基础】卒的遍历

C++
#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int MAXN = 10;

int n, m;
int cnt = 0;

// pre[x][y] 存储 (x,y) 的前一个点
pair<int, int> pre[MAXN][MAXN];

// vis[x][y] 标记 (x,y) 是否已经走过
bool vis[MAXN][MAXN];

// 只能向右(dx=0, dy=1)或向下(dx=1, dy=0)走
const int dx[] = {1, 0}, dy[] = {0, 1};

// 打印路径:从起点 (1,1) 到 (x,y) 的路径
void print(int x, int y) {
    if (x == 1 && y == 1) {  // 递归终止条件:回到起点
        cout << "1,1";  // 打印起点
        return;
    }
    // 先递归打印前一个点
    print(pre[x][y].first, pre[x][y].second);
    // 再打印当前点
    cout << "->" << x << "," << y;
}

inline void dfs(int x, int y) {
    // 如果到达终点 (n,m)
    if (x == n && y == m) {
        cnt++;  // 路径数 +1
        cout << cnt << ":";  // 输出当前是第几条路径
        print(n, m);
        cout << endl;
        return;
    }

    // 尝试向右或向下走
    for (int i = 0; i < 2; i++) {
        int xx = x + dx[i];  // 计算下一步的坐标
        int yy = y + dy[i];

        // 如果越界
        if (xx > n || xx < 1 || yy > m || yy < 1)
            continue;

        // 如果这个点还没走过
        if (!vis[xx][yy]) {
            vis[xx][yy] = 1;  // 标记为已访问
            pre[xx][yy] = {x, y};  // 记录前一个点
            dfs(xx, yy);  // 继续搜索
            vis[xx][yy] = 0;  // 回溯,取消标记
            pre[xx][yy] = {0, 0};记录
        }
    }
}

int main() {
    cin >> n >> m;  // 输入行数 n 和列数 m
    dfs(1, 1);  // 从起点 (1,1) 开始搜索
    return 0;
}

T4 P1238 走迷宫

C++
#include <bits/stdc++.h>
using namespace std;

#define ll long long;
const int MAXN = 19;

int n, m;
int nx, ny;     // 终点坐标 (nx, ny)
int sx, sy;     // 起点坐标 (sx, sy)
int cnt = 0;    // 记录路径总数

int a[MAXN][MAXN];  // 存储地图,1表示可走,0表示不可走

// pre[x][y] 存储 (x,y) 的前一个点,用于回溯路径
pair<int, int> pre[MAXN][MAXN];

// vis[x][y] 标记 (x,y) 是否已经走过
bool vis[MAXN][MAXN];

// 方向数组:左、上、右、下(注意:顺序!)
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};

// 打印路径:从起点 (sx,sy) 到 (x,y) 的路径
void print(int x, int y) {
    if (x == sx && y == sy) {  // 递归终止条件:回到起点
        cout << "(" << x << "," << y << ")";  // 打印起点
        return;
    }
    // 先递归打印前一个点
    print(pre[x][y].first, pre[x][y].second);
    // 再打印当前点
    cout << "->(" << x << "," << y << ")";
}

inline void dfs(int x, int y) {
    // 如果到达终点 (nx, ny)
    if (x == nx && y == ny) {
        cnt++;  // 路径数 +1
        print(nx, ny);
        cout << endl;
        return;
    }

    // 尝试四个方向(左、上、右、下)
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];

        // 如果越界
        if (xx > m || xx < 1 || yy > n || yy < 1)
            continue;

        // 如果这个点可以走且没走过
        if (!vis[xx][yy] && a[xx][yy] == 1) {
            vis[xx][yy] = 1;  // 标记为已访问
            pre[xx][yy] = {x, y};  // 记录前一个点
            dfs(xx, yy);  // 继续搜索
            vis[xx][yy] = 0;  // 回溯
        }
    }
}

int main() {
    cin >> m >> n;  // 输入行数 m 和列数 n

    // 读取地图,1表示可走,0表示不可走
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++)
            cin >> a[i][j];

    cin >> sx >> sy;  // 输入起点坐标
    cin >> nx >> ny;  // 输入终点坐标

    vis[sx][sy] = 1;  // 标记起点为已访问
    dfs(sx, sy);      // 从起点开始搜索

    // 如果没有找到路径,输出-1
    if (cnt == 0)
        cout << -1 << endl;

    return 0;
}

作业

T1 跳马问题

C++
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const int MAXH = 1e3 + 7;
const int mod = 1e9 + 7;
int n,m;
ll total;
const int dy[] = {1,2,2,1},dx[]={2,1,-1,-2};
bool vis[MAXH][MAXH];
inline void dfs(int x,int y){
	if(x == n && y == m){
		total++;
		return;
	}
	for(int i = 0;i < 4;i++){
		int xx = x + dx[i];
		int yy = y + dy[i];
		if(xx < 0 || xx > n || yy < 0 || yy > m) //以(0,0)为起点
			continue;
		if(!vis[xx][yy]){
			vis[xx][yy] = 1;
			dfs(xx,yy);
			vis[xx][yy] = 0;
		}
	}
}
signed main()
{
	scanf("%d%d",&n,&m);
	dfs(0,0);
	printf("%lld\n",total);
	return 0;
}

T2 单词方阵

C++
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const int MAXH = 1e2 + 7;
const int mod = 1e9 + 7;
string s[MAXH],ans[MAXH];
const int dx[]={0,-1,-1,-1,0,1,1,1},dy[]={1,1,0,-1,-1,-1,0,1};
int n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin>>s[i];
    for (int i = 1; i <= n; i++)
	    for (int j = 1; j <= n; j++)
	    	ans[i] += '*';
    for (int i = 1; i <= n; i++)
    	for (int j = 0; j <= n - 1; j++)
    		if (s[i][j] == 'y')
		    {
		        for (int k=0; k<=7; k++)
		        {
		            int t1=i,t2=j;
		            bool bb=1;
		            for (int kk = 1; kk<=6; kk++)
		            {
		                t1 += dx[k]; t2 += dy[k];
		                if (t1<1||t1>n||t2<0||t2>n-1) bb=0;
		                if (bb == 0) break;
		                if (kk == 1 && s[t1][t2]!='i') bb=0;
		                if (kk == 2 && s[t1][t2]!='z') bb=0;
		                if (kk == 3 && s[t1][t2]!='h') bb=0;
		                if (kk == 4 && s[t1][t2]!='o') bb=0;
		                if (kk == 5 && s[t1][t2]!='n') bb=0;
		                if (kk == 6 && s[t1][t2]!='g') bb=0;
		            }
		            t1 = i; t2 = j;
		            if (bb) for (int kk = 0; kk <= 6; kk++) ans[t1][t2] = s[t1][t2],t1 += dx[k],t2 += dy[k];
		        }
		    }

    for (int i = 1; i <= n; i++)
    	cout << ans[i] << endl;
    return 0;
}

T3 [蓝桥杯 2024 国 Python B] 不同的总分值

C++
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const int MAXH = 1e5 + 7;
const int mod = 1e9 + 7;
set<int>st;
int n = 10;
const int v[] = {5,5,10,10,15,15,20,20,25,25};
inline void dfs(int x,int sum){
	if(x == n){
		st.insert(sum);
		return;
	}
	dfs(x+1,sum);
	dfs(x+1,sum+v[x]);
}
signed main()
{
	dfs(0,n);
	cout << st.size();
	return 0;
}
原文