HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏
Fire Net
Problem Description
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.
Input
be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.
Output
Sample Input
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
5
1
5
2
4
————————————————————————————————————————————————————————————
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int n,maxnum;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
char mp[6][6];
bool cheak(int x, int y)
{
int flag = 0;
if (x >= 1 && x <= n&&y >= 1 && y <= n&&mp[x][y] == '.')
{
flag = 1;
for (int i = x; i >= 1; i--)
{
if (mp[i][y] == '#')
flag = 0;
if (mp[i][y] == 'X')
break;
}
for (int i = y; i >= 1; i--)
{
if (mp[x][i] == '#')
flag = 0;
if (mp[x][i] == 'X')
break;
}
}
return flag;
}
void dfs(int x,int y,int num)
{
if (x == n && y == n)
{
if (cheak(x, y))
{
num++;
}
if (num > maxnum)
maxnum = num;
return;
} if (cheak(x, y))
{
mp[x][y] = '#';
if (y < n)
dfs(x, y + 1, num+1);
else
dfs(x + 1, 1, num+1); mp[x][y] = '.';
}
if (y < n)
dfs(x, y + 1, num);
else
dfs(x + 1, 1, num);
return;
}
int main()
{
while (~scanf(" %d",&n)&&n)
{
for (int i = 1; i <= n;i++)
for (int j = 1; j <= n; j++)
{
scanf(" %c", &mp[i][j]);
}
maxnum = 0;
dfs(1, 1, 0);
printf("%d\n", maxnum); }
return 0;
}
方式二:二分图匹配
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN; //u,v数目
int g[MAXN][MAXN];//编号是0~n-1的
int linker[MAXN];
bool used[MAXN]; bool dfs(int u)
{
int v;
for(v=0; v<vN; v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
} int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=0; u<uN; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
} int main()
{
char s[10][10];
int x[10][10],y[10][10];
int n,n1,n2;
while(~scanf("%d",&n)&&n)
{
memset(x,0,sizeof x);
memset(y,0,sizeof y);
for(int i=0; i<n; i++)
{
scanf("%s",&s[i]);
}
uN=vN=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n;)
{
while(s[i][j]=='X'&&j<n)
{
x[i][j]=-1;
j++;
} while(s[i][j]!='X'&&j<n)
{
x[i][j]=uN;
j++;
}
uN++;
}
} for(int j=0; j<n; j++)
{
for(int i=0; i<n;)
{
while(s[i][j]=='X'&&i<n)
{
y[i][j]=-1;
i++;
} while(s[i][j]!='X'&&i<n)
{
y[i][j]=vN;
i++;
}
vN++;
}
}
memset(g,0,sizeof g);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
if(x[i][j]!=-1&&y[i][j]!=-1)
g[x[i][j]][y[i][j]]=1;
}
printf("%d\n",hungary()); }
return 0;
}
HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏的更多相关文章
- HDU1181 变形课(DFS) 2016-07-24 13:31 73人阅读 评论(0) 收藏
变形课 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒 ...
- HDU2212 DFS 2016-07-24 13:52 56人阅读 评论(0) 收藏
DFS Problem Description A DFS(digital factorial sum) number is found by summing the factorial of eve ...
- HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏
Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...
- 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏
#include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...
- 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...
- 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
随机推荐
- No matter how hard it is or no matter how bad it gets, I am going to make it!
No matter how hard it is or no matter how bad it gets, I am going to make it! He always had a yearni ...
- HTML 求阶乘之和
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- java.lang.NullPointerException - 如何处理空指针异常
当应用程序试图null在需要对象的情况下使用时抛出.这些包括: 调用null对象的实例方法. 访问或修改null对象的字段. 把长度null当作一个数组. 像访问或修改null阵列一样访问或修改插槽. ...
- Containerpilot 配置文件 之 Watches
watch是在consul进行监视的服务配置. watch轮询服务的状态,并在服务变得健康,变得不健康或者实例数量发生变化时发出事件. 请注意,watch不包括行为; watch只发出事件,以便job ...
- 归并排序/合并排序c++实现
#include <iostream>#include<string.h> using namespace std;class merges{public:void merge ...
- ios 8 联系人ABPeoplePickerNavigationController
一. ios 联系人ABPeoplePickerNavigationControllerDelegate方法,新添加下面两个联系人选中方法,适配iOS8需要实现 // Called after a p ...
- tmpFile.renameTo(classFile) failed解决
完整异常: 严重: Servlet.service() for servlet [bjbr] in context with path [/HerPeisWechat] threw exception ...
- 查看端口号根据pid号找到相关占用端口应用
查看端口号根据pid号找到相关占用端口应用 8080 端口被占用不知道被哪个应用软件占用,下面我来教你查出那个该死的应用 方法/步骤 1 首先用netstat 找到端口对应的pid号,找到之后 ...
- Course Schedule课程表12(用Topological Sorting)
[抄题]: 现在你总共有 n 门课需要选,记为 0 到 n - 1.一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]给定n门课以及他们的先决条件,判 ...
- JFinal WEB MVC和Struts简要对比
JFinal遵循COC原则,零配置,无xml,而struts需要配置来支持action.result.interceptor配置与使用. JFinal开发效率非常之高,相对Struts开发效率能提升五 ...