week4-CSP Test 1

Cuckoo East adventure

Meaning of the questions:

A ring containing the 26 English letters, arranged clockwise from a to z, a pointer to a, each can be rotated clockwise or counterclockwise a grid. As a clockwise rotation through z, counterclockwise rotation to b. Now there is a string (length <= 10000), output a number of times to obtain the minimum required to rotate the string.

Enter:
Enter the single line is a string.

Output:
the number of outputs at least to turn.


Ideas:

The letter strings for each traversal, the start point b is initialized to 0, the use of letter codes ascII -'a 'so as to become a digital (referred to as a result of c). Recycling function min (absolute value (cb), 26- absolute value (CB)) to find the minimum number of rotation, and then referred to as a new starting point c. Obtained by accumulating the number of rotation of the final result.


Code:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
char s[10010];
int main()
{
	int count=0;
	int b=0;
	int c;
	cin>>s;
	_for(i,0,strlen(s))
	{
		c=s[i]-'a';
		count=count+min(abs(c-b),26-abs(c-b));
		b=c; 
	}
	cout<<count<<endl;
	return 0;
	
}

B - East cuckoo want to eat

Meaning of the questions:

Cuckoo East exam week, and a total of n-day exam Monday. He did not want to exam week so tired, I intended to have a nice dinner every day. He decided to eat fried every day, 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.

Input:
Enter two lines, a first line of the input integer n (1 <= n <= 100000), the number of days of examination weeks.
The second row has the number n, the i-th ai (0 <= ai <= 10000) represents the i-th day the number of fried East buy cooing.

Output:
If you meet the cuckoo East strange request, output "YES", if not met, output "NO". (Output without quotation marks)


Ideas:

1. Is there a coupon determined by determining the final hand to meet the requirements.
2. If the number of the first day to buy fried, fried look to buy from the first day, the day to buy fried is divided by two to get to 1, then prove ticket, do not buy it destroy, take jian next day can be -1. At this judgment the next day, the next day if the number of fried <0, then there is no representative of coupons, do not qualify, then repeat the process until the last day, the last day to buy fried if the remainder obtained with two 0, then no hands voucher, qualified, or have extra hands vouchers, does not meet the conditions. Thereby obtaining a final result
3 since the determination process is not considered good results had 70 points, because of a [n + 1] is not initialized, that determines whether or not less than 0.

	if(a[n+1]<0) cout<<"NO"<<endl;

The revised Code:

#include<iostream>
#include<cstring>
#include<algorithm>
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int a[100001];
int main()
{
	int n;
	cin>>n;
	_for(i,0,n)
	{
		cin>>a[i];
	}
	_for(i,0,n)
	{
		if(a[i]<0)
		{
			cout<<"NO"<<endl;
			exit(0);
		}
		if(a[i]%2==1)
		{
			a[i+1]--;
		}
			
				
	}
	if(a[n-1]%2==0) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}

C - terrible cosmic rays

As we all know, God has reached the Swiss CS undergraduate ceiling, but everyone knows there is day, there are people outside Gou. In the vastness of the universe, there is a biological called Gou dog, the creature born will be able to reach the level of knowledge of human graduate, and naturally good at CSP, and even the first level of the country! But the most frightening thing is that it can emit cosmic rays! Cosmic rays can destroy a person's IQ, down-Chi fight!
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 will emit some distance after splitting, approximately 45 ° to the direction of the two split 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.
Swiss now God wants to bring his little brothers dog challenge Gou, but Rui God do not want their IQ drops so ordinary undergraduate level course, so Rui God to ask you to help him calculate the total number of positions will be "down Chi combat. "

Input:
input of the first line contains a positive integer n (n <= 30), n represents cosmic rays will split views.
The second line contains n positive integers a1, a2 ... an, ai represents the i-th to i-th division number of cosmic rays per unit length of stay on its original direction.

Output:
Output a number ans, indicates how many positions will be reduced intellectual combat.


Ideas:

The problem is the need to extend in several directions at the same time, just as the maze can be four directions around, so you can use DFS to resolve, this time a two-dimensional array can be set to record whether the ray passes, but also to avoid ray duplication. Processing of the current position of each ray direction of cosmic rays will arrive and reach the finish line in both directions after the splinter and then process the other two directions.
The idea is there, but when dealing with the issue because of the time and inexperienced to write too much trouble, there is no better coordinate record, leading through the point will not be repeated, addition is the time complexity is too large.


WA code:

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int a[35];
bool vis[1000][1000]={false};
queue<int> q[35];
int main()
{
	int n;
	int count=0; 
	vis[500][500]=true;
	int x=500,y=500;
	q[0].push(1);
	cin>>n;
	_for(i,0,n)
		cin>>a[i];
	_for(i,0,n)
	{
		while(!q[i].empty())
		{
			switch(q[i].back())
			{
				case 1:
					q[i+1].push(2);
					q[i+1].push(8);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x][y+j]!=true)
						{
							vis[x][y+j]=true;
							count++;
						}
					}
					y=y+a[i];
					q[i].pop();
					break;
				case 2:
					q[i+1].push(1);
					q[i+1].push(3);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x+j][y+j]!=true)
						{
							vis[x+j][y+j]=true;
							count++;
						}
					}
					x=x+a[i];
					y=y+a[i];
					q[i].pop();
					break;
				case 3:
					q[i+1].push(2);
					q[i+1].push(4);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x+j][y]!=true)
						{
							vis[x+j][y]=true;
							count++;
						}
					}
					x=x+a[i];
					q[i].pop();
					break;
				case 4:
					q[i+1].push(3);
					q[i+1].push(5);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x+j][y-j]!=true)
						{
							vis[x+j][y-j]=true;
							count++;
						}
					}
					x=x+a[i];
					y=y-a[i];
					q[i].pop();
					break;
				case 5:
					q[i+1].push(4);
					q[i+1].push(6);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x][y-j]!=true)
						{
							vis[x][y-j]=true;
							count++;
						}
					}	
					y=y-a[i];
					q[i].pop();
					break;
				case 6:
					q[i+1].push(5);
					q[i+1].push(7);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x-j][y-j]!=true)
						{
							vis[x-j][y-j]=true;
							count++;
						}
					}
					x=x-a[i];
					y=y-a[i];
					q[i].pop();
					break;
				case 7:
					q[i+1].push(8);
					q[i+1].push(6);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x-j][y]!=true)
						{
							vis[x-j][y]=true;
							count++;
						}
					}		
					x=x-a[i];
					q[i].pop();
					break;
				case 8:
					q[i+1].push(1);
					q[i+1].push(7);
					for(int j=1;j<=a[i];j++)
					{
						if(vis[x-j][y+j]!=true)
						{
							vis[x-j][y+j]=true;
							count++;
						}
					}	
					x=x-a[i];
					y=y+a[i];
					q[i].pop();
					break;
				default:
					break; 
				}
			}
		}
	cout<<count<<endl;
	return 0;
		
}

No record good location cause an error, and later modified several times failed to get the right to change the program, the students will help the code, read his thoughts, you can set up eight directions as the maze of position

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

While taking advantage of

const int dirction[8]={0,1,2,3,4,5,6,7};

And save them in the structure, can not use this switch
will direction of two adjacent numbers to divide each direction, both directions only need to 0 is set to 1 and 7; 7 adjacent two directions 6 and can be set to 0. Due to time reasons, did not write the correct code, it would need to strengthen the code power, to improve their level.

Released seven original articles · won praise 0 · Views 123

Guess you like

Origin blog.csdn.net/weixin_44465341/article/details/104981368