zoukankan      html  css  js  c++  java
  • codeforces116B

    Little Pigs and Wolves

     CodeForces - 116B 

    Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.

    A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.

    They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.

    What is the maximum number of little pigs that may be eaten by the wolves?

    Input

    The first line contains integers n and m (1 ≤ n, m ≤ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each — that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.

    It is guaranteed that there will be at most one wolf adjacent to any little pig.

    Output

    Print a single number — the maximal number of little pigs that may be eaten by the wolves.

    Examples

    Input
    2 3
    PPW
    W.P
    Output
    2
    Input
    3 3
    P.W
    .P.
    W.P
    Output
    0

    Note

    In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.

    sol:看上去貌似不会贪心,也不会XJB搜索,于是打了个二分图匹配(智减inf)

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0');    return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=15,M=10005;
    const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
    int n,m;
    char Map[N][N];
    int Id[N][N],Pig=0,Wolf=0;
    namespace Picture
    {
        int tot=0,Next[M],to[M],head[N*N];
        inline void add(int x,int y)
        {
            Next[++tot]=head[x];
            to[tot]=y;
            head[x]=tot;
            return;
        }
        int Pipei[N*N];
        bool Used[N*N];
        inline bool dfs(int x)
        {
            int i;
            for(i=head[x];i;i=Next[i])
            {
                if(Used[to[i]]) continue;
                Used[to[i]]=1;
                if(!Pipei[to[i]]||dfs(Pipei[to[i]]))
                {
                    Pipei[to[i]]=x; return true;
                }
            }
            return false;
        }
        inline int ErfenPipei()
        {
            int i,ans=0;
            for(i=1;i<=Wolf;i++)
            {
                memset(Used,0,sizeof Used);
                if(dfs(i)) ans++;
            }
            return ans;
        }
    }
    #define Pic Picture
    int main()
    {
        int i,j,k;
        R(n); R(m);
        for(i=1;i<=n;i++)
        {
            scanf("%s",Map[i]+1);
            for(j=1;j<=m;j++)
            {
                if(Map[i][j]=='P') Id[i][j]=++Pig;
                else if(Map[i][j]=='W') Id[i][j]=++Wolf;
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++) if(Map[i][j]=='W')
            {
                for(k=0;k<4;k++)
                {
                    int xx=i+dx[k],yy=j+dy[k];
                    if(Map[xx][yy]=='P')
                    {
                        Pic::add(Id[i][j],Id[xx][yy]);
                    }
                }
            }
        }
        Wl(Picture::ErfenPipei());
        return 0;
    }
    View Code
  • 相关阅读:
    Javascript DOM 编程艺术读书笔记16/03/25
    2014 Multi-University Contest 1.1 hdu4861 打表找规律
    汇编小记16/3/23
    汇编小记16/3/22
    hdoj 4940 强连通图
    Head FIRST HTML & CSS 16/03/17
    html&css一些有用的网站整理
    dosbox+debug 模拟dos
    汇编小记16/3/15
    解决windwos另存为,保存文件时无法选择“桌面”文件夹
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10624007.html
Copyright © 2011-2022 走看看