跳到正文

个人记录

7.25 课堂自我总结

lx01220122约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月26日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 6 分钟

版本

  1. v1当前

1.课堂笔记

二维DFS

迷宫路径模板(一共有多少走法);

C++
bool ck(int x,int y){
	return (条件);
}
void dfs(int x,int y){
	if(x==a&&y==b){//到了终点
		ans++;
		return ;
	}
	for(int i=0;i<2;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(ck(xx,yy)){
			v[xx][yy]=1;
			dfs(xx,yy);
			v[xx][yy]=0;//要回溯
		} 
	}
} 

迷宫路径模板(一条路径);

C++
bool ck(int x,int y){
	return (条件);
}
void dfs(int x,int y){
	if(x==a&&y==b){//到了终点
		//做出反应
		return ;
	}
	for(int i=0;i<2;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(ck(xx,yy)){
			v[xx][yy]=1;
			dfs(xx,yy);//不要回溯
		} 
	}
} 

连通块模板

C++
bool ck(int x,int y){
	return (条件);
}
void dfs(int x,int y){
	ans++;
	for(int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(ck(xx,yy)){
			v[xx][yy]=1;
			dfs(xx,yy);
		} 
	}
} 

2. 题目总结

P1958 上学路线

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e7+10;
int a,b,k;
bool v[25][25];
int ans;
int dx[2]={0,1};
int dy[2]={1,0};
bool ck(int x,int y){
	return (x>=1&&x<=a&&y>=1&&y<=b&&v[x][y]==0);//判断是否符合条件;
}
void dfs(int x,int y){
	if(x==a&&y==b){//到了终点
		ans++;//是一种走法;
		return ;
	}
	for(int i=0;i<2;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(ck(xx,yy)){
			v[xx][yy]=1;
			dfs(xx,yy);
			v[xx][yy]=0;//回溯
		} 
	}
} 
int main(){
	cin>>a>>b;
	cin>>k;
	for(int i=1;i<=k;i++){
		int c,d;
		cin>>c>>d;
		v[c][d]=1;//标记障碍
	}
	v[1][1]=1;
	dfs(1,1); 
	cout<<ans;
	return 0; 
} 

P1683 入门

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e7+10;
int n,m,k;
int sx,sy; 
char a[25][25];//注意类型(一不小心就没注意到o(╥﹏╥)o)
bool v[25][25];
int ans;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
bool ck(int x,int y){
	return (x>=1&&x<=n&&y>=1&&y<=m&&v[x][y]==0&&a[x][y]!='#');
}
void dfs(int x,int y){
	ans++;//记录连通块大小
	for(int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(ck(xx,yy)){
			v[xx][yy]=1;
			dfs(xx,yy);//不要回溯!!!
		} 
	}
} 
int main(){
	cin>>m>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
			if(a[i][j]=='@'){//找到起点
				sx=i;//记录
				sy=j;
			}
		}
	}
	v[sx][sy]=1;
	dfs(sx,sy); 
	cout<<ans;
	return 0; 
} 

U587530 迷宫的第一条出路

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
struct nd{
	int x,y;
};
const int N=1e7+10;
int n,k;
int sx,sy; 
char a[25][25];
bool v[25][25];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
vector<nd> ans;
void coutt(){//输出函数
	cout<<"(1,1)";
	for(int i=0;i<ans.size();i++){
		cout<<"->"<<"("<<ans[i].x<<","<<ans[i].y<<")";
	}
}
bool ck(int x,int y){
	return (x>=1&&x<=n&&y>=1&&y<=n&&v[x][y]==0&&a[x][y]!='1');
}
void dfs(int x,int y,int s){
	if(x==n&&y==n){
		coutt();//到了终点开始输出
		return ;
	}
	for(int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(ck(xx,yy)){
			v[xx][yy]=1;
			ans.push_back({xx,yy});
			dfs(xx,yy,s+1);//一条路不要回溯!!!
			ans.pop_back(); //还原动态数组!!!
		} 
	}
} 
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
	}
	v[1][1]=1;
	dfs(1,1,0); 
	return 0; 
} 
原文