头像

Baymax0211




离线:3天前


最近来访(1)
用户头像
乌搏猿

活动打卡代码 AcWing 1113. 红与黑

Baymax0211
6个月前
#include <iostream>
#include <queue>

using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;

const int N = 25;

int n, m;

char g[N][N];

int bfs(int x, int y) {
    queue<PII>q;
    q.push({x, y});
    g[x][y] = '#';

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

    while (q.size()) {
        auto t = q.front();
        q.pop();
        res++;
        for (int i = 0; i < 4; i++) {
            int tx = t.x + dx[i], ty = t.y + dy[i];
            if (tx < 0 || tx >= n || ty < 0 || ty >= m || g[tx][ty] != '.') {
                continue;
            }
            g[tx][ty] = '#';
            q.push({tx, ty});
        }


    }
     return res;
}

int dfs(int x, int y) {
    int res = 1;

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

    for (int i = 0; i < 4; i++) {
        int tx = x + dx[i], ty = y + dy[i];
        if (tx < 0 || tx >= n || ty < 0 || ty >= m || g[tx][ty] != '.') {
            continue;
        }
        g[tx][ty] = '#';
        res += dfs(tx, ty);
    }
    return res;
}

int main() {
    while (cin >> m >> n, n || m) {
        for (int i = 0; i < n; i++) cin >> g[i];
        int x, y;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (g[i][j] == '@') {
                    x = i;
                    y = j;
                }
            }
        }

        cout << dfs(x, y) << endl;
    }
    return 0;
}


活动打卡代码 AcWing 756. 蛇形矩阵

Baymax0211
6个月前
#include <iostream>

using namespace std;

const int N = 101;
int n, m;
int q[N][N];

int main() {
    cin >> n >> m;

    int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};
    int d = 0;
    int x = 0;
    int y = 0;
    for (int i = 1; i <= n*m; i ++) {
        q[x][y] = i;
        int tx = x + dx[d];
        int ty = y + dy[d];

        if(tx >= n || tx < 0 || ty >= m || ty < 0 || q[tx][ty]) {
            d = (d + 1) % 4;
            tx = x + dx[d];
            ty = y + dy[d];
        }
        x = tx;
        y = ty;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << q[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}


活动打卡代码 AcWing 898. 数字三角形

Baymax0211
6个月前
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 501;
int n;
//int w[N][N];
int f[N][N];

int main () {
    cin >> n;

    for (int i = 1; i <= n; i++)  {
        for (int j = 1; j <= i; j++) {
            //cin >> w[i][j];
            cin >> f[i][j];
        }
    }

    // for (int i = 1; i <= n; i++) {
    //     f[n][i] = w[n][i];
    // }

    for (int i = n - 1; i ; i--) {
        for (int j = 1; j <= i; j++) {
            //f[i][j] = max(f[i + 1][j] , f[i + 1][j + 1]) + w[i][j];
            f[i][j] += max(f[i + 1][j] , f[i + 1][j + 1]);
        }
    }
    cout << f[1][1];

    return 0;
}


活动打卡代码 AcWing 104. 货仓选址

Baymax0211
6个月前
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100000;

int n;
int a[N];

int main () {
    cin >> n;
    for (int i = 0; i < n ; i++) {
        cin >> a[i];
    }
    sort(a, a + n);
    int res = 0;

    for (int i = 0; i < n; i++) {
        res += abs(a[i] - a[n / 2]);
    }
    cout << res;
}




Baymax0211
7个月前
class Solution {
public:
    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {
        int n = nums.size();
        vector<int>sum(n + 1, 0);    
        for (int i = 0; i < n; i++) {
            sum[i + 1] = sum[i] + nums[i];    
        }

        vector<int> res;
        for (int i = 0; i < n; i++) {
            int tmp1 = (i + 1 - 0) * nums[i]  - sum[i + 1] - sum[0];
            int tmp2 = sum[n] - sum[i + 1] - (n - i - 1) * nums[i];
            res.push_back(tmp1 + tmp2);
        }

        return res;
    }
};



Baymax0211
8个月前
#define  N  100000
class Solution {

public:
    int waysToMakeFair(vector<int>& nums) {
        int n = nums.size(); 
        int sum1[N] = {0};
        int sum2[N] = {0};
        int index1 = 1;
        int index2 = 1;
        for (int i = 0; i < n; i += 2) {
            sum1[index1] = sum1[index1 - 1] + nums[i];
            index1++;
        }
        for (int i = 1; i < n; i += 2) {
            sum2[index2] = sum2[index2 - 1] + nums[i];
            index2++;
        }        
        int res = 0;
        for (int i = 1; i <= n ; i++) {
            int tmp1, tmp2;
            if(i % 2 == 1 ) {
                tmp1 = sum1[i/2] + sum2[index2 - 1] - sum2[i/2];
                tmp2 = sum2[i/2] + sum1[index1 - 1] - sum1[i/2 + 1];
            } else {
                tmp1 = sum1[i/2] + sum2[index2 - 1] - sum2[i/2 ];
                tmp2 = sum2[i/2 - 1] + sum1[index1 - 1] - sum1[i/2];
            }
            if (tmp1 == tmp2) {
                res++;
            }
        }
        return res;
    }
};



Baymax0211
8个月前
class Solution {
public:
    string getSmallestString(int n, int k) {

        string res;
        int tmp = 26;
        for (int i = 1; i <= n; i++) {
            int leave = k - tmp;
            if (leave > n - i ) {
                res.push_back('a' + tmp - 1);
            } else {
                while (leave < n - i) {
                    tmp--;
                   leave = k - tmp;
                }
                res.push_back('a' + tmp - 1);
            }
            k = leave;
        }
        reverse(res.begin(), res.end());
        return res;
    }
};



Baymax0211
8个月前
class Solution {
public:
    bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
        string a = "";
        string b = "";
        for (auto &x : word1) a += x;
        for (auto &y :word2) b += y;

        return a == b;
    }
};


活动打卡代码 LeetCode 9. 回文数

Baymax0211
9个月前
bool isPalindrome(int x){

    int m[100] = {};
    int i = 0;
    int j = 0;
    if(x<0)
        {return false;}

    while(x){
        m[i] = x%10;
        x = x/10;
        i += 1;
    }
    while(j!=i/2){
        if(m[j]!=m[i-1-j])
            return false;
        j += 1;
    }
    return true;
}




Baymax0211
9个月前
int myAtoi(char* str) {
    long ret = 0;
    int flag = 1;//默认正数
    //去除空格及判断符号位
    for (; *str == 32; str++);
    switch (*str) {
    case 45:
        flag = -1;
    case 43:
        str++;
    }
    //排除非数字的情况
    if (*str < 48 || *str>57) return 0;

    while (*str >= 48 && *str <= 57) {
        ret = ret * 10 + (*str - 48);
        //判断溢出
        if ((int)ret != ret) {
            return (flag == 1) ? (INT_MAX) : (INT_MIN);
        }
        str++;
    }
    ret *= flag;
    return (int)ret;
}