poj 2488

10 篇文章 0 订阅
比较好的深搜入门题目(但当时也WA了==)
#include <iostream>
using namespace std;

const int maxn = 30;
int map[maxn][maxn];//到达 结点时 的步数
bool visited[maxn][maxn];
bool success = false;

struct
{
	int r,c;
}route[maxn];

int row,col;

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

bool in_range(int ro,int co)
{
	if(ro >= 1 && ro <= row && co >= 1 && co <= col)
		return true;
	return false;
}


void dfs(int ro,int co,int step)
{
	if(step==row * col)
	{
		for(int i = 0;i < step;i++)
			cout<<char(route[i].r + 'A' - 1)<<route[i].c;
		cout<<endl<<endl;
		success = true;
		return;
	}

	int tr,tc;
	for(int i = 0;i < 8;i++)
	{
		tr = ro + dr[i];
		tc = co + dc[i];
		if(in_range(tr,tc) && !visited[tr][tc])
		{
			visited[tr][tc] = true;
			//比较重要
			route[step].r = tr;
			route[step].c = tc;
			dfs(tr,tc,step + 1);

			visited[tr][tc] = false;
			if(success)
				break;
		}
	}//for int i
}


int main()
{
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	int casenum;
	cin>>casenum;
	for(int cnt = 1;cnt <= casenum;cnt++)
	{
		cin>>col>>row;
		memset(map,-1,sizeof(map));
		memset(visited,false,sizeof(visited));

		cout<<"Scenario #"<<cnt<<":"<<endl;
		success = false;
		route[0].r = 1;
		route[0].c = 1;
		visited[1][1] = true;
		dfs(1,1,1);

		if(!success)
			cout<<"impossible"<<endl<<endl;

	}//for cnt
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值