為什麼這樣會WA(第一、五個測資)?
#include <stdio.h>
#include <string.h>
const int mx[] = { 0, 1, 1, -1 };
const int my[] = { 1, 1, 0, 1 };
int matrix[ 10 ][ 10 ];
int m;
int check( int x, int y, int dir );
int main( void )
{
int ans;
char c;
int i, j;
while ( scanf( "%d", &m ) == 1 ) {
memset( matrix, 0, sizeof( matrix ) );
ans = 0;
for ( i = 0; i <= m - 1; ++i ) {
while ( scanf( "%d%c", &j, &c ) == 2 ) {
if ( j == 0 ) break;
matrix[ j - 1 ][ i ] = 1;
if ( c == '\n' ) break;
}
}
for ( i = 0; i <= m - 1; ++i ) {
if ( check( i, 0, 0 ) ) ans++;
if ( check( 0, i, 2 ) ) ans++;
}
for ( i = 1; i <= m - 1; ++i ) {
if ( check( 0, i, 1 ) ) ans++;
if ( check( i, 0, 1 ) ) ans++;
if ( check( m - 1, i, 3 ) ) ans++;
if ( check( i, 0, 3 ) ) ans++;
}
if ( check( 0, 0, 1 ) ) ans++;
printf( "%d\n", ans );
}
return 0;
}
int check( int x, int y, int dir )
{
int cnt = 0;
int px = x, py = y;
int status = matrix[ x ][ y ];
while ( px >= 0 && px <= m - 1 && py >= 0 && py <= m - 1 ) {
if ( matrix[ px ][ py ] == status ) {
++cnt;
px += mx[ dir ];
py += my[ dir ];
} else {
return 0;
}
}
return ( cnt >= 3 );
}