• HDU4499


    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
    --------------------------------暴力--------------看不懂过程就输出我注释掉的再观察一下
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    typedef long long LL;
    const int MAXN = 15;
    const int INF = 0x3f3f3f3f;
    
    int n, m, dis[][2] = {0, 1, 1, 0, 0, -1, -1, 0};
    int vis[MAXN][MAXN], ans = 0;
    
    bool check(int x, int y)
    {
        int i, j, k;
        int flag;
        for(k = 0;k < 4;++k)
        {
            flag = 0;
            i = x + dis[k][0];
            j = y + dis[k][1];
            while(i >= 0 && j >= 0 && i < n && j < m)
            {
                if(flag && vis[i][j] == 1)
                    return false;
                else if(flag && vis[i][j] == -1)
                    break;
                if(vis[i][j] && !flag)
                    flag++;
                i += dis[k][0];
                j += dis[k][1];
            }
        }
        return true;
    }
    
    void dfs(int x, int y, int cnt)
    {
        if(x >= n)
        {
            ans = max(ans, cnt);
    //        cout << endl;
    //        for(int i = 0;i < n;++i)
    //        {
    //            for(int j = 0;j < m;++j)
    //                cout << vis[i][j] << " ";
    //            cout << endl;
    //        }
            return ;
        }
        if(y != m - 1)
        {
            dfs(x, y + 1, cnt);
            if(vis[x][y] != -1 && check(x, y))
            {
                vis[x][y] = 1;
                dfs(x, y + 1, cnt + 1);
                vis[x][y] = 0;
            }
        }
        else
        {
            dfs(x + 1, 0, cnt);
            if(vis[x][y] != -1 && check(x, y))
            {
                vis[x][y] = 1;
                dfs(x + 1, 0, cnt + 1);
                vis[x][y] = 0;
            }
        }
    }
    
    int main()
    {
        int q;
        while(scanf("%d%d%d", &n, &m, &q) != EOF)
        {
            memset(vis, 0, sizeof(vis));
            ans = 0;
            int x, y;
            while(q--)
            {
                scanf("%d%d", &x, &y);
                vis[x][y] = -1;
            }
            dfs(0, 0, 0);
            printf("%d
    ", ans);
        }
        return 0;
    }
     
    现在所有的不幸都是以前不努力造成的。。。
  • 相关阅读:
    4.10Python数据处理篇之Matplotlib系列(十)---文本的显示
    4.9Python数据处理篇之Matplotlib系列(九)---子图分布
    4.8Python数据处理篇之Matplotlib系列(八)---Figure的学习
    4.7Python数据处理篇之Matplotlib系列(七)---matplotlib原理分析
    4.6Python数据处理篇之Matplotlib系列(六)---plt.hist()与plt.hist2d()直方图
    4.5Python数据处理篇之Matplotlib系列(五)---plt.pie()饼状图
    4.4Python数据处理篇之Matplotlib系列(四)---plt.bar()与plt.barh条形图
    4.3Python数据处理篇之Matplotlib系列(三)---plt.plot()折线图
    Angular 定时器$timeout和$interval,延时调用
    javascript中top、clientTop、scrollTop、offsetTop的讲解(转载加总结)
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/9449488.html
Copyright © 2020-2023  润新知