思路:d[x][y][z]表示以z方向走到(x, y)的转弯次数。

如果用优先队列会超时,因为加入队列的节点太多,无用的节点不能及时出队,会造成MLE,用单调队列即可。

AC代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1000 + 5;
map<PI, int>dir;
const int dx[] = {0,0,-1,1};
const int dy[] = {1,-1,0,0};
int n, m, d[maxn][maxn][4], G[maxn][maxn];
struct node{
	int x, y, tc, dir;
	node() {}
	node(int x, int y, int tc, int dir):x(x), y(y), tc(tc), dir(dir) {
	}
};
bool bfs(int x1, int y1, int x2, int y2) {
	memset(d, inf, sizeof(d));
	queue<node>q;
	for(int i = 0; i < 4; ++i) {
		d[x1][y1][i] = 0;
		q.push(node(x1, y1, 0, i));
	}
	while(!q.empty()) {
		node p = q.front(); q.pop();
		int x = p.x, y = p.y, tc = p.tc, dir = p.dir;
		if(tc > d[x][y][dir]) continue;
		for(int i = 0; i < 4; ++i) {
			int px = x + dx[i], py = y + dy[i];
			if(px < 0 || py < 0 || px >= n || py >= m) continue;
			int pt = tc;
			if(i != dir) ++pt; //发生偏转
			if(pt > 2) continue;
			if(px == x2 && py == y2) return true;
			if(G[px][py]) continue;
			if(pt < d[px][py][i]) {
				d[px][py][i] = pt;
				q.push(node(px, py, pt, i));
			}
		}
	}
	return false;
}
int main() {
	int q;
	while(scanf("%d%d", &n, &m) == 2 && n && m) {
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < m; ++j)
				scanf("%d", &G[i][j]);
		scanf("%d", &q);
		int x1, y1, x2, y2;
		while(q--) {
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			--x1, --y1, --x2, --y2;
			if( G[x1][y1] && G[x2][y2] && G[x1][y1] == G[x2][y2] && bfs(x1, y1, x2, y2)) printf("YES\n");
			else printf("NO\n");
		}
	}
	return 0;
}

如有不当之处欢迎指出!

HDU - 1175 bfs的更多相关文章

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

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

  2. hdu 1175 bfs+priority_queue

    连连看 如上图所示如果采用传统bfs的话,如果按照逆时针方向从(1,1)-->(3,4)搜索,会优先选择走拐四次弯的路径导致ans错误: Time Limit: 20000/10000 MS ( ...

  3. hdu 1175

    #include <iostream> #include <string> #include <stdio.h> using namespace std; int ...

  4. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  5. HDU(1175),连连看,BFS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...

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

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

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

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

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

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

  9. HDU 1175 连连看(BFS)

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

随机推荐

  1. python_19_异常处理

    什么是异常处理? -- 对于用户输入,不想让用户看见出错信息,对异常进行处理 异常处理的框架是什么? try: 可能出错的程序1 可能出错的程序2        #程序1出错了,不在执行程序2 exc ...

  2. 重温MFC

    1. Button控件 2. 旋转和高级编辑控件 3. 标签控件和属性页 4. 列表控件 5. 树控件 6. 进度条控件和滑动条控件 7. 滚动条 8.工具栏和状态栏

  3. Mysql基本命令二

    删除id>10的记录:delete from user where id>10; 设置user表的自增字段起始值为10:alter table user anto_increment=10 ...

  4. 【转】shell学习笔记(三)——引用变量、内部变量、条件测试、字符串比较、整数比较等

    1.env显示当前的环境变量 2.PS1='[\u@\h \w \A] \$' 可以设置bash的命令与提示符. 3.echo $$ 显示当前bash的PID号 4.echo $?显示上一条指令的回传 ...

  5. TCP粘包和拆包问题

    问题产生 一个完整的业务可能会被TCP拆分成多个包进行发送,也有可能把多个小的包封装成一个大的数据包发送,这个就是TCP的拆包和封包问题. 下面可以看一张图,是客户端向服务端发送包: 1. 第一种情况 ...

  6. adb命令介绍与使用

    DB的概念 adb的全称为Android Debug Bridge,是起到调试桥的作用.通过adb,我们可以在ecplise中方便的通过DDMS来调试Android程序,其实他就是一个debug工具. ...

  7. 文件首行为空白行,为什么该行字符串长度为1(line.length()=1)

    问题描述:最近编写程序遇到一个问题,文件首行的内容为空,但调用line0.length()返回的确为1 .如下图: 最初认为可能存在制表符,或者换行符的原因,于是调用了line0.trim();方法, ...

  8. Technical debt

    What is Technial debt? Technical debt is not bug. It is that the feature can work, but it is not a p ...

  9. Linux 下编译安装xDebug命令速记

    下载xdebug-2.2.4.tgz软件链接: http://pan.baidu.com/s/1jGHYRMA #解压 xdebugtar -zxvf xdebug-2.2.4.tgz #进入xdeb ...

  10. 关于C#连接Oracle数据库 尝试加载Oracle客户端时引发BadImageFormatException 如果在安装32位Oracle客户端组件的情况下以64位模式运行,将出现此问题

    这个问题已经困扰了我快一个月了,各种百度,各种博客,可是,一个个都试过了,什么下载32位客户端,配置环境变量什么的,纯属扯犊子,开发环境win10 64位    oracle 11g r2 64位,这 ...