HDU 1175 连连看【BFS】
题意:给出起点和终点的棋子,不能经过别的棋子,而且转弯的次数不能超过2次,问能否消除
和逃离迷宫一样,每个节点记录下来它的当前的方向和转弯的次数,再搜
注意特判起点的棋子和终点的棋子为0或者不一样的情况,这样的话就不用搜了,直接输出NO
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int g[][];
int turn[][];
int n,m;
int dir[][]={,,-,,,,,-}; struct node{
int x,y;
int turn;
int dir;
}; node st,en; void bfs(){
queue<node> q;
node u;
u.x=st.x;u.y=st.y;u.turn=;u.dir=-;
turn[u.x][u.y]=;
q.push(u); while(!q.empty()){
u=q.front();q.pop(); node v;
for(int i=;i<;i++){
v.x=u.x+dir[i][];
v.y=u.y+dir[i][];
v.turn=u.turn;
v.dir=u.dir; if(u.x==en.x&&u.y==en.y&&u.turn<=){
printf("YES\n");
return;
}
if(v.dir!=i&&v.dir!=-) v.turn++; if(v.x==en.x&&v.y==en.y&&v.turn<=){
printf("YES\n");
return;
} if(v.x<||v.x>n||v.y<||v.y>m||g[v.x][v.y]!=) continue; if(v.turn>) continue; if(v.turn<=turn[v.x][v.y]){
turn[v.x][v.y]=v.turn;
v.dir=i;
q.push(v);
}
}
}
printf("NO\n");
} int main(){
while(scanf("%d %d",&n,&m)!=EOF&&n&&m){
for(int i=;i<=n;i++){
for(int j=;j<=m;j++) cin>>g[i][j];
} int q;
scanf("%d",&q);
while(q--){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++) turn[i][j]=;
scanf("%d %d %d %d",&st.x,&st.y,&en.x,&en.y); if(g[st.x][st.y]!=g[en.x][en.y]||!g[st.x][st.y]||!g[en.x][en.y]) printf("NO\n");
else bfs();
}
}
return ;
}
HDU 1175 连连看【BFS】的更多相关文章
- HDU 1175 连连看(超级经典的bfs之一)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others) ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
- 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 ...
- HDU 1175 连连看
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1175 连连看 DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...
- Hdu 1175 连连看(DFS)
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...
- HDU 1175 连连看 (深搜+剪枝)
题目链接 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以 ...
- hdu 1175 连连看 (深搜)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以 ...
随机推荐
- python 3.x 学习笔记16 (队列queue 以及 multiprocessing模块)
1.队列(queue) 用法: import queue q = queue.Queue() #先进先出模式 q.put(1) #存放数据在q里 作用: 1)解耦 2)提高效率 class qu ...
- jar文件配置冲突问题transformResourcesWithMergeJavaResForDebug
先看本人AS报错异常 Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > c ...
- JAVA在线观看视频教程完整版
今天给大家介绍一下JAVA在线观看视频教程完整版,我们知道Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语 ...
- 我的nginx+php是如何配置的?
nginx使用homebrew安装,安装之后 ngxin 安装目录:/usr/local/Cellar/nginx/1.8.0 删除掉默认的www目录,创建一个自己方便找到的 删除掉默认的www目录 ...
- 01《UML大战需求分析》阅读笔记之一
在大二的时候就已经在课堂上对UML也就是统一建模语言有了初步的了解,但是却不怎么明白,虽然可以画图可以完成任务,但是有些糊里糊涂.所以特地把这门书作为精读书籍,想要更加深度地学习UML.很多内容只用语 ...
- Long型转换成IP段String、StringIP段转换成Long型
/** 把long类型的Ip转为一般Ip类型:xx.xx.xx.xx * * @param ip * @return */ public static String getIpFromLong(Lon ...
- Pyhton学习——Day38
#CSS:Cascading Style Sheets——层叠样式表# CSS的四种引入方式# 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用.###### ...
- node——简单的服务器启动+乱码问题解决,响应报文头
这个是一个比较简单的代码 // 1.加载hrrp模块 var http=require('http'); // 2.创建一个http服务对象 var server=http.createServer( ...
- Job for mysqld.service failed because the control process exited with error code
启动MySQL时抛出: Job for mysqld.service failed because the control process exited with error code. See &q ...
- 注解实战Beforeclass和Afterclass
package com.course.testng;import org.testng.annotations.*; public class BasicAnnotation { //最基本的注解,用 ...