
就是在输入的时候把 ‘o’ 的放在队里,然后,直接BFS就可以了。感觉是水题。
#include<iostream>
#include<queue>
using namespace std;
#define pa pair<int, int>
const int maxn = 1e3 + ;
char cc[maxn][maxn];
int xx[] = { , , -, , };
int yy[] = { , , , , - };
char kk[] = " udlr";
int n, m, ans;
queue<pa>q; int main(){
cin >> n >> m;
for (int i = ; i <= n; ++i){
for (int j = ; 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 = ; i <= ; ++i){
int x = ss.first + xx[i], y = ss.second + yy[i];
if ((x >= && x <= n&&y >= && y <= m) && cc[x][y] == kk[i]){ pair<int, int>h; h.first = x; h.second = y; q.push(h); }
}
}
cout << ans << endl;
}