• hdoj--5093--Battle ships(二分图经典建图)


    Battle ships

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1061    Accepted Submission(s): 377



    Problem Description
    Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.

    Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.

    But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.

    The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:

    A battleship cannot lay on floating ice
    A battleship cannot be placed on an iceberg

    Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
     

    Input
    There is only one integer T (0<T<12) at the beginning line, which means following T test cases.

    For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
     

    Output
    For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.
     

    Sample Input
    2 4 4 *ooo o### **#* ooo* 4 4 #*** *#** **#* ooo#
     

    Sample Output
    3 5
     

    Source
    2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

    题意:给你一张n*m的图,图中*表示海洋空地,o表示浮冰,#表示冰山,现在你作为指挥官,安排军舰,军舰不可以放在浮冰或者冰山上,并且两艘军舰不可以在同行或者同列,除非中间有冰山阻挡,输出最多安排多少军舰
    二分图的一个经典建图,将同行的*合并,遇到#换行,并且不同行的*不可以在一起,建出新图G,跑一边匈牙利算法就行,这个题跟Fire net差不多

    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    using namespace std;
    struct node
    {
    	int x,y;
    }rec[100][100];
    int used[10010],pipei[10010];
    vector<int>G[10010];
    int n,m,row,cur;
    char map[100][100];
    void getmap()
    {
    	cur=row=0;
    	bool flag=false;
    	for(int i=0;i<n;i++)
    	{
    		for(int j=0;j<m;j++)
    		{
    			if(map[i][j]=='*')
    			rec[i][j].x=row,flag=true;
    			if(map[i][j]=='#')
    			row++,flag=false;
    		}
    		if(flag)
    		row++;
    	}
    	flag=false;
    	for(int j=0;j<m;j++)
    	{
    		for(int i=0;i<n;i++)
    		{
    			if(map[i][j]=='*')
    			rec[i][j].y=cur,flag=true;
    			if(map[i][j]=='#')
    			cur++,flag=false;
    		}
    		if(flag)
    		cur++;
    	}
    	for(int i=0;i<n;i++)
    	{
    		for(int j=0;j<m;j++)
    		{
    			if(map[i][j]=='*')
    			{
    				int x=rec[i][j].x;
    				int y=rec[i][j].y;
    				G[x].push_back(y);
    			}
    		}
    	}
    }
    int find(int x)
    {
    	for(int i=0;i<G[x].size();i++)
    	{
    		int y=G[x][i];
    		if(!used[y])
    		{
    			used[y]=1;
    			if(pipei[y]==-1||find(pipei[y]))
    			{
    				pipei[y]=x;
    				return 1;
    			}
    		}
    	}
    	return 0;
    }
    void solve()
    {
    	int sum=0;
    	memset(pipei,-1,sizeof(pipei));
    	for(int i=0;i<row;i++)
    	{
    		memset(used,0,sizeof(used));
    		sum+=find(i);
    	}
    	printf("%d
    ",sum);
    }
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d%d",&n,&m);
    		for(int i=0;i<n;i++)
    		scanf("%s",map[i]);
    		for(int i=0;i<1000;i++)
    		G[i].clear();
    		getmap();
    		solve();
    	}
    	return 0;
    }


  • 相关阅读:
    Java学习之路----计算圆形的面积和周长
    数据库系统的基本组成内容
    软件测试的含义以及测试的对象
    wg sync.WaitGroup执行顺序
    go channel
    字符串操作
    scanf
    py停止工作
    jira索引失败
    py kafka
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273481.html
Copyright © 2020-2023  润新知