您的位置:首页 > 其它

Codeforces 327D Block Tower【思维+Bfs】

2017-06-27 18:45 387 查看
D. Block Tower

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with
n rows and m columns (it contains
n × m cells). The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any building. The rest of cells are empty. In some empty cell Iahub can build exactly
one tower of two following types:

Blue towers. Each has population limit equal to 100.

Red towers. Each has population limit equal to 200. However, it can be built in some cell only if in that moment at least one of the neighbouring cells has a Blue Tower. Two cells are neighbours is they share a side.

Iahub is also allowed to destroy a building from any cell. He can do this operation as much as he wants. After destroying a building, the other buildings are not influenced, and the destroyed cell becomes empty (so Iahub can build a tower in this cell if
needed, see the second example for such a case).

Iahub can convince as many population as he wants to come into his city. So he needs to configure his city to allow maximum population possible. Therefore he should find a sequence of operations that builds the city in an optimal way, so that total population
limit is as large as possible.

He says he's the best at this game, but he doesn't have the optimal solution. Write a program that calculates the optimal one, to show him that he's not as good as he thinks.

Input
The first line of the input contains two integers n and
m (1 ≤ n, m ≤ 500). Each of the next
n lines contains m characters, describing the grid. The
j-th character in the
i-th line is '.' if you're allowed to build at the cell with coordinates
(i, j) a tower (empty cell) or '#' if there is a big hole there.

Output
Print an integer k in the first line
(0 ≤ k ≤ 106) — the number of operations Iahub should perform to obtain optimal result.

Each of the following k lines must contain a single operation in the following format:

«B x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a blue tower at the cell
(x, y);
«R x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a red tower at the cell
(x, y);
«D x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — destroying a tower at the cell
(x, y).
If there are multiple solutions you can output any of them. Note, that you shouldn't minimize the number of operations.

Examples

Input
2 3
..#
.#.


Output
4
B 1 1
R 1 2
R 2 1
B 2 3


Input
1 3
...


Output
5
B 1 1
B 1 2
R 1 3
D 1 2
R 1 2


题目大意:

现在给你一个N*M大小的矩阵,其中点表示空地(可建筑),井号表示不可建筑位子。

现在一共有两种建筑,一种是蓝色的,一种是红色的,蓝色的可以居住100人,红色的可以居住200人。

蓝色的建筑没有建筑要求,红色的建筑需要建立在蓝色建筑的边上(红色建筑的上下左右一个位子要有蓝色建筑)。

我们也可以对建筑进行拆除,问怎样建筑能够使得居住的人最多。

思路:

贪心的去想,我们肯定是想要多建立起来红色的建筑才行啊。

所以我们一开始对于整个地图都进行建筑蓝色建筑。

然后通过拆除一些蓝色建筑来重新建筑红色建筑。

以此来得到最多的居住人数的建设方案。

很显然,对于一个联通块来讲,只要最后剩下一个蓝色建筑即可,其他建筑均可拆除之后建立其红色建筑。

那么这里我们Bfs一下,记录一个数组step【i】【j】,表示走到(i,j)这个点需要的步数。

那么我们从大的step【i】【j】开始拆除,最终拆除到step【i】【j】==1为止。

那么我们对于每个联通块都进行一次Bfs,然后用优先队列维护一下即可。。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int op,x,y;
} ans[1000050];
struct node2
{
int x,y;
} now,nex;
struct node3
{
int x,y,val;
bool friend operator <(node3 a,node3 b)
{
return a.val<b.val;
}
};
int n,m,output;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int step[505][505];
char a[505][505];
void Bfs(int sx,int sy)
{
queue<node2>s;
now.x=sx;
now.y=sy;
s.push(now);
step[sx][sy]=1;
while(!s.empty())
{
now=s.front();
s.pop();
for(int i=0; i<4; i++)
{
int xx=now.x+fx[i];
int yy=now.y+fy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
{
if(a[xx][yy]=='B'&&step[xx][yy]==0)
{
step[xx][yy]=step[now.x][now.y]+1;
nex.x=xx;
nex.y=yy;
s.push(nex);
}
}
}
}
}

int main()
{
while(~scanf("%d%d",&n,&m))
{
output=0;
memset(step,0,sizeof(step));
for(int i=1;i<=n;i++)
{
scanf("%s",a[i]+1);
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(a[i][j]=='.')
{
ans[output].op=1;
ans[output].x=i;
ans[output++].y=j;
a[i][j]='B';
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(step[i][j]==0&&a[i][j]=='B')
{
Bfs(i,j);
}
}
}
priority_queue<node3>s;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='B')
{
node3 tmp;
tmp.x=i;
tmp.y=j;
tmp.val=step[i][j];
s.push(tmp);
}
}
}
while(!s.empty())
{
node3 tmp=s.top();
s.pop();
if(tmp.val==1)continue;
int x=tmp.x;
int y=tmp.y;
a[x][y]='R';
ans[output].op=3;
ans[output].x=x;
ans[output++].y=y;
ans[output].op=2;
ans[output].x=x;
ans[output++].y=y;
}
printf("%d\n",output);
for(int i=0; i<output; i++)
{
if(ans[i].op==1)printf("B ");
if(ans[i].op==2)printf("R ");
if(ans[i].op==3)printf("D ");
printf("%d %d\n",ans[i].x,ans[i].y);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 327D