POJ 3009 Curing2.0
Title: In the These drawings in a direction to launch a pinball, the rock to stop, the stone disappeared, if the ball is close to the stone can not be fired, midway out of the calculation failed, the number of launches not more than 10 times. The minimum number of launches.
Idea: or DFS, because the stone will disappear after the collision, should pay attention to the restoration of the map. A single launch may be more than one step, plus a variety of situations. When the idea is clear, the DFS function is carefully categorized.
1#include <iostream>2#include <algorithm>3 using namespacestd;4 Const intINF =1000000;5 intdx[4] = {1, -1,0,0};6 intdy[4] = {0,0, -1,1};7 inth, W;8 intg[ at][ at];9 intMin_step;Ten //adding step to the function parameters of Dfs to simplify the thinking One voidDfsintXintYintStep) { A intNX, NY; - //a better pruning. - if(Step >=Ten)return ; the - for(intD=0; d<4; d++) { -NX = x; NY =y; -NX = x+Dx[d]; +NY = y+Dy[d]; - //first out of bounds and wall-sticking. + if(NX <0|| NY <0|| NX >= H | | NY >= W)Continue ; A if(G[nx][ny] = =1)Continue; at //The end of this while is divided into three cases, after which the three cases are discussed - while(! (G[nx][ny] = =1|| G[nx][ny] = =3)) { -NX + =Dx[d]; -NY + =Dy[d]; - if(NX <0|| NY <0|| NX >= H | | NY >= W) Break; - } in //This judgment is necessary . - if(NX <0|| NY <0|| NX >= H | | NY >= W)Continue; to + if(G[nx][ny] = =3) { -min_step = min (Min_step, step+1); the } * Else if(G[nx][ny] = =1){ $G[nx][ny] =0;Panax NotoginsengDFS (Nx-dx[d], ny-dy[d], step+1); - //The restoration of the map, this step is still very important theG[nx][ny] =1; + } A } the } + - intMain () $ { $ intsx, SY; - while(cin>>w>>h,w| |h) - { theMin_step =INF; - for(intI=0; i)Wuyi { the for(intj=0; j<w; J + +) - { WuCin>>G[i][j]; - } About } $ //application of the find switch - BOOLFound=false; - for(intI=0; i) - { A for(intj=0; j<w; J + +) + { the if(G[i][j] = =2) - { $SX = i; SY =J; theFound=true; the Break; the } the } - if(found) in { the Break; the } About } theDFS (SX, SY,0); the if(Min_step! =INF) the { +cout<<min_step<<Endl; - } the Else Bayi { thecout<<-1<<Endl; the } - } - return 0; the}
POJ 3009 Curing2.0