Description
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or  vertically along the chess grid. At each move, it can either simply move to another empty cell in the same line without any other chessman along the route or perform an eat action. The eat action, however, is the main concern in this problem. 
An eat action, for example, Cannon A eating chessman B, requires two conditions:
1、A and B is in either the same row or the same column in the chess grid.
2、There is exactly one chessman between A and B.
Here comes the problem.
Given an N x M chess grid, with some existing chessmen on it, you need put maximum cannon pieces into the grid, satisfying that any two cannons are not able to eat each other. It is worth nothing that we only account the cannon pieces you put in the grid, and no two pieces shares the same cell.
Input
There are multiple test cases. 
In each test case, there are three positive integers N, M and Q (1<= N, M<=5, 0<=Q <= N x M) in the first line, indicating the row number, column number of the grid, and the number of the existing chessmen.
In the second line, there are Q pairs of integers. Each pair of integers X, Y indicates the row index and the column index of the piece. Row indexes are numbered from 0 to N-1, and column indexes are numbered from 0 to M-1. It guarantees no pieces share the same cell.
Output
There is only one line for each test case, containing the maximum number of cannons. 
SampleInput
4 4 2
1 1 1 2
5 5 8
0 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0
SampleOutput
8
9

题意就是给你一个n*m的棋盘,然后上面已经有了 棋子,并给出这些棋子的坐标,但是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮,但是炮之间不能互相吃,吃的规则我们斗懂得 炮隔山打嘛,问你最多能放几个炮

法一:并查集

#include <iostream>
#include <stack>
#include <stdio.h>
using namespace std;
const int MAX_N = 10000 + 100;
const int MAX_M = 100000 + 100;
int p[MAX_N];
int _find(int x)
{
    return p[x] == x ? x : (p[x] = _find(p[x]));
}
int n, m;
stack <int> s;
struct Edge
{
    int u, v;
};
Edge edge[MAX_M];
int res[MAX_M];

int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        for(int i = 0; i < n; i++)
            p[i] = i;
        for(int i = 0; i < m; i++)
            scanf("%d%d", &edge[i].u, &edge[i].v);
        res[m - 1] = n;
        for(int i = m - 1; i > 0; i--)
        {
            int a = _find(edge[i].u);
            int b = _find(edge[i].v);
            if(a != b)
            {
                p[a] = b;
                res[i - 1] = res[i] - 1;
            }
            else
                res[i - 1] = res[i];
        }
        for(int i = 0; i < m; i++)
            printf("%d\n", res[i]);
    }
    return 0;
}

法二,DFS

数据范围很小,明显是搜索。

主要剪枝,就是不要和前面的冲突了、

/* ***********************************************
Author        :kuangbin
Created Time  :2013/8/24 14:38:00
File Name     :F:\2013ACM练习\比赛练习\2013通化邀请赛\1007.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int n,m;
int g[10][10];
int ans ;

void dfs(int x,int y,int cnt)
{
    if(x >= n)
    {
        ans = max(ans,cnt);
        return;
    }
    if(y >= m)
    {
        dfs(x+1,0,cnt);
        return;
    }
    if(g[x][y] == 1)
    {
        dfs(x,y+1,cnt);
        return;
    }
    dfs(x,y+1,cnt);
    bool flag = true;
    int t;
    for(t = x-1;t >= 0;t--)
        if(g[t][y])
        {
            break;
        }
    for(int i = t-1;i >= 0;i--)
        if(g[i][y])
        {
            if(g[i][y]==2)flag = false;
            break;
        }
    if(!flag)return;
    for(t = y-1;t >= 0;t--)
        if(g[x][t])
            break;
    for(int j = t-1;j >= 0;j--)
        if(g[x][j])
        {
            if(g[x][j] == 2)flag = false;
            break;
        }
    if(!flag)return;
    g[x][y] = 2;
    dfs(x,y+1,cnt+1);
    g[x][y] = 0;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int Q;
    int u,v;
    while(scanf("%d%d%d",&n,&m,&Q) == 3)
    {
        memset(g,0,sizeof(g));
        while(Q--)
        {
            scanf("%d%d",&u,&v);
            g[u][v] = 1;
        }
        ans = 0;
        dfs(0,0,0);
        printf("%d\n",ans);
    }
    return 0;
}

Cannon的更多相关文章

  1. HDU 5091---Beam Cannon(线段树+扫描线)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...

  2. Parallel Computing–Cannon算法 (MPI 实现)

    原理不解释,直接上代码 代码中被注释的源程序可用于打印中间结果,检查运算是否正确. #include "mpi.h" #include <math.h> #includ ...

  3. 炮(cannon)

    炮(cannon)[题目描述] 众所周知,双炮叠叠将是中国象棋中很厉害的一招必杀技.炮吃子时必须隔一个棋子跳吃,即俗称“炮打隔子”. 炮跟炮显然不能在一起打起来,于是rly一天借来了许多许多的炮在棋盘 ...

  4. hdu 4499 Cannon dfs

    Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...

  5. hdu 4499 Cannon(暴力)

    题目链接:hdu 4499 Cannon 题目大意:给出一个n*m的棋盘,上面已经存在了k个棋子,给出棋子的位置,然后求能够在这种棋盘上放多少个炮,要求后放置上去的炮相互之间不能攻击. 解题思路:枚举 ...

  6. hdu 5091 Beam Cannon(扫描线段树)

    题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+ ...

  7. hdu4499 Cannon (DFS+回溯)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4499 Cannon ...

  8. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  9. HDU 4499 Cannon (搜索)

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

随机推荐

  1. 好用的eclipse properties插件

    eclipse默认编辑器: 在有汉字的情况,特别是注释是汉字的情况,你会非常蛋疼的 JP的properties插件:http://propedit.sourceforge.jp/eclipse/upd ...

  2. java 时间戳和PHP时间戳 的转换[10位和13位]

    2013-08-02 14:06 9826人阅读 评论(2) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 总结一下java 时间戳和PHP时间戳 的转换问题:  由于精度不同,导 ...

  3. linux 系统文件的特殊权限

    文件权限与归属 Linux系统中的一切都是文件,但每个文件的类型不尽相同,并且Linux系统会用不同的符号来加以区分,常见的包括有 -:普通文件,d:目录文件,l:链接文件,b:块设备文件,c:字符设 ...

  4. Android API之android.provider.ContactsContract

    android.provider.ContactsContract ContactsContract是联系人provider和app的contract.定义了已支持的URL和column.取代了之前的 ...

  5. Servlet乱码问题

    数据像水流一样从一个地方流向另一个地方. 文本流是特殊的二进制流. 既然提到乱码问题,那就必然是用错误的编码去解释二进制流. 在传输过程中必然都是以二进制流传输的. 所以,我们需要考虑的是: 有几个数 ...

  6. Java虚拟机学习 - 垃圾收集器 (4)

    HotSpot JVM收集器 上面有7中收集器,分为两块,上面为新生代收集器,下面是老年代收集器.如果两个收集器之间存在连线,就说明它们可以搭配使用. Serial(串行GC)收集器 Serial收集 ...

  7. Android四款系统架构工具

    开发者若想开发出一款高质量的应用,一款功能强大的开发工具想必是不可或缺的.开发工具简化了应用的开发流程,也能使开发者在应用开发本身投入更多的精力.本文就为大家带来4款实用的Android应用架构工具. ...

  8. Concurrency Managed Workqueue(一)workqueue基本概念

    一.前言 workqueue是一个驱动工程师常用的工具,在旧的内核中(指2.6.36之前的内核版本)workqueue代码比较简单(大概800行),在2.6.36内核版本中引入了CMWQ(Concur ...

  9. SSH2框架实现注冊发短信验证码实例

    这两天開始写程序了,让用SSH2框架,曾经没有接触过Java项目更没有接触过SSH2框架,所以用注冊開始了我Java之旅.后来发现,后台代码挺easy理解的,跟.net的差点儿相同.就是层与层之间的调 ...

  10. C++11新特性(1) 右值引用

    在C++中,左值(lvalue)是能够获取其地址的一个量.因为常常出如今赋值语句的左边.因此称之为左值.比如一个有名称的变量. 比如: int a=10; //a就是一个左值. 传统的C++引用,都是 ...