acwing 1101. 献给阿尔吉侬的花束(蓝桥杯)

题目:1101. 献给阿尔吉侬的花束

在这里插入图片描述
在这里插入图片描述

思路:bfs

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int,int >PII;
const int N=1e5+10;
const int mod=100000007;
int fx[4]={1,-1,0,0},fy[4]={0,0,1,-1};
int t;
int r,c;
char a[205][205];
bool sta[205][205];
int main(){
    cin>>t;
    while(t--){
        memset(sta,0,sizeof sta);
        cin>>r>>c;
        queue<pair<PII,int >> q;
        int s1=0,s2=0,e1=0,e2=0;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                //scanf("%c",&a[i][j]);
                cin>>a[i][j];
                if(a[i][j]=='S') {
                    PII t={i,j};
                    q.push({t,0});
                    s1=i,s2=j;
                }
                else if(a[i][j]=='E'){
                    e1=i,e2=j;
                }
            }
        }
        //for(int i=0;i<r;i++){
        //    for(int j=0;j<c;j++){
        //        cout<<a[i][j];
        //    }
        //    cout<<endl;
        //}
        //cout<<s1<<s2<<endl;
        // cout<<e1<<e2<<endl;
        bool flag=0;
        while(q.size()){
            PII t=q.front().first;
            int cc=q.front().second;
            int x=t.first,y=t.second;
            //cout<<endl<<x<<y;
            q.pop();
            if(x==e1&&y==e2){
                printf("%d\n",cc);
                flag=1;
                break;
            }
            for(int i=0;i<4;i++){
                int xx=x+fx[i];
                int yy=y+fy[i];
                //cout<<endl<<xx<<yy;
                if(xx<0||xx>=r||yy<0||yy>=c) continue;
                
                if(!sta[xx][yy]&&a[xx][yy]!='#'){
                    q.push({{xx,yy},cc+1});
                    sta[xx][yy]=1;
                }
            }
        }
        if(flag==0){
            puts("oop!");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值