跳到正文

个人记录

2.3-407-迷宫上搜索

David_Lu约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年2月3日
收录
2026年2月4日
更新
2026年11月16日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

T729137 dfs01

题意

有一个a∗ba*b大小的矩阵,有nn个地方不能走,只能往右,下走,请问从(1,1)(1,1)到(a,b)(a,b)有几种走法

思路

用基本的迷宫上搜索即可,nn个不能走的地方只需提前标记即可

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 25;
int n, m;
int a[N][N];
int v[N][N];
int d[2][2] = { {0, 1}, {1, 0}};
int res;
void dfs(int x, int y){
	if(x == n && y == m){
		res++;
		return ;
	}
	for(int i = 0;i < 2;i++){
		int nx = x + d[i][0];
		int ny = y + d[i][1];
		if(nx > n || ny > m || nx < 1 || ny < 1){
			continue;
		}
		if(v[nx][ny] == 1){
			continue;
		}
		dfs(nx, ny);
	}
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n>> m;
	int q;
	cin >> q;
	while(q--){
		int x, y;
		cin >> x>> y;
		v[x][y] = 1;
	}
	dfs(1, 1);
	cout << res;
	return 0;
}

赛时分析

得分

100100分

用时

1010分钟

T729139 dfs02

题意

有一个N∗MN*M的地块,每个地块都有一个高度值和一个宝藏,从(sx,sy)(sx,sy)降落,每次只能到更小的格子

思路

用迷宫上的搜索即可,注意只能走更小的格子,同时降落的地点也有宝箱

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 55;
int n, m;
int a[N][N];
int v[N][N];
int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int maxn = 0;
int cnt;
void dfs(int x, int y, int sum){
	maxn = max(maxn, sum);
	v[x][y] = 1;
	for(int i = 0;i < 4;i++){
		int nx = x + d[i][0];
		int ny = y + d[i][1];
		if(nx > n || ny > m || nx < 1 || ny < 1){
			continue;
		}
		if(a[nx][ny] >= a[x][y]){
			continue;
		}
		if(v[nx][ny] == 1){
			dfs(nx, ny, sum);
		}
		dfs(nx, ny, sum + 1);
		v[nx][ny] = 1;
	}
	v[x][y] = 0;
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n>> m;
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			cin >> a[i][j];
		}
	}
	int sx, sy;
	cin >> sx>> sy;
	dfs(sx, sy, 1);
	cout << maxn;
	return 0;
}

赛时分析

得分

9090分

用时

1515分钟

错因

写成同样的格子也能走了

T729140 dfs03

题意

有个n∗mn*m大小的棋盘,有一匹马在(x,y)(x,y),求马最少走多少步能到(s,t)(s,t)

思路

用迷宫上的搜索依次枚举即可,注意马有88个方向能走

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
int n, m, x, y, s, t;
int a[N][N];
int d[8][2] = { {-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}};
int v[N][N];
int minn = INT_MAX;
void dfs(int x, int y, int cnt){
	if(x == s && y == t){
		minn = min(minn, cnt);
		return ;
	}
	v[x][y] = 1;
	for(int i = 0;i < 8;i++){
		int nx = x + d[i][0];
		int ny = y + d[i][1];
		if(nx > n || ny > m || nx < 1 || ny < 1){
			continue;
		}
		if(v[nx][ny] == 1){
			continue;
		}
		dfs(nx, ny, cnt + 1);
	}
	v[x][y] = 0;
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n>> m>> x>> y>> s>> t;
	dfs(x, y, 0);
	cout << minn;
	return 0;
}

赛时分析

得分

100100分

用时

3535分钟

T729141 dfs04

题意

有一个迷宫,可以向上下左右各走一格,有的地方能走,有的不能走,输出第一条从迷宫左上角走到迷宫右上角的路线图,搜索顺序为左、上、右、下

思路

有迷宫上的搜索找出第一条路输出即可,注意最后一步到(n,m)(n,m)无法写入数组,要单独输出,且输出完后要结束程序

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 55;
int n, m;
char a[N][N];
int v[N][N];
int d[4][2] = { {0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int maxn = 0;
int h;
vector <int> xx, yy;
int mp[N][N];
void dfs(int x, int y){
	if(x == n && y == n){
		for(int i = 0;i < xx.size() ;i++){
			cout << '('<< xx[i]<< ','<< yy[i]<< ")->";
		}
		cout << '('<< n<< ','<< n<< ')';
		exit(0);
	}
	v[x][y] = 1;
	xx.push_back(x);
	yy.push_back(y);
	for(int i = 0;i < 4;i++){
		int nx = x + d[i][0];
		int ny = y + d[i][1];
		if(nx > n || ny > n || nx < 1 || ny < 1){
			continue;
		}
		if(v[nx][ny] == 1 || mp[nx][ny] == 1){
			continue;
		}
		dfs(nx, ny);
	}
	xx.pop_back();
	yy.pop_back();
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++){
			cin >> a[i][j];
			if(a[i][j] == '1'){
				mp[i][j] = 1;
			}
		}
	}
	dfs(1, 1);
	return 0;
}

赛时分析

得分

1414分

用时

55分钟

错因

最后没有时间写了,只能骗分

T729141 dfs04

题意

思路

用迷宫上的搜索枚举所有路线,如果是不同颜色,金币加1,如果无色且上次没用魔法,改成红色,金币加2,且记录此次用了魔法

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 35;
int m, n;
int x, y, c;
int mp[N][N];
int v[N][N];
int minn = 1e9;
int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void dfs(int x, int y, int sum, int use){
	if(sum >= minn){
		return ;
	}
	if(x == m && y == m){
		minn = sum;
		return ;
	}
	v[x][y] = 1;
	for(int i = 0;i < 4;i++){
		int nx = x + d[i][0];
		int ny = y + d[i][1];
		if(nx > m || ny > m || nx < 1 || ny < 1){
			continue;
		}
		if(v[nx][ny] == 1){
			continue;
		}
		if(mp[nx][ny] == 0){
			if(use == 0){
				mp[nx][ny] = mp[x][y];
				dfs(nx, ny, sum + 2, 1);
				mp[nx][ny] = 0;
			}
			continue;
		}
		if(mp[x][y] != mp[nx][ny]){
			dfs(nx, ny, sum + 1, 0);
		}else{
			dfs(nx, ny, sum, 0);
		}
	}
	v[x][y] = 0;
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> m>> n;
	for(int i = 1;i <= n;i++){
		cin >> x>> y>> c;
		mp[x][y] = c + 1;
	}
	dfs(1, 1, 0, 0);
	if(minn == 1e9){
		cout << -1;
		return 0;
	}
	cout << minn;
	return 0;
}

赛时分析

得分

55分

用时

77分钟

错因

最后没有时间写了,只能骗分

原文