HDU 1045 Fire Net(DFS)
2016-03-30 21:32
441 查看
Fire Net
[align=left]Problem Description[/align]Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
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.
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <algorithm> using namespace std; char mp[120][120]; int n,ans; bool check(int x,int y) { int i; if(mp[x][y]!='.') return false; if(x>=0)//up for(i=x; i>=0; i--) if(mp[i][y]=='X') break; else if(mp[i][y]=='1') return false; if(x<n)//down for(i=x; i<n; i++) if(mp[i][y]=='X') break; else if(mp[i][y]=='1') return false; if(y>=0)//left for(i=y; i>=0; i--) if(mp[x][i]=='X') break; else if(mp[x][i]=='1') return false; if(y<n)//right for(i=y; i<n; i++) if(mp[x][i]=='X') break; else if(mp[x][i]=='1') return false; return true; } void dfs(int m) { int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(check(i,j)) { mp[i][j]='1'; dfs(m+1); mp[i][j]='.'; } } } ans=max(ans,m); } int main() { while(cin>>n&&n) { ans=0; for(int i=0; i<n; i++) cin>>mp[i]; dfs(0); printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- php7 读取php.ini[4]
- BZOJ 2822 AHOI 2012 树屋阶梯 卡特兰数 高精度
- Structs
- C# kinect v2学习笔记(四) 深度图像
- 协方差详解
- Android中ListView分页加载数据
- 【LeetCode】18. 4Sum
- 转换构造函数
- css中文本超出部分省略号代替
- c++ primer —— 练习 14.2
- shell教程八:except脚本 实现无密登录
- 新的开始,走在路上
- 初学Qt之--在Qt中调用外部C语言模块
- caffe中cifar10的full_train模型出现nan的解决办法
- poj-1905-Expanding Rods
- Java解析XML的四种方法--DOM4j
- ++与--(程序中不经意间出的错误记录)
- VC 调试中一些常见的错误信息及解决方法
- 大数快速运算法则
- TuShare(2):使用TuShare,抓取股票数据并存储到数据库