UVA 11882 Biggest Number

9 篇文章 0 订阅


题目链接

</pre><pre name="code" class="cpp">#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;

const int maxn = 33;

struct Node {
    int a[maxn], len;
    void clear() {
        len = 0;
    }
    void print() {
        //cout<<len<<endl;
        for(int i = 0; i < len; ++i) printf("%d", a[i]);
        printf("\n");
    }
    bool operator < (const Node &rhs) const {
        if(len != rhs.len) return len < rhs.len;
        for(int i = 0; i < len; ++i)
            if(a[i] != rhs.a[i]) return a[i] < rhs.a[i];
        return false;
    }
};

int fx[] = {1, 0, -1, 0};
int fy[] = {0, 1, 0, -1};

Node ans, now, tmp;
char mat[maxn][maxn];
bool vis[maxn][maxn];
bool vis2[maxn][maxn];
int n, m;
int can[maxn];

int maxlen(int x, int y) {
    queue<int> que; que.push(x * maxn + y);
    int ret = 1;
    can[0] = mat[x][y] - '0';
    memset(vis2, 0, sizeof(vis2));
    vis2[x][y] = 1;
    while(!que.empty()) {
        int tmp = que.front(); que.pop();
        int nx = tmp / maxn, ny = tmp % maxn;
        for(int i = 0; i < 4; ++i) {
            int px = nx + fx[i], py = ny + fy[i];
            if(!isdigit(mat[px][py]) || vis[px][py] || vis2[px][py]) continue;
            vis2[px][py] = true;
            can[ret++] = mat[px][py] - '0';
            que.push(px * maxn + py);
        }
    }
    return ret;
}

void dfs(int x, int y) {
    now.a[now.len++] = mat[x][y] - '0';
    vis[x][y] = true;
    for(int i = 0; i < 4; ++i) {
        int px = x + fx[i], py = y + fy[i];
        if(!isdigit(mat[px][py]) || vis[px][py]) continue;
        int wantlen = maxlen(px, py);
        if(now.len + wantlen < ans.len) continue;
        if(now.len + wantlen == ans.len) {
            sort(can, can + wantlen);
            tmp = now;
            for(int i = wantlen - 1; i >= 0; --i) tmp.a[tmp.len++] = can[i];
            if(tmp < ans) continue;
        }
        dfs(px, py);
    }
    if(ans < now) ans = now;
    --now.len;
    vis[x][y] = false;
}

int main() {
    while(scanf("%d%d", &n, &m) != EOF) {
        if(n + m == 0) break;
        memset(mat, 0, sizeof(mat));
        for(int i = 1; i <= n; ++i) scanf("%s", &mat[i][1]);
        ans.clear(); now.clear();
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j)
                if(isdigit(mat[i][j])) dfs(i, j);
        }
        ans.print();
    }
}



         

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值