思路: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. TPYBoard开发板搭建与阿里云服务发送数据

       今天给大家带来的是TPYBoard V202开发板的一次测试项目使用心得.而测试项目就是给服务端发送硬件底层数据,而数据有产品名称,WF模块MAC地址,温湿度数据.      什么是MicroP ...

  2. HashiCorp Vault介绍

    HashiCorp Vault是一款企业级私密信息管理工具.说起Vault,不得不提它的创造者HashiCorp公司.HashiCorp是一家专注于DevOps工具链的公司,其旗下明星级产品包括Vag ...

  3. 安卓studio导入jra包和so包,百度地图so包加载

    导入so包 这个我只接受测试可用的一种方法 第一步:把so包放在libs目录下,可以是文件夹也可以是单独的一个个so文件 然后在src同级的目录下找到build.gradle文件下如下信息 sourc ...

  4. PHP 构造方法 __construct()(转)

    PHP 析构方法 __destruct() 构造方法是类中的一个特殊方法.当使用 new 操作符创建一个类的实例时,构造方法将会自动调用,其名称必须是 __construct() . 在一个类中只能声 ...

  5. 02-Go语言数据类型与变量

    Go基本类型 布尔型: bool - 长度: 1字节 - 取值范围: true,false - 注意事项: 不可以用数字代表true或false 整型: int/uint - 根据运行平台可能为32或 ...

  6. Core Animation 文档翻译 (第四篇)

    Core Animation 文档翻译(第四篇) 让Layer的content动画起来 核心动画的基础接口以及为拥有Layer的View做的动画扩展接口,使得为Layer制作复杂动画变得简单化.例如改 ...

  7. java存放数据的5个地方

    1.寄存器:最快的存储区,位于处理器内部,但是寄存器的数量极其有限,所以寄存器根据需求进行分配,你不 能直接控制,也不能在程序中感觉到寄存器存在的任何迹象.(C/C+允许向寄存器建议寄存器配, 但它不 ...

  8. svn一整套使用,从下载到整个服务器搭建完成的详细说明

    SVN服务器的本地搭建和使用 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...

  9. 玩转spring boot——简单登录认证

    前言 在一个web项目中,某些页面是可以匿名访问的,但有些页面则不能.spring mvc提供了HandlerInterceptor接口来应对,只需要重写preHandle方法便可以实现此功能.那么使 ...

  10. Python基础篇(八)

    key words:私有变量,类静态变量,生成器,导入Python模块,r查看模块可以使用的函数,查看帮助信息,启动外部程序,集合,堆,时间模块,random模块,shelve模块,文件读取等 > ...