链接地址:http://acm.fzu.edu.cn/problem.php?pid=1046#include<iostream> #include<stack> using namespace std; #define ableway 0 int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; bool boo=false; int map[8][8]; int start_x,start_y,end_x,end_y; struct temp{ int x;int y; }; stack<temp>stack1; void DFS(int x,int y,int time) { int temp_x,temp_y; if(x==end_x&&y==end_y) { if(time==0){boo=true;}//刚好到门的时候时间用完 return ; } if(time<0)return;//用尽则直接return; for(int i=0;i<4;i++) { temp_x=x+dx[i]; temp_y=y+dy[i]; if(map[temp_x][temp_y]==ableway) { time--; map[temp_x][temp_y]=-1; DFS(temp_x,temp_y,time); map[temp_x][temp_y]=0; time++; } } } int main() { int time; int count; register int i,j; int row,clomn; char str[20]; while (scanf("%d%d%d",&row,&clomn,&time)!=EOF&&(row||clomn||time)) { memset(map,-1,sizeof(map));//关键地方 boo=false;count=0; for (i=1;i<=row;i++) { scanf("%s",str); for (j=1;j<=clomn;j++) { if (str[j-1]=='S') { start_x=i;start_y=j; map[i][j]=0; } else if (str[j-1]=='D') { end_x=i; end_y=j; map[i][j]=0; count++; } else if (str[j-1]=='.') { map[i][j]=0; count++; } } } if(time>count) printf("NO/n"); else { DFS(start_x,start_y,time); if(!boo) { printf("NO/n"); } else printf("YES/n"); } } return 0; }