Programming WEEK4 CSP simulation

Programming WEEK4 CSP simulation

Question A: The Adventures of Coodong

Gu Gu Dong is a playful child, one day, he got a magic ring from the ancient ruins. This ring is composed of alphabets end to end. There is a pointer on the ring, which initially points to the letter a. Gugudong can rotate clockwise or counterclockwise one frame at a time. For example, a rotates clockwise to z and counterclockwise to b. Gu Gudong has a string in his hand, but he is too stupid, so he came to ask for your help and asked how many times he had to turn at least.
Insert picture description here

1. Sample Input and Output

Input

hzet

Output

31

2. Problem-solving ideas and codes

Calculate the clockwise spacing and counterclockwise spacing of each letter and the previous letter, whichever is the number of rotations. The sum is the total number of rotations.
code show as below:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int time=0;
    string str;
    cin>>str;
    int size=str.size();
    for(int i=0;i<size;i++)
    {
        int a,b;
        if(i==0)
        {
            a=1;
            b=str[0]-96;
        } else{
            a=str[i]-96;
            b=str[i-1]-96;
        }
        int tmp=a-b;
        if(tmp<0)
            tmp=tmp+26;
        int tmp1=b-a;
        if(tmp1<0)
            tmp1=tmp1+26;
        if(tmp<=tmp1)
            time = time + tmp;
        else
            time = time + tmp1;
    }
    cout<<time<<endl;
    return 0;
}

B: Gugudong wants to eat

The Gugudong test week started, and there are n days on Monday. He didn't want to be so tired during the exam week, so he planned to eat well every day. He decided to eat raw fried every day, and Gugudong needs to buy ai raw fried every day. However, in order to stimulate consumption, there are only two ways to buy shengjian: ① buy two shengjian at a time on a certain day. ② Buy a raw fried today, and buy a raw fried for tomorrow, the store will give a voucher, the next day to use the voucher to get. There are no other purchase methods, these two purchase methods can be used countless times, but Gu Gudong is a frugal good boy, he left after training, not allowed to have a voucher at the end of training. Gu Gu Dong is very rich, you don't need to worry about Gu Gu Dong being rich, but Gu Gu Dong is too stupid, he wants to ask you if he can just buy ai shengjian every day during the exam week. Among them (1≤n≤100000), (1≤ai≤10000)

1. Sample Input and Output

Input

4
1 2 1 2 

Output

2

2. Problem-solving ideas and codes

Since there are only two buying methods, either buy two at once or buy one each today and tomorrow.
Therefore, the daily purchase situation can be traversed from front to back and processed as follows: If the current day's purchase situation is an odd number, then the next day's purchase quantity is reduced by 1. Once the traversed data shows a negative number or the number of purchases on the last day is still an odd number, the purchase plan is not feasible. Otherwise it is feasible.

code show as below:

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int * p;
    p=new int[n];
    for(int i=0;i<n;i++) {
        cin >> p[i];
    }
    for(int i=0;i<n-1;i++)
    {
        if(p[i]<0)
        {
           cout<<"NO"<<endl;
           return 0;
        }
        if(p[i]%2==1)
            p[i+1]--;
    }
    if(p[n-1]%2==1)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    cout<<"YES"<<endl;
    return 0;
}

Question C terrible cosmic rays

As we all know, Raytheon has reached the ceiling of CS undergraduates, but they do not know that there is heaven and there are people outside. In the vast universe, there is a creature called Gou Gou, which is born to reach the level of human graduate knowledge, and is naturally good at CSP, even the first level in the country! But the most terrible thing is that it can Send out cosmic rays! Cosmic rays can destroy people's IQ and carry out intelligence reduction strikes!
Cosmic rays will spread on an infinite two-dimensional plane (it can be seen as a two-dimensional grid map), and the initial direction is upward by default. The cosmic ray will split after a certain distance is emitted,
splitting two cosmic rays in the direction of 45∘ 45 ∘ to the left and right of the direction, and the power will not change at the same time! Unit length.
Now Ruishen has to take his younger brothers to challenge Gou Go, but Ruishen does not want to lower his IQ to the level of ordinary undergraduate zjm. Reduce intelligence to combat ".

Insert picture description here

1. Sample Input and Output

Input

The first line n is the number of splits, the
second line n is the distance traveled by each split

4
4 2 3 2

Output

Output the number of hit points
39
schematic diagram

2. Problem-solving ideas and codes

The first thing that comes to mind during the game is direct DFS, and then use a two-dimensional array to store the points reached. But TLE, only 4 points passed.
Code:

#include<iostream>
#include<algorithm>
using namespace std;
class point
{
public:
    point(){}
    point(int a,int b)
    {
        _a=a;
        _b=b;
    }
public:
    int _a;
    int _b;
};
int dx[]={0,1,1,1,0,-1,-1,-1};
int dy[]={1,1,0,-1,-1,-1,0,1};
int n;
int *p;
int map[600][600]={0};
int pathlength=0;

void addlength(point start,int direaction,int length,int i)
{
    if(i==n)
        return ;
    point newpoint(start._a,start._b);
    for(int j=0;j<length;j++)
    {
        newpoint._a=newpoint._a+dx[direaction];
        newpoint._b=newpoint._b+dy[direaction];
        if(map[newpoint._a+300][newpoint._b+300]!=0)
            continue;
        else {
            pathlength++;
            map[newpoint._a+300][newpoint._b+300]=1;
        }
    }
    int direaction1=(direaction+1)%8;
    int direaction2=(direaction+7)%8;
    addlength(newpoint,direaction1,p[i+1],i+1);
    addlength(newpoint,direaction2,p[i+1],i+1);
}

int main()
{
    cin>>n;
    p=new int[n];
    for(int i=0;i<n;i++)
    {
        cin>>p[i];
    }
    addlength({0,0},0,p[0],0);
    cout<<pathlength<<endl;
}

There are currently two feasible solutions to this problem, one is memory search, and the other is to use symmetry to reduce a split process. In contrast, it is better to think of memory-less search codes.
Since the range of the graph is small (300 300), there are 8 directions for each grid. When searching in the same layer, record the direction and starting point of the extension. If there are points with the same search direction and in the same position, you can skip this search. The record array is 150 300 300 8.
Paste the standard answer code:

#include<iostream>
using namespace std;
int vis[330][330][31][8];
int mp[330][330];
int a[35];
int n,ans;
int dir[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

void dfs(int x,int y,int cnt,int d)
{
    if(vis[x][y][cnt][d]) return ;
    vis[x][y][cnt][d]=1;
    for(int i=1;i<=a[cnt];i++) {
        x += dir[d][0], y += dir[d][1];
        if (!mp[x][y]) ans++, mp[x][y] = 1;
    }
        if(cnt<n)
        {
            dfs(x,y,cnt+1,(d+1)%8);
            dfs(x,y,cnt+1,(d+7)%8);
        }
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    dfs(160,160,1,2);
    cout<<ans<<endl;
    return 0;
}
Published 8 original articles · Likes2 · Visits 252

Guess you like

Origin blog.csdn.net/lawrenceY/article/details/104981434