2Q. Rescue

错误案例1

#include<bits/stdc++.h>
using namespace std;
int Vis[205][205];
char MAP[205][205];
const int inf=0x3f3f3f3f;
int minhh(int x,int y)
{
    if(x<=y)return x;
    else return y;
}
struct glass
{
    int x,y,steps;    
}gg;
int main() 
{    
    int N,M,i,j;
    queue<struct glass>q;
    queue<struct glass>p;
    while(scanf("%d %d",&N,&M)!=EOF){
    
    getchar();//清空输入流,不然会输出错误
    for(j=1;j<=N;j++)
        for(i=1;i<=M+1;i++)//连续录入字符要注意换行符问题,也占据一个变量 
        {
            scanf("%c",&MAP[j][i]);
            if(MAP[i][j]=='a'){gg.x=i;gg.y=j;gg.steps=0;}//录入天使起始位置 
        }
        memset(Vis,0,sizeof(Vis));
        
        q.push(gg);
        while(!q.empty())
        { 
            gg=q.front();
            q.pop();
            if(MAP[gg.x][gg.y]=='r'){i=-1;
            break;}
            if(Vis[gg.x][gg.y]==0)
            {    
                Vis[gg.x][gg.y]=1;                
                gg.x--;gg.steps++;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.x++;
                
                gg.x++;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.x--;
                
                gg.y++;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.y--;
                
                gg.y--;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.y++;gg.steps--;
                
                while(!p.empty())
                    {
                    gg=p.front();
                    q.push(gg);
                    p.pop();
                    }//把遇到守卫的情况往下压一层        
            }    
        }
        if(i==-1)printf("%d\n",gg.steps);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
         
        while(!q.empty())//清空队列 
        {
            q.pop();
        }
    }    
    return 0;
}
总结

1.这里的本意是利用增加一个p队列存储遇到X时,层数对应不上的情况,但实际上这样写不够全面,比如当你对一个steps为12的叶子判断时,他会产生几个steps为13的叶子,而你把因为遇到X导致的steps为14的叶子放在steps为13的叶子后面,本来没问题,但是判断下一个steps为12的叶子时,就会产生新的steps为13的叶子,会跟在steps为14的叶子后面,导致答案出错

错误案例二(还是不够全面)

#include<bits/stdc++.h>
using namespace std;
int Vis[205][205];
char MAP[205][205];
const int inf=0x3f3f3f3f;
int minhh(int x,int y)
{
    if(x<=y)return x;
    else return y;
}
struct glass
{
    int x,y,steps;    
}gg,kk;
int main() 
{    
    int N,M,i,j;
    queue<struct glass>q;
    queue<struct glass>p;
    while(scanf("%d %d",&N,&M)!=EOF){
    
    getchar();//清空输入流,不然会输出错误
    for(j=1;j<=N;j++)
        for(i=1;i<=M+1;i++)//连续录入字符要注意换行符问题,也占据一个变量 
        {
            scanf("%c",&MAP[j][i]);
            if(MAP[i][j]=='a'){gg.x=i;gg.y=j;gg.steps=0;}//录入天使起始位置 
        }
        memset(Vis,0,sizeof(Vis));
        
        q.push(gg);
        while(!q.empty())
        { 
            gg=q.front();
            q.pop();
            if(MAP[gg.x][gg.y]=='r'){i=-1;
            break;}
            if(Vis[gg.x][gg.y]==0)
            {    
                Vis[gg.x][gg.y]=1;                
                gg.x--;gg.steps++;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.x++;
                
                gg.x++;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.x--;
                
                gg.y++;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.y--;
                
                gg.y--;
                if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
                if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
                else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
                gg.y++;gg.steps--;
                
                if(!q.empty())
                {
                    kk=q.front();gg=p.front();
                    if(kk.steps==gg.steps-1)
                    while(!p.empty())
                    {
                        gg=p.front();
                        q.push(gg);
                        p.pop();
                    }
                    
                }
                if(q.empty())while(!p.empty())
                    {
                    gg=p.front();
                    q.push(gg);
                    p.pop();
                    }//把遇到守卫的情况往下压一层        
            }    
        }
        if(i==-1)printf("%d\n",gg.steps);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
         
        while(!q.empty())//清空队列 
        {
            q.pop();
        }
    }    
    return 0;
}

### 关于 PAT 甲级 1088 题目测试点 2 的解决方案 对于 PAT 甲级 1088 题目中的测试点 2,理解题目背景和具体需求至关重要。此题涉及城市间的救援行动,在给定的数据范围内寻找最优路径并计算最大救援队伍规模。 #### 数据结构的选择 为了高效处理此类图论问题,通常采用邻接表来表示城市之间的连接关系。这不仅节省空间而且便于实现广度优先搜索 (BFS) 或者深度优先搜索 (DFS),从而有效地探索所有可能的路径[^3]。 ```cpp vector<int> adj[MAX_N]; // 存储每座城市的相邻城市列表 queue<pair<int,int>> q; // BFS队列,存储当前节点及其累计人数 ``` #### 处理边界条件 考虑到可能存在多个起点或终点的情况,初始化时需特别注意这些特殊情况下的逻辑判断。当遇到无法到达目标位置的情形,则应返回特定提示信息[^4]。 ```cpp if (!found_path){ cout << "There are X accounts and no account is modified"; } ``` #### 主要算法流程 通过 BFS 来遍历整个地图,记录下每一个可以抵达的城市以及此时携带的最大救援力量数目。一旦触及目的地即刻停止搜索过程,并输出最终结果。 ```cpp while(!q.empty()){ auto [cur_city, cur_rescue_count]=q.front();q.pop(); if(cur_city==target_city){ max_rescue=max(max_rescue,cur_rescue_count); continue; } for(auto next_city : adj[cur_city]){ int new_rescue_count=min(rescue_teams[next_city],cur_rescue_count); if(visited[next_city]<new_rescue_count){ visited[next_city]=new_rescue_count; q.push({next_city,new_rescue_count}); } } } ``` 上述方法能够确保覆盖到所有潜在的有效路径,同时保持较高的时间效率,适用于本题设定内的各种输入情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值