也不想解釋了,只是做來找回做題的感覺。遲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;}