HDU - 1175 bfs
思路: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的更多相关文章
- hdu 1175(BFS&DFS) 连连看
题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...
- hdu 1175 bfs+priority_queue
连连看 如上图所示如果采用传统bfs的话,如果按照逆时针方向从(1,1)-->(3,4)搜索,会优先选择走拐四次弯的路径导致ans错误: Time Limit: 20000/10000 MS ( ...
- hdu 1175
#include <iostream> #include <string> #include <stdio.h> using namespace std; int ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
- HDU 1175 连连看(超级经典的bfs之一)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others) ...
- HDU - 1175 连连看 【DFS】【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1175 思路 这种题一想到就用搜索, 但是内存是32m 用 bfs 会不会MLE 没错 第一次 BFS的 ...
- HDU 1175 连连看(BFS)
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- MyISAM 存储引擎的特点及优化方法
MyISAM: MyISAM 管理非事务表.是ISAM 的扩展格式.除了提供ISAM里所没有的索引的字段管理等的大量功能.MyISAM 还使用一种表格锁定的机制.来优化多个并发的读写操作.My ...
- python 爬虫入门----案例爬取上海租房图片
前言 对于一个net开发这爬虫真真的以前没有写过.这段时间学习python爬虫,今天周末无聊写了一段代码爬取上海租房图片,其实很简短就是利用爬虫的第三方库Requests与BeautifulSoup. ...
- PHP 第3方评论系统
这段时间,无觅 评论也下线不能使用了. 客户好几个网站使用了.无觅 评论,前面也是用的是多说还是什么,总之也是第3方评论,没想到没过多久,又停止使用了. 没办法,网站还是需要评论系统,一气之下,自己做 ...
- vue项目中关于axios的简单使用
axios介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 官方仓库:https://github.com/axios/axios 中文文档:htt ...
- C#:导入Excel通用类(CSV格式)
一.引用插件LumenWorks.Framework.IO.dll(CsvReader) 插件下载地址:https://pan.baidu.com/s/1c3kTKli 提取密码 dz7j 二.定义 ...
- cmake编译安装mysql 5.6.12
cmake安装mysql 5.6.12 从mysql 5.5 开始就要用cmake编译安装 下载mysql 下载地址:http://pan.baidu.com/s/1o68xxqE 一.安装mysql ...
- 将Session放入Redis
默认情况下,我们的PHP是以文件的形式保存Session数据,所以,每次读写会话信息,就需要去访问硬盘. 为了解决会话信息夸域名问题,即为了实现同一时刻只能一个地方登录,同时也解决读写会话信息必须访问 ...
- 从UUID想到的
1.UUID的定义 通用唯一标识符(UUID)被设计成一个在时间和空间上都独一无二的数字,常被用作唯一性标识. UUID是一个由5位十六进制数的字符串表示的128比特数字,其格式为 aaaaaaaa- ...
- Java基础之Throwable,文件加载
Java中的异常与错误都继承自Throwable,Exception又分为运行时异常(RuntimeException)和编译时异常. 运行时异常是程序的逻辑不够严谨或者特定条件下程序出现了错误,例如 ...
- c语言中的#ifdef和#ifndef
#include "stdio.h"#include "conio.h"#define MAX#define MAXIMUM(x,y) (x>y)?x:y ...