HDU 4499 Cannon (暴力求解)

题意:给定一个n*m个棋盘,放上一些棋子,问你最多能放几个炮(中国象棋中的炮)。

析:其实很简单,因为棋盘才是5*5最大,那么直接暴力就行,可以看成一行,很水,时间很短,才62ms。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int a[30];
int ans;

bool isly(int x){
    int b[] = { x+1, x-1, x+m, x-m};
    int d[] = { x/m*m+m-1, x/m*m, n-1, 0};
    int c[] = { 1, -1, m, -m};

    for(int i = 0; i < 4; ++i){
        bool ok = false;
        for(int j = b[i]; (i & 1 ? j >= d[i] : j <= d[i]); j += c[i]){
            if(ok && a[j] != -1){
                if(a[j] == 1)  return false;
                else break;
            }
            else if(!ok && a[j] != -1) ok = true;
        }
    }
    return true;
}

void dfs(int x, int cnt){
    ans = max(ans, cnt);
    for(int i = x; i < n; ++i){
        if(a[i] != -1 || !isly(i))  continue;
        a[i] = 1;
        dfs(i+1, cnt+1);
        a[i] = -1;
    }
}

int main(){
    int c;
    while(scanf("%d %d %d", &n, &m, &c) == 3){
        n *= m;
        memset(a, -1, sizeof(a));
        for(int i = 0; i < c; ++i){
            int x, y;
            scanf("%d %d", &x, &y);
            a[x*m+y] = 0;
        }

        ans = 0;
        dfs(0, 0);
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-05 15:25:19

HDU 4499 Cannon (暴力求解)的相关文章

hdu 4499 Cannon(暴力)

题目链接:hdu 4499 Cannon 题目大意:给出一个n*m的棋盘,上面已经存在了k个棋子,给出棋子的位置,然后求能够在这种棋盘上放多少个炮,要求后放置上去的炮相互之间不能攻击. 解题思路:枚举行放的情况,用二进制数表示,每次放之前推断能否放下(会不会和已经存在的棋子冲突),放下后推断会不会互相攻击的炮,仅仅须要对每一个新加入的炮考虑左边以及上边就能够了. #include <cstdio> #include <cstring> #include <algorithm&

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

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 ACM 4499 Cannon 暴力DFS

分析:N和M很小,直接暴力搜索即可. #include<iostream> using namespace std; #define N 6 #define M 6 int vis[N][M]; //有炮设为2,无炮为0,其他为1 int n,m,q,ans; #define max(a,b) ((a)>(b)?(a):(b)) bool Valid(int i,int j) //只需要判断当前和前面的即可(行列) { int fg=0; int k; if(vis[i][j]==1)

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 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 eac

HDU 4499 Cannon

题意: 思路: #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; const int MAXN

HDU 1431 素数回文【暴力求解】

/* 题目大意:找一个范围内的所有素数回文数 解题思路:打一个表将1亿以内所有的素数回文数找出来,大概有780个这样子 关键点:暴力求解 解题人:lingnichong 解题时间:2014-08-29 12:02:55 解题体会:如果按一般方法打个素数表,很容易超内存(MLE),所以就先将所有的素数回文全部算出来,再在这个数组里面找在题上那个范围的所有素数回文数 */ 素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655

第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)

/** * 最大子数组的暴力求解算法,复杂度为o(n2) * @param n * @return */ static MaxSubarray findMaxSubarraySlower(int[] n) { long tempSum = 0; int left = 0; int right = 0; long sum = Long.MIN_VALUE; for (int i = 0; i < n.length; i++) { for (int j = i; j < n.length; j++