SDUT 1269-走迷宫(DFS打印路径)
走迷宫
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描写叙述
输入
输出
演示样例输入
5 4
1 1 0 0
1 1 1 1
0 1 1 0
1 1 0 1
1 1 1 1
1 1
5 4
演示样例输出
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
path[]数组保存路径。 。 无脑dfs
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
struct node
{
int x,y;
}path[1000];
int n,m,sx,sy,ex,ey,ok,step;
bool vis[16][16],ma[16][16];
int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
void print()
{
for(int i=0;i<step-1;i++)
printf("(%d,%d)->",path[i].x,path[i].y);
printf("(%d,%d)\n",ex,ey);
}
void dfs(int x,int y)
{
if(x==ex&&y==ey)
{
ok=1;
print();
return ;
}
for(int i=0;i<4;i++)
{
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx>=1&&tx<=m&&ty>=1&&ty<=n&&!vis[tx][ty]&&ma[tx][ty])
{
path[step].x=tx;
path[step++].y=ty;
vis[tx][ty]=1;
dfs(tx,ty);
vis[tx][ty]=0;
step--;
}
}
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
ok=0;
memset(path,-1,sizeof(path));
memset(vis,0,sizeof(vis));
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
scanf("%d",&ma[i][j]);
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
vis[sx][sy]=1;step=0;
path[step].x=sx;path[step++].y=sy;
dfs(sx,sy);
if(!ok)
puts("-1");
}
return 0;
}
SDUT 1269-走迷宫(DFS打印路径)的更多相关文章
- SDUT 1269 走迷宫(BFS)
点我看题目 题意:中文不详述. 思路 :上上上场比赛让一个BFS给虐了,上次比赛让一个三维的给废掉了.......所以急于从水题刷起......还因为数组开小了WA了5,6次 #include < ...
- sdut 2449走迷宫【最简单的dfs应用】
走迷宫 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_ 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m) ...
- NYOJ306 走迷宫(dfs+二分搜索)
题目描写叙述 http://acm.nyist.net/JudgeOnline/problem.php?pid=306 Dr.Kong设计的机器人卡多非常爱玩.它经常偷偷跑出实验室,在某个游乐场玩之不 ...
- luogu P1238 走迷宫--DFS模板好(水)题
题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数据来描述的,分别表示 ...
- sdut1269 走迷宫(dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1269 连着做了三个基本的dfs,终于弄懂了搜索 ...
- openjudge走迷宫(DFS)
题目: 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 走迷宫(DFS)
题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2449&cid=1181 目前dfs 里的递归还是不很懂,AC代码如下: #incl ...
- HDU_1010——小狗走迷宫DFS
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- WordPress主题开发:style.css主题信息标记
在最简单的情况下,一个WordPress主题由两个文件构成: index.php ------------------主模版 style.css -------------------主样式表 而且s ...
- 实用ExtJS教程100例-001:开天辟地的Hello World
ExtJS功能繁多,要想彻底的了解确实很困难.作为初学者,如何能找到一条快速的通道呢?我觉得,如果你有Javascript的基础,那就不要惧怕ExtJS的复杂,从动手开始,遇到问题,解决问题,积累经验 ...
- rocketmq 管理控制台
docker pull styletang/rocketmq-console-ng docker run -e "JAVA_OPTS=-Drocketmq.namesrv.addr=1 ...
- 【转】Android思维导图
转自:http://www.open-open.com/lib/view/open1421201191375.html
- EditText中文文档
感谢农民伯伯的翻译文:http://www.cnblogs.com/over140/archive/2010/09/02/1815439.html 属性名称 描述 android:autoLink 设 ...
- 关于MySQL的行转列的简单应用(二)---group函数
MySQL的行转列.列转行.连接字符串 concat.concat_ws.group_concat函数用法使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一 ...
- Spring的AsyncHandlerInterceptor
AsyncHandlerInterceptor提供了一个afterConcurrentHandlingStarted()方法, 这个方法会在Controller方法异步执行时开始执行, 而Interc ...
- 使用C#反射机制访问类的私有成员【转】
首先我必须承认访问一个类的私有成员不是什么好做法.大家也都知道私有成员在外部是不能被访问的.而一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员访问,可以套用下面这种非常好的方式 ...
- ARouter 路由 组件 跳转 MD
目录 简介 支持的功能 典型应用 简单使用 进阶使用 更多功能 其他 Q&A Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs bai ...
- 异常捕获 UncaughtExceptionHandler MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...