subtask 7和10两个点没过,求调
查看原帖
subtask 7和10两个点没过,求调
1790845
He2050107267楼主2025/7/23 14:14
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    char arr[1000][1000] = { 0 };
    vector <bool> target(128, false);
    int unique = 0, max_ch_i = 0, max_ch_j = 0;
    char max_ch = 0;
    for (int i = 0;i < n;i++) {
        for (int j = 0;j < m;j++) {
            cin >> arr[i][j];
            if (arr[i][j] != '#' && !target[arr[i][j]]) {
                target[arr[i][j]] = true;
                unique++;
                if (arr[i][j] > max_ch) {
                    max_ch = arr[i][j];
                    max_ch_i = i;
                    max_ch_j = j;
                }
            }
        }
    }

    if (unique == 1) {
        cout << max_ch << endl;
        return 0;
    }

    int count = 0;
    char current_ch = max_ch;
    int current_i = max_ch_i, current_j = max_ch_j;
    while (count < unique) {
        cout << current_ch;
        char shang = arr[current_i - 1][current_j];
        char xia = arr[current_i + 1][current_j];
        char zuo = arr[current_i][current_j - 1];
        char you = arr[current_i][current_j + 1];

        char tmp_max = 0;
        int tmp_i = 0, tmp_j = 0;
        if (shang > tmp_max && shang != '#') {
            tmp_max = shang;
            tmp_i = current_i - 1;
            tmp_j = current_j;
        }
        if (xia > tmp_max && xia != '#') {
            tmp_max = xia;
            tmp_i = current_i + 1;
            tmp_j = current_j;
        }
        if (zuo > tmp_max && zuo != '#') {
            tmp_max = zuo;
            tmp_i = current_i;
            tmp_j = current_j - 1;
        }
        if (you > tmp_max && you != '#') {
            tmp_max = you;
            tmp_i = current_i;
            tmp_j = current_j + 1;
        }
        current_ch = tmp_max;
        current_i = tmp_i;
        current_j = tmp_j;
        if (current_ch == max_ch)
            return 0;
        else
            count++;
    }
    return 0;
}
2025/7/23 14:14
加载中...