It's easy, just stick to the code.
poj1979 DFS
The main idea: give you a two-dimensional array,. means can reach, #表示障碍, @ is the starting position, ask you can reach the largest place how many, every time only go up and down around
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intN, M, sx, Sy, ans;intpd[ -][ -];Charmaze[ -][ -];intdx[4] = {0,1,0, -1}, dy[4] = {1,0, -1,0};voidDfsintXinty) {ans+ +, pd[x][y] =1; for(inti =0; I <4; i++) { intNX = x + dx[i], NY = y +Dy[i]; if(NX <0|| NX >= N | | NY <0|| NY >= m)Continue; if(Pd[nx][ny] = =1|| Maze[nx][ny]! ='.')Continue; DFS (NX, NY); }}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) { if(n = =0&& m = =0) Break; intt =N; N= m, M =T; for(inti =0; I < n; i++) scanf ("%s", Maze[i]); for(inti =0; I < n; i++) for(intj =0; J < M; J + +) if(Maze[i][j] = ='@') SX = i, sy =J; Ans=0; memset (PD,0,sizeof(PD)); DFS (SX, SY); printf ("%d\n", ans); } return 0;}
View Code
poj3009 DFS
Main topic:
is to ask for an ice pot to move 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.
Note: The boundary may be out, the boundary is no obstacle to resist!
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intN, M, SX, Sy, TX, ty, ans;intmaze[ -][ -];intdx[4] = {0,1,0, -1}, dy[4] = {1,0, -1,0};intDfsintKintXinty) { if(x = = tx && y = = ty)returnK1; if(k >Ten)return-1; intNX, NY, min =-1, Res; for(inti =0; I <4; i++) {NX= x + dx[i], NY = y +Dy[i]; if(NX <0|| NY <0|| NX > N-1|| NY > M1|| Maze[nx][ny] = =1)Continue; while(1) { if(NX = = TX && NY = = ty)returnK; NX+ = Dx[i], NY + =Dy[i]; if(NX <0|| NY <0|| NX > N-1|| NY > M1) Break; if(NX = = TX && NY = = ty)returnK; if(Maze[nx][ny] = =1) {Maze[nx][ny]=0; Res= DFS (k +1, Nx-dx[i], NY-Dy[i]); Maze[nx][ny]=1; if(min = =-1) min =Res; Else if(Res! =-1&& res < min) min =Res; Break; } } } returnmin;}intMain () { while(SCANF ("%d%d", &m, &n)! =EOF) {memset (Maze,0,sizeof(Maze)); if(n = =0&& m = =0) Break; for(inti =0; I < n; i++) for(intj =0; J < M; J + +) {scanf ("%d", &Maze[i][j]); if(Maze[i][j] = =2SX = i, sy = j, Maze[i][j] =0; if(Maze[i][j] = =3) tx = i, Ty = j, Maze[i][j] =0; } ans= DFS (1, SX, SY); printf ("%d\n", ans); } return 0;}
View Code
poj3669 BFS
Main topic:
Given a few coordinates, there is a meteor rain at the T moment in these coordinates.
How to find a safe place in the shortest period of time.
Methods: Pre-treatment, the location of each coordinate with a meteorite pretreatment, so in the BFS will be very simple, such as not to consider to stay at the original point do not understand, or go back and so on
#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespacestd;intdt[ -][ -];intdx[5] = {0,0,1,0, -1}, dy[5] = {0,1,0, -1,0};structNode {intx, y, t;};intBFs () {if(dt[0][0] ==0)return-1; if(dt[0][0] == -1)return 0; Queue<node>Q; Node TEMP,NEWT; Temp.x=0, temp.y =0, temp.t =0; Q.push (temp); while(!Q.empty ()) {Temp=Q.front (); Q.pop (); for(inti =1; I <5; i++) {Newt.x= temp.x +Dx[i]; Newt.y= Temp.y +Dy[i]; NEWT.T= temp.t +1; if(Newt.x <0|| Newt.y <0)Continue; if(Dt[newt.x][newt.y] = =-1)returnnewt.t; if(Dt[newt.x][newt.y] <= newt.t)Continue; Q.push (newt); DT[NEWT.X][NEWT.Y]=newt.t; } } return-1;}intMain () {intm; while(SCANF ("%d", &m)! =EOF) { intx, y, t; memset (DT,-1,sizeof(DT)); for(intj =0; J < M; J + +) {scanf ("%d%d%d", &x, &y, &t); for(inti =0; I <5; i++) { intNX = x + dx[i], NY = y +Dy[i]; if(NX <0|| NY <0)Continue; if(Dt[nx][ny] = =-1) Dt[nx][ny] =T; Else if(Dt[nx][ny] > t) dt[nx][ny] =T; }} printf ("%d\n", BFS ()); } return 0;}
View Code
[DFS] & [BFS] poj1979 poj3009 poj3669