求助!!卡在最后一个测试点#10
查看原帖
求助!!卡在最后一个测试点#10
447573
zevan楼主2024/8/2 15:08

卡在#10最后一个点上了

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define SZ(v) ((int)v.size())
#define pii pair<int, int>
#define int long long
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef double db;
using namespace std;
const int N = 2e5+10;
const int mod = 1e9+7;
int _;

int n, m;
char a[1005][1005];

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

void solve() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if(a[i][j] != '#') {
                string t = "";
                t.pb(a[i][j]);
                if(ans < t) {
                    ans = t;
                }
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] != '#') {
                string t = "";
                t.pb(a[i][j]);
                if(t > ans) {
                    ans = t;
                }
                for(int k = 1; k <= 4; k++) {
                    int nx = i + dx[k];
                    int ny = j + dy[k];
                    if(nx > n || ny > m || nx < 1 || ny < 1) continue;
                    if(a[nx][ny] == '#') continue;
                    t.pb(a[nx][ny]);
                }
                if(t > ans) {
                    ans = t;
                }
            }
        }
    }
    
    if(SZ(ans) > 1) {
        if(ans[0] == ans[1]) {
            cout << ans[0] << "\n";
        } else {
            cout << ans << "\n";
        }
    } else {
        cout << ans << "\n";
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    _ = 1;
    // cin >> _;
    
    while(_--) {
        solve();
    }
    
    return 0;
}
2024/8/2 15:08
加载中...