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 nower at 2009-08-05 11:06:42 on Problem 2488
#include"stdio.h"
#include"string.h"
int chess[50][50];
int p,q,n,flag;
int r[500],c[500],num;
int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},
               {1,-2},{1,2},{2,-1},{2,1}};
void dfs(int x,int y)
{
	int i,xx,yy;
	if(flag==1)
		return ;
	if(num==p*q)
	{
		for(i=1;i<=num;i++)
			printf("%c%d",c[i]+'A'-1,r[i]);
		printf("\n\n");
		flag=1;
		return ;
	}
	for(i=0;i<8;i++)
	{
		xx=x+dir[i][0];
		yy=y+dir[i][1];
		if(xx>0&&xx<=p&&yy>0&&yy<=q&&chess[xx][yy]==0)
		{
			chess[xx][yy]=1;
			num++;r[num]=xx;c[num]=yy;
			dfs(xx,yy);
			if(flag==1)
				break;
			chess[xx][yy]=0;
			num--;
		}
		if(flag==1)
			break;
	}
	if(flag==1)
		return ;
}
int main()
{
	int times=1;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&p,&q);
		memset(chess,0,sizeof(chess));
		memset(r,0,sizeof(r));
		memset(c,0,sizeof(c));
		flag=0;
		chess[1][1]=1;r[1]=1;c[1]=1;
		num=1;
		printf("Scenario #%d:\n",times++);
		dfs(1,1);
		if(flag==0)
			printf("impossible\n\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