1. 程式人生 > >有一個n*m的格子,求起點到終點的最小字典序

有一個n*m的格子,求起點到終點的最小字典序


#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
int mp[1010][1010];
int vis_front[1010][1010];
int vis[1010][1010];

const int BUF=40000010;
char Buf[BUF],*buf=Buf;

inline void read(int&a){
    for(a=0;*buf<48;buf++);
    while(*buf>47)
        a=a*10+*buf++-48;}

int main(){
    fread(Buf,1,BUF,stdin);
    int t;
    read(t);
    int n,m;
    while(t--){
        read(n);
        read(m);
        int x,y;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                read(mp[i][j]);
        mem0(vis_front);
        if(mp[1][1]==0||mp[n][m]==0){
            printf("Oh,the life is too difficult!\n");
            continue;
        }
        for(int i=1;i<=n;i++)           //從前走到後
            for(int j=1;j<=m;j++){
                if(i==1&&j==1){
                    vis_front[1][1]=1;
                }
                else if(i==1){
                    if(mp[i][j]!=0&&vis_front[i][j-1])
                        vis_front[i][j]=1;
                }
                else if(j==1){
                    if(mp[i][j]!=0&&vis_front[i-1][j])
                        vis_front[i][j]=1;
                }
                else{
                    if(mp[i][j]!=0&&(vis_front[i-1][j]||vis_front[i][j-1]))
                        vis_front[i][j]=1;
                }
            }
        if(vis_front[n][m]==0){
            printf("Oh,the life is too difficult!\n");
            continue;
        }
        mem0(vis);
        for(int i=n;i>=1;i--)
            for(int j=m;j>=1;j--){
                if(i==n&&j==m){
                    vis[n][m]=1;
                }
                if(vis_front[i][j]==1&&vis[i][j]==1){
                    if(vis_front[i-1][j]==1)
                        vis[i-1][j]=1;
                    if(vis_front[i][j-1]==1)
                        vis[i][j-1]=1;
                }
            }
        printf("%d",mp[1][1]);
        for(int i=1;i<n+m-1;i++){
            if(i==1){
                x=y=1;
            }
            if(x+1<=n&&y+1<=m){
                if(vis[x][y+1]==1&&vis[x+1][y]==1){
                    if(mp[x][y+1]<mp[x+1][y]){
                        printf(" %d",mp[x][y+1]);
                        y++;
                    }
                    else{
                        printf(" %d",mp[x+1][y]);
                        x++;
                    }
                }
                else if(vis[x][y+1]==1){
                    printf(" %d",mp[x][y+1]);
                    y++;
                }
                else if(vis[x+1][y]==1){
                    printf(" %d",mp[x+1][y]);
                    x++;
                }
            }
            else if(x+1<=n&&vis[x+1][y]==1){
                    printf(" %d",mp[x+1][y]);
                    x++;
            }
            else if(y+1<=n&&vis[x][y+1]==1){
                    printf(" %d",mp[x][y+1]);
                    y++;
            }
        }
        printf("\n");
    }
    return 0;
}