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 ...
随机推荐
- SharePoint 2016 站点注册工作流服务报错
前言 安装完SharePoint 2016工作流环境,本来以为万事大吉了,结果给站点注册的时候报错了.搜了很多文章,发现后面要加上-Force参数. 错误截图 使用的为站点注册工作流服务的PowerS ...
- 大西洋帝国第一季/全集Boardwalk Empire1迅雷下载
大西洋帝国 第一季 Boardwalk Empire Season 1 (2010) 本季看点:1920年,联邦政府颁布禁酒令后,公开售卖酒类商品成为一种违法行为.在新泽西州的西南部的大西洋城没有任何 ...
- 用SimpleAdapter来设置ListView的内容
Mainactivit.java package com.kale.listview; import java.util.ArrayList; import java.util.HashMap; im ...
- [Web 前端] inline-block元素设置overflow:hidden属性导致相邻行内元素向下偏移
cp from : https://blog.csdn.net/iefreer/article/details/50421025 在表单修改界面中常会使用一个标签.一个内容加一个修改按钮来组成单行界面 ...
- Svg.js 图片加载
一.SVG.Image 1.创建和修改图片 var draw = SVG('svg1').size(300, 300); //SVG.Image 加载图片文件 var image = draw.ima ...
- 什么是Platform Services Controller
Platform Services Controller (PSC) is a component of the VMware Cloud Infrastructure Suite. PSC deal ...
- Max Points on a Line leetcode java
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- Ubuntu通过 lshw 工具包查看物理网卡名称
步骤1:安装相关工具包 apt-get install lshw lshw-gtk 步骤2:执行lshw命令进行查看硬件信息
- 深入理解FFM原理与实践
原文:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html 深入理解FFM原理与实践 del2 ...
- HTTP认证与https简介
HTTP请求报头: Authorization HTTP响应报头: WWW-Authenticate HTTP认证是基于质询/回应(challenge/response)的认证模式. HTTP认证 ...