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. sqlserver中的锁

    NOLOCK(不加锁) 此选项被选中时,SQL Server 在读取或修改数据时不加任何锁. 在这种情况下,用户有可能读取到未完成事务(Uncommited Transaction)或回滚(Roll ...

  2. java 乱码问题-Dfile.encoding=UTF-8

    http://blog.csdn.net/telnetor/article/details/5555361 问题描述:程序涉及到国际化问题,httpclient抓回来的数据乱七八糟的乱码,在转了几次编 ...

  3. C#面向对象的一些东西

    最近在复习C#面向对象,也就是说常说的3大特性:封装.继承和多态.首先说一下封装,其实封装最大的目的也是为了实现代码的解耦和重用.代码也是安全的(对外它隐藏了具体的实现,就好比我们拿个遥控器就能操作电 ...

  4. 如何理解systemstate

    什么是systemstate一个systemstate是由在实例中调用生成systemstats时由每一个进程的进程状态组成.而每一个进程状态是由每一个进程所持有的当前对象所对应的详细对象状态信息组成 ...

  5. iOS开发——基于corelocation位置定位——工具类

    (代码工具类已写好,空闲时间整理成文档,待更新……)

  6. (hdu)5391 Zball in Tina Town

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendl ...

  7. unsigned int 转 RGB

    unsigned int颜色存储格式:0xaabbggrr,其中a,b,g,r分别表示,透明度.蓝色.绿色.红色. 方法一:使用windows宏 unsigned int clr = 0x00FF00 ...

  8. cleartool mkview snapshot windows

    mkview 用法详解:mkview - Support - IBM 创建View的命令相对来讲十分直截了当. cleartool mkview -snapshot -tag ViewName -vw ...

  9. Ubuntu下gcc及g++环境配置

    直接在命令行中输入以下命令即可. sudo apt-get install build-essential 安装完成后输入 gcc 和 g++ 进行确认.

  10. sql server 2008数据复制

    SQL Server 2008数据库复制是通过发布/订阅的机制进行多台服务器之间的数据同步,我们把它用于数据库的同步备份.这里的同步备份指的是备份服务器与主服务器进行实时数据同步,正常情况下只使用主数 ...