poj3009-curling 2.0
Test instructions
Required to move an ice pot from the starting point "2" to the End "3" with a minimum number of steps
Where 0 is the moving area and 1 is the stone area, the ice pot will not stop if it is thinking of a certain direction, nor will it change direction (think of curling on the ice) unless the curling hits the stone 1 or reaches the end 3.
If you can reach the end point within 10 steps, output the number of steps required to reach the end, otherwise output-1
Note that the following are:
After the curling hits the stone, the curling will stop in front of the stone, at which point the movement of the curling is allowed to change, and the stone will break and the area of the stone is changed from 1 to 0. In other words, the curling will not take the place of the stone after it hits the stone.
The end point is a large friction area, and if the curling reaches the end of 3, it stops moving at the end point.
Problem Solving Ideas:
DFS, backtracking, the use of conditions for excellent pruning to control time
The following code will WA ... Don't want to break AH:-( Temporary stay
1#include <iostream>2 using namespacestd;3 Const intMAXN = -;4 intSQUARE[MAXN][MAXN];5 intw,h;6 intr0,c0,r2,c2;7 intBESTP;///Optimal Solution8 Const intdh[4] = {-1,0,1,0};///up 0, right 1, Bottom 2, left 39 Const intdw[4] = {0,1,0,-1};Ten One BOOLInsideintXinty) A { - returnx>=1&& x <= h && y>=1&& y<=W; - } the - BOOLWalkintXintYintDirint&XX,int&yy) -{///promise not to slip outside. - ///Face wall, need to change square + ///when you reach the end, stop. - ///if feasible, change the current position x, y + intTEMPX = x+Dh[dir]; A intTempy = y+Dw[dir]; at ///first detect if the current position can be slid - if(Inside (tempx,tempy) && square[tempx][tempy] = =1)return false;///The first step is the wall, not sliding - - while(true) - { - if(!inside (Tempx,tempy))return false;//slipped out of the field in if(Square[tempx][tempy] = =3)//reached the end. - { toxx = Tempx;yy =Tempy; + return true; - } the if(Square[tempx+dh[dir]][tempy+dw[dir]] = =1)///Slide the next step to the wall * { $xx = Tempx;yy =Tempy;Panax NotoginsengSquare[tempx+dh[dir]][tempy+dw[dir]] =0;///Change Site Status - return true; the } +Tempx+=dh[dir];tempy+=dw[dir];///Strike a grid A } the } + voidDfsintXintYintDeep ) - { $ $ ///Critical Conditions - if(X==r2 && y==C2) -{///finding the best solution the if(Deep <BESTP) -BESTP =Deep ;Wuyi return; the } - for(intI=0; i<=3; i++) Wu { - intXx,yy; About BOOLFlag =Walk (x,y,i,xx,yy); $ if(Flag && deep<Ten) -DFS (xx,yy,deep+1); - ///Backtracking - if(Flag && square[xx][yy]! =3) A { +Square[xx+dh[i]][yy+dw[i]] =1;///Change Site Status the } - $ } the return; the } the the intMain () - { in while(Cin>>w>>h && W &&h) the { the for(intI=1; i<=h;i++)///Line About for(intj=1; j<=w;j++)///column the { theCin>>Square[i][j]; the if(square[i][j]==2) + { -r0=i;c0=J; thesquare[i][j]=0;Bayi } the if(square[i][j]==3) the { -R2=i;c2=J; - } the } theBESTP = -; theDFS (R0,C0,0); the if(bestp>Ten) - { thecout<<-1<<Endl; the } the Else94cout<<bestp<<Endl; the } the return 0; the}
Poj3009-curling 2.0 (WA)