HDU1175

HDU1175
#include  < stdio.h >
#include 
< stdlib.h >
#include 
< string .h >

const   int  N  =   1005 ;
int  n, m;
int  map[N][N];
int  x1, y1, x2, y2;
int  r[N][N], c[N][N];

bool  solve() {
    
int  s1  =  (map[x1][y1] != 0 ), s2  =  (map[x2][y2] != 0 ) ;
    
if ( x1  ==  x2  &&  y1  ==  y2)  return   false ;
    
for ( int  x  =   0 ; x  <  n; x  ++ ) {
        
if ( abs(c[x1][y1]  -  c[x][y1])  ==  (s1 && x < x1)  &&  
            abs(c[x2][y2] 
-  c[x][y2])  ==  (s2 && x < x2)  &&
            abs(r[x][y1] 
-  r[x][y2] )  ==   0  
        ) 
return   true ;
    }
    
for ( int  y  =   0 ; y  <  m; y  ++ ) {
        
if ( abs(r[x1][y1]  -  r[x1][y])  ==  (s1 && y < y1)  &&  
            abs(r[x2][y2] 
-  r[x2][y])  ==  (s2 && y < y2)  &&
            abs(c[x1][y] 
-  c[x2][y] )  ==   0
        ) 
return   true ;
    }
    
return   false ;
}

int  read(){
    
int  d, c;
    
while ( c  =  getchar(), c == '   ' || c == ' \n ' );
    d 
=  c - ' 0 ' ;
    
while ( c  =  getchar(), c >= ' 0 ' && c <= ' 9 ' )d = d * 10 + c - ' 0 ' ;
    
return  d;
}

int  main() {
    n
= read();m = read();
    
while (n || m) {
        
for ( int  i  =   1 ; i  <= n; i  ++  ) {
            
for ( int  j  =   1 ; j  <=  m; j  ++ ) {
                map[i][j] 
=  read();
                r[i][j] 
=  r[i][j - 1 +  (map[i][j]  !=   0 );
                c[i][j] 
=  c[i - 1 ][j]  +  (map[i][j]  !=   0 );
            }
        }
        
int  t;
        scanf(
" %d " & t);
        
for ( int  i  =   0 ; i  <  t; i  ++ ) {
            scanf(
" %d %d %d %d " & x1,  & y1,  & x2,  & y2);
            
if ( solve()  &&  map[x1][y1]  ==  map[x2][y2]   &&  map[x1][y1] != 0 ) {
                puts(
" YES " );
            } 
else  {
                puts(
" NO " );
            }
        }
        n
= read();m = read();
    }
}

你可能感兴趣的:(HDU1175)