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

2488 例子过了,Runtime Error ,郁闷中,请各位兄弟赐教~

Posted by YAOZHIYI_SYSU at 2010-06-14 01:17:41
#include<iostream>
using namespace std;
const int M = 8;
bool visited[M][M];
int road[M*M][2];
int rows , columns;
int move[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
bool FirstWayFound;
void init(int rows , int columns)
{
	FirstWayFound = false;
	for(int i = 0 ; i < rows ; i ++)
	{
		for(int j = 0 ; j < columns ; j ++)
		{
			visited[i][j] = false;
		}
	}
}
bool check(int i , int j)
{
	if(i >= 0 && i < rows && j >= 0 && j < columns)
	{
		if(!visited[i][j])
		{
			return true;
		}
		return false;
	}
	return false;
}
void df(int i , int j , int depth)
{
	int k;
	if(check(i , j))
	{   
		visited[i][j] = true;
		road[depth][0] = i;
		road[depth][1] = j;
		if(depth == rows * columns - 1)
		{
			FirstWayFound = true;
		}
		else
		{
			for( k = 0; !FirstWayFound && k < 8; k ++)
			{
			    df(i + move[k][0] , j + move[k][1] , depth + 1);
			}
		}
		visited[i][j] = false;
	}
}
void solve(int rows , int columns)
{
	init(rows , columns);
	int depth;
	int i ,j ,k;
	for(i = 0 ; i < rows ; i++)
	{
		for(j = 0 ;!FirstWayFound && j < columns; j ++)
		{
			depth = 0;
			df(i , j , depth); // 如果能够搜索成功
		}
	}
	if(!FirstWayFound)
	    cout<<"impossible"<<endl;
	else
	{
		for(k = 0 ; k < rows * columns  ; k ++)
		{
			cout<<(char)(road[k][0] + 'A')<< (char)(road[k][1] + '1');
		}
		cout<< endl;
	}
	cout<<endl;
}

int main()
{
	int n;
	cin>>n;
	int i;
	for(i = 0 ; i < n;i ++)
	{
		cout<<"Scenario #"<< i << ":"<<endl;
		cin >> columns>> rows;
		solve(rows , columns);
	}
	return 1;
}

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