poj2488A Knight's Journey

标准dfs,注意跳出条件
#include <stdio.h>
#include <string.h>
int xx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};  
int yy[8] = {-1, 1, -2, 2, -2, 2, -1, 1}; 
int map[26][26];
char movex[30];
int movey[30]={1};
int m,p,q;
void dfs(int x,int y);
int main(int argc, char *argv[])
{
	int n,k,l;
	int i,j,flag;
	scanf("%d",&n);
	for(k=1;k<=n;k++)
	{
		flag=1;
		m=1;
		movex[0]='A';
		memset(map,0,sizeof(map));
		scanf("%d%d",&p,&q);
		dfs(1,1);
		printf("Scenario #%d:\n",k);
		if(m==p*q)
		for(l=0;l<q*p;l++)
		{
			printf("%c%d",movex[l],movey[l]);
		}
		else
		printf("impossible");
		printf("\n\n");
	} 
	return 0;
}
void dfs(int x,int y)
{
	int i,j;
	if(m==p*q)return ;
	else
	{
	map[x][y]=1;
		for(i=0;i<8;i++)
		{
			if(x+xx[i]>=1&&x+xx[i]<=q&&y+yy[i]>=1&&y+yy[i]<=p&&map[x+xx[i]][y+yy[i]]==0&&m!=p*q)
			{
			x+=xx[i];y+=yy[i];
			movex[m]='A'+x-1;
			movey[m++]=y; 
			dfs(x,y);
			if(m!=p*q)
				{
					map[x][y]=0;
					x-=xx[i];y-=yy[i];
					m--;		
				
				}
						
			}		
		}
		
	}
		


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值