Fabled Rooks UVA

来源:互联网 发布:mysql 默认访问地址 编辑:程序博客网 时间:2024/04/29 16:21

题目传送门

题意:给你一个n*n的棋盘,在这个棋盘上放置n个车是他们不互相攻击,且第i个车一定在矩形Ri中。

思路:这个题可以发现车的横坐标与纵坐标是没有联系的,然后我们可以分开来进行计算,这个题按理说是可以直接贪心过的,但是我的贪心策略错了,然后就不知道怎么改了。就换成了匈牙利算法写匹配,也是一样可以的。

#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <fstream>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <sstream>#include <stack>#include <string>#include <vector>#define MAXN 5010#define MAXE 210#define INF 10000000#define MOD 1000000007#define LL long long#define pi acos(-1.0)using namespace std;int match[MAXN];int check[MAXN];struct Node {  int num;  int id;};Node xx[MAXN], yy[MAXN];bool cmp(const Node &x1, const Node &x2) { return x1.id < x2.id; }bool dfs(int x, vector<int> vec[]) {  for (int i = 0; i < vec[x].size(); ++i) {    int to = vec[x][i];    if (!check[to]) {      check[to] = 1;      if (match[to] == -1 || dfs(match[to], vec)) {        match[to] = x;        return true;      }    }  }  return false;}int main() {  std::ios::sync_with_stdio(false);  int n;  while (cin >> n && n) {    vector<int> vec1[MAXN];    vector<int> vec2[MAXN];    memset(match, -1, sizeof(match));    memset(xx, 0, sizeof(xx));    memset(yy, 0, sizeof(yy));    int x1, y1, x2, y2;    for (int i = 1; i <= n; ++i) {      cin >> x1 >> y1 >> x2 >> y2;      for (int j = x1; j <= x2; ++j) {        vec1[i].push_back(j);      }      for (int j = y1; j <= y2; ++j) {        vec2[i].push_back(j);      }    }    bool flag = true;    for (int i = 1; i <= n; ++i) {      memset(check, 0, sizeof(check));      if (!dfs(i, vec1)) {        flag = false;        break;      }    }    if (flag) {      for (int i = 1; i <= n; ++i) {        xx[i].id = match[i];        xx[i].num = i;      }      memset(match, -1, sizeof(match));      for (int i = 1; i <= n; ++i) {        memset(check, 0, sizeof(check));        if (!dfs(i, vec2)) {          flag = false;          break;        }      }      for (int i = 1; i <= n; ++i) {        yy[i].id = match[i];        yy[i].num = i;      }    }    if (flag) {      sort(xx + 1, xx + n + 1, cmp);      sort(yy + 1, yy + n + 1, cmp);      for (int i = 1; i <= n; ++i)        cout << xx[i].num << ' ' << yy[i].num << endl;    } else {      cout << "IMPOSSIBLE\n";    }  }  return 0;}/*81 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 881 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 80*/
原创粉丝点击