难点在于判断转弯小于两次  这个还好

主要是   走过的路还能再走 但是去掉标记数组会超时

*******所以用     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)的更多相关文章

  1. hdu1175 连连看(bfs疯狂MLE和T,遂考虑dfs+剪枝)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1175/ 题目大意就是给出地图,上面有若干的数,相当于连连看,给了q个查询,问给出的两个位置的数能否在两次转弯以内 ...

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

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

  3. HDU 1175 连连看(BFS)

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

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

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

  5. HDU1175 连连看(bfs) 2016-07-24 13:27 115人阅读 评论(0) 收藏

    连连看 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通 ...

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

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

  7. 连连看(简单搜索)bfs

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

  8. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

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

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

随机推荐

  1. (一)求 int 型数据在内存中存储时 1 的个数

    题目:求 int 型数据在内存中存储时 1 的个数 描述:输入一个 int 型数据,计算出该 int 型数据在内存中存储时 1 的个数 运行时间限制: 10 sec 内存限制:128 MByte 输入 ...

  2. u-boot移植(十二)---代码修改---支持DM9000网卡

    一.准备工作 1.1 原理图 CONFIG_DM9000_BASE 片选信号是接在nGCS4引脚,若要确定网卡的基地址,则要根据片选信号的接口去确定. 在三星2440的DATASHEET中memory ...

  3. ADB not responding

    1.如下错误: 2.执行 如下代码 : netstat -aon|findstr "5037" 3.打开任务管理器  找到PID 为 5536 的进程  将该进程关闭 4.重启 A ...

  4. WF控制台工作流(2)

    using System; using System.Linq; using System.Activities; using System.Activities.Statements; namesp ...

  5. resolution will not be reattempted until the update interval of repository-group has elapsed or updates are forced

    Failed to execute goal on project safetan-web: Could not resolve dependencies for project com.safeta ...

  6. sqlite limit offset

    limit 0,20 表示从第1条开始取20条数据 limit 20 offset 2  表示从第2条开始取出20条数据

  7. Database学习 - mysql 数据库 索引

    索引 索引在mysql 中也叫 '键',是存储引擎用来快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索引优化应该是对查询性能优化 ...

  8. MySQL或MariaDB忘记root密码

    当我们忘记数据库密码时,我们可以通过如下来修改! 编辑配置文件(提前最好进行备份) 然后重启服务 systemctl restart mariadb 或者 systemctl restart mysq ...

  9. mysql系列九、mysql语句执行过程及运行原理(分组查询和关联查询原理)

    一.背景介绍 了解一个sql语句的执行过程,了解一部分都做了什么,更有利于对sql进行优化,因为你知道它的每一个连接.where.分组.子查询是怎么运行的,都干了什么,才会知道怎么写是不合理的. 大致 ...

  10. windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速

    原文地址:http://www.jianshu.com/p/c245d46d43f0 写在前面的话 2016年11月29日,Google Brain 工程师团队宣布在 TensorFlow 0.12 ...