BFS。wa了一下午,原来是YES,写成了Yes。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; typedef struct node_st{
int x, y;
int dir;
int turn;
node_st() {}
node_st(int xx, int yy, int ddir, int tturn) {
x = xx; y = yy; dir = ddir; turn = tturn;
}
} node_st; int map[][];
int n, m;
int dirs[][] = {{,},{,-},{,},{-,}};
char turns[][]; bool bfs(int bx, int by, int ex, int ey) {
queue<node_st> que;
bool val = false;
node_st node;
int x, y, dir, turn; memset(turns, , sizeof(turns));
turns[bx][by] = ;
que.push(node_st(bx, by, -, )); while ( !que.empty() ) {
node = que.front();
//printf("x=%d, y=%d, dir=%d, turn=%d\n", node.x, node.y, node.dir, node.turn);
if (node.x == ex && node.y == ey) {
val = true;
break;
}
que.pop();
for (int i=; i<; ++i) {
x = node.x + dirs[i][];
y = node.y + dirs[i][];
dir = i;
turn = node.turn;
if (x< || x>n || y< || y>m)
continue;
if (map[x][y] && (x!=ex || y!=ey))
continue;
if (dir!=node.dir && node.dir!=-)
++turn;
//printf("\tx=%d, y=%d, dir=%d, turn=%d\n", x, y, dir, turn);
if (turn > )
continue;
if (turns[x][y]== || turn+ <= turns[x][y]) {
turns[x][y] = turn+;
que.push(node_st(x, y, dir, turn));
}
}
} return val;
} int main() {
int q, x1, x2, y1, y2;
int i, j; while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
for (i=; i<=n; ++i)
for (j=; j<=m; ++j)
scanf("%d", &map[i][j]);
scanf("%d", &q);
while (q--) {
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if (x1==x2 && y1==y2) {
printf("NO\n");
continue;
}
if (map[x1][y1]!=map[x2][y2] || map[x1][y1]== || map[x2][y2]==) {
printf("NO\n");
continue;
}
if ( bfs(x1, y1, x2, y2) )
printf("YES\n");
else
printf("NO\n");
}
} return ;
}

【HDOJ】1175 连连看的更多相关文章

  1. hdoj 1175 连连看

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

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

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

  3. HDU 1175 连连看

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

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

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

  5. HDU 1175 连连看(BFS)

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. hdu 1175 连连看 DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...

  7. Hdu 1175 连连看(DFS)

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

  8. hdoj 1175 (bfs)

    题意: 判断两点之间是否可以通过至多有两次转变方向以达到相连,就是平时玩的连连看游戏,但是不能从外面绕过去. 思路:bfs,给每个加入的队列的点添加转变方向次数turn和点当前要走的方向dir属性,起 ...

  9. HUD 1175 连连看

    连连看 Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

随机推荐

  1. MVC小系列(十二)【RenderAction和RenderPartial区别】

    二者作用:RenderAction:渲染分部视图到页面上,要求提供Action和控制器名称RenderPartial:渲染分部视图到页面上,要求提供分部视图的名称,即路径,如果是在当前控制下或者sha ...

  2. javascript Object的长度

    1.示例 var obj = { a:"hello", b:"world", c:"hehe" } var key = 0; for(var ...

  3. 加载jar文件输出class和method

    package file import java.util.jar.JarEntry import java.util.jar.JarFile import org.junit.Test; class ...

  4. Leaflet交流

    GIS科研网 Leaflet交流 谢绝转载 http://www.3sbase.com欢迎加群交流  108299288 http://www.3sbase.com/3sbase/webgistest ...

  5. 九度OJ 1435 迷瘴

    题目地址:http://ac.jobdu.com/problem.php?pid=1435 题目描述: 通过悬崖的yifenfei,又面临着幽谷的考验—— 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满 ...

  6. 24种设计模式--状态模式【State Pattern】

    现在城市发展很快,百万级人口的城市一堆一堆的,那其中有两个东西的发明在城市的发展中起到非常重要的作用:一个是汽车,一个呢是...,猜猜看,是什么?是电梯!汽车让城市可以横向扩展,电梯让城市可以纵向延伸 ...

  7. VMware中Ubuntu忘记密码的解决办法

    在VMware中安装了Ubuntu 11.04,经过了一个长假,再次登录的时候居然进不去了,一开始不知道怎样在虚拟机中进入到Grub启动界面,网上搜索了一番,按照以下步骤重新为用户设定了新密码. 重启 ...

  8. 《sed的流艺术之一》-linux命令五分钟系列之二十一

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  9. 常用的工具GCC GDB Make Makefile

    系统调用系统调用是操作系统提供给外部应用程序的一组特殊的接口.应用程序通过这组特殊“接口”来获得操作系统内核提供的服务.在 C 语言中,操作系统的系统调用通常通过函数调用的形式完成, 这是因为这些函数 ...

  10. 一个小程序[Socrates]中学到的Perl点滴

    1. 抓取网页源文件,只要三行代码 use LWP::Simple; $url='http://music.baidu.com/top/dayhot'; $page=get($url) or die ...