http://acm.hdu.edu.cn/showproblem.php?pid=1010
Analysis:
Simple DFS problem, it is suitable for novice practice.
Test instructions
To a diagram there are "X", "S", ".", "D" of these kinds,
Ask if you can get to D from S at t moment,
Each lattice can only go at most once,
The main thing is to pay attention to pruning
The quality of pruning affects the time of the problem,
No, not even t.
//Easy-to-understand wording//1568kb 62MS#include <iostream>#include <sstream>#include <iomanip>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <bitset>#include <string>#include <numeric>#include <algorithm>#include <functional>#include <iterator>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <cctype>#include <complex>#include <ctime>typedef Long LongLL;Const DoublePI =ACOs(-1.0);Const Long LongMoD =1e9+7;using namespace STD;intN,m,t;intBx,by;intOkChars[Ten][Ten];intd[4][2] = {-1,0,0,-1,0,1,1,0};voidDfsintXintYintT) {if(OK | | T <ABS(X-BX) +ABS(y-by) | | (x + bx + y + by + T)%2)//Pruning return;if(T = =0&& x = = bx && y = = by) {OK =1;return; } for(inti =0; I <4; i++) {intNOWX = x + d[i][0];intNowy = y + d[i][1];if(Nowx >=0&& Nowx < N && Nowy >=0&& Nowy < M && S[nowx][nowy]! =' X ') {S[nowx][nowy] =' X '; DFS (Nowx,nowy,t-1);if(OK)return; S[nowx][nowy] ='. '; } }}intMain () {//freopen ("Int.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intAx,ay;intNum while(scanf(" %d%d%d", &n,&m,&t) && N + M + T) {num =0; for(inti =0; i < n;i++)scanf('%s ', S[i]); for(inti =0; i < n;i++) for(intj =0; J < m;j++) {if(S[i][j] = =' S ') {ax = i; ay = j; }Else if(S[i][j] = =' D ') {bx = i; by = J; }Else if(S[i][j] = =' X ') num++; }if(M * n-num <= T)//The remaining number of squares to walk is less than the total time{puts("NO");Continue; }Else{OK =0; S[ax][ay] =' X '; DFS (AX,AY,T);puts(OK?)"YES":"NO"); } }return 0;}
//code size slightly larger wording//Worth Learning//1572kb 0MS#include <iostream>#include <queue>#include <cstdio>#include <memory.h>using namespace STD;Const intSize=8;inline int ABS(intN) {returnN>0? n:-n; }inline intDisintXintY) {return ABS(x) +ABS(y); }structpos{intx, y;};intn,m,t,sx,sy,dx,dy,dir[][2]={{-1,0},{1,0},{0,-1},{0,1}};CharM[size][size];BOOLVis[size][size];BOOLIsR (intSxintSY) { queue<pos>Q POS Tmp={sx,sy};memset(Vis,0,sizeofVIS); Q.push (TMP); vis[sx][sy]=1; while(!q.empty ()) {Tmp=q.front (); Q.pop (); for(intI=0;i<4; ++i) {inttx=tmp.x+dir[i][0],ty=tmp.y+dir[i][1];if(Tx==dx && Ty==dy)return 1;if(tx<0|| ty<0|| Tx>=n | | Ty>=m | | Vis[tx][ty] | | m[tx][ty]==' X ')Continue; POS Tmp2={tx,ty}; Q.push (TMP2); vis[tx][ty]=1; } }return 0;}intLefts (intSxintSY) {intstep=0; queue<pos>Q POS Tmp={sx,sy};memset(Vis,0,sizeofVIS); Q.push (TMP); vis[sx][sy]=1; while(!q.empty ()) {Tmp=q.front (); Q.pop (); for(intI=0;i<4; ++i) {inttx=tmp.x+dir[i][0],ty=tmp.y+dir[i][1];if(tx<0|| ty<0|| Tx>=n | | Ty>=m | | Vis[tx][ty] | | m[tx][ty]==' X ')Continue; POS Tmp2={tx,ty}; Q.push (TMP2); vis[tx][ty]=1; ++step; } }returnStep;}BOOLDFS (intSxintSyintcur) {if(Sx==dx && sy==dy &&!cur)return 1;intLeft=cur-dis (Dx-sx,dy-sy);if(left&1|| left<0|| !ISR (sx,sy) | | Lefts (sx,sy) <cur)return 0; for(intI=0;i<4; ++i) {inttx=sx+dir[i][0],ty=sy+dir[i][1];if(tx<0|| ty<0|| Tx>=n | | Ty>=m | | m[tx][ty]==' X ')Continue; m[tx][ty]=' X ';if(DFS (tx,ty,cur-1))return 1; m[tx][ty]='. '; }return 0;}intMain () { while(scanf(" %d%d%d", &n,&m,&t) && n+m+t) { for(intI=0; i<n;++i) {scanf('%s ', M[i]); for(intj=0; j<m;++j) {if(m[i][j]==' S ') {sx=i; sy=j;}Else if(m[i][j]==' D ') {dx=i; dy=j;} }} m[sx][sy]=' X ';puts(DFS (sx,sy,t)?"YES":"NO"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1010 Tempter of the Bone (DFS)