社区讨论

38分求调

P1825[USACO11OPEN] Corn Maze S参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mdoas7nw
此快照首次捕获于
2025/07/29 16:51
7 个月前
此快照最后确认于
2025/07/29 17:12
7 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define N 386
using namespace std;
struct node{
    int x,y;
};
int n,m,sx,sy,fx,fy;
int ans[N][N],ht[30][5];
int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
char a[N][N];
bool vised[N][N];
//ans表示到达第i行第j列所需最小时间
//ht记录两个滑梯坐标
//sx,sy为起点坐标,fx,fy是终点坐标
list<node>l;
inline int read();
inline void bfs();
int main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) ans[i][j]=INT_MAX;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            char x;
            cin>>x;
            if(x=='@'){
                sx=i;sy=j;
                ans[sx][sy]=0;
            }else if(x=='='){
                fx=i;fy=j;
            }else if(x>='A' && x<='Z'){
                ht[x-'A'+1][++ht[x-'A'+1][0]]=i;
                ht[x-'A'+1][++ht[x-'A'+1][0]]=j;
            }else if(x=='#') vised[i][j]=1;
            a[i][j]=x;
        }
    }
    l.push_back((node){sx,sy});
    bfs();
    cout<<ans[fx][fy]<<endl;
    return 0;
}
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
    return x*f;
}
inline void bfs(){
    while(!l.empty()){
        int x=l.front().x,y=l.front().y;
        for(int i=0;i<4;i++){
            int nxx=x+dx[i],nxy=y+dy[i];
            if(nxx<1 || nxx>n || nxy>m || nxy<1 || vised[nxx][nxy]) continue;
            if(ans[nxx][nxy]>ans[x][y]+1){
                ans[nxx][nxy]=min(ans[nxx][nxy],ans[x][y]+1);
                if(a[nxx][nxy]>='A' && a[nxx][nxy]<='Z'){
                    int temp=a[nxx][nxy]-'A'+1;
                    if(nxx==ht[temp][1]){
                        if(!vised[ht[temp][3]][ht[temp][4]]){
                            l.push_back((node){ht[temp][3],ht[temp][4]});
                            ans[ht[temp][3]][ht[temp][4]]=min(ans[ht[temp][3]][ht[temp][4]],ans[nxx][nxy]);
                        }
                    }else{
                        if(!vised[ht[temp][1]][ht[temp][2]]){
                            l.push_back((node){ht[temp][1],ht[temp][2]});
                            ans[ht[temp][1]][ht[temp][2]]=min(ans[ht[temp][1]][ht[temp][2]],ans[nxx][nxy]);
                        }
                    }
                }
                l.push_back((node){nxx,nxy});
            }
        }
        vised[x][y]=1;
        l.pop_front();
    }
}

回复

0 条回复,欢迎继续交流。

正在加载回复...