超时4个点求条:
| QAQ | QWQ |
|---|---|
| Sample | AC×3 |
| All | AC×35TLE×4 |
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector<pair<int, int> > v;
char a[1005][1005];
bool vis[1005][1005];
int ans = 0, h, w, d,
stored[1005][1005],
dx[] = { 0, 0, 1, -1},
dy[] = { 1, -1, 0, 0};
void dfs(int x, int y, int d) {
stored[x][y] = max(stored[x][y], d);
if (!vis[x][y]) {
ans ++;
vis[x][y] = 1;
}
if (d == 0) {
return;
}
for (int i = 0; i < 4; i ++) {
if (a[x + dx[i]][y + dy[i]] == '.' &&
(stored[x + dx[i]][y + dy[i]] < d - 1 || !stored[x + dx[i]][y + dy[i]])) {
dfs(x + dx[i], y + dy[i], d - 1);
}
}
}
int main() {
cin >> h >> w >> d;
for (int i = 1; i <= h; i ++) {
for (int j = 1; j <= w; j ++) {
cin >> a[i][j];
if (a[i][j] == 'H') {
v.push_back(make_pair(i, j));
}
}
}
for (auto i : v) {
dfs(i.first, i.second, d);
}
cout << ans;
return 0;
}
另附:抢到了本题第一个帖子