ACM hdu1175 连连看

本文探讨了在编程大赛中遇到的一道类似问题,通过应用路径搜索算法(广度优先搜索BFS)来解决迷宫路径寻找的问题。详细介绍了算法实现步骤,包括初始化数据结构、边界判断和搜索过程,最终判断目标位置是否可达。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.hdu.edu.cn/showproblem.php?pid=1175

今天华为编程大赛,最后一题跟这个差不多,悲剧呀呀呀,太水了~


#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;


struct T
{
   int x,y,turn;
}q[1000024];

int map[1024][1024],n,m,k;
int d[4][2]={ 0,1,1,0,0,-1,-1,0  };
int hash[1024][1024];
int push( const int x,const int y,const int turn,int end )
{
    struct T t;
    t.x=x;t.y=y;
    t.turn=turn;
    q[ end++ ] = t;
    return end;
}
bool judge( int x,int y )
{
   if( x>n||x<=0||y>m||y<=0 )
     return false;
   else if( map[x][y]==0 )
     return true;
   else return false;
}
bool BFS( int X1,int Y1,int X2,int Y2 )
{
    memset( hash,0,sizeof( hash ) );
    int first=0,end=0;
    end=push( X1, Y1, -1, end );
    hash[X1][Y1]=1;
    while( first< end )
    {
        if( q[first].turn>=2 ) return false;
        for( int i=0;i<4; i++ )
        {
           int dx=q[first].x + d[i][0];
           int dy=q[first].y + d[i][1];
           int turn=q[first].turn + 1;
           while( judge( dx,dy )||( dx==X2 && dy == Y2 ) )
           {
                if( dx==X2 && dy == Y2 && map[dx][dy]==map[X1][Y1] )
                    return true;
                if( !hash[dx][dy] )
                {
                  hash[dx][dy]=1;
                  end = push( dx,dy,turn,end );
                }
                dx += d[i][0];
                dy += d[i][1];
           }
        }
        first++;
    }
    return false;
}
int main()
{
   int x,X1,Y1,X2,Y2;
   while( scanf( "%d%d",&n,&m ),n||m )
   {
      for( int i=1;i<=n;i++ )
        for( int j=1;j<=m;j++ )
            scanf( "%d",&map[i][j] );
       scanf( "%d%d%d%d",&X1,&Y1,&X2,&Y2 );
          if( X1==X2&&Y1==Y2 )
            printf( "NO\n" );
          else
          {
              if( map[X1][Y1]!=0&&map[X1][Y1]==map[X2][Y2]&&BFS( X1,Y1,X2,Y2 ) )
                printf( "YES\n" ) ;
              else printf( "NO\n" );
         }
   }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值