This question dfs + pruning
Pruning 1: Mark and return if you find the answer
Pruning 2: In each step, if the remaining steps are less than 0 or odd, return
Why return for odd numbers?
Because path A = translation B of part of the shortest path + path C of leaving the shortest path + path D of returning to the shortest path
Observation shows that C=D
So the remaining steps must be even.
Here's a good explanation. Parity pruning
source:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool b[10][10]={0};
bool flag;
int bx,by,ex,ey,m,n,t;
int m1[5]={0,1,-1,0,0},m2[5]={0,0,0,1,-1},a[10][10];
void dfs(int x,int y,int s)
{
if(flag==1) return;
if(x==ex&&y==ey&&s==t)
{
flag=1;
return;
}
int tem=t-s-abs(ex-x)-abs(ey-y);
if(tem&1||tem<0) return;
int xx,yy;
for(int i=1;i<=4;i++)
{
xx=x+m1[i];
yy=y+m2[i];
if(xx<1||xx>n||yy<1||yy>m||b[xx][yy]==1||a[xx][yy]==-1) continue;
b[xx][yy]=1;
dfs(xx,yy,s+1);
b[xx][yy]=0;
}
}
int main(){
while(1)
{
scanf("%d%d%d",&n,&m,&t);
if(n==0&&m==0&&t==0) break;
memset(b,0,sizeof(b));
char ch[10],c;
for(int i=1;i<=n;i++)
{
scanf("%s",ch);
for(int j=1;j<=m;j++)
{
c=ch[j-1];
if(c=='X') a[i][j]=-1;
else if(c=='S') a[i][j]=3,bx=i,by=j;
else if(c=='D') a[i][j]=1,ex=i,ey=j;
else if(c=='.') a[i][j]=0;
}
}
b[bx][by]=1;
flag=0;
dfs(bx,by,0);
if(flag==1) printf("YES\n");
else printf("NO\n");
}
return 0;
}