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

以为数据会很大,吓的我一直不敢提交,结果32ms,汗~~~

Posted by stupidjohn at 2010-05-23 14:08:41 on Problem 2488
#include<iostream>
using namespace std;
int direction[8][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};
int map[26][26];
int m,n;
int steps,flag,times;
int position[700];
int init()
{
	int i,j;
	for(i=0;i<m;i++)
	for(j=0;j<n;j++)
	map[i][j]=0;
	steps=-1;
	flag=0;
}
int isin(int i,int j)
{
	return (i>=0&&i<m&&j>=0&&j<n);
}
void run(int i,int j)
{
	int k,newx,newy;	
	if(flag) return;
	position[++steps]=i*26+j;
	map[i][j]=1;
	if(steps==m*n-1)
	{
		flag=1;
		for(k=0;k<=m*n-1;k++)
		cout<<(char)(position[k]/26+'A')<<position[k]%26+1;
		cout<<endl;
		return;
	}
	for(k=0;k<8;k++)
	{
		newx=i+direction[k][0];
		newy=j+direction[k][1];
		if(isin(newx,newy)&&map[newx][newy]==0)
		{
			run(newx,newy);
		}
	}
	steps--;
	map[i][j]=0;
}
int main()
{
	int i,j,k;
	cin>>times;
	for(k=1;k<=times;k++)
	{
		cin>>n>>m;
		init();
		cout<<"Scenario #"<<k<<":"<<endl;
		for(i=0;i<(m+1)/2;i++)
		{
			for(j=0;j<=i;j++)
			{
				if(flag) break;
				init();
				run(i,j);
			}
			if(flag) break;
		}
		if(!flag) cout<<"impossible"<<endl;
		cout<<endl;
	}
	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