Program design thinking Week4-CSP simulation game

Program design thinking Week4-CSP simulation game

A- cuckoo East adventure

Description

A pointer on a ring, the first point to the letter a. Cuckoo East each can rotate clockwise or counter-clockwise one space. For example, a clockwise rotation through z, counterclockwise rotation to b. Cuckoo East hands of a string, but he is dumb, so he came to ask your help, ask how many times requires a minimum of turn.
String input line number, at least to turn the output.

Here Insert Picture Description

Sample

input:
zeus

output:
18

Idea

First calculate the length of the string that is the number of revolutions, from a beginning, for each letter, there are two options, rotating clockwise or counterclockwise from the starting character to character, and select the shortest path to reach the method of the letter, and the next rotation of the initial letter updated accordingly. Finally, the cumulative number of cells to obtain a minimum of turn.

Summary

This question is relatively simple, mainly to measure the number of cells to be rotated clockwise and counterclockwise, and then every time you want to remember to update the initial letter

Codes

#include <iostream>
#include <string>
#include <cmath>
#include<cstdlib>
#include<cstdio>
using namespace std;

int main()
{
	cin.sync_with_stdio(false);
	string str;
	cin >> str;
	int size = str.length();
	int sum = 0;
	char now = 'a';
	for (int i = 0; i < size; i++) {
		int left = abs(str[i] - now);
		int right = 26-left;
		sum += left < right ? left : right;
		now = str[i];
	}
	printf("%d\n", sum);
}


B- Cuckoo East want to eat

Description

Cuckoo East ai need to buy a fried every day. But fried shops in order to stimulate consumption, there are only two ways to buy: ① one-time buy two fried one day. ② buy a fried today, but for tomorrow to buy a fried, the store will give a ticket, the next day to come and collect tickets. Not the rest of the purchase, the purchase of these two ways can be used many times, but the cuckoo East is a thrifty boy, he left end of the training, the hands of a ticket is not allowed at the end of training. Cuckoo East very rich, you do not need to worry about money East cuckoo, cuckoo East but stupid, he just wanted to ask you whether he could buy a fried ai day exam week.
Test input week days n (1 <= n <= 100000), and the number n days daily fried buy the ai (0 <= ai <= 10000).

Sample

input:
4
1 2 1 2

output:
YES

input:
3
1 0 1

output:
NO

Idea

Because every day if you select the second option next day must accumulate tickets run out, so tickets before the ticket with a record one day accumulated, when the number of the previous day to buy tickets one day the number of accumulated announcement came from the failed purchase, otherwise deductions after the previous day's tickets second option until only need to buy a fried or fried not need to buy a. After the cycle to determine whether the ticket is cleared at the end of the last day.

Summary

This question is in fact not difficult, there are a few to note:
① 1 <= the n-<= 100000, carefully read the title, pay attention to the variable range.
② the beginning, I fell into a dead end recursion, that the first solution and the second solution can be adopted zero or more times, which results in many cases. In fact, at most only a second option, regardless of the situation several times a day using the second option, can be used as alternative to the first option, so a lot of simple, value ticket of only 0 or 1.

Codes

#include <iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

int a[100000];
int n;
bool flag = true;

int main()
{
	cin.sync_with_stdio(false);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	int ticket = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] - ticket < 0) { flag = false; break; }
		ticket = (a[i] - ticket) % 2;

	}
	if (ticket != 0)flag = false;
	if (flag)cout << "YES";
	else cout << "NO";


}


C- terrible cosmic rays

Description

Cosmic rays propagation (FIG can be seen as a two-dimensional grid) in an infinite two-dimensional plane, the default initial upward direction. Cosmic rays are emitted after a distance split in this direction
of about 45 ° to split the two directions of cosmic rays, while the power of the same! Cosmic rays will split n times, each time division unit lengths ai will advance in the direction of splitting.
Calculate the total number of positions will be "down wisdom against"
separatist input cosmic rays number n (n <= 30), and the i-th division of the cosmic rays will continue to go in its original direction length ai (ai <= 5).

Here Insert Picture Description

Sample

input:
4
4 2 2 3

output:
39

Idea

This is a variation of the maze, you can use DFS or BFS resolved. Rays exist in the n-th division multiple locations may overlap, so it is necessary to calculate the total number of powerhouse location is down against Chile, where we use the set data structures to ensure that each location appears only once in the collection. Split ray of the direction of movement there are eight kinds of situations which can go sideways anyway, the situation with the storage array movement.
We used the recursive DFS forward search to the last split split from the last path, for each division, the split is determined according to the parameter dir-axis direction, when dir equal to 0 or 4 on behalf of two splinter rays symmetric about the y-axis when dir equal to 1 or 5 on behalf of y = x straight line of symmetry, when dir equal to 2 or 6 Representative symmetrical about the x-axis, when dir equal to 3 or 7 on behalf of the straight line y = -x symmetry, wherein a first symmetry Add set point of the path, the axis of symmetry and wherein a known symmetric rays, can be deduced another point of symmetry on the nature of the rays is added set between perpendicular bisectors, has been pushed forward so that all available positions reached, the final set size is the number of positions to be reduced intellectual combat.

Summary

This is a modification of a typical maze, the key is split path memory of the position deduplication i.e., before that channel maze except that the direction of movement including 8, generally difficult.
At first I attempted to use BFS problem-solving, but after a few times out or pruning:

int bfs() {
	Q.push(point(0,0,0));
	while (!Q.empty()) {
		point now = Q.front();
		Q.pop();
		point temp;
		for (int i = 1; i <= a[index]; i++)
		{
			//cout << now.x + dx[now.dir] * i << " " << now.y + dy[now.dir] * i << endl;
			P.push_back(point2(now.x + dx[now.dir] * i, now.y + dy[now.dir] * i));
		}

		temp.x = now.x + dx[now.dir] * a[index];
		temp.y = now.y + dy[now.dir] * a[index];
		temp.dir = now.dir;
		

		sort(P.begin(), P.end(), cmp);

		int u = 0, v = 1;
		while (v < P.size()) {
			if (P[u] == P[v]) {  P.erase(P.begin() + v); }
			else { u = v; v = u + 1; }
		}
		if (index == 0)index++;
		else { 
			k++;
			if (pow(2, index+1) == k)index++;
		}
		
		if (index == n)return P.size();

		int d1 = temp.dir+1;
		int d2 = temp.dir - 1;
		if (d1 == 8)d1 = 0;
		if (d2 == -1)d2 = 7;
		Q.push(point(temp.x, temp.y, d1));
		Q.push(point(temp.x, temp.y, d2));
	}

}

Then I use DFS of memory, it can be thought of as the last point on a divided split, and a known symmetry axis of the radiation split may be symmetric properties of perpendicular bisectors derive another split beam, and then forward push start from one symmetrical copying, until the entire copy FIG.

Codes

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
struct point {
	int x, y;
	bool operator <(const point & p) const {
		if (x != p.x)return x < p.x;
		else return y < p.y;
	}
};
set<point> P;
int n;
int a[31];

int dx[] = { 0,1,1,1,0,-1,-1,-1 };
int dy[] = { 1,1,0,-1,-1,-1,0,1 };

void dfs(int x, int y, int index,int dir) {
	if (index > n) return;
	
	 
	dfs(x + dx[dir] * a[index], y + dy[dir] * a[index], index + 1 ,(dir + 1) % 8);
	//对称复制
	set<point> temp;
	for (auto &Q : P) {
		if (dir % 4 == 0) {
			temp.insert({ 2 * x - Q.x,Q.y });
		}
		else if (dir % 4 == 1) {
			temp.insert({ x + Q.y - y,y + Q.x - x });
		}
		else if (dir % 4 == 2) {
			temp.insert({Q.x,2 * y -Q.y });
		}
		else if (dir % 4 == 3) {
			temp.insert({ x + y - Q.y,y - Q.x + x });
		}
	}
	P.insert(temp.begin(), temp.end());
	
	for (int i = 0; i < a[index]; i++) {
		x = x + dx[dir];
		y = y + dy[dir];
		P.insert({ x,y });
	}

}
int main() {
	cin.sync_with_stdio(false);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	dfs(0, 0, 0, 0);
	cout << P.size() << endl;
	return 0;
}

Published 21 original articles · won praise 5 · Views 783

Guess you like

Origin blog.csdn.net/weixin_44578615/article/details/104909815