zoukankan      html  css  js  c++  java
  • NYOJ 237 游戏高手的烦恼

    游戏高手的烦恼
    时间限制:1000 ms | 内存限制:65535 KB
    难度:5
    描述
    有一位传说级游戏高手,在闲暇时间里玩起了一个小游戏,游戏中,一个n*n的方块形区域里有许多敌人,玩家可以使用炸弹炸掉某一行或者某一列的所有敌人。他是种玩什么游戏都想玩得

    优秀的人,所以,他决定,使用尽可能少的炸弹炸掉所有的敌人。

    现在给你一个游戏的状态,请你帮助他判断最少需要多少个炸弹才能炸掉所有的敌人吧。

    比如说,下图中X表示敌人

    X . X
    . X .

    . X .

    则,他只需要炸掉第1行与第2列就能炸掉所有的敌人,所以只需要两颗炸弹就可以了。

    输入
    第一行是一个整数T,表示测试数据的组数(0<T<=400)。
    每组测试数据的第一行有两个整数n,K,其中n表示游戏方形区域的大小。(n<=500,K<=10 000)
    随后的K行,每行有两个整数i,j表示第i行,第j列有一个敌人(行和列都从1开始编号)。(1<=i,j<=n)

    输出
    对于每组测试数据,输出一个整数表示最少需要的炸弹颗数


    样例输入
    1
    3 4
    1 1
    1 3
    2 2
    3 2

    样例输出
    2

    来源
    POJ翻译而来

    上传者
    张云聪

    解题:参考月老的难题,最大匹配问题。。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #define LL long long
    13 #define INF 0x3f3f3f3f
    14 using namespace std;
    15 const int maxn = 510;
    16 int n,llink[maxn<<1];
    17 vector<int>g[maxn<<1];
    18 bool used[maxn<<1];
    19 bool dfs(int u) {
    20     for(int v = 0; v < g[u].size(); v++) {
    21         if(!used[g[u][v]]) {
    22             used[g[u][v]] = true;
    23             if(llink[g[u][v]] == -1 || dfs(llink[g[u][v]])) {
    24                 llink[g[u][v]] = u;
    25                 return true;
    26             }
    27         }
    28     }
    29     return false;
    30 }
    31 int main() {
    32     int t,i,j,m,u,v;
    33     scanf("%d",&t);
    34     while(t--) {
    35         scanf("%d%d",&n,&m);
    36         j = n<<1;
    37         for(i = 0; i <= j; i++) {
    38             g[i].clear();
    39         }
    40         while(m--) {
    41             scanf("%d%d",&u,&v);
    42             g[u].push_back(n+v);
    43         }
    44         memset(llink,-1,sizeof(llink));
    45         int ans = 0;
    46         n <<= 1;
    47         for(i = 1; i <= n; i++) {
    48             memset(used,false,sizeof(used));
    49             if(dfs(i)) ans++;
    50         }
    51         printf("%d
    ",ans);
    52     }
    53     return 0;
    54 }
    View Code
    
    
    
     
  • 相关阅读:
    [WC2010]重建计划
    [Codeforces150E] Freezing with Style
    [Codeforces915F] Imbalance Value of a Tree
    [Codeforces1055F] Tree and XOR
    [Codeforces1117G]Recursive Queries
    [Codeforces587F]Duff is Mad
    [Codeforces547E]Mike and Friends
    [2020团体程序设计天梯赛-总决赛L3-2] 传送门
    第05组 Beta冲刺 (1/5)
    第05组 Alpha冲刺 总结
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3870245.html
Copyright © 2011-2022 走看看