zoukankan      html  css  js  c++  java
  • Battle ships

    Battle ships

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


    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
    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn=108;
    typedef long long ll;
    int T;
    char str[66][66];
    int n,m;
    int row_cnt,col_cnt;
    int col[66][66],row[66][66],mk[3000][3000];
    void row_solve(){
        row_cnt=1;
        for(int i=1;i<=n;++i){
            int flag=0;
            for(int j=1;j<=m;++j){
                if(str[i][j]=='*'){
                    row[i][j]=row_cnt;
                    flag=1;
                }
                if(str[i][j]=='#'){
                    row_cnt++;
                    flag=0;
                }
            }
            if(flag)row_cnt++;
        }
    }
    void col_solve(){
        col_cnt=1;
        for(int i=1;i<=m;++i){
            int flag=0;
            for(int j=1;j<=n;++j){
                if(str[j][i]=='*'){
                    col[j][i]=col_cnt;
                    flag=1;
                }
                if(str[j][i]=='#'){
                    col_cnt++;
                    flag=0;
                }
            }
            if(flag)col_cnt++;
        }
    }
    int link[3000],used[3000];
    bool dfs(int cur){
        for(int i=1;i<=col_cnt;++i){
            if(mk[cur][i]&&!used[i]){
                used[i]=1;
                if(link[i]==-1||dfs(link[i])){
                    link[i]=cur;
                    return true;
                }
            }
        }
        return false;
    }
    int hungry(){
        for(int i=1;i<=max(row_cnt,col_cnt);++i)link[i]=-1;
        int ans=0;
        for(int i=1;i<=row_cnt;++i){
            memset(used,0,sizeof(used));
            if(dfs(i)){
                ans++;
            }
        }
        return ans;
    }
    int main(){
        //freopen("1.txt","r",stdin);
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;++i){
                scanf("%s",str[i]+1);
            }
            row_solve();
            col_solve();
            memset(mk,0,sizeof(mk));
            for(int i=1;i<=n;++i){
                for(int j=1;j<=m;++j){
                    if(str[i][j]=='*')mk[row[i][j]][col[i][j]]=1;
                }
            }
            printf("%d
    ",hungry());
        }
        return 0;
    }
  • 相关阅读:
    Python数值计算:一 使用Pylab绘图(2)
    Python数值计算:一 使用Pylab绘图(1)
    Gromacs处理amino acid residues小结
    实用正则表达式匹配和替换
    将Word转为带书签的PDF
    QT在windows下实现截屏操作并保存为png图片
    VMware虚拟机无法识别U盘解决方案
    C/C++/Qt 统计运行时间
    ffmpeg 中 swscale 的用法
    用qt代码怎样编写图片保存格式[qt4.6]
  • 原文地址:https://www.cnblogs.com/czy-power/p/11313850.html
Copyright © 2011-2022 走看看