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.
  • 相关阅读:
    【图论】拓扑排序应用
    【图论】广度优先搜索和深度优先搜索
    最小生成树-Prim算法和Kruskal算法
    最短路径—Dijkstra算法和Floyd算法
    【图论】信手拈来的Prim,Kruskal和Dijkstra
    javascript获取iframe框架中页面document对象,获取子页面里面的内容,iframe获取父页面的元素,
    javascript 中的 true 或 false
    解决IIS7该问.svc文件的错误问题
    mysql常用函数
    异步上传文件,ajax上传文件,jQuery插件之ajaxFileUpload
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5671730.html
Copyright © 2011-2022 走看看