连连看 (BFS)
难点在于判断转弯小于两次 这个还好
主要是 走过的路还能再走 但是去掉标记数组会超时
*******所以用 v.step<=f[v.x][v.y]即可!!! 这个思想非常重用!!
查了我一个小时的错误终于找出来了!!!!!!! !!!!!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std; int world[][];
int f[][];
int dx[]={,,-,};
int dy[]={,,,-};
int n,m;int sx,sy,ex,ey; struct node
{
int x,y,d1,chance;
node(int x=,int y=,int d1=,int chance=):x(x),y(y),d1(d1),chance(chance){} }; void bfs()
{
memset(f,,sizeof(f)); node u(sx,sy,-,);
queue<node>q;
q.push(u);
while(!q.empty())
{
node u=q.front();q.pop();
if(u.x==ex&&u.y==ey&&u.chance<=){printf("YES\n");return ;} for(int i=;i<;i++)
{ node v(u.x+dx[i],u.y+dy[i],u.d1,u.chance); if(v.x>=&&v.x<=n&&v.y>=&&v.y<=m&&((v.x==ex&&v.y==ey)||world[v.x][v.y]==))//这里(v.x==ex&&v.y==ey)不能改成与目标内容相同!!!
{
if(v.d1!=-)
{
if(v.d1!=i)
{
v.chance++;v.d1=i;
} }
else v.d1=i;
if(v.chance>)continue;
if(v.chance<=f[v.x][v.y])
{ // printf("%d %d %d\n",v.x,v.y,v.chance);
f[v.x][v.y]=v.chance;
q.push(v);
}
} } }
printf("NO\n"); } int main()
{ while(cin>>n>>m,(n+m))
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&world[i][j]); int Q;scanf("%d",&Q);
for(int i=;i<=Q;i++)
{
scanf("%d%d%d%d",&sx,&sy,&ex,&ey); if(world[sx][sy]!=world[ex][ey]||!world[sx][sy]||!world[ex][ey]||(ex==sx&&sy==ey))
printf("NO\n");
else
bfs();
} } return ;
}
判断条件需谨慎QAQ
时隔一个月重做一遍
当初的代码真是歪歪扭扭 现在看来好尴尬。。。
其实这题不用采用这种转向次数vis就可以过的 后面貌似有一题必须要改变
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000+5
int dx[]={,,,-};
int dy[]={,-,,};
int sx,sy,ex,ey;
int n,m;
int mp[N][N];
int vis[N][N]; bool inmap(int x,int y)
{
return x>=&&x<=n&&y>=&&y<=m;
} struct node
{
int x,y;
int chance;int dic;
node(int x,int y,int chance,int dic):x(x),y(y),chance(chance),dic(dic){}
}; void bfs()
{
queue<node>q;
node u(sx,sy,,-);
q.push(u);
memset(vis,,sizeof vis);
while(!q.empty())
{
node u=q.front();q.pop();
if(u.x==ex&&u.y==ey){printf("YES\n");return ;} for(int i=;i<;i++)
{
node v=u;
v.x+=dx[i];
v.y+=dy[i];
if(inmap(v.x,v.y)&&!vis[v.x][v.y]&&(mp[v.x][v.y]==||(v.x==ex&&v.y==ey) ) )//这里要是改成mp[v.x][v.y]==mp[ex][ey]会错
{
if(v.dic==-)v.dic=i;
else if(v.dic!=i)
{
v.dic=i;v.chance--;
}
if(v.chance<)continue;
q.push(v);
vis[v.x][v.y]=;
}
}
}
printf("NO\n");
} int main()
{
while(scanf("%d%d",&n,&m),(n+m))
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&mp[i][j]);
int q;cin>>q;
while(q--)
{
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
if(mp[sx][sy]!=mp[ex][ey] ||mp[sx][sy]== )
printf("NO\n");
else
bfs();
}
}
}
连连看 (BFS)的更多相关文章
- hdu1175 连连看(bfs疯狂MLE和T,遂考虑dfs+剪枝)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1175/ 题目大意就是给出地图,上面有若干的数,相当于连连看,给了q个查询,问给出的两个位置的数能否在两次转弯以内 ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
- HDU 1175 连连看(BFS)
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1175(BFS&DFS) 连连看
题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...
- HDU1175 连连看(bfs) 2016-07-24 13:27 115人阅读 评论(0) 收藏
连连看 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通 ...
- HDU 1175 连连看(超级经典的bfs之一)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others) ...
- 连连看(简单搜索)bfs
连连看Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ2308连连看dfs+bfs+优化
DFS+BFS+MAP+剪枝 题意: 就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路: 首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
随机推荐
- 9 Web开发——springmvc自动配置原理
官方文档目录: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-sp ...
- 二、主目录 Makefile 分析(3)
2.8 编译选项 重新回到 主 makefile 中执行 2.8.1 u-boot 的编译顺序 代码166 到 189 行,这些是 u-boot 文件编译的顺序,由代码可以看到,首先是从cpu/$( ...
- Linux 重启网卡失败 Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
linux下重启网卡使用命令 : service network restart 时报错: [root@slave01 hadoop]# service network restart Startin ...
- asp.net mvc4 Json问题
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- luogu P2113 看球泡妹子
2333 这么水的蓝题 f[i][j] 表示看了i场比赛,小♀红的什么东西为j时小♂明的什么值 强行压维蛤蛤 剩下的转移很简单(注意i的循环顺序从后往前,01背包) (具体见代码) #include& ...
- 第16月第27天 pip install virtualenv ipython sip brew search
1. pip install virtualenv virtualenv testvir cd testvir cd Scripts activate pip https://zhuanlan.zhi ...
- SpringMVC的JSON数据交互(七)-@Response,@RestController,@RequestBody用法
1.@RequestBody (自动将请求的数据封装为对象) 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConve ...
- Python 对图片进行人脸识别
import cv2 def detect(path): img = cv2.imread(path) cascade = cv2.CascadeClassifier("/vagrant/d ...
- nginx配置集群
1.准备两个Tomcat 首先在Linux机器上部署两个Tomcat,端口分别为80和8080 2.分别部署测试应用 在两个tomcat下分别部署同一个应用testapp,很简单,就是在页面显示当前系 ...
- centos7.2环境下安装smokeping对网络状态进行监控
centos7.2环境下安装smokeping对网络状态进行监控 安装smokeping建议用centos7,用centos6.5一直卡在smokeping那里,下载不了perl的扩展插件,可能是因为 ...