题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175

解题思路:从出发点开始DFS。出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路径。

 #include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1005
#define M 1005
int map[N][M],n,m;
bool vis[N][M],ok;
int move[][]={{,},{-,},{,},{,-}};
void can(int x,int y,int l1,int l2,int time,int last)//time记录已经转过几次弯
{
int i,xx,yy;
if(ok)
return;
if(time>)
return;
for(i=;i<&&ok==false;i++)
{
xx=x+move[i][];
yy=y+move[i][];
if(xx>&&xx<=n&&yy>&&yy<=m)
{
if(xx==l1&&yy==l2)
{
if(time<){
ok=true;return;
}
else if(i==last)
{
ok=true;return;
}
}
if(map[xx][yy]==&&!vis[xx][yy])
{
vis[xx][yy]=true;
if(i==last)
can(xx,yy,l1,l2,time,last);
else
can(xx,yy,l1,l2,time+,i);
vis[xx][yy]=false;
}
}
}
return;
}
int main()
{
// freopen("in.in","r",stdin);
int k,i,j;
while(scanf("%d%d",&n,&m),n+m)
{
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&map[i][j]);
scanf("%d",&k);
int x,y,l1,l2;
while(k--)
{
ok=false;
scanf("%d%d%d%d",&x,&y,&l1,&l2);
if(map[x][y]!=map[l1][l2])
ok=false;
else if(map[x][y]==)
ok=false;
else
{
if((((x+)==l1||(x-)==l1)&&l2==y)||((y-)==l2||(y+)==l2)&&x==l1)
ok=true;
else
{
memset(vis,false,sizeof(vis));
int tt,rr;
for(i=;i<&&ok==false;i++)
{
tt=x+move[i][];
rr=y+move[i][];
if(tt>&&tt<=n&&rr>&&rr<=m&&map[tt][rr]==&&!vis[tt][rr]){
vis[tt][rr]=true;
can(tt,rr,l1,l2,,i);//i记录该步走的方向
vis[tt][rr]=false;
}
}
}
}
if(ok)
printf("YES\n");
else
printf("NO\n");
}
}
return ;
}

hdu 1175 连连看 DFS的更多相关文章

  1. HDU 1175 连连看 (DFS+剪枝)

    <题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...

  2. HDU 1175 连连看(超级经典的bfs之一)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others)     ...

  3. hdu 1175(BFS&DFS) 连连看

    题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...

  4. Hdu 1175 连连看(DFS)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...

  5. HDU - 1175 连连看 【DFS】【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1175 思路 这种题一想到就用搜索, 但是内存是32m 用 bfs 会不会MLE 没错 第一次 BFS的 ...

  6. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

  7. hdu 1175 连连看 (深搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以 ...

  8. HDU 1175 连连看 (深搜+剪枝)

    题目链接 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以 ...

  9. hdu 1175 连连看 DFS_字节跳动笔试原题

    转载至:https://www.cnblogs.com/LQBZ/p/4253962.html Problem Description "连连看"相信很多人都玩过.没玩过也没关系, ...

随机推荐

  1. Simple screenshot that explains the singleton invocation.

    Here is the code: /* Some class,such as a config file,need to be only one.So we need to control the ...

  2. magic_quotes_gpc、mysql_real_escape_string、addslashes的区别及用法

    本篇文章,主要先重点说明magic_quotes_gpc.mysql_real_escape_string.addslashes 三个函数方法的含义.用法,并举例说明.然后阐述下三者间的区别.关系.一 ...

  3. SSL VPN 详解

    SSL VPN是专栏VPN系列技术原理的最后一篇,SSL VPN作为远程接入型的VPN,已经具备非常广阔的前景,它的主要适应场景是取代L2TP Over IPSec,但功能要比L2TP Over IP ...

  4. git 教程 git.oschina.net

    官方说明:http://git.oschina.net/oschina/git-osc/wikis/%E5%B8%AE%E5%8A%A9#ssh-keys 安装完成后,在开始菜单里找到"Gi ...

  5. Spring_DI利用set方法赋值Demo

    Person.java public class Person { private Long pid; private String pname; private Student student; p ...

  6. hdoj 2049 错排

    代码: #include <stdio.h> int main(){ int n,a,b,i,j; __int64 s[22],h[22]; s[1]=0; s[2]=1; s[3]=2; ...

  7. 第17章课后题(高级Perl技巧)

    17.1 写一个程序,从文件中读取一组字符串(每行一个),然后让用户键入模式以便进行字符串匹配. 对于每个模式,程序应该说明文件里共有多少字符串匹配成功,分别是哪些字符串. 对于所键入的每个新模式,不 ...

  8. php——composer 1、安装使用

    Composer 是PHP中用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的库文件. 系统要求 运 ...

  9. jQuery API中文文档

    jQuery API中文文档 http://www.css88.com/jqapi-1.9/category/events/event-handler-attachment/ jQuery UI AP ...

  10. Python - 多元组(tuple)

    声明一个多元组 (4, 5, 6) 这是列表 [4, 5, 6] 与列表不一样在于多元组使用() 来组织元素而list使用方括号[] 而且多元组不能更改,用于当你的数组不想像list一样会被更改时就使 ...