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:

  1. Blue towers. Each has population limit equal to 100.
  2. 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:

  1. «B x y» (1 ≤ xn, 1 ≤ ym) — building a blue tower at the cell (x, y);
  2. «R x y» (1 ≤ xn, 1 ≤ ym) — building a red tower at the cell (x, y);
  3. «D x y» (1 ≤ xn, 1 ≤ ym) — 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.

Sample test(s)
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

中文意思:

就是在 空的地方(字符'-')建造房子,蓝色房子是100人口,红色房子是200人口(建造红色房子时,旁边必须有蓝色房子),要求人口最大,只需输出可用的解就可以了。k是可行解的步数。

题解:

此题就是将尽可能的建造红色房子,但要求在建造红色房子时,它旁边必须有个蓝色房子。但输出无顺序要求。  首先我们将图分成几块(被#完全隔开)。你可以发现这样一点。你将所有的先全部建造成蓝色,然后再慢慢的从边际(也就是叶子)开始逐个的将此变成红色。看样例3,你就明白了。如此,每块分割开的一个个块将最后变成一个蓝的房子跟红色的其他房子。我们很容易知道,这时居住人口是最多的。因此,DFS即可解决这个问题。

因为n<=m<=500。我们的最大步数是3*m*n<k<=10^6次。所以这样的解是可以的。

我们的k值就是 空房数量*3-蓝色房子(一个块只有1个)*2;k可以直接在DFS时出来(先压点到栈)。

之后不断弹出栈中元素,分别输出 D操作跟R操作即可。。

/*
* @author ipqhjjybj
* @date 20130705
*
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#define clr(x,k) memset((x),(k),sizeof(x))
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std; char s[505][505];
int n,m;
struct node{
int x,y;
bool f; //表示是否为一个块的开头
node(){};
node(int xx,int yy,bool first){
x=xx,y=yy,f=first;
};
};
stack<node> sn;
bool visit[505][505];
int k;
int x1[4]={0,0,1,-1};
int y1[4]={1,-1,0,0};
#define legal(x,a) (1<=x&&x<=a)
void dfs(int xx,int yy){
int _x,_y,i;
for(i=0;i<4;i++){
_x=xx+x1[i],_y=yy+y1[i];
if(legal(_x,n)&&legal(_y,m)&&!visit[_x][_y]&&s[_x][_y]=='.'){
visit[_x][_y]=true;
sn.push(node(_x,_y,false));
dfs(_x,_y);
}
}
}
int main(){
// freopen("D.in","r",stdin);
int i,j;
while(scanf("%d %d",&n,&m)!=EOF){
getchar();
for(i=1;i<=n;i++)
gets(s[i]+1);
sn.empty();
k=0;
clr(visit,false);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
if(!visit[i][j]&&s[i][j]=='.'){
k-=2;
visit[i][j]=true;
sn.push(node(i,j,true));
dfs(i,j);
}
}
k+=sn.size()*3;
printf("%d\n",k);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(visit[i][j])
printf("B %d %d\n",i,j);
node t;
while(!sn.empty()){
t = sn.top(); sn.pop();
if(!t.f){
printf("D %d %d\n",t.x,t.y);
printf("R %d %d\n",t.x,t.y);
}
}
}
return 0;
}

CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单的更多相关文章

  1. CodeForces - 327D Block Tower

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. CoderForces 327D Block Tower

    Portal:http://codeforces.com/problemset/problem/327/D 一座红塔200人,一座蓝塔100人,只有与蓝塔相邻才可以建红塔. '.'处可建塔 '#'处不 ...

  3. Codeforces Round #191 (Div. 2) D. Block Tower

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. [翻译] 初看 ASP.NET Core 3.0 即将到来的变化

    [翻译] 初看 ASP.NET Core 3.0 即将到来的变化 原文: A first look at changes coming in ASP.NET Core 3.0 在我们努力完成下一个 m ...

  5. 网络营销行业十大看了就想吐的“滥词”

    网络营销行业在国内的互联网界已"猖獗"数年之久,它是一个让企业爱让用户恨的行业.有互联网的地方,就有网络营销的机会,有了机会就有了相关产业的存在,只不过是业大业小的问题.但是随着互 ...

  6. 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版

    代码走查25条疑问   代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...

  7. 刚看完了一本关于javascript的书感觉受益匪浅,原来不懂的东西这么多,想问问怎么成为大神?求教!!!!!!

    刚看完了一本关于javascript的书感觉受益匪浅,原来不懂的东西这么多,想问问怎么成为大神?求教!!!!!!

  8. 今天开dev的时候,config update一下别人的,但是忘了自己改过目录了,导致光看ip,想了半天,为什么接口不对

    今天开dev的时候,config update一下别人的,但是忘了自己改过目录了,导致光看ip,想了半天,为什么接口不对 baseUrl: {     //     // dev: 'http://1 ...

  9. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

随机推荐

  1. ArduinoYun教程之配置Arduino Yun环境

    ArduinoYun教程之配置Arduino Yun环境 配置Arduino Yun 不管你使用前面介绍的哪种方式连接Arduino Yun.如今都能够配置你的Arduino Yun了.首先须要的是使 ...

  2. SymmetricDS文档翻译--【Chapter 3. 具体配置(Configuration)[section C]】

    3.6. Routers 眼下的实现中提供的Route实现包括: 1.      Default Router:这个Router发送全部的数据到Router中定义的目标节点所属的组中的全部的节点. 2 ...

  3. shell中判断用法

    测试结构: 测试命令可用于测试表达式条件的真假,true,则返回0,false,则返回非0:这一点c/c++有区别:       格式: test  expression #expression是一个 ...

  4. Fedora20安装完Nvidia后启动一直黑屏解决办法。

    安装完Fedora20后,把Nvidia驱动装上后重起机器一直黑屏时,切换到命令行下:Alt+F2  登陆上去,然后直接更新: su -c ‘yum update’ ,再重起就OK了.

  5. Vistual Studio 2012更换皮肤

    早就装上VS2012了,可是除了在家里练习玩玩的时候使用外,在公司都还在用2010,也没好好研究过2012.这两天把公司的电脑换了系统,也就把vs换成了2012.可是看着不是白白的皮肤就是深色的皮肤, ...

  6. Ansi,UTF8,Unicode,ASCII编码的差别

    近日须要不同的编码,关于上述编码,一直迷迷糊糊,查了些资料,总算大致了解了,以下全是从网上搜来的: 1.  ASCII和Ansi编码    字符内码(charcter code)指的是用来代表字符的内 ...

  7. Swift - 设置应用程序图标的提醒个数(右上角小红圈)

    使用UILocalNotification除了可以实现本地消息的推送功能(可以设置推送内容,推送时间,提示音),还可以设置应用程序右上角的提醒个数. 下面演示如何设置,效果图如下: --- AppDe ...

  8. 使用POI来实现对Excel的读写操作

    事实上我感觉直接贴代码就好了.代码里面差点儿做到每一行一个凝视.应该看起来会比較简单 代码托管在github上:https://github.com/chsj1/ExcelUtils package ...

  9. 网页调试技巧:抓取马上跳转的页面POST信息或者页面内容

    http://www.qs5.org/Post/625.html 网页调试技巧:抓取马上跳转的页面POST信息或者页面内容 2016/02/02 | 心得分享 | 0 Replies 有时候调试网页或 ...

  10. 在chrome中使用axure生成原型的问题

    来自:非原型不设计