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. Click ListView Item跳转Activity

    今天学习了ListView点击Item跳转,修改上一篇代码bindData方法 lv.setOnItemClickListener(new OnItemClickListener() { public ...

  2. cmd命令积累

    dir:展示所有目录 cd fileName:进入下一个目录 cd .. :返回上一层目录 cd\:返回根目录

  3. jdbc-connect-oracle12c-pdb/cdb(jdbc连接oracle12c的pdb和cdb)

      1       本文简介: 通过特意引发问题,聚焦问题,解决问题,并循序渐进 最后总结jdbc连接oracle12c中cdb和pdb的条件. 软件环境:Redhat7.1+orcacle12c 2 ...

  4. Mac OS X 配置环境变量

    /etc/profile:/etc/bashrc 是针对系统所有用户的全局变量,只有root用户才能修改这两个文件,对一般用户来说是他们是只读的.一般用户要想修改它们,可以在命令前加sudo,意思是以 ...

  5. Ambiguous handler methods mapped for HTTP path

    一.问题:映射重复导致的错误 java代码如下: @RequestMapping(value = "/info/{remove}/{id}", method = RequestMe ...

  6. 例子: 自制Flask首页导航.

    # -*- coding:utf-8 -*- ''' Created on 2015年10月19日 ''' from flask import Flask, render_template impor ...

  7. HDU 3127 WHUgirls(DP 完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3127 题目大意:将一块长x宽y的矩形布料,剪成小的矩形(每个给定的小矩形都对应一个价值),使得所有小矩 ...

  8. UVA 11464 Even Parity(部分枚举 递推)

    Even Parity We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a on ...

  9. 在HTML中插入回车换行

    在制作EPUB的时候,发现原来收集的html文本根本就没有换行,这个在代码里面是无法忍受的,因为看着比较混乱,全部都是在一行里面,这个就像写作文的时候,你看到的文字全部都是在一行里面显示出来的,根本就 ...

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

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