如题:广度优先搜索
#include < stdio.h >

int hash[105][105];

typedef struct
  {
int l, c;
}type;
type q[50000];
int head, tail;

 int move[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };

void inith( int n )
  {

for ( int i=0; i<n; i++ )
 {
for ( int j=0; j<n; j++ )
 {
hash[i][j] = 0;
}
}
}

void initq()
  {

head = 0;
tail = -1;
}

void inq( type *a )
  {
tail ++;
q[tail].l = a->l;
q[tail].c = a->c;
}

void outq( type *a )
  {

a->l = q[head].l;
a->c = q[head].c;
head ++;
}

int testempty()
  {

if( head > tail )
 {
return 1;
}
return 0;
}

int bfs( type *s, int cor, int n )
  {

int ans = 0;
int tl, tc;
type a, b;

initq();

inq( s );
ans ++;
hash[s->l][s->c] = -1;

while ( ! testempty() )
 {
outq( &a );

for ( int i=0; i<4; i++ )
 {
tl = a.l + move[i][0];
tc = a.c + move[i][1];

if ( tl>=0 && tl<n && tc>=0 && tc<n && hash[tl][tc]==cor )
 {
b.l = tl;
b.c = tc;
inq( &b );
ans ++;
hash[tl][tc] = -1;
}
}
}

return ans;
}

int main()
  {

int n;
char ch;
type in;
int i, j;
while ( 1 )
 {
scanf( "%d%c", &n, &ch );
if ( n==0 )
 {
break;
}

inith( n );

for ( i=0; i<n-1; i++ )
 {
for ( j=0; j<n; j++ )
 {
int x, y;
scanf( "%d%d", &x, &y );
hash[x-1][y-1] = i + 1;
}

}

for ( i=0; i<n; i++ )
 {
for ( j=0; j<n; j++ )
 {
if ( hash[i][j] != -1 )
 {
in.l = i;
in. c = j;
if ( bfs( &in, hash[i][j], n ) != n )
 {
break;
}
}
}
if ( j < n )
 {
break;
}
}

if ( i<n )
 {
printf( "wrong\n" );
}
else
 {
printf( "good\n" );
}
}
return 0;
}

|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论

阅读排行榜
评论排行榜
|
|