标签:icpc acm acm-icpc 贪心 iostream
2 2 2 11 11 3 3 001 111 101
111 101
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxx = 1000 + 10;
int n, m;
char str[maxx][maxx];
char ans[maxx*maxx];
char ans_str[maxx*maxx];
bool vis[maxx][maxx];
int kjudge;
int dir1[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node1{
    int x;
    int y;
    bool operator < (const node1 & a)const{
        return a.x+a.y > x+y;
    }
};
priority_queue<node1> pp1;
queue<node1> p1;
int dir2[2][2] = {{1,0},{0,1}};
struct node2{
    int x;
    int y;
    int num;
    int id;
    bool operator < (const node2& a) const {
        if(a.num == num) return a.id < id;
        return a.num < num;
    }
};
priority_queue<node2> pp2;
queue<node2> p2;
bool ok(int x,int y){
    if(x<0||x>=n)
        return false;
    if(y<0||y>=m)
        return false;
    return true;
}
bool ok1(int x,int y){
    if(ok(x,y)&&str[x][y] == '0'){
        return true;
    }
    return false;
}
bool ok2(int x,int y){
    if(ok(x,y)&&vis[x][y] == false){
        return true;
    }
    return false;
}
void output(int len){
    int i = 0;
    if(ans_str[0] == '9') i++;
    for(; i < len; ++i){
        printf("%c",ans_str[i]);
    }
    printf("\n");
}
int bfs2(int x,int y){
    node2 k;
    if(str[n-1][m-1] == '9'){
        printf("0\n");
        return -1;
    }
    if(x == n-1 &&y == m-1){
        printf("%c\n",str[n-1][m-1]);
        return -1;
    }
    memset(vis,false,sizeof(vis));
    memset(ans,'_',sizeof(ans));
    k.x = x;
    k.y = y;
    k.num = 1;
    ans[0] = str[k.x][k.y];
    k.id = str[k.x][k.y] - '0';
    vis[x][y] = true;
    pp2.push(k);
    int now = k.num;
    int judge = k.id;
    while(!pp2.empty()){
        node2 q = pp2.top();
        pp2.pop();
        if(now != q.num){
            now = q.num;
            judge = q.id;
        }
        if(judge == q.id){
            for(int i = 0;i < 2; ++i){
                int dx = dir2[i][0]+q.x;
                int dy = dir2[i][1]+q.y;
                if(dx == n-1 && dy == m-1){
                    ans[q.num] = str[dx][dy];
                    if(!kjudge){
                        for(int j = 0;j <= q.num; ++j){
                            ans_str[j] = ans[j];
                        }
                    }else{
                        int mj = 0;
                        for(int j = 0;j <= q.num; ++j){
                            if(ans_str[j] > ans[j]){
                                mj = 1;
                                break;
                            }
                        }
                        if(mj){
                            for(int j = 0;j <= q.num; ++j){
                                ans_str[j] = ans[j];
                            }
                        }
                    }
                    while(!pp2.empty()) pp2.pop();
                    return q.num+1;
                }
                if(ok2(dx,dy)){
                    if(ans[q.num] == '_')ans[q.num] = str[dx][dy];
                    else ans[q.num] = min(ans[q.num],str[dx][dy]);
                    vis[dx][dy] = true;
                    node2 temp;
                    temp.id = str[dx][dy] - '0';
                    temp.x = dx;
                    temp.y = dy;
                    temp.num = q.num+1;
                    pp2.push(temp);
                }
            }
        }
    }
}
void bfs1(){ ///找起点位置
    node1 k;
    k.x = 0;
    k.y = 0;
    kjudge = 0;
    int klen = -1;
    if(str[0][0] == '0') {
        p1.push(k);
        str[0][0] = '9';
        while(!p1.empty()){
            node1 q = p1.front();
            pp1.push(q);
            p1.pop();
            for(int i = 0;i < 4; ++i){
                int dx = dir1[i][0] + q.x;
                int dy = dir1[i][1] + q.y;
                if(ok1(dx,dy)){
                    node1 temp;
                    temp.x = dx;
                    temp.y = dy;
                    str[dx][dy] = '9';
                    p1.push(temp);
                }
            }
        }
        int judge  = pp1.top().x + pp1.top().y;
        while(!pp1.empty()){
            node1 temp = pp1.top();
            if(judge == temp.x+temp.y){
                klen = bfs2(temp.x,temp.y);
            }
            pp1.pop();
        }
    }else{
        klen = bfs2(0,0);
    }
    if(klen != -1)output(klen);
}
int main(){
    int T;
    cin>>T;
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i = 0;i < n; ++i){
            scanf("%s",str[i]);
        }
        bfs1();
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:icpc acm acm-icpc 贪心 iostream
原文地址:http://blog.csdn.net/u012844301/article/details/47186949