• hdu 4499 Cannon dfs


    Cannon

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=4499

    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.

    Sample Input

    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

    Sample Output

    8
    9

    HINT

    题意

    在一个象棋棋盘上放炮,要求两个炮不能互相打到,然后问你最多能放几个炮

    题解:

    直接dfs就好了,范围很小

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)  
    #define maxn 2000001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int a[30],b[30],visit[7][7],n,m,q,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(visit[x][y]){
            dfs(x,y+1,cnt);
            return;
        }
        dfs(x,y+1,cnt);
        int t,flag=0;
        for(t=y-1;t>=0;t--)
            if(visit[x][t]) break;
        for(int i=t-1;i>=0;i--)
        {
            if(visit[x][i]==2) {flag=1;break;}
            if(visit[x][i]) break;
        }
        if(flag)return;//判断这一列上是否存在炮互吃
        for(t=x-1;t>=0;t--)
            if(visit[t][y]) break;
        for(int i=t-1;i>=0;i--){
            if(visit[i][y]==2) {flag=1;break;}
            if(visit[i][y]) break;
        }
        if(flag) return;//判断这一行上是否存在炮互吃
        visit[x][y]=2;
        dfs(x,y+1,cnt+1);
        visit[x][y]=0;//回溯
    }
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&q)!=EOF){
            memset(visit,0,sizeof(visit));
            for(int i=0;i<q;i++){
                scanf("%d%d",&a[i],&b[i]);
                visit[a[i]][b[i]]=1;
            }
            ans=0;
            dfs(0,0,0);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Tomcat6.0的Thisisverylikelytocreateamemoryleak异常
    一个不错的能将HTML表格导出为excel,pdf等的jquery插件
    关于tomcat8在windows2008下高并发下问题的解决方案
    应用部署到JBOSS上遇到的问题
    [置顶] 将项目从tomcat 迁移到JBoss
    struts2 if标签示例
    用Java集合中的Collections.sort方法对list排序的两种方法
    SAP 关于标准成本、计划成本、目标成本、实际成本
    “癌症村”里的“净水神器”
    iis虚拟目录引发的路径问题
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4548442.html
Copyright © 2020-2023  润新知