二分图的最大分配(hdu 1045)


Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6538    Accepted Submission(s): 3713

Problem Description

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. 



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

The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will 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

For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.

 

 

Sample Input

4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0

 

 

Sample Output

51524

 

 

#include<stdio.h>
#include<string.h>


int n;                      //方格大小 
char maze[5][5];            //方格的内容 
int mazer[5][5];            //按行缩点,如果某行连续几个点都是'.',则这几个点都编相同的序号 
int mazec[5][5];            //按列缩点,如果 某列连续几个点都是'.',则这几个点都编相同的序号 
int path[20][20];          //行和列之间编号之间的联系,表示二分图之间可以连接的边 
int link[20];             //二分图不同子集之间的联系 
int used[20];             //二分图使用时候的内部标记 
int rn,cn;               //行列缩点的编号 


int dfs(int n)
{
	for(int i=0;i<cn;i++)
	{
		if(path[n][i]==1 && used[i]==0)
		{
			used[i] = 1;
			if(link[i]==0 || dfs(link[i]))        //link[i]==0表示要连接的点没有其他的边连接着;dfs[link[i]]表示要连接的点有边连接着,则让连接着边的点再搜索其他的点进行连接 
			{
				link[i] = n;
				return 1;
			}
		}
	}
	return 0;
}

int match()               //可以进行二分图的最大匹配 
{
	memset(link, 0, sizeof(link));
	int ans = 0;
	
	for(int i=0;i<rn;i++)
	{
		memset(used, 0, sizeof(used));
		ans += dfs(i);	
	}
	return ans;
}


int main()
{
	while(scanf("%d", &n), n)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%s", maze[i]);
		}
		
		memset(path, 0, sizeof(path));
		memset(mazer, 0, sizeof(mazer));
		memset(mazec, 0, sizeof(mazec));
		
		rn = cn = 1;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(maze[i][j]=='.' && mazer[i][j]==0)          //按行缩点 
				{
					for(int k=j;maze[i][k]=='.' && k<n;k++)
					{
						mazer[i][k] = rn;
					}
					rn++;
				}
				
				if(maze[j][i]=='.' && mazec[j][i]==0)          //按列锁点 
				{
					for(int k=j;maze[k][i]=='.' && k<n;k++)
					{
						mazec[k][i] = cn;
					}
					cn++;
				}
			}
		}
		
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(mazer[i][j]!=0 && mazec[i][j]!=0)   //连边,表示二分图可以连接的边 
				{
					path[mazer[i][j]][mazec[i][j]] = 1;
				}
			}
		}
		
		printf("%d\n", match());
	}
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
人工智能关于最大二分图的程序代码: #include<stdio.h> #include<string.h> main() { bool map[100][300]; int i,i1,i2,num,num1,que[300],cou,stu,match1[100],match2[300],pque,p1,now,prev[300],n; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&cou;,&stu;); memset(map,0,sizeof(map)); for(i1=0;i1<cou;i1++) { scanf("%d",&num;); for(i2=0;i2<num;i2++) { scanf("%d",&num1;); map[i1][num1-1]=true; } } num=0; memset(match1,int(-1),sizeof(match1)); memset(match2,int(-1),sizeof(match2)); for(i1=0;i1<cou;i1++) { p1=0; pque=0; for(i2=0;i2<stu;i2++) { if(map[i1][i2]) { prev[i2]=-1; que[pque++]=i2; } else prev[i2]=-2; } while(p1<pque) { now=que[p1]; if(match2[now]==-1) break; p1++; for(i2=0;i2<stu;i2++) { if(prev[i2]==-2&&map;[match2[now]][i2]) { prev[i2]=now; que[pque++]=i2; } } } if(p1==pque) continue; while(prev[now]>=0) { match1[match2[prev[now]]]=now; match2[now]=match2[prev[now]]; now=prev[now]; } match2[now]=i1; match1[i1]=now; num++; } if(num==cou) printf("YES\n"); else printf("NO\n"); } } dfs实现过程: #include<stdio.h> #include<string.h> #define MAX 100 bool map[MAX][MAX],searched[MAX]; int prev[MAX],m,n; bool dfs(int data) { int i,temp; for(i=0;i<m;i++) { if(map[data][i]&&!searched[i]) { searched[i]=true; temp=prev[i]; prev[i]=data; if(temp==-1||dfs(temp)) return true; prev[i]=temp; } } return false; } main() { int num,i,k,temp1,temp2,job; while(scanf("%d",&n)!=EOF&&n!=0) { scanf("%d%d",&m,&k); memset(map,0,sizeof(map)); memset(prev,int(-1),sizeof(prev)); memset(searched,0,sizeof(searched)); for(i=0;i<k;i++) { scanf("%d%d%d",&job;,&temp1;,&temp2;); if(temp1!=0&&temp2;!=0) map[temp1][temp2]=true; } num=0; for(i=0;i<n;i++) { memset(searched,0,sizeof(searched)); dfs(i); } for(i=0;i<m;i++) { if(prev[i]!=-1) num++; } printf("%d\n",num); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值