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 namespace std;
using  namespace  std;
int n,m,t,ans;
int ma[M][M];
bool bfs(int x,int y)//判断x,y能不能放
{
	int xx=x-1;
	int cnt=0;
	while(xx>=0)
	{

		if(ma[xx][y]==1&&cnt==1) return false;
		if(ma[xx][y]==1||ma[xx][y]) cnt++;
		xx--;
	}
	xx=x+1;
	cnt=0;
	while(xx<n)
	{

		if(ma[xx][y]==1&&cnt==1) return false;
		if(ma[xx][y]==1||ma[xx][y]) cnt++;
		xx++;
	}
	int yy=y-1;
	cnt=0;
	while(yy>=0)
	{

		if(ma[x][yy]==1&&cnt==1) return false;
		if(ma[x][yy]==1||ma[x][yy]) cnt++;
		yy--;
	}
	yy=y+1;
	cnt=0;
	while(yy<m)
	{

		if(ma[x][yy]==1&&cnt==1) return false;
		if(ma[x][yy]==1||ma[x][yy]) cnt++;
		yy++;
	}
	return true;
}
void dfs(int x,int y,int tmp)
{
	if(tmp>ans) ans=tmp;
	for(int i=x;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(i==x&&j<y) continue;
			if(ma[i][j]!=-1&&bfs(i,j))
			{
				ma[i][j]=1;
				dfs(i,j+1,tmp+1);
				ma[i][j]=0;
			}
		}
	}
}
int main()
{
	  while(~scanf("%d%d%d",&n,&m,&t))
	  {
        memset(ma,0,sizeof(ma));
        int  x,y;
        while (t--)
		{
            scanf( "%d %d" ,&x,&y);
            ma[x][y]=-1;
        }
        ans=0;
        dfs(0,0,0);
        printf("%d\n",ans);
    }
}

HDU 4499 Cannon (暴力搜索),布布扣,bubuko.com

时间: 2024-10-09 15:42:34

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 搜索

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 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 (暴力求解)

题意:给定一个n*m个棋盘,放上一些棋子,问你最多能放几个炮(中国象棋中的炮). 析:其实很简单,因为棋盘才是5*5最大,那么直接暴力就行,可以看成一行,很水,时间很短,才62ms. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include &

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 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

hdu 1399 Starship Hakodate-maru (暴力搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399 题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值. 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while(scanf("%d",&n),n) 10 { 11 int j,