poj2488骑士马走

#include<stdio.h>
#include<stdlib.h>
int data[100][100] = {0};
int Dx[8] = {-1,1,-2,2,-2,2,-1,1};
int Dy[8] = {-2,-2,-1,-1,1,1,2,2};
int row,col;
int cun[100][2] = {0};
bool ret = false;
void DFS(int x,int y,int step);
int main()
{
	int n;
	//freopen("a.txt","r",stdin);
	scanf("%d",&n);
	for(int l = 0;l < n;l++)
	{
		ret = false;
		scanf("%d",&row);
		scanf("%d",&col);
		printf("Scenario #%d:\n",l + 1);
		
		data[0][0] = 1;
		DFS(0,0,1);
		data[0][0] = 0;
		if(ret){
			goto v;
		}
		
		if(ret == false){
			printf("impossible\n\n");
			continue;
		}
		v:for(int k = 0;k < row * col;k++)
		  {
				printf("%c%d",cun[k][1] + 65,cun[k][0] + 1);
				cun[k][0] = 0;
				cun[k][1] = 0;
		  }
		  printf("\n\n");
	}
	return 0;
}
void DFS(int x,int y,int step)
{
	if(step == row * col)
	{
		ret = true;
		return;
	}
	for(int i = 0;i < 8;i++)
	{
		int Nx = x + Dx[i];
		int Ny = y + Dy[i];
		if(data[Nx][Ny] == 0 && Nx >= 0 && Nx < row && Ny >= 0 && Ny < col)
		{
			data[Nx][Ny] = 1;
			cun[step][0] = Nx;
			cun[step][1] = Ny;
			DFS(Nx,Ny,step + 1);
			data[Nx][Ny] = 0;
			if(ret)
			{
				return;
			}
			cun[step][0] = 0;
			cun[step][1] = 0;
			
		}
	}
}

 

posted on 2017-01-05 14:28  霸王程  阅读(67)  评论(0编辑  收藏  举报