Blue Bridge Cup algorithm training steps and blood (DFS, C++)

Test algorithm training steps and blood

Resource limit
Time limit: 1.0s Memory limit: 256.0MB
Problem description
  There are n*n squares, where there are m obstacles, and the i-th obstacle will cost you p[i] points of blood. Initially you have C point blood, you need to go from (1,1) to (n,n), and ensure that the blood volume is greater than 0, find the minimum number of steps.
Input format
  3 integers n, m, c in the first line, indicating the size of the board, the number of obstacles and your blood volume.    The
  next m lines, each line describes an obstacle. Contains three integers xyp, indicating that the obstacle is in the x-th row and the y-th column, and the blood consumption is p.
Output format
  If the number of steps can be reached, if not, output "No".
Sample input
10 10 10
2 8 35
1 10 25
9 9 63
5 6 46
2 6 43
8 7 92
5 3 54
3 3 22
7 9 96
9 10 13
Sample output
18
Data scale and
  each number in the agreed input data Range.
  0<n,m<100,

Foreword: DFS is still a little rusty, it took a long time to debug, and it is still not proficient in recursive QAQ

Ideas and analysis:

1. This is a typical DFS, because this question only requires you to figure out the number of steps you need to reach the end, so you don’t need to think too much about whether the process is the optimal solution (in case you are asked to figure out where to reach the end) What about the highest HP remaining?), so this question is relatively simple. No matter what the path is, it must be a total of (n-1) * 2 steps to go from 1 , 1 to n, n . You only need to judge whether you can survive. Just finish:

	if(活着到终点)
		printf("%d", n * 2 - 2);
	else
		printf("No");

2. Then first create an n*n array to store the map, and then dfs it:

int n, m, heart;					//初始化一下
	cin >> n >> m >> heart;
	memset(a, 0, sizeof a);
	int i, j, k;
	while (m--)
	{
    
    
		cin >> i >> j >> k;
		a[i][j] = k;
	}
// x,y是坐标,n是边界,blood是剩余血量
void dfs(int x, int y, int n, int blood)
{
    
    

	if (x == n && y == n)
		{
    
    
		    flag = 1;		//flag等于1代表活着到终点
		}
    if(flag == 1) return;	//活着就一直return回main
	if ( x < n && y <= n && blood - a[x + 1][y] > 0)
	{
    
    	
		//如果可以向右走就 ,x + 1, y
		dfs(x + 1, y, n, blood - a[x + 1][y]);
	}

	if ( x <= n && y < n && blood - a[x][y + 1] > 0)
	{
    
    
		//如果可以向下走就 ,x, y + 1
		dfs(x, y + 1, n, blood - a[x][y+1]);
	}
}

3. The initial HP should be:

	int blood = heart - a[1][1];	//起始血量要减去a[1][1]

The AC code is as follows:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int a[102][102];		//地图
int flag=0;				//ans
void dfs(int x, int y, int n, int blood)
{
    
    

	if (x == n && y == n)
		{
    
    
		    flag = 1;
		}
    if(flag == 1) return;
	if ( x < n && y <= n && blood - a[x + 1][y] > 0)
	{
    
    
		dfs(x + 1, y, n, blood - a[x + 1][y]);
	}

	if ( x <= n && y < n && blood - a[x][y + 1] > 0)
	{
    
    
		dfs(x, y + 1, n, blood - a[x][y+1]);
	}
}


int main() {
    
    
	int n, m, heart;
	cin >> n >> m >> heart;
	int blood;
	memset(a, 0, sizeof a);
	int i, j, k;
	while (m--)
	{
    
    
		cin >> i >> j >> k;
		a[i][j] = k;
	}
	blood = heart - a[1][1];
	dfs(1,1,n,blood);
	if(flag)
		printf("%d", n * 2 - 2);
	else
		printf("No");
	return 0;
}

Afterword: Hurry up and practice more dfs, or how to participate in the Blue Bridge (Retrospective) Cup QAQ

Guess you like

Origin blog.csdn.net/Kyrie_irving_kun/article/details/113856279