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;
}