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 ...
随机推荐
- 架构:Eventually Consistent - Revisited
原文地址:http://i.cnblogs.com/EditPosts.aspx?opt=1. I wrote a first version of this posting on consisten ...
- Quartz:不要重复造轮子,一款企业级任务调度框架。
背景 第一次遇到定时执行某些任务的需求时,很多朋友可能设计了一个小类库,这个类图提高了一个接口,然后由调度器调度所有注册的接口类型,我就是其中之一,随着接触的开源项目越来越多,我的某些开发习惯受到了影 ...
- C# byte[]和文件FileStream相互转化
, pReadByte.Length); } catch { return false; ...
- 让Orchard支持多个Layout
默认Orchard只有一个Layout,有的时候,我们的站点往往需要多个母版页.那么,如果要让Orchard支持多个Layout,以下是一种解决方案. 一:创建LayoutFilter using S ...
- C# 异步编程Task整理(一)
一.简介 .Net Framework 4.0新增了一个System.Threading.Tasks命名空间,它包含的类提供了任务的相关操作.使用任务不仅可以获得一个抽象层,还能对底层线程进行很多统一 ...
- [转]PHP SOCKET编程
FROM : http://blog.csdn.net/hguisu/article/details/7448528 1. 预备知识 一直以来很少看到有多少人使用php的socket模块来做一些事情, ...
- jquery fullPage
FROM : http://www.dowebok.com/77.html 应用: http://txhd.163.com/
- Guava CharMatcher
概述 CharMatcher提供了多种对字符串处理的方法, 它的主要意图有: 1. 找到匹配的字符 2. 处理匹配的字符 CharMatcher内部主要实现包括两部分: 1. 实现了大量公用内部类, ...
- Android ActionBar使用介绍
一.什么是ActionBar 有图有真相,看一下图片就了解的差不多了 对于大多数应用,操作栏可以分割为 4 个不同的功能区域. 1. 应用图标 应用图标是您应用的标志.在应用图标位置摆放您自己的 lo ...
- Poly2Tri介绍[转]
https://blog.csdn.net/xys206006/article/details/83002326 这是Poly2Tri介绍的翻译博文.原文链接:http://sites-final.u ...