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 ...
随机推荐
- Delphi操作Excel(Use Oel)
Use ComObj: procedure TorderMore1.BitBtn2Click(Sender: TObject);var xlsFile:WideString; var ExcelA ...
- js中对话框的使用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- JDK、JRE和JAR区别(转载)
JDK里面的工具也是用Java编写的,它们本身运行的时候也需要一套JRE,如C:/Program Files/Java/jdk1.5.x/目录下的JRE.而C:/Program Files/Java/ ...
- ionic2
拨打电话: <access origin="tel:*" launch-external="yes" /> 发邮件: <access orig ...
- jquery获取input file的文件名,具有兼容性
var str=$(this).val();var arr=str.split('\\');//注split可以用字符或字符串分割var fileName=arr[arr.length-1];//这就 ...
- (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数
原版 sorted [抄题]: [思维问题]: 存sum - nums[i](补集),若出现第二次则调出 [一句话思路]: hashmap中,重要的数值当做key,角标当做value. [画图]: [ ...
- [leetcode]250. Count Univalue Subtrees统计节点值相同的子树
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- 27-x的y次方的后三位数
题目内容: 输入描述 数据分n组,对于每组数据有两个正整数x和y(x的y次方必须大于100) 输出描述 对于每组输出,输出一个值,即x的y次方结果的最后三位数 提示:13的13次方为:30287510 ...
- 通过html5 的EventSource来进行数据推送
以往我们要获取服务器的数据更新,一般通过ajax的定时请求,不过这样效率就低了.我们通过html5的EventSource可以很方便的获取服务器的数据更新,不过IE好像不支持. 例1如下: ind ...
- vc通过webbrowser操作ie元素
1>需要引用 webbrowser2.h,mshtml.h //m_web绑定的webbrowser的变量 CComQIPtr<IHTMLDocument2,&IID_IHTMLD ...