HDU 1045 Fire Net(DFS 与8皇后问题类似)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14780 Accepted Submission(s): 8932
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..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
1
5
2
4
题目意思:
题目给出一个n和一幅N*N的图,图中包含'.','X'两种字符,
要求在上面放上碉堡,碉堡能攻击他所在的行和列,但不能攻击墙,求最多能放多少个碉堡.
X是墙,不能放炮台
与八皇后类似,炮台不能存在于同行同列,除非中间隔墙
直接dfs
code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
char G[][];
int n,sum;
bool check(int x,int y)//检查该点能不能放炮台
{
if(G[x][y]=='X')//该点是墙,不能放直接退出
return false;
for(int i=x;i<n;i++)//判断该点的下面不能右炮台(除非有墙)
{
if(G[i][y]=='X')
break;
if(G[i][y]=='S')
return false;
}
for(int i=x;i>=;i--)//上
{
if(G[i][y]=='X')
break;
if(G[i][y]=='S')
return false;
}
for(int j=y;j<n;j++)//右
{
if(G[x][j]=='X')
break;
if(G[x][j]=='S')
return false;
}
for(int j=y;j>=;j--)//左
{
if(G[x][j]=='X')
break;
if(G[x][j]=='S')
return false;
}
return true;
}
void dfs(int k)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(check(i,j))
{
G[i][j]='S';
dfs(k+);
G[i][j]='.';
}
}
}
if(sum<k)
sum=k;
}
int main()
{
while(cin>>n,n)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>G[i][j];
}
}
sum=;
dfs();
cout<<sum<<endl;
}
return ;
}
HDU 1045 Fire Net(DFS 与8皇后问题类似)的更多相关文章
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- 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)
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- 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) ...
- HDU 1045 Fire Net 状压暴力
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1045 Fire Net 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- Servlet开发(一)
1. Servlet简介 Servlet是服务器端程序,主要用来交互式地浏览和修改数据,生成动态web内容.Servlet是SUN公司提供的一个接口,广义的Servlet可以指任何实现了Servlet ...
- PL/SQL: numeric or value error: character to number conversion error
在最简单的plsql块编程中出现这个错误,是因为 DBMS_OUTPUT.PUT_LINE('the x is '+x);这里面不能用“+”,而是要用“||” DECLARE x number; ; ...
- thinkphp引入头文件
<include File="Public:regheader" />
- express实现todolist
app.js var express = require('express'); var todoController = require('./controllers/todoController. ...
- 从零开始的全栈工程师——html篇1.5
列表与边距探讨和行块 一.列表 1.无序列表(UL) 1)内部必须有子标签<li></li>2)天生自带内外边距 p也是自带 大家会发现用UL的时候内容前面会出现一个像这样的一 ...
- arm汇编学习(六)---跳转到thumb状态
通常函数返回使用 pop {r7,pc}或bx lr等方式(bx,b类似jmp为跳转指令,但bx可以指定跳转区域究竟为thumb还是arm指令.thumb指令指令的时候,直接填写该地址却总是产生SIG ...
- recvfrom WSAEFAULT 10014 的错误记录
在使用 recvfrom 时一直没报错, 像这样 recvfrom(sock, Data, , (sockaddr*)&server_addr, &send_size) 当我看见百度百 ...
- 深入理解java的形参和实参
转载声明:本文转载自公众号「码匠笔记」. 前几天在头条上看到一道经典面试题,引发了一些思考.也是写这篇文章的导火索. 背景 请看题: public classMain{ publicsta ...
- .Net程序员应该掌握的正则表达式
Regular Expression Net程序员必然要掌握正则的核心内容:匹配.提取.替换.常用元字符. 正则表达式是用来进行文本处理的技术,是语言无关的,在几乎所有语言中都有实现. 常用元字符 . ...
- .net 面向对象程序设计深入](1)UML
1.UML简介 Unified Modeling Language (UML)又称统一建模语言或标准建模语言. 简单说就是以图形方式表现模型,根据不同模型进行分类,在UML 2.0中有13种图,以下是 ...