QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#485754 | #1819. Cleaning Robot | XC220477 | RE | 0ms | 0kb | C++17 | 2.8kb | 2024-07-21 05:31:34 | 2024-07-21 05:31:35 |
answer
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
int n, m, k;
vector<vector<int>> a;
vector<vector<int>> ps;
vector<vector<int>> visited;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
bool bfs(int size)
{
queue<pair<int, int>> q;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i][j] == 0 && q.empty())
{
if (j > m - size || i > n - size ||
ps[i + size - 1][j + size - 1] - ps[i + size - 1][j - 1] -
ps[i - 1][j + size - 1] + ps[i - 1][j - 1] > 0)
{
return false;
}
q.push({i, j});
}
visited[i][j] = 0;
}
}
while (!q.empty())
{
int r = q.front().first;
int c = q.front().second;
q.pop();
visited[r][c]++;
visited[r + size][c]--;
visited[r][c + size]--;
visited[r + size][c + size]++;
for (int i = 0; i < 4; i++)
{
int newr = r + dr[i];
int newc = c + dc[i];
if (newr > 0 && newc > 0 && newr <= n + 1 - size && newc <= m + 1 - size && visited[newr][newc] <= 0 &&
ps[newr + size - 1][newc + size - 1] - ps[newr + size - 1][newc - 1] -
ps[newr - 1][newc + size - 1] + ps[newr - 1][newc - 1] == 0)
{
q.push({newr, newc});
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
visited[i][j] += visited[i - 1][j] + visited[i][j - 1] - visited[i - 1][j - 1];
if (a[i][j] == 0 && visited[i][j] <= 0) return false;
}
}
return true;
}
int main()
{
cin >> n >> m >> k;
a = vector<vector<int>>(n, vector<int>(m));
ps = vector<vector<int>>(n, vector<int>(m));
visited = vector<vector<int>>(n, vector<int>(m));
for (int i = 0; i < k; i++)
{
int r, c;
cin >> r >> c;
a[r][c] = 1;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + a[i][j];
}
}
if (!bfs(1))
{
cout << -1 << endl;
return 0;
}
int l = 1;
int r = n;
if (m < n) r = m;
while (l < r)
{
int mid = (l + r + 1) / 2;
bool valid = bfs(mid);
if (valid) l = mid;
else r = mid - 1;
}
cout << l << endl;
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
10 7 1 8 3