也不想解释了,只是做来找回做题的感觉。迟D出个这方面的小总结,越来越发现对所学知识作总结的重要性了
/*1010Tempter of the Bone*/
#include<stdio.h>
#include<math.h>
int n,m,time;
int xs,ys,xd,yd;
int direc[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char str[11][11];
bool dfs(int y,int x,int T)
{
int i,_x,_y;
int lelt=time-T;
int min=abs(y-yd)+abs(x-xd);
if(y==yd && x==xd && T==time) return true;
if(T>time) return false;
if(lelt<min) return false;
for(i=0;i<4;i++){
_x=x+direc[i][0];
_y=y+direc[i][1];
if(_y>=0 && _y<n && _x>=0 && _x<m && str[_y][_x]!='X'){
str[_y][_x]='X';
if(dfs(_y,_x,T+1))
return true;
str[_y][_x]='.';
}
}
return false;
}
int main()
{
int i,j,count,flag;
while(scanf("%d %d %d",&n,&m,&time)==3&&(n||m||time)){
getchar();count=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%c",&str[i][j]);
if(str[i][j]=='X')
count++;
if(str[i][j]=='S'){
xs=j;ys=i;
}
if(str[i][j]=='D'){//这居然没加花括号。。。。。。。
xd=j;yd=i;
}
}
getchar();
}
flag=abs(ys-yd)+abs(xs-xd);
if(n*m-count<time||(flag%2!=time%2)){
printf("NO\n");
continue;
}
str[ys][xs]='X';
if( dfs(ys,xs,0))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
本文介绍了一个基于深度优先搜索(DFS)的迷宫寻路算法实现,通过递归方式尝试所有可能路径来寻找起点到终点的通路,并考虑了时间限制因素。文章通过具体的C语言代码展示了如何在给定的时间内完成迷宫寻路。
425

被折叠的 条评论
为什么被折叠?



