Record a failed coding plane

  A driverless startup company that I really want to go to has passed both technical aspects and the interviewer's evaluation is also good, but I lost in the final coding aspect. Originally there was a fluke mentality, but I still received a call from HR to reject it on my birthday.

  Very worthless, I shed a few big tears, and after thinking about the pain, I turned over the questions that I hadn't done that day and knocked it out again, and found that it was really quite simple to straighten out the thinking. It is understandable to be rejected.

  

  The picture above is the question given by the interviewer. The description of the title is roughly: There is a person trapped in the center of the five-pointed star. What is the shortest way to get out? The cost of reaching a node is the number on the node, and going out means reaching the white area without a number, and the number of layers of the maze is not limited.

  At first, I wanted to use brute force search to solve the problem. The interviewer asked me to calculate the time complexity of this algorithm, and the result was an exponential of n. Later, after being reminded by the interviewer, I thought of the method of dynamic programming, but because the nested vector was used for the first time, the correspondence between the points was also very confusing, the code was written in a mess, and I wanted to give up many times in the middle. In the end, I submitted a semi-finished product, and I didn't want to look at my code again.

  I reorganized my thoughts today: I don’t consider going back (this is just an assumption, it’s the simplest case, I’ll think about it later when I go back), retract from the n layer, and write down the difference between the n-1 layer The smallest node among the n-layer nodes connected to each node of the as follows:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

vector< vector<int> > matrix;
vector<int> res;
int shortest;


void dg(int layer_num)
{
	if (layer_num == 0)
	{
         // First use of std::min() function, defined in #include<algorithm>
		shortest = min ({res [0], res [1], res [2], res [3]});
		return;
	}
		
	int i = layer_num;
	
	for (int j = 0; j < 4 * i; j++)
	{
		int minX;

		if (j == 0)
		{
			minX = min({ res[4 * (i + 1) - 1], res[0], res[1] });
			res[j] = minX + matrix[i][j];
			continue;
		}
		int x = (j - 1) / i;
		if (j%i == 0)
		{
			minX = min({ res[j + x], res[j + x + 1], res[j + x + 2] });
		}
		else
		{
			minX = min({ res[j + x], res[j + x + 1] });
		}

		res[j] = minX + matrix[i][j];
	}
	dg(i - 1);

}
intmain()
{
	int layer_num;
	
	vector<int> layer;
	int ele;

	// read graph data
	cin >> layer_num;
	for (int i = 0; i < layer_num; i++)
	{
		if (i == 0)
		{
			cin >> ele;
			layer.push_back(ele);
			matrix.push_back(layer);
			layer.clear();
			continue;
		}
		for (int j = 0; j < 4 * i; j++)
		{
			cin >> ele;
			layer.push_back(ele);
			if (i == layer_num - 1)
			{
				res.push_back(ele);
			}
				
		}
		matrix.push_back(layer);
		layer.clear();
	}

	dg(matrix.size() - 2);

	cout << shortest + matrix[0][0]<< endl;



	return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324845687&siteId=291194637