《算法集训传送门》
?引言
铭记于心 | ||
---|---|---|
?✨?我唯一知道的,便是我一无所知?✨? |
? ❄️我们的算法之路❄️?
众所周知,作为一名合格的程序员,算法 能力 是不可获缺的,并且在算法学习的过程中我们总是能感受到算法的✨魅力✨。
☀️?短短几行代码,凝聚无数前人智慧;一个普通循环,即是解题之眼?☀️
?二分,?贪心,?并查集,?二叉树,?图论,?深度优先搜索(dfs),?宽度优先搜索(bfs),?数论,?动态规划等等, 路漫漫其修远兮,吾将上下而求索! 希望在此集训中与大家共同进步,有所收获!!!???
今日主题:模拟
?⭐️第一题?
✨题目
✨思路:
思路
✨代码:
// class Solution {
// public:
// string addBinary(string a, string b) {
// char *t1,*t2;
// int A=strtol(a.c_str(),&t1,2);
// int B=strtol(b.c_str(),&t2,2);
// int c=A+B;
// string ans="";
// if(c==0) return "0";
// while(c){
// if(c&1)ans+='1';
// else ans+='0';
// c>>=1;
// }
// reverse(ans.begin(),ans.end());
// return ans;
// }
// };
class Solution {
public:
string addBinary(string a, string b) {
int al = a.size();
int bl = b.size();
while(al < bl)
{
a = '0' + a;
++ al;
}
while(al > bl)
{
b = '0' + b;
++ bl;
}
for(int j = a.size() - 1; j > 0; -- j)
{
a[j] = a[j] - '0' + b[j];
if(a[j] >= '2')
{
a[j] = (a[j] - '0') % 2 + '0';
a[j-1] = a[j-1] + 1;
}
}
a[0] = a[0] - '0' + b[0];
if(a[0] >= '2')
{
a[0] = (a[0] - '0') % 2 + '0';
a = '1' + a;
}
return a;
}
};
?⭐️第二题?
✨题目
✨思路:
封装四个方向在一个数组里,设定如果不碰壁就不更改方向,一直走往前走(需要加一个二维标记数组)
✨代码:
class Solution {
private:
static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return {};
}
int rows = matrix.size(), columns = matrix[0].size();
vector<vector<bool>> visited(rows, vector<bool>(columns));
int total = rows * columns;
vector<int> order(total);
int row = 0, column = 0;
int directionIndex = 0;
for (int i = 0; i < total; i++) {
order[i] = matrix[row][column];
visited[row][column] = true;
int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
directionIndex = (directionIndex + 1) % 4;
}
row += directions[directionIndex][0];
column += directions[directionIndex][1];
}
return order;
}
};
?⭐️第三题?
✨题目
✨思路:
经典模拟
✨代码:
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int x = 0, y = 0, di = 0;
unordered_set<pair<int, int>> obstacleSet;
for (vector<int> obstacle: obstacles)
obstacleSet.insert(make_pair(obstacle[0], obstacle[1]));
int ans = 0;
for (int cmd: commands) {
if (cmd == -2)
di = (di + 3) % 4;
else if (cmd == -1)
di = (di + 1) % 4;
else {
for (int k = 0; k < cmd; ++k) {
int nx = x + dx[di];
int ny = y + dy[di];
if (obstacleSet.find(make_pair(nx, ny)) == obstacleSet.end()) {
x = nx;
y = ny;
ans = max(ans, x*x + y*y);
}
}
}
}
return ans;
}
};
?⭐️第四题?
✨题目
✨代码:
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n; i++)
{
int b;
cin >> b;
if(b == 0)
{
continue;
}
string now;
cin >> now;
for(int j = 0; j < b; j++)
{
if(now[j] == 'U'){a[i]--;}
else if(now[j] == 'D'){a[i]++;}
if(a[i] < 0){a[i]+=10;}
if(a[i] > 9){a[i]-=10;}
}
}
for(int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main(){
int t;
cin>> t;
while(t--)
{
solve();
}
return 0;
}
?写在最后?:
相信大家对今天的集训内容的理解与以往已经有很大不同了吧,或许也感受到了算法的魅力,当然这是一定的,路漫漫其修远兮,吾将上下而求索!伙伴们,明天见!???
本文含有隐藏内容,请 开通VIP 后查看