Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么会WA呀!!!

Posted by wolf711988 at 2008-07-29 18:24:38 on Problem 1753
下边是我的代码,各位牛人,帮忙看看。。。不知道为什么人WA??




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

#define MAX_N 65540

//black:1, white:0
int weight[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 
				2048, 4096, 8192, 16384, 32768 }; //2的0次方到2的15次方

int occured[ MAX_N ];
int sour_state;
const int dest_state1 = 0; //后十六位全0,即全white
const int dest_state2 = 65535; //后十六位全1,即全black
int queue[ MAX_N ];
int front, rear;
int flip[] = { 1, -1, 4, -4 };
char s[ 4 ][ 8 ];


int init()
{
	int i, j;

	sour_state = 0;
	for( i = 0; i < 4; i++ )
	{
		scanf( "%s", s[ i ] );
		for( j = 0; j < 4; j++ )
		{
			if( s[ i ][ j ] == 'b' )
				sour_state += weight[ 15 - ( i * 4 + j ) ]; //对应位 置1
		}
	}
	if( sour_state == dest_state1 
		|| sour_state == dest_state2 )
	{
		return 1;
	}
	return 0;
}

int BFS()
{
	int cur_state, new_state;
	int op;
	int i, j, pos;

	memset( occured, 0, sizeof( occured ) );
	front = rear = 0;
	//初始状态进队
	queue[ rear ] = sour_state;
	rear = ( rear + 1 ) % MAX_N;
	occured[ sour_state ] = 1;

	while( front != rear )
	{
		//出队
		cur_state = queue[ front ];
		front = ( front + 1 ) % MAX_N;

		//十六种变化方法
		for( i = 15; i >= 0; i-- ) 
		{
			op = 0;
			//四个方向扫描,产生对应的操作码
			for( j = 0; j < 4; j++ )
			{
				pos = i + flip[ j ];
				if( pos >= 0 && pos <= 15 )
					op += weight[ pos ]; //对应位 置1
			}
			op += weight[ i ]; //自己本身也要翻转
			new_state = cur_state ^ op;
			if( !occured[ new_state ] )
			{
				if( new_state == dest_state1 
					|| new_state == dest_state2 )
				{
					return occured[ cur_state ];
				}
				//进队
				occured[ new_state ] = occured[ cur_state ] + 1;
				queue[ rear ] = new_state;
				rear = ( rear + 1 ) % MAX_N;
			}
		}
	}
	return -1;
}

int main()   
{   
	
#ifndef ONLINE_JUDGE     
	   FILE *fin;
	
       fin = freopen( "t.in", "r", stdin );
       if( !fin )
       {
       		printf( "reopen in file failed...\n" );   
   			exit( 0 );
       }
#endif
	
	int T, min;

	scanf( "%d", &T );
//	printf( "%d\n", sizeof( int ) );
	while( T-- )
	{
		if( init() == 1 )
			printf( "0\n" );
		else
		{
			min = BFS();
			if( min > 0 )
				printf( "%d\n", min );
			else
				printf( "Impossible\n" );
		}
		if( T )
			printf( "\n" );

	}

#ifndef ONLINE_JUDGE      
       fclose( stdin );   
#endif   
       return 0;   
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator