hdu 1045:Fire Net(DFS经典题)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5863 Accepted Submission(s): 3280
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
.X..
....
XX..
.... XX
.X .X.
X.X
.X. ...
.XX
.XX ....
....
....
....
#include <iostream> using namespace std;
char a[][];
int cnt,n;
bool judge(int x,int y) //判断这一步可不可以走
{
if(a[x][y]=='X')
return ;
if(a[x][y]=='*')
return ;
int i;
//判断这一行上有无碉堡
for(i=y;i>=;i--){
if(a[x][i]=='*')
return true;
if(a[x][i]=='X')
break;
}
for(i=y;i<=n;i++){
if(a[x][i]=='*')
return true;
if(a[x][i]=='X')
break;
}
//判断这一列上有无碉堡
for(i=x;i>=;i--){
if(a[i][y]=='*')
return ;
if(a[i][y]=='X')
break;
}
for(i=x;i<=;i++){
if(a[i][y]=='*')
return ;
if(a[i][y]=='X')
break;
}
//可以走
return ;
}
void dfs(int cx,int cy,int cn)
{
if(cn>cnt) cnt=cn; //记录最大值
int x=-,y=-;
int i,j;
for(i=cx;i<=n;i++) //选择这一步的位置
for(j=;j<=n;j++){
if(i==cx && j<=cy)
continue;
if(judge(i,j))
continue;
x=i;y=j;
a[x][y] = '*';
dfs(x,y,cn+);
a[x][y] = '.';
}
if(x==- && y==-)
return ;
}
int main()
{
while(cin>>n){
if(n==) break;
int i,j;
for(i=;i<=n;i++) //输入地图
for(j=;j<=n;j++)
cin>>a[i][j];
cnt = ;
dfs(,,); //深搜
cout<<cnt<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1045:Fire Net(DFS经典题)的更多相关文章
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1045 Fire Net(DFS)
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 1045 Fire Net(dfs,跟8皇后问题很相似)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
- HDU 1045(Fire Net)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...
- HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】
Fire Net Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- HDU 1045 Fire Net 状压暴力
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- Unity Shaders Vertex & Fragment Shader入门
http://blog.csdn.net/candycat1992/article/details/40212735 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Sha ...
- 多线程 用户级线程和内核级线程 from C++多核高级编程
转 http://book.51cto.com/art/201006/206946.htm 6.1.1 用户级线程和内核级线程 2010-06-21 20:37 齐宁/董泽惠 译 清华大学出版社 字号 ...
- Windows 2008远程多用户登录的配置方法(转载)
在使用Windows2008远程登录功能时,如果需要进行多用户登录,可以采用以下配置方法: 首先要启用远程桌面这一功能:右击“我的电脑”→属性→远程配置→远程桌面,就可以配置相应的远程桌面功能了.下 ...
- 如何卸载lnmp
转自:https://www.centos.bz/2011/07/howto-remove-lnmp/ killall nginx /etc/init.d/mysql stop killall mys ...
- hdu.5202.Rikka with string(贪心)
Rikka with string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Power Network(网络流最大流 & dinic算法 + 优化)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24019 Accepted: 12540 D ...
- Parencodings(imitate)
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20679 Accepted: 12436 De ...
- The Dragon of Loowater
The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into ...
- [BZOJ1064][Noi2008]假面舞会
[BZOJ1064][Noi2008]假面舞会 试题描述 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢 ...
- ubuntu 快速安装jre
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-i ...