Summer Vacation Algorithm 7.14, Day13

Summer Vacation Algorithm 7.14, Day13

simulation

first question

Binary Summation

class Solution {
public:
    string addBinary(string a, string b) {
        int t = 0;
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());
        string ans;
        for(int i = 0;i < a.length() || i < b.length();i++)
        {
            if(i < a.length()){
                t+=a[i]-'0';
            }
            if(i < b.length()){ 
                t+=b[i]-'0';
            }
            if(t >=2)
            {
                ans += to_string(t%2);
                 t = 1;
            }
            else
            {
                ans += to_string(t);
                t  = 0;
            }
        }
        if(t) ans+= '1';
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

Question 2

Spiral matrix

This topic was also written on the school’s oj, and the core code was copied directly.

However, I also changed some places, replacing the array with a vector container, which also borrowed from the blog of a big guy in the group.

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        int top = 0, bottom = m-1, left = 0, right = n-1;
        int cnt = m*n;
        vector<int> res;
        while(cnt>=1){
            for(int j=left; j<=right && cnt>=1; j++, cnt--)
                res.push_back(matrix[top][j]);
            top++;
            //from top to bottom 
            for ( int i=top; i<=bottom && cnt>= 1 ; i++, cnt--)
                res.push_back(matrix[i][right]);
            right--;
            // right to left 
            for ( int j=right; j>=left && cnt>= 1 ; j--, cnt--)
                res.push_back(matrix[bottom][j]);
            bottom--;
            //from bottom to top 
            for ( int i=bottom; i>=top && cnt>= 1 ; i--, cnt--)
                res.push_back(matrix[i][left]);
            left++;
        }
        return res;
    }
};

Question 3

Simulate a walking robot

This question is too hard, too hard, torn apart. .

Look at other people’s code. . . I don’t quite understand yet.

class Solution
{
public:
    int robotSim(vector<int>& commands, vector<vector<int>>& obstacles)
    {
        set<pair<int, int>> st;
        for(vector<int>&vec:obstacles)
        {
            st.insert(pair<int,int>(vec[0],vec[1]));
        }
        int dx[4]= {0,1,0,-1};
        int dy[4]= {1,0,-1,0};
        int i=0;
        int max_len=0;
        int x=0;
        int y=0;
        for(int& com:commands)
        {
            if(com==-1)
            {
                i=(i+1)%4;
            }
            else  if (com== -2 )
            {
                i=(i+3)%4;
            }
            else
            {
                for(int s=0; s<com; ++s)
                {
                    if(!st.count(pair<int,int>(x+dx[i],y+dy[i])))
                    {
                        x+=dx[i];
                        y+=dy[i];
                        max_len=max(max_len,x*x+y*y);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        return max_len;
    }
};

Question 4

C. Cypher

Let’s not talk about it, the last question is completely closed… let’s put it away…, this question is just a simple simulation, 0 and 9 are specially judged, and the rest are ++either --.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cin>>n;
        int a[n+5];
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
        }
        string str;
        for(int i=0; i<n; i++)
        {
            string str2;
            int m;
            cin>>m>>str2;
            int cnt=a[i];
            for(int j=0; j<m; j++)
            {
                if(str2[j]=='U')
                {
                    cnt--;
                    if(cnt==-1)
                    {
                        cnt=9;
                    }
                }
                else
                {
                    cnt++;
                    if(cnt==10)
                    {
                        cnt=0;
                    }
                }
            }
            str+=cnt+'0';
        }
        for(int i=0; i<str.size(); i++)
        {
            cout<<str[i]<<' ';
        }
        cout<<endl;
    }
    return 0;
}

It's really cracked today... let's take a day off and let yourself have a day off.
People always say that it will get better in the future, but you need to fight for it yourself! !

Leave a Comment

Your email address will not be published. Required fields are marked *