Loading

P1913 L国的战斗之伞兵(广搜BFS)

就是在输入的时候把 ‘o’ 的放在队里,然后,直接BFS就可以了。感觉是水题。

#include<iostream>
#include<queue>
using namespace std;
#define pa pair<int, int>
const int maxn = 1e3 + 10;
char cc[maxn][maxn];
int xx[] = { 0, 1, -1, 0, 0 };
int yy[] = { 0, 0, 0, 1, -1 };
char kk[] = " udlr";
int n, m, ans;
queue<pa>q;

int main(){
    cin >> n >> m;
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= m; ++j)
        {
            cin >> cc[i][j];
            if (cc[i][j] == 'o'){ 
                pair<int, int>ss;
                ss.first = i;    ss.second = j;
                q.push(ss);
            }
        }
    }

    while (q.size()){
        pair<int, int>ss = q.front(); q.pop();        ans++;
        for (int i = 1; i <= 4; ++i){
            int x = ss.first + xx[i], y = ss.second + yy[i];
            if ((x >= 1 && x <= n&&y >= 1 && y <= m) && cc[x][y] == kk[i]){ pair<int, int>h; h.first = x; h.second = y; q.push(h); }
        }
    }
    cout << ans << endl;
}

 

posted @ 2019-04-04 18:04  青山新雨  阅读(254)  评论(0编辑  收藏  举报