现在的位置: 首页 > 综合 > 正文

hdu 1175 连连看

2018年12月28日 ⁄ 综合 ⁄ 共 1085字 ⁄ 字号 评论关闭

 

 

 

#include<stdio.h>
#include<queue>
using namespace std;
#define N 1005
struct node
{
    int x,y,gw,fx;
};
int n,m,t,sx,sy,sz,ex,ey,ez,h,dir[4][2]={0,-1, 0,1, -1,0, 1,0}; 
int map[N][N];
char ev[N][N];
int judge(int x,int y)
{
    if(x>0 && x<=n && y>0&&y<=m && map[x][y]==0&&ev[x][y]=='0')
        return 1;
    return 0;
}

int bfs(int x,int y)

{
   queue<node>q;
   node cur,next;
   int k;
    cur.x=x;
    cur.y=y;
    cur.gw=0;
    cur.fx=-1;
    q.push(cur);
    ev[x][y]='1';
      while(!q.empty())
      {
          cur=q.front();
          q.pop();
          for(k=0;k<4;k++)
          {
              next.x=x=cur.x+dir[k][0];
              next.y=y=cur.y+dir[k][1];
              next.fx=cur.fx;
              next.gw=cur.gw;
              if(cur.fx==-1)
              {
                  next.gw=0;
                  next.fx=k;
              }
              else if(cur.fx!=k)
              {next.gw++;next.fx=k;}
              if(next.gw<=2&&next.x==ex&&next.y==ey)
                       return 1;
               if(judge(x,y))
               {
                   
                   if(next.gw<=2)
                   {q.push(next);ev[x][y]='1';}
               }
          }
      }
      return 0;
}
int main()
{
    int i,j,ans=0,flag,l,k,t;
    while(scanf("%d%d",&n,&m),n||m)
    {
       
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&map[i][j]);
             scanf("%d",&t);
             for(k=1;k<=t;k++)
             {
                 scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
                memset(ev,'0',sizeof(ev));
                 if(sx==ex&&sy==ey)
                     ans=0;
                 else if(map[sx][sy]!=map[ex][ey]||map[sx][sy]==0||map[ex][ey]==0)
                     ans=0;
                  else ans=bfs(sx,sy);
                  if(ans)
                   printf("YES\n");
                   else
                    printf("NO\n");
             }
           
    }
    return 0;
}

 

抱歉!评论已关闭.