QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#483972#1819. Cleaning Robotqojcoding123TL 0ms3584kbC++174.0kb2024-07-19 15:04:452024-07-19 15:04:45

Judging History

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

  • [2024-07-19 15:04:45]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3584kb
  • [2024-07-19 15:04:45]
  • 提交

answer

#include <iostream>
#include <vector>
#include <map>
#include <limits.h>
#include <algorithm>
using namespace std;

map<vector<int>, int> visited;
int n, m, k;

map<vector<int>, int> obs;


void dfs(int x, int y){
  //vector<int> loc = {xpos, ypos, zpos};
  if (visited[{x, y}] == 1) {
    return;
  }
  /*visited.push_back(loc);
  for (vector<int> neighbor : adj_list[loc]) {

    dfs(neighbor[0], neighbor[1], neighbor[2]);
  }*/
  visited[{x, y}] = 1;

  if (x+1 <= n && obs[{x+1, y}] != 1) {
    dfs(x+1, y);
  }
  if (x-1 > 0 && obs[{x-1, y}] != 1) {
    dfs(x-1, y);
  }

  if (y+1 <= m && obs[{x, y+1}] != 1) {
    dfs(x, y+1);
  }
  if (y-1 > 0 && obs[{x, y-1}] != 1) {
    dfs(x, y-1);
  }
}

int main() {
  //std::cout << "Hello World!\n";
  
  
  cin >> n >> m >> k;
  if (k == m*n) {
    //cout << "one\n";
    cout << -1 << "\n";
  } else {
    map<int, vector<int>> xs;
    map<int, vector<int>> ys;

    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;

      if (xs.find(x) == xs.end()) {
        xs[x] = {y};
      } else {
        xs[x].push_back(y);
      }

      if (ys.find(y) == ys.end()) {
        ys[y] = {x};
      } else {
        ys[y].push_back(x);
      }

      obs[{x, y}] = 1;

    }

    int firstx = 1;
    int firsty = 1;
    bool found = 0;

    for(int i = 1; i < n+1; i++) {
      for(int j = 1; j < m+1; j++) {
        if (obs.count({i, j})) {
          firstx = 1;
          firsty = 1;
          found = 1;
          break;
        }
        if (found) {
          break;
        }
      }
    }

    dfs(firstx, firsty);
    if (visited.size() < n*m - k) {
      //cout << "two\n";
      cout << -1 << "\n";
    } else {
      int maxside;
      //if (k == 0) {
      maxside = min(n, m);
      /*} else {
        maxside = min({n - maxx, m - maxy, minx - 1, miny - 1});
      }*/

      int minx = -1;
      int miny = -1;
      int maxx = 0;
      int maxy = 0;


      map<int, vector<int>> ::iterator itx;
      for (itx = xs.begin(); itx != xs.end(); itx++)
      {
        vector<int> vals = itx->second;
        sort(vals.begin(), vals.end());
        for (int j = 0; j < vals.size() - 1; j++) {
          if (vals[j+1] - vals[j] - 1 != 0 && vals[j+1] - vals[j] - 1 < maxside) {
            maxside = vals[j+1] - vals[j] - 1;
          }
          if (miny == -1 && vals[j] != 1) {
            miny = vals[j];
          }
          if (vals[j] != m) {
            maxy = vals[j];
          }
        }

        if (miny == -1 && vals[vals.size()-1] != 1) {
          miny = vals[vals.size()-1];
        }
        if (vals[vals.size()-1] != m) {
          maxy = vals[vals.size()-1];
        }
      }

      map<int, vector<int>> ::iterator ity;
      for (ity = ys.begin(); ity != ys.end(); ity++)
      {
        vector<int> vals = ity->second;
        sort(vals.begin(), vals.end());
        for (int j = 0; j < vals.size() - 1; j++) {
          if (vals[j+1] - vals[j] - 1 != 0 && vals[j+1] - vals[j] - 1 < maxside) {
            maxside = vals[j+1] - vals[j] - 1;
          }
          if (minx == -1 && vals[j] != 1) {
            minx = vals[j];
          }
          if (vals[j] != n) {
            maxx = vals[j];
          }
        }
        if (minx == -1 && vals[vals.size()-1] != 1) {
          minx = vals[vals.size()-1];
        }
        if (vals[vals.size()-1] != n) {
          maxx = vals[vals.size()-1];
        }
      }
      //cout << "maxside: " << maxside << "\n";
      maxside = min({maxside, n - maxx, m - maxy});
      //cout << "maxside: " << maxside << "\n";

      if (minx != -1) {
        maxside = min(maxside, minx-1);
        //cout << "xmaxside: " << maxside << "\n";
      }
      if (miny != -1) {
        maxside = min(maxside, miny-1);
        //cout << "ymaxside: " << maxside << "\n";
      }
      
      //cout << "three\n";
      cout << maxside << "\n";
    }
  }
}

详细

Test #1:

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

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: