hdu 1010 深搜+回溯

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.