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

AC,贴代码~不算快16MS

Posted by bamboo111 at 2011-04-27 00:01:56 on Problem 2488
#include <stdio.h>
#include <memory.h>

int visit[26][26],locatex[626],step[8][2]={-1,-2,1,-2,-2,-1,2,-1,-2,1,2,1,-1,2,1,2};
char locatey[626];
int p,q,cas;

void put()
{
	int i,j;
	for(i=0;i<p*q;i++)
		printf("%c%d",locatey[i],locatex[i]+1);
	printf("\n");
}

int search(int deep,int r,int x)
{
	int i,sx,sy;
	if(deep==p*q-1) {locatex[deep]=r;locatey[deep]=x+'A';return 1;}
	for(i=0;i<8;i++)
	{
		sx=r+step[i][0];sy=x+step[i][1];
		if(sx>=0&&sx<p&&sy>=0&&sy<q&&visit[sx][sy])
		{
			visit[sx][sy]=0;locatex[deep]=r;locatey[deep]=x+'A';
			if(search(deep+1,sx,sy)) return 1;
			visit[sx][sy]=1;locatex[deep]=-1;locatey[deep]='0';
		}
	}
	return 0;
}

int main()
{
	int i;
	scanf("%d",&cas);
	for(i=1;i<=cas;i++)
	{
		scanf("%d %d",&p,&q);
		memset(visit,1,sizeof(visit));
		visit[0][0]=0;
		printf("Scenario #%d:\n",i);
		if(search(0,0,0)) put();
		else if(p==1 && q==1) printf("A1\n");
		else printf("impossible\n");
		if(i!=cas) printf("\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