Test instructions: Rescue operations, Angel R has a number of friends a(friends, here is a few times, not see test instructions), Angels are locked in a cell, waiting for friends to save, to save the shortest distance angel.
Solution: If you do not prune, the map of 200*200 will time out, you can use the angel as the starting point for DFS, record the minimum value to reach map[x][y], to reach the minimum of each a.
#include <iostream>#include<cstring>#include<cstdio>using namespacestd;Const intM =205;CharMap[m][m];intMinl[m][m]; The record reaches the minimum of x, Y, and if it reaches the minimum value again, then there is no need to go downintMinltor;intVisited[m][m];intn,m;//int step;intdire[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};voidDfsintV1,intV2,intStep) { if(Map[v1][v2] = ='a') { if(Step <minltor) {Minltor=step; Record the minimum value to reach a } Else return; } for(inti =0; I <4; i++) { intx = v1+dire[i][0]; inty = v2+dire[i][1]; if(!visited[x][y] && map[x][y]!='#'&& x < n && x >=0&& y < m && y >=0) { if(step+1>Minl[x][y])Continue; There's no need to go any further.ElseMinl[x][y] = step+1;//update minimum value visited[x][y]=1; if(Map[x][y] = ='x') DFS (X,y,step+2); ElseDFS (x,y,step+1); Visited[x][y]=0; } }}intMain () { while(Cin >> N >>m) {intStep =0; Minltor=1000000; intx, y; for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {cin>>Map[i][j]; VISITED[I][J]=0; MINL[I][J]=1000000; if(Map[i][j] = ='R') {x=i; Y=J; }}} dfs (X,y,step); if(Minltor = =1000000) cout<<"Poor ANGEL have to stay in the prison all his life.\n"; Elsecout << Minltor <<Endl; } return 0;}
Blog original, reproduced please indicate the source.
hdoj1242 (Dfs pruning solution)