Cannon

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;
}

时间: 2024-10-27 05:14:20

Cannon的相关文章

HDU4499 Cannon DFS 回溯的应用

题意就是给你一个n*m的棋盘,然后上面已经有了 棋子,并给出这些棋子的坐标,但是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮,但是炮之间不能互相吃,吃的规则我们斗懂得 炮隔山打嘛,问你最多能放几个炮 肯定是搜索了,n,m最大才5,可能挺久没做了,对于回溯反而把握不好了,写了好久调试了好久,才过 #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

hdu 4499 Cannon 暴力dfs搜索

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 338 Problem Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move

HDOJ 5091 Beam Cannon 扫描线

线段树+扫描线: 我们用矩形的中心点来描述这个矩形,然后对于每个敌舰,我们建立一个矩形中心的活动范围,即矩形中心在该范围内活动就可以覆盖到该敌舰.那么我们要求的问题就变成了:任意一个区域(肯定也是矩形的)最多能被矩形覆盖的最大值. Beam Cannon Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 159    Accepted S

HDU 4499.Cannon 搜索

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 965    Accepted Submission(s): 556 Problem Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move

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

题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,现在要有一个W?H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,假设有点(x,y),那么(x,y)~(x+W,y+H)形成的矩形,以框的右下角落的位置是可以圈住(x,y) 点,所以N个点即为N个矩形,求覆盖的最大次数,扫描线裸题. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&

hdu4499 Cannon (DFS+回溯)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4499 Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 214 Problem Des

线段树+扫描线 HDOJ 5091 Beam Cannon

题目传送门 1 /* 2 题意:给出若干个点的坐标,用一个矩形去覆盖,问最多能覆盖几个点 3 线段树+扫描线:思路是先建一棵以[y, y + h]的树,左右儿子[x, x + w] 4 以这棵树为范围,从左到右扫描,更新点数,x的+1, x+w的-1(超过矩形范围) 5 ans = 每次更新时所覆盖点数的最大值 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <iostream> 10 #includ

hdu 5091 Beam Cannon 离散化+扫描线+线段树

Beam Cannon Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 207 Problem Description Recently, the γ galaxies broke out Star Wars. Each planet is warring for resou