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;
}
复制代码

 

posted @   智人心  阅读(52)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示