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

哪位可以帮忙指点一下哪里出错了,感谢万分!!

Posted by 13826499098 at 2008-12-01 16:58:40 on Problem 3009
//poj 3009 Curling 2.0

#include <iostream>
using namespace std;

int board[30][30];
int r, c;
int s_r, s_c;
int estep;

struct pos
{
	int x;
	int y;
};

bool ok ( int i, int & x, int & y )
{
	if ( i == 1 )                          //方向1
	{
		for ( int j=y-1; j>=0; j-- )
		{
			if ( board[x][j]==3 )         { y=j; return true; }
			if ( board[x][j]==1&&j==y-1 ) return false;
			if ( board[x][j]==1 )         { y=j+1; return true; }			
		}
		return false;
	}
	if ( i == 2 )                           //方向2
	{
		for ( int j=x-1; j>=0; j-- )
		{
			if ( board[j][y]==3 )         {	x=j; return true; }
			if ( board[j][y]==1&&j==x-1 ) return false;
			if ( board[j][y]==1 )         {	x=j+1; return true; }			
		}
		return false;
	}
	if ( i == 3 )                           //方向3
	{
		for ( int j=y+1; j<c; j++ )
		{
			if ( board[x][j]==3 )         {	y=j; return true; }
			if ( board[x][j]==1&&j==y+1 ) return false;
			if ( board[x][j]==1 )         {	y=j-1; return true; }			
		}
		return false;
	}
	if ( i == 4 )                           //方向4
	{
		for ( int j=x+1; j<r; j++ )
		{
			if ( board[j][y]==3 )         {	x=j; return true; }
			if ( board[j][y]==1&&j==x+1 ) return false;
			if ( board[j][y]==1 )         {	x=j-1; return true; }			
		}
		return false;
	}
	return false;
}

void _change ( int i, int x, int y )         //改变球停止位置下一位的状态
{
	if ( i==1 && y-1>=0 && board[x][y-1]==1 )  board[x][y-1]=0;
	if ( i==2 && x-1>=0 && board[x-1][y]==1 )  board[x-1][y]=0;
	if ( i==3 && y+1 <c && board[x][y+1]==1 )  board[x][y+1]=0;
	if ( i==4 && x+1 <r && board[x+1][y]==1 )  board[x+1][y]=0;
}

void _changeback ( int i, int x, int y )     //改回初始状态
{
	if ( i==1 && y-1>=0 && board[x][y-1]==0 )  board[x][y-1]=1;
	if ( i==2 && x-1>=0 && board[x-1][y]==0 )  board[x-1][y]=1;
	if ( i==3 && y+1 <c && board[x][y+1]==0 )  board[x][y+1]=1;
	if ( i==4 && x+1 <r && board[x+1][y]==0 )  board[x+1][y]=1;
}

void dfs ( pos p, int step )
{
	if ( step >= estep || step > 10 )
		return;
	if ( board[p.x][p.y] == 3 )
		if ( step < estep )
		{
			estep = step;
			return;
		}
	for ( int i=1; i<=4; i++ )
	{                                       //方向:2
		pos pp = p;                         //      ^
		if ( ok( i, pp.x, pp.y ) )          //      |
		{                                   //1 < - p - > 3
			_change( i, pp.x, pp.y );       //      |
			dfs( pp, step+1 );              //      4
			_changeback( i, pp.x, pp.y );		
		}
	}
}

int main ()
{
	freopen ( "3009.in","r",stdin );
	while ( scanf( "%d%d",&c, &r ) && r && c )
	{
		int i, j;
		for ( i=0; i<r; i++ )
			for ( j=0; j<c; j++ )
			{
				scanf ( "%d",&board[i][j] );
				if ( board[i][j] == 2 )
				{
					s_r = i;
					s_c = j;
				}
			}
		pos p; estep = 100000000;
		p.x = s_r; p.y = s_c;
		dfs ( p, 0 );
		if ( estep != 100000000 )
			printf ( "%d\n",estep );
		else
			printf ( "-1\n" );
	}
	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