1238 走迷宫

news/2024/7/20 22:02:10 标签: 深度优先, 算法

1238 走迷宫

em
做完这个题去提交作业
这个题应该又是一个图论
我最烦这样的路线输出题了,我服了
不过这个题就是一个搜索,简单的不能再简单的搜索,就是输出有点难
我试试

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;//搜索的顺序 左上右下 
int n,m;
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};
int flag;
int startx,starty,endx,endy;
int book[200][200];//标记数组 
struct st
{
	int x;
	int y;//记录横纵坐标 
}b[200];
void print(int n)
{
	flag=1;//有路
	for(int i=1;i<n;i++)
	{
		printf("(%d,%d)->",b[i].x,b[i].y);
	 } 
	 printf("(%d,%d)\n",b[n].x,b[n].y);
}
void dfs(int n,int startx,int starty,int endx,int endy)
{
	for(int i=0;i<=3;i++)//方位
	{
		book[startx][starty]=0;
		startx+=dx[i];
		starty+=dy[i];
		 if(book[startx][starty]==1)
		 {
		 	b[n].x=startx;
			b[n].y=starty;//记录路径的数组 
			if(startx==endx&&starty==endy) print(n);//搜索道了
			else dfs(n+1,startx,starty,endx,endy);//接着往下搜索 
		 }
		 startx-=dx[i];
		 starty-=dy[i];
		 book[startx][starty]=1;//回溯
	 } 
 } 
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>book[i][j];//只有1 0 
		}
	 } 
	 
	 cin>>startx>>starty>>endx>>endy;
	 b[1].x=startx,b[1].y=starty;
	 dfs(2,startx,starty,endx,endy);//第一个点已经有了,所以从2开始
	 //记录的是坐标 
	 if(flag==0) cout<<-1<<endl;//没有路 
	return 0;
}

http://www.niftyadmin.cn/n/849553.html

相关文章

LD_LIBRARY_PATH

Linux环境变量名&#xff0c;该环境变量主要用于指定查找共享库&#xff08;动态链接库&#xff09;时除了默认路径之外的其他路径(该路径在默认路径之前查找)[1]。 参考资料 [1]LD_LIBRARY_PATH_百度百科

促销方式汇总图

我在想&#xff0c;商品可以促销&#xff0c;你能不能用下面的手段&#xff0c;推销你自己&#xff1f; 转自腾讯大讲堂转载于:https://www.cnblogs.com/kissazi2/archive/2012/06/10/2543806.html

python 多级字典赋值,Python列表嵌套字典转树状结构思路求助

各位大神&#xff0c;求助直接上代码&#xff1a;现有一个列表&#xff1a;b_list [{udomain: u, ubusiness_id: 1, ubusiness3: ujetty, ubusiness2: unginx, ubusiness1: umusic, ubusiness4: u[\u8fd0\u8425\u4e2d], uremarks: , department_id: 1},{udomain: None, ubusin…

1209 修理牛棚

1209 修理牛棚 我读了两遍才大概弄明白题意 #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<cstring> #include<algorithm> using namespace std; int n,m,s; int a[205],b[205]; int ans; int main()…

php 友好url,PHP友好URL的实现_PHP教程

下面的代码主要是伪静态的实现&#xff0c;搜索引擎喜欢/*** 获得友好的URL访问** accesspublic* return array*/function getQueryString(){$_SGETS explode(“/”,substr($_SERVER[PATH_INFO],1));$_SLEN count($_SGETS);$_SGET $_GET;for($i0;$iif(!empty($_SGETS[$i]) &…

oracle真实案例之闪回区存储空间耗尽解决方法(ORA-19815解决方法)

这个是一个真实的oracle&#xff08;ORA-19815解决方法&#xff09;的案例&#xff0c;希望对大家有帮助。 今天朋友公司的平台出现了登陆缓慢、查询数据慢问题&#xff0c;并且通过spotlight监控oracle也出现登陆不成功现象&#xff0c;通过查看系统的内存、进程等&#xff0c…

1877 音量调节

1877 音量调节 背包&#xff0c;动态规划 对于每一个数据&#xff0c;都可以进行调高或者调低 貌似这个题很简单的样子 如果大暴力会怎么 我们设f[i][[j]为i首歌能达到的音量为j&#xff0c;能为1&#xff0c;不能为0 我们只需要枚举最大的f[n][i]&#xff0c;从后往前找到一个…

《iPhone与iPad开发实战—iOS经典应用剖析》连载七

3.3.4主视图代码在本应用中主视图中使用的视图是MainView&#xff0c;事实上在很多应用中我们不用为视图控制器自定义一个视图类的而是直接使用UIView基类就可以了。由于本应用是按照旧版本的Xcode模板编写的&#xff0c;我们还是按照旧版本介绍吧。在Cocoa MVC设计模式中&…