Title: Naruto and Sasuke
Sasuke was lured away by big snake pill. How long can Naruto catch up with him?
A map (in the form of a two-dimensional matrix) and the location of Sasuke and Naruto are known. You can go to every position on the map, but there are some positions with big snake pill's subordinates. You need to defeat big snake pill's subordinates to get to these positions. Naruto has a certain number of chakras. Each unit of chakra can defeat a big snake pill's subordinate. Suppose that Naruto can move up, down, left and right. It takes 1 unit time to move each distance. It doesn't take time to defeat the subordinates of big snake pill. If Naruto chakra is consumed, he can only go to the position where there is no subordinate of big snake pill, and can't move to the position where there is subordinate of big snake pill. Sasuke did not move during this period, nor did the big snake pill's men. How long does it take for Naruto to catch up with Sasuke?
input
The first line of input contains three integers: m, N, t. Represents the map of row m and column n and the initial number of chakras t of Naruto. 0 < M,N < 200,0 ≤ T < 10
After that is the map of row M and column N, where @ stands for Naruto and + stands for Sasuke. *On behalf of the channel, Chen represents the subordinates of big snake pill.
output
The output contains an integer R, which represents the minimum time required for Naruto to catch up with Sasuke. If Naruto cannot catch up with Sasuke, output - 1.
sample input
Sample input 1
4 4 1
#@##
**##
###+
Sample input 2
4 4 2
#@##
**##
###+
sample output
Sample output 1
6
Sample output 2
4
Solutions:
dfs+ pruning
dfs:
First, the dots of dfs chirp, then the dots around dfs chirp;
Termination conditions - chakra < 0
Optimal pruning:
*Pruning 1: * create a three-dimensional array SEP with the initial value set to the maximum value. Save the current time (temptime) of x,y and chakra to sep[x][y] [chakra]. If you come to this point x,y again and have the same chakra, and the time is larger, then the total time of this dfs will be greater than the former, that is, it can't be the minimum time - abandoned.
*Pruning 2: * the current time used is greater than the fastest total time - abandon it.
#include<iostream> #include<cstring> using namespace std; int Time; //The minimum time to find Sasuke int temptime; //Time in process, zero before each dfs int M,N,T; //M is the number of rows, N is the number of columns, and T is the number of chakras char map[210][210]; //Save map int narotoX; //Initial line of Naruto int narotoY; //Initial column of Naruto int visited[210][210]; //The initial value is 0. When traversing, it will be marked as 1 to prevent the same point from walking repeatedly int lx[4]={1,-1,0,0}; //Traversal array of next row number int ly[4]={0,0,1,-1}; //Traversal array of next column number int x1,y1; int sep[210][210][12]; //Three dimensional array -- for optimal pruning void Dfs(int x, int y,int chakora){ if(x<0||x>=M||y<0||y>=N||visited[x][y]) return; //Out of the map - direct Failure Return if (chakora<0) return; //Chakra less than 0, direct Failure Return if(temptime>=Time) return; //Pruning 2, fail to return if(temptime>=sep[x][y][chakora]) return; //Pruning 1, fail to return sep[x][y][chakora]=temptime; //At this time, temptime must be less than sep[x][y][chakora] and saved if(map[x][y]=='#') chakora--; //Currently xy has enemies, chakra reduced else if (map[x][y]=='+'){ //Current xy helps if(temptime<Time) Time=temptime; //Judge that temptime is less than the minimum time at this time. If it is, save it return; } visited[x][y]=1; //Mark as accessed temptime++; //Time + for(int i=0;i<4;i++){ //Four points up, down, left and right of dfs x1=x+lx[i]; y1=y+ly[i]; Dfs(x1,y1,chakora); } visited[x][y]=0; //All traversal complete -- return to the upper level and mark this point as not accessed temptime--; //The time spent visiting this point is also cut off } int main(){ while(cin>>M>>N>>T){ for (int i=0;i<M;i++) for (int j=0;j<N;j++){ cin>>map[i][j]; if (map[i][j]=='@'){ narotoX=i; narotoY=j;}} //Save location of Naruto memset(sep,0x3f,sizeof sep); //Initialize sep to maximum //Why to assign 0x3f //http://www.manongjc.com/detail/10-jreyaemnqbgivzi.html Time=1<<30; temptime=0; //Initialize temptime Dfs(narotoX,narotoY,T); if (Time==1<<30) cout<<"-1"<<endl; else cout<<Time<<endl; } return 0; }