cf 327D Block Tower

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 Input
  
  
2 3 ..# .#. 1 3 ...
 

Sample Output
  
  
4 B 1 1 R 1 2 R 2 1 B 2 3 5 B 1 1 B 1 2 R 1 3 D 1 2 R 1 2
题意 ;
在空的地方建房子,建蓝房子人数100人,建红房子200人 ; 建红房子的时候必须要有蓝房子在旁边. 要求使得总人数最大啊,即尽可能多的建红房子. 输出方案的操作.

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

从块头开始,向四周搜索,即建蓝房子,每建一个蓝房子,就压入栈,之后在反过来建红房子,这是绝对可行的,

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

我们的k值就是 空房数量*3-蓝色房子(一个块只有1个)*2;k可以直接在DFS时出来(先压点到栈)。k就是操作数. 也可以这样想,因为我们对空格的操作是,空格数是x, 先建蓝房子,之后摧毁蓝房子,第三步再建红房子. 所以每个空格是操作三步, 但是,每个块的必须要有一个蓝房子(块头), 所以不是每个空格都操作三步,每块的块头操作了一次 所以,总操作 x*3-(块的数量)*2 ;

#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<queue> #include<stack> #include<iostream> #include<algorithm> using namespace std ; char s[505][505] ; int  n , m , k ;  bool vist[505][505]; int x1[4]={0,0,1,-1}; //四个方向 int Y1[4]={1,-1,0,0}; struct node { int x , y ; //坐标 bool f ; //是否块头 node(int xx,int yy,bool frist) //压入栈的元素 { x=xx ; y=yy; f=frist ; } }; stack<node>sn ; //使用系统栈 void dfs(int xx ,int yy) //从次点向四周搜索,建蓝房子 {       int i , j ;  int _x,_y ;  for( i = 0 ; i < 4 ; i++)  {    _x = xx + x1[i] ;    _y = yy + Y1[i] ;    if( !vist[_x][_y] && ( _x>=1 && _x<=n  ) && (_y>=1&&_y<=m)&&s[_x][_y]=='.')      { //入栈的条件          vist[_x][_y]=true ; //标记此点建了蓝房子          sn.push(node(_x,_y,false)) ; //压入栈,记住参数是false,因为这个点不是块头          dfs(_x,_y); //再继续向下搜索.      }  } } int  main() { int i , j ; while(~scanf("%d%d",&n,&m)) {   getchar();   memset(vist,false,sizeof(vist));   for( i = 1 ; i <= n ; i++)          gets(s[i]+1) ; //坐标从1开始    k=0; for( i =  1 ; i <= n ; i++)   for( j = 1 ; j <= m ; j++)   {      if(s[i][j]=='.'&&!vist[i][j])      {       k -= 2 ; //找到一个块头,减去两个操作数.       vist[i][j]=true ;       sn.push( node(i,j,true) );//压入栈,参数为true.是块头       dfs(i,j); //以这个点为块头,向四周搜索      }   }         k += sn.size() * 3 ; //k为总的块头数减去2,再加上空格数*3 ;    printf("%d\n",k); //输出操作数    for( i = 1 ; i <=  n ; i++)      for( j = 1  ; j <= m ; j++ )          if(vist[i][j]) //先输出建蓝房子的方案     printf("B %d %d\n",i,j) ;      while(!sn.empty()) { node 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; }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值