Codeforeces - 723D -Lakes in Berland

Codeforeces - 723D -Lakes in Berland

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean.
Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it’s possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it’s impossible to add one more water cell to the set such that it will be connected with any other cell.
You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map.
The next n lines contain m characters each — the description of the map. Each of the characters is either ‘.’ (it means that the corresponding cell is water) or ‘*’ (it means that the corresponding cell is land).
It is guaranteed that the map contain at least k lakes.
Output
In the first line print the minimum number of cells which should be transformed from water to land.
In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them.
It is guaranteed that the answer exists on the given data.

Examples

input
5 4 1
0****
0*..*
0****
0**.*
0..**

output
1
0****
0*..*
0****
0****
0..**

input
3 3 0
0***
0*.*
0***
output
1
0***
0***
0***

Note
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.

PS. 由於這個編輯器的原因,不能直接把星號 * 打出來,所以我加了一個前導零,輸出的時候請無視前導零。

題目大意:
有一個 n*m 的矩陣,有湖、海、陸地三種屬性,靠邊的 . 是海,內陸的 . 是湖。求要填的土塊的數量。

分析:
只要把海和湖區分開來,逐一填土就行了。

AC代碼:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,k;
char s[55][55];
bool vis[55][55];
int dx[6]={0,0,1,-1};
int dy[6]={1,-1,0,0};
int num,cnt,islake;
int ans;
struct lake{/*此處用到結構體,用於記錄湖的任意一點的座標,及整個湖的面積。*/
    int x,y,num;
}lk[3600];
bool cmp(lake a,lake b)
{
    return a.num<b.num;
}

void dfs(int x,int y)
{
    vis[x][y]=1;
    num++;
    if (x==0||x==n-1||y==0||y==m-1) islake=0;//海
    for (int i=0;i<4;i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if (nx>=0&&nx<n&&ny>=0&&ny<m &&s[nx][ny]=='.' &&!vis[nx][ny]) dfs(nx,ny);
    }
}

void fill_in(int x,int y)
{
    vis[x][y]=1;
    ans++;
    s[x][y]='*';
    for (int i=0;i<4;i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if (nx>=0&&nx<n&&ny>=0&&ny<m &&s[nx][ny]=='.' &&!vis[nx][ny]) fill_in(nx,ny);
    }
}

int main()
{
    cin>>n>>m>>k;
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
        cin>>s[i][j];
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
        if (!vis[i][j] && s[i][j]=='.')
    {
        num=0;
        islake=1;
        dfs(i,j);
        if (islake) lk[cnt++] = lake{i,j,num};//把湖的座標及面積存入
    }

    memset (vis,0,sizeof (vis));
    sort (lk,lk+cnt,cmp);
    for (int l=0;l<cnt-k;l++)
    {
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<m;j++)
            {
                if (i==lk[l].x&&j==lk[l].y) fill_in(i,j);
            }
        }
    }
    cout<<ans<<endl;
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++)
        cout<<s[i][j];
        cout<<endl;
    }

}
//這題做得我頭都暈了,寫了大概四五份不同的代碼,其實思路都差不多,可是不懂爲什麼,就是有地方卡殼。而這一份代碼還是我從博客園一個大神那拿來的,感謝 @flipped 。

未AC代碼:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
char s[55][55];
int vis[55][55],seq[55][3];
int num=0;int n,m,k;

struct node {
    int x,y,cnt;
};

void bfs1(int x,int y)
{
    int head=1,tail=1;
    vis[x][y]=-1;
    seq[head][1]=x;seq[head][2]=y;
    while (head<=tail)
    {
        int xv=seq[head][1],yv=seq[head][2];
        for (int i=-1;i<=1;i++)
        {
            for (int j=-1;j<=1;j++)
            {
                if (i*j==0 && s[xv+i][yv+j]=='.' && !vis[xv+i][yv+j] && xv+i>0&&xv+i<=n&&yv+j>0&&yv+j<=m)
                {
                    tail++;
                    seq[tail][1]=xv+i;seq[tail][2]=yv+j;
                    vis[xv+i][yv+j]=-1;
                }
            }
        }
        head++;
    }
}

node bfs2(int x,int y)
{
    int cntt=1;
    vis[x][y]=1;
    int head=1,tail=1;
    seq[head][1]=x;seq[head][2]=y;
    while (head<=tail)
    {
        int xv=seq[head][1],yv=seq[head][2];
        for (int i=-1;i<=1;i++)
        {
            for (int j=-1;j<=1;j++)
            {
                if (i*j==0 && !vis[xv+i][yv+j] && s[xv+i][yv+j]=='.' && xv+i>0&&xv+i<=n&&yv+j>0&&yv+j<=m)
                {
                    tail++;
                    seq[tail][1]=xv+i;seq[tail][2]=yv+j;
                    vis[xv+i][yv+j]=1;
                }
            }
        }
        head++;cntt++;
    }
    node tmp;
    tmp.x=seq[tail][1];
    tmp.y=seq[tail][2];
    tmp.cnt=cntt;
    return tmp;
}

void fill_in(int x,int y)
{
    s[x][y]='*';num++;
    for (int i=-1;i<=1;i++)
    {
        for (int j=-1;j<=1;j++)
        {
            if (i*j==0 && s[x+i][y+j]=='.' && vis[x+i][y+j]==1 &&x+i>0&&y+j>0&&x+i<=n&&y+j<=m) {s[x+i][y+j]='*';fill_in(x+i,y+j);}
        }
    }
}

int cmp(node a,node b)
{
    return a.cnt<b.cnt;
}

int main()
{
    int k1=0;
    node now[2550];
    cin>>n>>m>>k;
    for (int i=1;i<=n;i++)//cin
    {
        for (int j=1;j<=m;j++)
        {
            cin>>s[i][j];
        }
    }

    for (int i=1;i<=n;i++)//海
    {
        for (int j=1;j<=m;j++)
        {
            if ((i==1||i==n||j==1||j==m) && s[i][j]=='.' && !vis[i][j]) bfs1(i,j);
        }
    }

    memset(seq,0,sizeof (seq));

    for (int i=1;i<=n;i++)//湖
    {
        for (int j=1;j<=m;j++)
        {
            if (s[i][j]=='.' && !vis[i][j]) now[++k1]=bfs2(i,j);
        }
    }

    sort (now+1,now+1+k1,cmp);

    for (int i=1;i<=k1-k;i++) fill_in(now[i].x,now[i].y);

    cout<<num<<endl;
    for (int i=1;i<=n;i++)//cout
    {
        for (int j=1;j<=m;j++)
        {
            cout<<s[i][j];
        }
        cout<<endl;
    }
}
/*這份其實也是差不多的思路,只不過用的是 bfs 來做的,寫起來略麻煩,最後還是 WA 了,我實在是看不出有什麼地方有漏洞,希望有大神看到可以指點!謝謝!*/
發佈了47 篇原創文章 · 獲贊 28 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章