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

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人,在106
     步数以内建造尽可能大的容纳量,可是在建造红色房子时,与它相邻的必须有蓝色房子。

思路:先将全部建成蓝色。图最大是500*500,假设三种步骤都进行是3*500*500,不会超过106
   题目并没有要求步骤尽量少。仅仅须要满足容纳量尽量多就可以,所以

所有建成蓝色后。再从边界開始改建成红色,因此DFS就可以完毕。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#define N 600
using namespace std; char s[N][N];
int vis[N][N]; struct Node
{
char a;
int x,y;
}f[1000009]; int n,m;
int num; int fun(char c,int x,int y)
{
s[x][y]=c;
num++;
f[num].a=c;
f[num].x=x;
f[num].y=y; } void dfs(int x,int y)
{
if(s[x][y]!='B' || vis[x][y] ) return;//假设是'#'或者已经被改造 vis[x][y]=1; if(y>1) dfs(x,y-1);
if(x>1) dfs(x-1,y);
if(y<m) dfs(x,y+1);
if(x<n) dfs(x+1,y); fun('D',x,y);
fun('R',x,y);
return;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1); memset(vis,0,sizeof vis);
num=0; for(int i=1;i<=n;i++)//先所有建成’B'
for(int j=1;j<=m;j++)
{
if(s[i][j]=='.')
fun('B',i,j);
} for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
if(s[i][j]=='B')
{
vis[i][j]=1; if(j>1) dfs(i,j-1);
if(i>1) dfs(i-1,j);
if(j<m) dfs(i,j+1);
if(i<n) dfs(i+1,j); }
} printf("%d\n",num);
for(int i=1;i<=num;i++)
{
printf("%c %d %d\n",f[i].a,f[i].x,f[i].y);
} }
return 0;
}

Codeforces Round #191 (Div. 2) D. Block Tower的更多相关文章

  1. 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game

    题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...

  2. Codeforces Round #191 (Div. 2)

    在div 188中,幸运的达成了这学期的一个目标:CF1800+,所以这次可以打星去做div2,有点爽. A.Flipping Game 直接枚举 B. Hungry Sequence 由于素数的分布 ...

  3. codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的 ...

  4. Codeforces Round #191 (Div. 2) E题

    状态压缩DP,算sum,本来是枚举的,结果TLE了.. #include <iostream> #include <cstring> #include <cstdio&g ...

  5. Codeforces Round #191 (Div. 2)---A. Flipping Game

    Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #191 (Div. 2) B. Hungry Sequence(素数筛选法)

    . Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. Codeforces Round #191 (Div. 2) A. Flipping Game(简单)

    A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  8. Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】

    A. Flipping Game     time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #578 (Div. 2)

    Codeforces Round #578 (Div. 2) 传送门 A. Hotelier 暴力即可. Code #include <bits/stdc++.h> using names ...

随机推荐

  1. 在Window和Linux下使用Zthread库

    ZThread库是一个开源的跨平台高级面向对象的线性和sycnchronization 库,以运行POSIX 和Win32 系统中的C++程序. ZThread库的主页:http://zthread. ...

  2. hdu 4284 Travel(floyd + TSP)

    虽然题中有n<=100个点,但实际上你必须走过的点只有H<=15个.而且经过任意点但不消耗C[i]跟D[i]可以为无限次,所以可以floyd预处理出H个点的最短路,之后剩下的...就成了裸 ...

  3. 快速学会搭建SVN服务器

    原文:快速学会搭建SVN服务器 SVN是一个版本控制工具,常用于我们软件开发项目中,用来管理我们团队共同使用的代码,文档等历史版本的管理,保持代码的更新,避免混乱. 需要工具: svn安装程序:免费下 ...

  4. NSDictionary、NSMutableDictionary基本使用

    郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  5. php学习之道:php中soap的使用实例以及生成WSDL文件,提供自己主动生成WSDL文件的类库——SoapDiscovery.class.php类

    1. web service普及: Webservice soap wsdl差别之个人见解 Web Service实现业务诉求:  Web Service是真正"办事"的那个,提供 ...

  6. linux kickstart 自动安装

    最近很多业务系统都是linux lnmp平台安装,反复的安装让人觉得很苦恼,仔细钻研了下kickstart .这里环境是red hat linux 5.8 32位,系统盘中的软件包里包含有kickst ...

  7. COST CUTTING THE ALAN GREENBERG WAY

    AnatBird.com COST CUTTING THE ALAN GREENBERG WAY

  8. Android开源项目pulltorefresh分析与简单使用

    在Android开发中有时我们须要訪问网络实时刷新数据.比方QQ好友在线状态最新信息,QQ空间须要显示很多其它的好友动态信息,EOE论坛client显示很多其它的文章帖子信息等.android-pul ...

  9. hdu4614(线段树+二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:给定一个区间[0,N-1],初始时每个位置上的数字都是0,可以对其进行以下两种操作: 1. ...

  10. hdu1540(线段树)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:是一条线上的点,D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢 ...