其他分享
首页 > 其他分享> > poj 3083(不是特别难,但是很麻烦)

poj 3083(不是特别难,但是很麻烦)

作者:互联网

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int k,t,w,h,sx,sy,sum,ex,ey;
int idx[45][45],data[45][45],ans[3];
char c[45][45];
void print(){
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cout<<data[i][j]<<"\t";
        }
        cout<<endl;
    }
    cout<<endl;
}
void dfs_shortest(int i,int j){
    int r,c;
    if(i<0||j<0||i>=h||j>=w)return;
    if(idx[i][j]>k&&data[i][j]!=0){
        idx[i][j] = k;
    }
    if(data[i][j] == -2)return;
//    print();
    k++;
    for(r=-1;r<=1;r++)
        for(c=-1;c<=1;c++)
            if((r*c==0)&&!(r==0&&c==0))
                if(i+r>=0&&i+r<h&&j+c>=0&&j+c<w)
                    if(idx[i+r][j+c]>k&&data[i+r][j+c]!=0){
                        idx[i+r][j+c] = k;
                        dfs_shortest(i+r,j+c);
                    }
    k--;
}
bool dfs_left(int direction,int i,int j){//direction=1向东direction=2向南direction=3向西direction=4向北
    int r,c;
//    cout<<i<<" "<<j<<" "<<k<<endl;
//    system("pause");
    if(i<0||j<0||i>=h||j>=w)return false;
    if(data[i][j]==0)return false;
    if(data[i][j]==-2)return true;
    k++;
    if(direction==1){
        if(dfs_left(4,i-1,j))
            return true;
        if(dfs_left(1,i,j+1))
            return true;
        if(dfs_left(2,i+1,j))
            return true;
        if(dfs_left(3,i,j-1))
            return true;
    }
    if(direction==2){
        if(dfs_left(1,i,j+1))
            return true;
        if(dfs_left(2,i+1,j))
            return true;
        if(dfs_left(3,i,j-1))
            return true;
        if(dfs_left(4,i-1,j))
            return true;
    }
    if(direction==3){
        if(dfs_left(2,i+1,j))
            return true;
        if(dfs_left(3,i,j-1))
            return true; 
        if(dfs_left(4,i-1,j))
            return true;
        if(dfs_left(1,i,j+1))
            return true;
    }
    if(direction==4){
        if(dfs_left(3,i,j-1))
            return true;
        if(dfs_left(4,i-1,j))
            return true;
        if(dfs_left(1,i,j+1))
            return true;
        if(dfs_left(2,i+1,j))
            return true;
    }
    return false;
}
bool dfs_right(int direction,int i,int j){
    if(i<0||j<0||i>=h||j>=w)return false;
    if(data[i][j]==0)return false;
    if(data[i][j]==-2)return true;
    k++;
    if(direction==1){
        if(dfs_right(2,i+1,j))
            return true;
        if(dfs_right(1,i,j+1))
            return true;
        if(dfs_right(4,i-1,j))
            return true;
        if(dfs_right(3,i,j-1))
            return true;
    }
    if(direction==2){
        if(dfs_right(3,i,j-1))
            return true;
        if(dfs_right(2,i+1,j))
            return true;
        if(dfs_right(1,i,j+1))
            return true;
        if(dfs_right(4,i-1,j))
            return true;
    }
    if(direction==3){
        if(dfs_right(4,i-1,j))
            return true;
        if(dfs_right(3,i,j-1))
            return true;
        if(dfs_right(2,i+1,j))
            return true;
        if(dfs_right(1,i,j+1))
            return true;
    }
    if(direction==4){
        if(dfs_right(1,i,j+1))
            return true;
        if(dfs_right(4,i-1,j))
            return true;
        if(dfs_right(3,i,j-1))
            return true;
        if(dfs_right(2,i+1,j))
            return true;
    }
    return false;
}
int main(){
    int i,j;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&w,&h);
        memset(data,0,sizeof(data));
        for(i=0;i<h;i++){
            scanf("%s",c[i]);
            for(j=0;j<w;j++)
                if(c[i][j]=='.'){
                    data[i][j] = 1;
                }
                else if(c[i][j]=='S'){
                    data[i][j] = -1;
                    sx = i;
                    sy = j;
                }
                else if(c[i][j]=='E'){
                    data[i][j] = -2;
                    ex = i;
                    ey = j;
                }
        }
        sum = 1000000;
        memset(idx,0x7f,sizeof(idx));
        k = 1;
        dfs_shortest(sx,sy); 
        ans[2] = idx[ex][ey];
        k = 1;
        if(sx==0)
            dfs_left(1,0,sy);
        else if(sy==0)
            dfs_left(4,sx,0);
        else if(sx==h-1)
            dfs_left(3,h-1,sy);
        else if(sy==w-1)
            dfs_left(2,sx,w-1);
        ans[0] = k;
        k = 1;
        if(sx==0)
            dfs_right(3,0,sy);
        else if(sy==0)
            dfs_right(2,sx,0);
        else if(sx==h-1)
            dfs_right(1,h-1,sy);
        else if(sy==w-1)
            dfs_right(4,sx,w-1);
        ans[1] = k;
        printf("%d %d %d\n",ans[0],ans[1],ans[2]);
//        dfs_left_first_start(sx,sy);
//        ans[0] = k+2;
//        k = 0;
//        dfs_right_first_start(sx,sy);
//        ans[1] = k+2;
    }
    return 0;
}

 

标签:direction,right,return,dfs,poj,麻烦,true,3083,left
来源: https://www.cnblogs.com/stevenzrx/p/15102741.html