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. Visual C++ 打印编程技术-打印基础知识

    打印机介绍 1.打印术语 *: 1 英寸= 2.54 厘米(cm)= 25.4 毫米(mm) cpi (Characters Per Inch): 每英寸内所含的字符数,用来表示字符的大小.间距 cp ...

  2. 数据操作So easy-LINQ解析

    1.LINQ是什么? LINQ是Language Integrated Query的缩写,即“语言集成查询”的意思.LINQ的提出就是为了提供一种跨越各种数据源的统一的查询方式,它主要包含4个组件-- ...

  3. javascript Window对象 第16节

    <html> <head> <title>浏览器对象</title> <script type="text/javascript&quo ...

  4. Oracle 10g创建表空间的完整步骤详解

    本文我们主要介绍了Oracle 10g创建表空间的完整步骤,包括表空间的创建与删除.为应用创建用户以及权限的授予等操作,希望能够对您有所帮助. AD:WOT2014:用户标签系统与用户数据化运营培训专 ...

  5. java之内存管理

    对于JVM的垃圾回收机制来说,是否回收一个对象的标准在于:是否还有引用变量引用该对象,只要有引用变量引用该对象,垃圾回收机制就不会回收它. 强引用:创建一个对象,并把这个对象赋给一个引用变量.这种引用 ...

  6. Codevs 1172 Hankson 的趣味题 2009年NOIP全国联赛提高组

    1172 Hankson 的趣味题 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Hanks 博 ...

  7. stringstream vs sprintf, sscanf.

    前言 以前一直认为 stringstream 远不如 sprintf. 近日突然萌发了看看 stirngstream 是不是真的如我想的那么烂 对比 // stringstream. stringst ...

  8. 24种设计模式--观察者模式【Observer Pattern】

     <孙子兵法>有云: “知彼知己,百战不殆:不知彼而知己,一胜一负:不知彼,不知己,每战必殆”,那怎么才能知己知彼呢?知己是很容易的,自己的军队嘛,很容易知道,那怎么知彼呢?安插间谍是很好 ...

  9. 【转】Java编程之字符集问题研究

    发现这是对字集说得最明了的一篇文章了. 转发自:http://tomcat-oracle.iteye.com/blog/2037160 1. 概述 本文主要包括以下几个方面:编码基本知识,java,系 ...

  10. 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(一)

    项目组用air来开发手游, 但有些在原生应用里很容易实现的功能没有办法在air中直接调用,比如说震动,服务等等.但Adobe 提供了一种方法让air间接调用本地代码(java,object-c...) ...