QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#483681#1819. Cleaning Robotpatboi008TL 0ms3648kbC++174.6kb2024-07-19 02:45:212024-07-19 02:45:22

Judging History

你现在查看的是最新测评结果

  • [2024-07-19 02:45:22]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3648kb
  • [2024-07-19 02:45:21]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

void applySetter(vector<vector<int>>& cleaned, int x1, int y1, int x2, int y2) {
    cleaned[x1][y1]++;
    cleaned[x1][y2 + 1]--;
    cleaned[x2 + 1][y1]--;
    cleaned[x2 + 1][y2 + 1]++;
}

bool checkCoverage(vector<vector<int>>& obs, vector<vector<int>>& cleaned, int n, int m) {
    vector<vector<int>> result(n + 1, vector<int>(m + 1, 0));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            result[i][j] = cleaned[i][j] + result[i - 1][j] + result[i][j - 1] - result[i - 1][j - 1];
            if (obs[i][j] == 0 && result[i][j] <= 0) {
                return false;
            }
        }
    }
    return true;
}

bool canCoverAll(vector<vector<int>>& obs, vector<vector<int>>& obsPrefix, int size, int n, int m) {
    vector<vector<int>> cleaned(n + 2, vector<int>(m + 2, 0));

    queue<pair<int, int>> q;
  int curx, cury;
  bool breaker = false;
    for (int i = 1; i <= n - size + 1; i++) {
        for (int j = 1; j <= m - size + 1; j++) {
            int x1 = i, y1 = j;
            int x2 = i + size - 1, y2 = j + size - 1;
            int obsCount = obsPrefix[x2][y2] - obsPrefix[x1 - 1][y2] - obsPrefix[x2][y1 - 1] + obsPrefix[x1 - 1][y1 - 1];
            if (obsCount == 0) {
              curx = i;
              cury = j;
              breaker = true;
              break;
            }
        }
      if(breaker){
        break;
      }
    }
  if(!breaker){
    return false;
  }
  q.push(make_pair(curx, cury));
  vector<vector<bool>> visited(n + 2, vector<bool>(m + 2, false));

  while(!q.empty()){
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    visited[x][y] = true;
    int x2 = x + size - 1;
    int y2 = y + size - 1;
    int obsCount = obsPrefix[x2][y2] - obsPrefix[x - 1][y2] - obsPrefix[x2][y-1] + obsPrefix[x-1][y-1];
    if(obsCount == 0){
      applySetter(cleaned, x, y, x2, y2);
      if(!visited[x+1][y] && x+1 <= n-size + 1){
        q.push(make_pair(x+1, y));
      }
      if(!visited[x][y+1] && y+1 <= m - size + 1){
        q.push(make_pair(x, y+1));
      }
      if(!visited[x-1][y] && x-1 >= 1){
        q.push(make_pair(x-1, y));
      }
      if(!visited[x][y-1] && y-1 >= 1){
        q.push(make_pair(x, y-1));
      }
    }
    
  }
    return checkCoverage(obs, cleaned, n, m);
}
/*
void visAll(vector<vector<bool>>& vis, vector<vector<int>>& obs, int i, int j, int& n, int& m){
  if(i <= 0 || j <= 0 || i > n || j > m || obs[i][j] == 1 || vis[i][j]){
    return;
  }
  vis[i][j] = true;
  visAll(vis, obs, i+1, j, n, m);
  visAll(vis, obs, i, j+1, n, m);
  visAll(vis, obs, i-1, j, n, m);
  visAll(vis, obs, i, j-1, n, m);
  return;
}
*/
int main() {
    int n, m, k;
    cin >> n >> m >> k;
  
    vector<vector<int>> obstructed(n + 2, vector<int>(m + 2, 0));
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        obstructed[a][b] = 1;
    }
  if(k == n*m){
    cout << "-1" << endl;
    return 0;
  }
  vector<vector<bool>> visited(n + 2, vector<bool>(m + 2, false));
  queue<pair<int, int>> q;
  
  bool breaker = false;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
      if(obstructed[i][j] == 0){
        q.push(make_pair(i, j));
        breaker = true;
        break;
      }
    }
    if(breaker){
      break;
    }
  }
  
  while(!q.empty()){
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    if(x <= 0 || y <= 0 || x > n || y > m || visited[x][y] || obstructed[x][y] == 1){
      continue;
    }
    visited[x][y] = true;
    q.push(make_pair(x+1, y));
    q.push(make_pair(x, y+1));
    q.push(make_pair(x-1, y));
    q.push(make_pair(x, y-1));
  }
  
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
      if(!visited[i][j] && obstructed[i][j] == 0){
        cout << "-1 " << endl;
        return 0;
      }
    }
  }
    vector<vector<int>> obsPrefix(n + 1, vector<int>(m + 1, 0));
  
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            obsPrefix[i][j] = obstructed[i][j] + obsPrefix[i - 1][j] + obsPrefix[i][j - 1] - obsPrefix[i - 1][j - 1];
        }
    }

    int left = 1, right = min(n, m), ans = 0;
    while (left <= right) {
        int mid = left + (right-left)/2;
        if (canCoverAll(obstructed, obsPrefix, mid, n, m)) {
            ans = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
  if(ans == 0){
    cout << "-1" << endl;
    return 0;
  }
  cout << ans << endl;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3648kb

input:

10 7 1
8 3

output:

2

result:

ok answer is '2'

Test #2:

score: -100
Time Limit Exceeded

input:

2236 2236 2214
28 1255
389 2175
730 592
1360 977
1225 752
1403 1798
1518 1381
147 745
659 249
951 1475
1826 1951
691 1033
81 1458
1487 1946
2106 1395
1995 629
470 891
1902 822
2210 2001
441 2130
1198 1539
2027 1101
215 1149
205 420
379 2104
308 1225
859 109
1417 2078
1764 376
1772 5
335 1113
917 118...

output:


result: