HDU 1983 Kaitou Kid - The Phantom Thief (2)

本文探讨了一种迷宫寻路算法的实现及其优化过程。通过对比两种不同的路径记录方式,发现采用数组记录整条路径的方法能有效避免陷入死循环的问题,并给出了具体的AC代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题是看题解的。。。

自己写的记录路径陷入死循环,搞不明白。。所以去看题解了,题解是将整条路径记录在一个数组里,而我是记录上一个节点。。。以后回来看看哪错了

AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

struct Point{
    int x,y,time,kx[70],ky[70];
    Point(){}
    bool Got;
    Point(int _x,int _y,int _time,bool _Got):
        x(_x),y(_y),time(_time),Got(_Got){}
};
int n,m,T,sx,sy,ans,dx[]={1,-1,0,0},dy[]={0,0,1,-1};
bool vis[10][10][2];
char map[10][10];

void bfs(int k){
    if(k>=ans) return;
    queue<Point> q;
    Point p;
    int x,y;
    bool ok=false;
    q.push(Point(sx,sy,0,false));
    memset(vis,false,sizeof(vis));
    vis[sx][sy][0]=true;
    while(!q.empty()){
        p=q.front();q.pop();
        if(map[p.x][p.y]=='E'&&p.Got){
            ok=true;
            break;
        }
        for(int i=0;i<4;i++){
            x=p.x+dx[i];y=p.y+dy[i];
            if(x<0||x>=n||y<0||y>=m||map[x][y]=='#'||p.time>=T) continue;
            Point tp=p;
            tp.x=p.x+dx[i];tp.y=p.y+dy[i];
            tp.time=p.time+1;
            if(map[tp.x][tp.y]=='J') tp.Got=true;
            if(vis[tp.x][tp.y][tp.Got]) continue;
            vis[tp.x][tp.y][tp.Got]=true;
            tp.kx[tp.time]=tp.x;
            tp.ky[tp.time]=tp.y;
            q.push(tp);
        }
    }
    if(ok){
        for(int i=1;i<p.time;i++){
            int nx=p.kx[i],ny=p.ky[i];
            if(map[nx][ny]=='.'||map[nx][ny]=='J'){
                char tmp=map[nx][ny];
                map[nx][ny]='#';
                bfs(k+1);
                map[nx][ny]=tmp;
            }
        }
    }
    else ans=k;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
#endif
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m>>T;
        for(int i=0;i<n;i++){
            scanf("%s",map[i]);
            for(int j=0;j<m;j++)
                if(map[i][j]=='S')
                    sx=i,sy=j;
        }
        ans=4;
        bfs(0);
        cout<<ans<<endl;
    }
    return 0;
}

死循环代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

struct Point{
    int x,y,time,prex,prey;
    Point(){}
    bool Got;
    Point(int _x,int _y,int _time,bool _Got):
        x(_x),y(_y),time(_time),Got(_Got){}
};
int n,m,T,sx,sy,ans,dx[]={1,-1,0,0},dy[]={0,0,1,-1},par[10][10];
bool vis[10][10][2];
char map[10][10];

void bfs(int k){
    queue<Point> q;
    Point p;
    int x,y;
    bool ok=false;
    q.push(Point(sx,sy,0,false));
    memset(vis,false,sizeof(vis));
    vis[sx][sy][0]=true;
    while(!q.empty()){
        p=q.front();q.pop();
        if(map[p.x][p.y]=='E'&&vis[p.x][p.y][1]){
            ok=true;
            break;
        }
        for(int i=0;i<4;i++){
            x=p.x+dx[i];y=p.y+dy[i];
            if(x<0||x>=n||y<0||y>=m||map[x][y]=='#'||p.time>=T) continue;
            if(map[x][y]=='J') {
                if(vis[x][y][1]) continue;
                par[x][y]=p.x*m+p.y;
                q.push(Point(x,y,p.time+1,true));
                vis[x][y][1]=true;
            }
            else if(map[x][y]=='.'||map[x][y]=='E'){
                if(vis[x][y][p.Got]) continue;
                par[x][y]=p.x*m+p.y;
                q.push(Point(x,y,p.time+1,p.Got));
                vis[x][y][p.Got]=true;
            }
        }
    }
    if(ok){
        for(int nx=par[p.x][p.y]/m,ny=par[p.x][p.y]%m;!(nx==sx&&ny==sy);){
            char tmp=map[nx][ny];
            map[nx][ny]='#';
            bfs(k+1);
            map[nx][ny]=tmp;
            int kk=par[nx][ny];
            nx=kk/m;ny=kk%m;
        }
    }
    else ans=k;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
#endif
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m>>T;
        for(int i=0;i<n;i++){
            scanf("%s",map[i]);
            for(int j=0;j<m;j++)
                if(map[i][j]=='S')
                    sx=i,sy=j;
        }
        bfs(0);
        cout<<ans<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值