跳到正文

个人记录

2.3考试复盘

Daniel_Chou_hn约 9 分钟
原文
文章信息与版本

文章信息

收录
2026年2月4日
更新
2026年2月4日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

1.dfs01

迷宫上搜索思想VS普通搜索思想 迷宫上搜索:我得搞一个方向数组(看方向) 普通搜索:我是不是要把迷宫都跑一遍?(看过程)

所以这道题就用 迷宫上搜索 来解:

  • 你可以用一个桶数组来储存方向(更快捷)
  • 编不出来啊啊啊啊
AC
C++
#include<bits/stdc++.h>
using namespace std;
bool vis[20][20];
int goingto[2][2]={{0,1},{1,0}};
int a,b,ans;
bool cango[20][20];
void dfs(int x,int y){
	if(x==b and y==a){
		ans++;
		return;
	}
	for(auto pos:goingto){
		int nx=x+pos[1];
		int ny=y+pos[0];
		if(nx>b or nx<1 or ny>a or ny<1)continue;
		if(cango[y][x]==1)continue;
		if(vis[y][x]==1)continue;
		vis[y][x]=1;
		dfs(nx,ny);
		vis[y][x]=0;
	}
}
int main(){
	cin>>a>>b;
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int y,x;
		cin>>y>>x;
		vis[y][x]=1;
	}
	dfs(1,1);
	cout<<ans;
	return 0;
}

2.dfs02

根本不用bool vis[][],因为数字会承担一切

也不用判断是否抵达终点,因为四周都比他大就不会运行了

AC
C++
#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int high[60][60];
int n,m,mmax=INT_MIN;
void dfs(int x,int y,int cnt){
	mmax=max(mmax,cnt+1);
	for(int i=0;i<4;i++){
		int nx=x+dir[i][1],ny=y+dir[i][0];
		if(nx>m or nx<1 or ny>n or ny<1)continue;
//		cout<<ny<<' '<<nx<<'\t'<<high[ny][nx]<<'\n';
		if(high[ny][nx]<high[y][x])dfs(nx,ny,cnt+1);

	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>high[i][j];
		}
	}
	int sx,sy;
	cin>>sy>>sx;
	dfs(sx,sy,0);
	cout<<mmax;
	/*
	5 5
	1 2 3 4 5
	6 7 8 9 10
	11 12 13 14 15
	16 17 18 19 20
	21 22 23 24 25
	5 5
	*/
	return 0;
}

3.dfs03

8方的方向数组

AC
C++
#include<bits/stdc++.h>
using namespace std;
int horse[8][2]={{1,2},{-1,2},{2,-1},{2,1},{1,-2},{-1,-2},{-2,1},{-2,-1}};/*
   | | | |
  =|=| |=|=
      O

*/
int tox,toy,n,m,mmin=INT_MAX;
bool vis[10][10];
void dfs(int x,int y,int cnt){
	if(x==tox and y==toy){
		mmin=min(mmin,cnt);
		return;
	}
	for(auto dir:horse){
		int nx=x+dir[1];
		int ny=y+dir[0];
		if(nx>m or nx<1 or ny>n or ny<1)continue;
		if(vis[ny][nx]==1)continue;
//		cout<<ny<<' '<<nx<<'\n';
		vis[ny][nx]=1;
		dfs(nx,ny,cnt+1);
		vis[ny][nx]=0;
	}
}
int main(){
	int x,y;
	cin>>n>>m>>y>>x>>toy>>tox;
	dfs(x,y,0);
	cout<<mmin;
	return 0;
}

4.dfs04

这不就是方向数组改版吗

AC
C++
#include<bits/stdc++.h>
using namespace std;
struct Ways{
	int x,y;
};
vector<Ways>ways;
int n;
bool mp[30][30];
bool vis[30][30];
int dir[4][2]={{0,-1},{-1,0},{0,1},{1, 0}};
void dfs(int x,int y){
	if(x==n and y==n){
    	ways.push_back({x,y});
		for(auto pos:ways){
			cout<<"("<<pos.y<<","<<pos.x<<((pos.x==n and pos.y==n)?")":")->");
		}
		exit(0);
	}
	for(auto pos:dir){
		int nx=x+pos[1];
		int ny=y+pos[0];
		if(nx>n or ny>n or nx<1 or ny<1)continue;
		if(mp[ny][nx]==1)continue;
		if(vis[ny][nx])continue;
//		cout<<ny<<' '<<nx<<'\n';
        vis[y][x]=1;
    	ways.push_back({x,y});
		dfs(nx,ny);
    	ways.pop_back();
        vis[y][x]=0;
	}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			char a;
			cin>>a;
			mp[i][j]=a-'0';
//			cout<<mp[i][j];
		}
	}
	dfs(1,1);
	return 0;
}

5.dfs05

多了几种选项而已

AC
C++
#include<bits/stdc++.h>
using namespace std;
int pad[30][30];
bool vis[30][30];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int m,mmin=INT_MAX;
void dfs(int x,int y,int coins,bool canuse){
	if(coins>=mmin)return;
	if(x==m and y==m){
		mmin=min(mmin,coins);
		return;
	}
	vis[y][x]=1;
	for(auto pos:dir){
		int nx=x+pos[1];
		int ny=y+pos[0];
		if(nx>m or ny>m or nx<1 or ny<1)continue;
		if(vis[ny][nx]==1)continue;
		if(pad[ny][nx]==0){
			if(canuse){
				pad[ny][nx]=pad[y][x];
				dfs(nx,ny,coins+2,0);
				pad[ny][nx]=0;
				continue;
			}
			else{
                continue;
            }
		}
		if(pad[ny][nx]!=pad[y][x])dfs(nx,ny,coins+1,1);
		else dfs(nx,ny,coins,1);
	}
	vis[y][x]=0;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int n;
	cin>>m>>n;
	for(int i=1;i<=n;i++){
		int x,y,c;
		cin>>y>>x>>c;
		pad[y][x]=c+1;
		// pad[y][x]++;
	}
	dfs(1,1,0,1);
    if(mmin==INT_MAX){
        cout<<-1;
        return 0;
    }
	cout<<mmin;
	return 0;
}
原文