Programming thinking CSP-M1

A-Gugudong's Adventure

topic

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.

schematic diagram

Insert picture description here

Ideas

Make the diameter of the current point past the center of the circle and divide the ring into two halves. If it is in the left half, turn clockwise; if in the right half, turn counterclockwise. That is, the absolute value of the ascii code difference between two characters is considered. If it is less than 13, it is clockwise; if it is greater than 13, it is counterclockwise;

Code

#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {
    string s;
    cin>>s;
    int begin=0,sum=0;
    for(int i=0;i<s.length();i++){
        int tmp=abs(begin-(s[i]-97));
        if(tmp<13){
            sum+=tmp;
        }
        else{
            sum+=(26-tmp);
        }
        begin=s[i]-97;
    }
    cout<<sum;
    return 0;
}

B-Gugudong wants to eat

topic

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.

Ideas

Because it is not allowed to have a voucher in the hand at the end, try to not use the second option when buying raw fried, then the number of vouchers in the hand is either 1 or 0. Consider the day i, if the raw fried bought on day i is 0:

  1. If you have a voucher in your hand, the voucher is wasted.
  2. If there is no coupon in hand, you can continue.

If the raw fried bought on day i is an even number:

  1. If you have a voucher in your hand, use one voucher. The number of raw fried foods bought becomes an odd number. If you use plan two at a time and use plan one for the rest, you will still have a coupon.
  2. There are no coupons in hand, and all of the second use schemes are still without coupons.

If the raw fried bought on day i is an odd number:

  1. If you have a voucher in your hand, use one voucher. The number of raw fried dishes bought becomes an even number, all of them use the second option, and no coupons in hand.
  2. No coupons in hand. Use plan one at a time, and use plan two at a time.

Code

#include <iostream>
using namespace std;

int main() {
    int n,num,quan=0;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>num;
        if(num<0){
            cout<<"NO";
            return 0;
        }
        else if(num==0){
            if(quan!=0){
                cout<<"NO";
                return 0;
            }
        }
        else if(num%2==1){
            if(quan==1)
                quan=0;
            else
                quan=1;
        }
    }
    if(quan!=0)
        cout<<"NO";
    else
        cout<<"YES";
    return 0;
}

to sum up

At the beginning, there was a test point because I thought I was wrong to buy 0 pieces of fried rice. I thought that as long as I bought 0 pieces of fried rice, the coupons are wasted but you can still continue.

C- terrible cosmic rays

topic

Cosmic rays will propagate on an infinite two-dimensional plane (it can be seen as a two-dimensional grid), 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 ° to the left and right of this direction, and the power will remain unchanged! The cosmic ray will split twice, and after each split, it will advance one unit length in the splitting direction.
Now Ruishen will take his younger brothers to challenge Gougou, but Ruishen does not want to lower his IQ to the level of ordinary undergraduates, so Ruishen will ask you to help him calculate how many positions will be dropped Wise blow ".

Ideas

There are up to 300 * 300 points covered by rays, memory dfs, and feasible pruning. Use a four-dimensional array to record the starting point of each split, and use a two-dimensional array to record the points covered by the rays. When finding the direction after splitting, you can find the modulus, or define two direction arrays to record the direction of the ray after splitting.

Code

#include <cstdio>
#include <algorithm>
using namespace std;

int n,ans=0;
int dx[]={0,-1,-1,-1,0,1,1,1};
int dy[]={1,1,0,-1,-1,-1,0,1};
int length[35];
bool vis[310][310],flag[35][305][305][7];

void dfs(int s,int x,int y,int dir){
    if(s>n)//分裂次数超过n
        return;
    if(flag[s][x][y][dir])
        return;
    flag[s][x][y][dir]=true;
    for(int i=1;i<=length[s-1];i++){
        if(!vis[x+i*dx[dir]][y+i*dy[dir]]){
            vis[x+i*dx[dir]][y+i*dy[dir]]=true;
            ans++;
        }
    }
    dfs(s+1, x+length[s-1]*dx[dir], y+length[s-1]*dy[dir], (dir+1)%8);
    dfs(s+1, x+length[s-1]*dx[dir], y+length[s-1]*dy[dir], (dir+7)%8);
}

int main() {
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&length[i]);
    }
    memset(vis,false,sizeof(vis));
    memset(flag, false, sizeof(flag));
    dfs(1, 155, 155, 0);
    printf("%d",ans);
    return 0;
}

to sum up

At first I didn't think of a better way to deal with the direction after the split, it was very troublesome to write, but later I thought of taking the model.

Published 24 original articles · praised 2 · visits 435

Guess you like

Origin blog.csdn.net/weixin_43805228/article/details/104980015