pku acm 2488 源码

#include <stdio.h>
#include <string.h>
int chess[30][30];
int visited[30][30];
int p,q;
char answer[1800];
int cnt;

int step[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};

bool dfs(int r,int c)
{
   visited[r][c]=cnt++;
   int tmpx=r,tmpy=c;
   for(int i=0;i<8;i++)
   {
	   tmpx=r+step[i][0];
	   tmpy=c+step[i][1];
	   if(tmpx<=p&&tmpx>0&&tmpy<=q&&tmpy>0)
	   {
		   if(visited[tmpx][tmpy]==0)
		   {
			   if(!dfs(tmpx,tmpy))
			   {
				   visited[tmpx][tmpy]=0;
				   cnt--;
			   }
		   }
	   }
   }
   if(cnt>p*q)
   {
	   answer[(visited[r][c]<<1)-2]=c+'A'-1;
       answer[(visited[r][c]<<1)-1]=r+'0';
	   return true;
   }
    return false;
}
int main()
{
	int n;
	scanf("%d",&n);
	int m=n;
	while (n--)
	{
		scanf("%d%d",&p,&q);
		memset(visited,0,sizeof(visited));
		memset(answer,0,sizeof(answer));
		printf("Scenario #%d:\n",m-n);
		cnt=1;
		if(dfs(1,1)){printf("%s\n\n",answer);}
        else{printf("impossible\n\n");}
	}
	return 0;
}


你可能感兴趣的:(ACM,DFS,pku,深度优先搜索,2488)