5分求助
查看原帖
5分求助
1063799
chenshenqi楼主2024/4/7 18:48
#include <bits/stdc++.h>
using namespace std;
struct pos{
    int x,y;
    pos (int ax=0,int ay=0){
        x=ax,y=ay;
    }
};
char a[105][105];
bool vis[105][105];
queue <pos> q;
int n,m,x,y,ans=0;
bool check(int x,int y){
    if (x<1||x>n||y<1||y>m||a[x][y]=='*'||vis[x][y]) return false;
    vis[x][y]=true;
    return true;
}
void bfs(){
    q.push(pos(x,y));
    while (!q.empty()){
        pos now=q.front();
        q.pop();
        int ax=now.x,ay=now.y;
        vis[ax][ay]=true;
        ans+=(a[ax][ay]=='S'?1:a[ax][ay]=='M'?5:10);
        if (check(ax+1,now.y)) q.push(pos(ax+1,ay));
        if (check(ax-1,now.y)) q.push(pos(ax-1,ay));
        if (check(ax,now.y+1)) q.push(pos(ax,ay+1));
        if (check(ax,now.y-1)) q.push(pos(ax,ay-1));
    }
    cout << ans;
}
int main(){
    cin >> n >> m;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){
            cin >> a[i][j];
        }
    }
    cin >> x >> y;
    bfs();
    return 0;
}
2024/4/7 18:48
加载中...