1739. 迷宫的所有路径-深度优先搜索-DFS

代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int n;
int fx[4]={0,1,0,-1};
int fy[4]={1,0,-1,0};
bool vis[100][100];
int q[35][3];
int c;
void print(int k){
	c++;
	cout<<c<<":";
	for(int i=1;i<=k;i++){
		cout<<q[i][1]<<","<<q[i][2];
		if(i!=k){
			cout<<"->";
		}
		
	}
	cout<<endl;
}
void dfs(int x,int y,int k){
	q[k][1]=x;
	q[k][2]=y;
	if(x==n&&y==n){
		print(k);
		
	}
	else{
		int tx,ty;
		for(int i=0;i<4;i++){
			tx=x+fx[i];
			ty=y+fy[i];
			
			if(tx>=1&&tx<=n&&ty>=1&&ty<=n&&vis[tx][ty]==false){
				vis[tx][ty]=true;
				dfs(tx,ty,k+1);	
				vis[tx][ty]=false; 
			}
				
				
		}
		
	}
}
int main(){
	cin>>n;
	vis[1][1]=true;
	
	dfs(1,1,1);
	return 0;
}
相关推荐
freexyn1 天前
Matlab自学笔记七十六:表达式的展开、因式分解、化简、合并同类项
笔记·算法·matlab
样例过了就是过了1 天前
LeetCode热题 不同路径
c++·算法·leetcode·动态规划
dog2501 天前
圆锥曲线和二次曲线
开发语言·网络·人工智能·算法·php
Wadli1 天前
27.单调队列
算法
Navigator_Z1 天前
LeetCode //C - 1031. Maximum Sum of Two Non-Overlapping Subarrays
c语言·算法·leetcode
Wect1 天前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·typescript
爱学习的张大1 天前
具身智能论文问答(三):Open VLA
人工智能·算法
wearegogog1231 天前
基于Q-learning的栅格地图路径规划MATLAB仿真程序
开发语言·算法·matlab
旖-旎1 天前
深搜练习(组合总和)(7)
c++·算法·深度优先·力扣
小O的算法实验室1 天前
2026年ASOC,基于人工势场的差分进化算法改进框架,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进