zoukankan      html  css  js  c++  java
  • 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 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
     
     
    题意:在n×m的棋盘上面有Q的棋子,它们之间不可以相互吃对方。现在要在棋盘上面增加棋子“炮”,问最多可以增加多少个炮使得炮之间不能相互吃对方(1<= N, M<=5, 0<=Q <= N x M)。炮a吃棋子b的规则是,a和b在一行或者一列,a和b之间有一个棋子。
     
    思路:n和m很小,直接暴力BFS搜索。注意炮可以增加的规则。
     
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int n,m;
    int ans=0;
    int edge[10][10];
    int dfs(int x,int y,int cou)
    {
        int i,j,t,sign,ok;
    /*
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
                cout<<edge[i][j]<<" ";
            cout<<endl;
        }
        cout<<endl;
    */
        if(cou>ans) ans=cou;
        edge[x][y]=2;
        /**当前行可以增加*/
        for(j=y+1; j<n; j++)
        {
            if(edge[x][j]==1) continue;
            sign=0;
            ok=1;
            for(t=j-1; t>=0; t--)
            {
                if(edge[x][t]!=0) sign++;
                if(sign==2&&edge[x][t]==2)
                {
                    ok=0;
                    break;
                }
            }
            if(ok==1)
            {
                sign=0;
                for(t=x-1; t>=0; t--)
                {
                    if(edge[t][j]!=0) sign++;
                    if(sign==2&&edge[t][j]==2)
                    {
                        ok=0;
                        break;
                    }
                }
                if(ok==1)
                {
                    edge[x][j]=2;
                    dfs(x,j,cou+1);
                    edge[x][j]=0;
                }
            }
        }
        /**当前行不能增加,加入后面的行*/
        for(i=x+1; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(edge[i][j]==1) continue;
                sign=0;
                ok=1;
                for(t=i-1; t>=0; t--)
                {
                    if(edge[t][j]!=0) sign++;
                    if(sign==2&&edge[t][j]==2)
                    {
                        ok=0;
                        break;
                    }
                }
                if(ok==1)
                {
                    edge[i][j]=2;
                    dfs(i,j,cou+1);
                    edge[i][j]=0;
                }
            }
        }
    }
    int main()
    {
        int i,j,q;
        int x,y;
        while(scanf("%d%d%d",&n,&m,&q)!=EOF)
        {
            memset(edge,0,sizeof(edge));
            while(q--)
            {
                scanf("%d%d",&x,&y);
                edge[x][y]=1;
            }
            ans=0;
            for(i=0; i<n; i++)
                for(j=0; j<m; j++)
                    if(edge[i][j]==0)
                    {
                        edge[i][j]=2;
                        dfs(i,j,1);
                        edge[i][j]=0;
                    }
            cout<<ans<<endl;
        }
        return 0;
    }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    EF5+MVC4系列(5) 删除的方法 1:系统推荐的先查询后remove删除的方法 2:自己new一个包含主键的类,然后 attach附加 remove删除;3:使用db.Entry 修改状态删除4:EntityState的几种状态
    指定webapi 返回 json 格式 ; GlobalConfiguration.Configuration.Formatters.Clear()
    远程桌面连接工具 Remote Desktop Manager 9.1.2.0 Enterprise 多国语言绿色版附注册码 简单使用
    .NET WebAPI 正确抛出错误详细信息
    观察者模式 发布订阅者模式
    7月目标 socket , 一致性哈希算法 ; mongodb分片; 分布式消息队列; 中间件的使用场景
    在js中 把 json对象转化为String对象的方法
    mvc4中的 webapi 的使用方式
    Uncaught TypeError: TableInit is not a constructor
    Jquery复选框操作
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5671730.html
Copyright © 2011-2022 走看看