Program design and algorithm week 09 test

Posted by mrjiggyhill on Tue, 17 Mar 2020 09:22:21 +0100

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?


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.


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

Sample output 2


dfs+ pruning
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.

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
	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
	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(){
	for (int i=0;i<M;i++)
		for (int j=0;j<N;j++){
			if (map[i][j]=='@'){
				narotoY=j;}}         //Save location of Naruto
		memset(sep,0x3f,sizeof sep);     //Initialize sep to maximum
		//Why to assign 0x3f
		 temptime=0;      //Initialize temptime
		 if (Time==1<<30) cout<<"-1"<<endl;
		 else cout<<Time<<endl;
		 return 0;
Published 1 original article · praised 0 · visited 4
Private letter follow

Topics: less