hdu4499 Cannon DFS

kaggle项目:纽约出租车行程时间NYC Taxi Trip Duration 纽约出租车形成时间预测(NYC Taxi Trip Duration)是Kaggle中的一个Playground竞赛。比赛数据集基于2016年纽约市黄色出租车行程记录数据,需要建立一个模型,根据单个行程属性,预测测试集中每次行程的持续时间。Kaggle项目模型XGBoost,评价指标RMSLE。 阅读详情

Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1062    Accepted Submission(s): 608


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
 


类似于八皇后,只是把皇后换成了中国象棋的炮,而且棋盘上原先就有一些棋子,问最后最多能放多少个不相互攻击的炮,数据量很小,直接DFS搜索


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int INF = 0x7fffffff;

int n, m, q, ans;
int mp[10][10];

void dfs(int x, int y, int cnt) {
	if (x >= n) {
		ans = max(ans, cnt);
		return;
	}
	if (y >= m) {
		dfs(x + 1, 0, cnt);
		return;
	}
	if (!mp[x][y]) {
		int i;
		bool flag = true;
		for (i = x - 1; i >= 0 && !mp[i][y]; i--) {}
		i--; //因为炮要隔一个棋子攻击
		for (; i >= 0 && !mp[i][y]; i--) {}
		if (i >= 0 && mp[i][y] == 2) flag = false;

		if (flag) {
			for (i = x + 1; i < n && !mp[i][y]; i++) {}
			i++;
			for (; i < n && !mp[i][y]; i++) {}
			if (i < n && mp[i][y] == 2) flag = false;
		}

		if (flag) {
			for (i = y - 1; i >= 0 && !mp[x][i]; i--) {}
			i--;
			for (; i >= 0 && !mp[x][i]; i--) {}
			if (i >= 0 && mp[x][i] == 2) flag = false;
		}

		if (flag) {
			for (i = y + 1; i < m && !mp[x][i]; i++) {}
			i++;
			for (; i < m && !mp[x][i]; i++) {}
			if (i < m && mp[x][i] == 2) flag = false;
		}
		if (flag) {
			mp[x][y] = 2;
			dfs(x, y + 1, cnt + 1); //当前位置放炮
			mp[x][y] = 0;
		}
	}
	//当前位置不放炮,此时原因有三种
	//1.当前位置有棋子了
	//2.当前位置放炮会相互攻击而不放
	//3.故意不放,为后面DFS放其他棋子腾空间
	//但是效果是等价的,都是不放,所以合并为一条
	dfs(x, y + 1, cnt);
}

int main()
{
	while (~scanf("%d%d%d", &n, &m, &q)) {
		memset(mp, 0, sizeof(mp));
		for (int i = 0; i < q; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			mp[x][y] = 1;
		}
		ans = -INF;
		dfs(0, 0, 0);
		printf("%d\n", ans);
	}
	return 0;
}




大中型超市收银系统(可直接运行) 光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开 立即下载

相关推荐

A-Level CS 计算机科学 考试知识点——考试介绍&第1章

前言:该系列文章是本人在给学生辅导本课程的过程中结合英文教材所总结的,该系列共分为30章,主要介绍了考试的核心知识点。若需要A-Level CS 计算机科学考试辅导也可以私信我。 目录 关于A-Level CS 计算机科学考试 考试简介 考试内容 考试时间 评分情况 第1章 信息表示 本章学习内容 1.01 数字系统 1.02 数字的内部编码 整数编码(Coding for integers) 二进制编码的十进制(Binary coded decimal——BCD) 1.03.

王俊的博客 6454

*dfs: Cannon

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 empt

Davenny:~我今天该吃点什么捏~ 387

从CAD图纸到GIS地图:手把手教你用Global Mapper处理DWG文件(含坐标配准)

本文详细介绍了如何使用Global Mapper将CAD图纸(DWG文件)转换为GIS地图,包括坐标配准、图层与属性转换等关键步骤。通过四参数变换和控制点匹配技术,确保数据精准转换,适用于测绘、城市规划等领域。文章还提供了高级处理技巧和质量控制流程,帮助用户高效完成GIS化工作。

weixin_29196891的博客 520

HDU-4499 Cannon (DFS)

很简单的搜索,但是还是WA了好久,按行枚举总出错(已经考虑每行可以放三个的情况),改成按点枚举就AC。。。

idealism_xxm的专栏 525

多思计算机组成原理实验:模型机课程设计

计算机组成原理实验教程 基于多思网络虚拟实验系统 张雯雰

Nick的博客 2万+

STM32CubeMx + FatFs 实战:SD卡DMA传输中断回调问题深度解析

本文深度解析了在STM32CubeMx环境下,结合FatFs文件系统并使用DMA进行SD卡读写时,程序可能出现的“卡死”问题。核心原因在于HAL库的弱函数回调机制未被正确处理,导致DMA传输完成中断后,状态机锁死。文章提供了两种实战修复方案:直接修改HAL库文件或在用户代码中重写回调函数,并给出了关键的配置检查点与调试技巧,帮助开发者彻底解决SD卡DMA传输中断回调失效的难题。

weixin_29176179的博客 330

HDU4499 - Cannon->DFS

G - Cannon Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4499 Description In Chinese Chess, there is one kind of powerful ch

qq_18661257的专栏 907

基于51单片机语音识别智能台灯设计

智能台灯;语音识别;STC89C52RC单片机

Benso_H的博客 5842

hdu 4499 Cannon(DFS)

2013年ACM亚洲赛区通化邀请赛的G题...还在练习中...

Visioner的自留地 1086

从芯片选型到光学集成:手持激光测距仪硬件设计全解析

本文全面解析手持激光测距仪的硬件设计,从芯片选型到光学集成,详细介绍了相位法测距技术的核心原理与方案选择。通过模块化设计、信号链路优化及PCB布局技巧,实现毫米级精度。文章还分享了量产化关键控制点,帮助工程师规避常见设计陷阱,提升产品可靠性。

weixin_30425949的博客 445

Spring AI Alibaba实战训练营-05模型输出质量评估

本文介绍了使用Spring AI Alibaba库评估AI模型输出质量的方案。项目通过5种评估器实现自动化评估:相关性评估、事实准确性评估、答案相关性评分、答案正确性评估和忠实度评估。开发步骤包括环境准备(Java 17、Maven、API Key获取)、创建Maven项目、配置Spring AI(通过环境变量安全设置API Key)以及实现RAG流程。重点强调了通过评估器自动检测AI输出的质量指标,帮助开发者优化AI应用性能。

program哲学 216

HDU 4499 Cannon (暴力搜索,回溯)

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 186 Problem Description In Chinese Chess, there

风尘_浪子 823

【知识分享】单端到混合S参数转换指南

信号完整性(SI)工程师几乎总是必须使用S参数,因为许多的高速接口和行业标准都正在转向串行链路架构,并使用基于S参数测量的频域限制指标,了解其工作过程将会很有意义。

一只豌豆象的博客 1576

HDU4499 Cannon DFS 回溯的应用

HDU4499 Cannon DFS 回溯的应用 题意就是给你一个n*m的棋盘,然后上面已经有了 棋子。并给出这些棋子的坐标,可是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮。可是炮之间不能互相吃。吃的规则我们斗懂得 炮隔山打嘛。问你最多能放几个炮 肯定是搜索了,n,m最大才5,可能挺久没做了,对于回溯反而把握不好了。...

weixin_30325971的博客 93

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

hdu4499Cannon dfs暴搜

//在一个n*m(n,m<=5)的棋盘上原来放着棋子 //在棋盘的空位上放炮,要求一个炮不能吃到 //另一个 #include #include #include using namespace std ; const int maxn = 10 ; int map[maxn][maxn] ; int n , m ; int judge(int i , int j) { int flag

ijbuhv的专栏 644

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

hdu 4499 Cannon dfs

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4499 题意: 在一个象棋棋盘上放炮,要求两个炮不能互相打到,然后问你最多能放几个炮 题解: dfs 一直dfs到最后一行最后一列 就可以 每个点只往上和往左扫了 vis[x][y]= 0 表示这个点没有棋子 1表示 普通的棋子 2表示 炮 代码: 1 #include ...

weixin_30300523的博客 105

hdu 4499 Cannon 暴力dfs搜索

题意:给你一个n*m的棋盘,有q个点已经放了棋。问最多还能放多少炮。要求炮与炮之间不能互相攻击。也就是两个炮直接不能隔着炮或者隔着棋。 做法:棋盘大小最大25个格子。2^25=3*10^7。如果枚举每个状态还要判断可不可行会超时。 所以我用了dfs。每次那个位置如果没有棋,就摆上炮dfs下去,每次摆判断和正上方一排冲突不冲突,还有左边一排冲不冲突。不冲突才可以摆。 再把炮去掉dfs下去。如果没有棋,就dfs不摆炮就行了。

万事屋 1045

HDU - 4499 Cannon 暴力dfs

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 th...

还我笑颜 276

hdu4499 Cannon (DFS+回溯)

DFS+回溯

2010

HDU-4499-Cannon(DFS剪枝)

Cannon 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 ...

qq_43461168的博客 212

HDU 4499 Cannon (DFS回溯)

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 999    Accepted Submission(s): 573 Problem Description In Chinese Chess, ther

aozil_yang的博客 461

hdu 4499 Cannon(暴力)

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

weixin_30323961的博客 79
上一篇: hdu5935 Car 贪心 卡精度
下一篇: hdu4496 D-City 并查集
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值