CSP simulates C terrible cosmic rays for the first time

Title description:

Cosmic rays will propagate on an infinite two-dimensional plane (can be seen as a two-dimensional grid), and the initial direction is upward by default. The cosmic ray splits after emitting a distance, and is 45 degrees to the left and right. Split two cosmic rays while keeping the same power! The cosmic ray will split n times, and advance by ai unit length after each split.

Enter description:

Enter the first line contains a positive integer n (n <= 30) means that the cosmic ray will split n times. The
second line contains n integers, a1, `a2, ..., an, i number ai (ai <= 5) Represents how many unit lengths the cosmic ray of the ith split will travel in the original direction.

Sample input:

4
4 2 2 3

Output description:

Output a number ans, representing the number of units that cosmic rays have traveled through.

Sample output:

39

Ideas:

The difficulty of this problem is the removal of repeated points on the path when splitting the rays.
A bool two-dimensional array note is created here to mark whether the point has been passed for deduplication. Because ai <= 5, n <= 30, 300 300 is enough for the array size , 400 400 is taken here .
Simulated cosmic rays traveling on a two-dimensional plane can use bfs or dfs. Here, the bfs method is used. Think of each split of cosmic rays as an outward expansion of bfs. Here, you can create a three-dimensional array, the meaning of each dimension is x coordinate, y coordinate, direction, used to mark the next point of expansion, if this point has been marked in the array, then this path You have already walked through the process of bfs, you can directly discard this path. In this way, time complexity can be greatly reduced.

Code:

#include <iostream>
#include <string.h>
#include <queue>

using namespace std;

struct point
{
	int x;
	int y;
	int length;
	point(int thex, int they)
	{
		x = thex;
		y = they;
		length = 0;
	}
	point(int thex, int they,int thelength)
	{
		x = thex;
		y = they;
		length = thelength;
	}
};
int dx[] = { 0,-1,-1,-1,0,1,1,1 };
int dy[] = { 1,1,0,-1,-1,-1,0,1 };
bool vis[400][400];
bool note[400][400][8];
queue<point>q;

int main()
{
	int n;
	cin >> n;
	point now(199, 199);
	int *go = new int[n]();
	for (int i = 0; i < n; i++)
		cin >> go[i];
	int count = 0, number = 1;
	q.push(now);
	memset(vis, false, 400 * 400 * sizeof(bool));
	for (int i = 0; i < n; i++)
	{
		memset(note, false, 400 * 400 * 8 * sizeof(bool));
		int temp = 0;
		for (int j = 0; j < number; j++)
		{
			now = q.front();
			int before = q.front().length;
			q.pop();
			for (int k = 0; k < go[i]; k++)
			{
				now.x += dx[before];
				now.y += dy[before];
				if (!vis[now.x][now.y])
				{
					vis[now.x][now.y] = true;
					count++;
				}
			}
			if (!note[now.x][now.y][(before + 1) % 8])
			{
				note[now.x][now.y][(before + 1) % 8] = true;
				now.length = (before + 1) % 8;
				q.push(now);
				temp++;
			}
			if (!note[now.x][now.y][(before + 7) % 8])
			{
				note[now.x][now.y][(before + 7) % 8] = true;
				now.length = (before + 7) % 8;
				q.push(now);
				temp++;
			}
		}
		number = temp;
	}
	cout << count << endl;
	return 0;
}
Published 32 original articles · praised 0 · visits 691

Guess you like

Origin blog.csdn.net/qq_43814559/article/details/104977915