hdu1175- 连连看BFS

来源:互联网 发布:什么是java分布式 编辑:程序博客网 时间:2024/05/16 23:54

                      Hdu 1175  连连看

Problem Description
“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
 
Input
输入数据有多组。每组数据的第一行有两个正整数n,m(0<n<=1000,0<m<1000),分别表示棋盘的行数与列数。在接下来的n行中,每行有m个非负整数描述棋盘的方格分布。0表示这个位置没有棋子,正整数表示棋子的类型。接下来的一行是一个正整数q(0<q<50),表示下面有q次询问。在接下来的q行里,每行有四个正整数x1,y1,x2,y2,表示询问第x1行y1列的棋子与第x2行y2列的棋子能不能消去。n=0,m=0时,输入结束。
注意:询问之间无先后关系,都是针对当前状态的!
 
Output
每一组输入数据对应一行输出。如果能消去则输出"YES",不能则输出"NO"。
 

Sample Input
3 41 2 3 40 0 0 04 3 2 141 1 3 41 1 2 41 1 3 32 1 2 43 40 1 4 30 2 4 10 0 0 021 1 2 41 3 2 30 0
 

Sample Output
YESNONONONOYES


这道题主要是考察BFS,刚开始提交的时候,错了很多次,才发现搜索的时候,我标记的是访问过的点不再访问。(这个思想是错的)。

错误的代码:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;#define M 1002struct point{   int x,y;   int dir;   int corner;   bool operator ==(const point &b)const{     if(this->x==b.x&&this->y==b.y)        return 1;     else        return 0;   }}st,e;int  m,n;int  mymap[M][M],vis[M][M];int  dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int mysearch(point s){    vis[s.x][s.y]=1;    point temp;    queue<point> q;    q.push(s);    while(!q.empty())    {        s=q.front();        printf("%d %d 转弯次数:%d\n",s.x,s.y,s.corner);        if(s==e)          return 1;        q.pop();        for(int i=0;i<4;i++)        {          temp.x=s.x+dir[i][0];          temp.y=s.y+dir[i][1];          temp.dir=i;          temp.corner=s.corner;          if(s.dir!=-1&&temp.dir!=s.dir)            temp.corner++;          if(temp.corner<3&&temp.x>0&&temp.x<=n&&temp.y>0&&temp.y<=m&&(mymap[temp.x][temp.y]==0||mymap[temp.x][temp.y]==mymap[e.x][e.y])&&!vis[temp.x][temp.y])          {                  q.push(temp);                  vis[temp.x][temp.y]=1;          }        }    }   // printf("sunquan");    return 0;}int main(){    int q,i,j;    while(~scanf("%d%d",&n,&m))    {        if(m==0&&n==0)            break;        for(i=1;i<=n;i++)            for(j=1;j<=m;j++)            scanf("%d",&mymap[i][j]);        scanf("%d",&q);        for(i=0;i<q;i++)        {            scanf("%d%d%d%d",&st.x,&st.y,&e.x,&e.y);            st.dir=-1;            st.corner=0;            if(st==e||mymap[st.x][st.y]==0||mymap[e.x][e.y]==0||mymap[st.x][st.y]!=mymap[e.x][e.y])            {                printf("NO\n");                continue;            }            memset(vis,0,sizeof(vis));            if(mysearch(st))                printf("YES\n");            else                printf("NO\n");        }    }    return 0;}

正确的思想是:

访问过的点可能再次访问,再次访问的条件是转过的弯较少

下面举一个特例。

4 4
2 3 1 0
0 0 0 0
4 0 2 0
1 0 0 0

1

1 3 4 1

  

正确答案应该是 YES。由于第一条路径 先访问标记了(4,2)。所以第二条合法路径,无法到达。

下面贴一个正确解法:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;#define M 1002#define INF 1<<30struct point{   int x,y;   int dir;   int corner;   bool operator ==(const point &b)const{     if(this->x==b.x&&this->y==b.y)        return 1;     else        return 0;   }}s,e;int  m,n;int  mymap[M][M],vis[M][M];int  dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};//方向依次为右、左、下、上int mysearch(){    queue<point> q;    point pre,temp;    s.dir=-1;    s.corner=0;    q.push(s);    while(!q.empty())    {        pre=q.front();       // printf("%d %d 转弯次数:%d\n",s.x,s.y,s.corner);        q.pop();        for(int i=0;i<4;i++)        {          temp.x=pre.x+dir[i][0];          temp.y=pre.y+dir[i][1];          temp.dir=i;          temp.corner=pre.corner;          if(pre.dir!=-1&&temp.dir!=pre.dir)            temp.corner++;       //判断是否出界,转角次数是否超过限制          if(temp.corner<3&&temp.x>0&&temp.x<=n&&temp.y>0&&temp.y<=m&&(mymap[temp.x][temp.y]==0||temp==e))          {              if(temp==e)               return 1;              ////如果当前路径到达该节点,转角次数更少则更新              if(temp.corner<=vis[temp.x][temp.y])                {                    q.push(temp);                    vis[temp.x][temp.y]=temp.corner;                }          }        }    }   // printf("sunquan");    return 0;}int main(){    int q,i,j;    while(~scanf("%d%d",&n,&m))    {        if(m==0&&n==0)            break;        for(i=1;i<=n;i++)            for(j=1;j<=m;j++)            scanf("%d",&mymap[i][j]);        scanf("%d",&q);        for(i=0;i<q;i++)        {            scanf("%d%d%d%d",&s.x,&s.y,&e.x,&e.y);            if(s==e||mymap[s.x][s.y]==0||mymap[e.x][e.y]==0||mymap[s.x][s.y]!=mymap[e.x][e.y])            {                printf("NO\n");                continue;            }//vis记录该点是否被访问过了,同时记录有转折次数,如果//新的转折次数小于以前访问时的转折次数那么更新,否则就不更新。            for(int i = 1; i <= n; i++)            for(int j = 1; j <= m; j++)              vis[i][j] = INF;            if(mysearch())                printf("YES\n");            else                printf("NO\n");        }    }    return 0;}

注意:第45行,temp==e 不能写成 mymap[temp.x][temp.y]=mymap[e.x][e.y].

因为,可能多组数字相同,但坐标不同,这里wa了好多次。

还有第50行,应该是"<=",虽然小于号能AC,但针对我上面那个用例,是不行的。

因为转角数相同的情况下,方向不同,可能结果就在下一个。





原创粉丝点击