hdoj_1010 Tempter of the Bone

本文提供了一种使用深度优先搜索(DFS)解决迷宫问题的方法,通过C++代码实现,旨在寻找从起点到终点的可行路径,并判断是否能在限定步数内到达。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

 

//C++代码
#include<iostream>
#include<cstdlib>
using namespace std;

int n,m,t,dx,dy;
char maze[8][8];
const int xx[]={-1,0,1,0};
const int yy[]={0,1,0,-1};

bool dfs(int x,int y,int cnt){
	if(cnt==t && x==dx && y==dy) return 1;
	if(x==0 || x==n+1 || y==0 || y==m+1) return 0;
	int k=(t-cnt)-abs(x-dx)-abs(y-dy);
	if(k<0 || k&1) return 0;
	for(int i=0;i<4;i++){
		int tx=x+xx[i],ty=y+yy[i];
		if(maze[tx][ty]!='X'){
			maze[tx][ty]='X';
			if(dfs(tx,ty,cnt+1)) return 1;
			maze[tx][ty]='.';
		}
	}
	return 0;
}

int main(){
	while(cin>>n>>m>>t,n||m||t){
		int i,j,sx,sy,num=0;
		for(i=1;i<=n;i++){
			for(j=1;j<=m;j++){
				cin>>maze[i][j];
				if(maze[i][j]=='S') sx=i,sy=j;
				else if(maze[i][j]=='D') dx=i,dy=j;
				else if(maze[i][j]=='X') num++;
			}
		}
		if(n*m-num<=t) cout<<"NO"<<endl;
		else{
			maze[sx][sy]='X';
			if(dfs(sx,sy,0)) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值