Codeforces 327D Block Tower (DFS)

Source: Internet
Author: User

Test instructions gives you a map of the city you can get on the map. Built a house # can't build a house red House can be installed 200 people Blue House can be installed 100 people only the neighboring location has the Blue House can build the Red house you can also tear down the already built house to demolish the point and become.

It is easy to think of this problem because there is no limit to the minimum number of steps can be left and right place are built blue house and then become the problem of connecting block each blue House connected block in turn removed the building red House finally there is only a blue house left

#include <bits/stdc++.h>using namespace Std;const int N = 505;char g[n][n];stack<pair<int, int> > S;int    X[] = {-1, 1, 0, 0};int y[] = {0, 0,-1, 1};int N, M, A, B, CNT, is_first;int dfs (int i, int j) {int r, C;    if (is_first) Is_first = 0;    Else S.push (Make_pair (i, j));    G[I][J] = ' R ', cnt + = 3;        for (int k = 0; k < 4; ++k) {r = i + x[k], C = j + Y[k];    if (g[r][c] = = '. ') Dfs (R, c);        }}int Main () {while (~scanf ("%d%d", &n, &m)) {cnt = 0;        for (int i = 1; I <= n; ++i) scanf ("%s", G[i] + 1);  for (int i = 1, i <= N; ++i) for (int j = 1; j <= m; ++j) if (g[i][j] = = '. ')        Is_first = 1, cnt-= 2, DFS (i, j);        printf ("%d\n", CNT); for (int i = 1, i <= N; ++i) for (int j = 1; j <= m; ++j) if (g[i][j]! = ' # ') printf ("B%d%d        \ n ", I, j);            while (!s.empty ()) {a = S.top (). First, B = S.top (). Second; Printf ("D%d%d\nr%d%d\n", A, B, a, b);        S.pop (); }} return 0;}

Block Tower

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

      blue Towers. Each have population limit equal to  red towers. Each have population limit equal to 

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 is not influenced, and the destroyed cells becomes empty (so Iahub can bu ILD 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 the he city. So he needs to configure the maximum population possible. Therefore He should find a sequence of operations that builds the city on an optimal-in-a-do, so-total population limit I s as large as possible.

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

Input

The first line of the input contains the integersNandm(1?≤? n,? m? ≤?500). Each of the nextNLines containsmCharacters, describing the grid. TheJ-th character in theI-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 on 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 ?   ( x ,? Y ) ;
  3. ? D x y? (1?≤? ) x? ≤? n,? 1?≤? y? ≤? m) -destroying a tower at the cell (x,? Y).

If There is multiple solutions you can output any of them. Note, that's you shouldn ' t minimize the number of operations.

Sample Test (s) input
2 3..#.#.
Output
4 B 1 1R 1 2R 2 1 B 2 3
Input
1 3 ...
Output
5 B 1 1 B 1 2R 1 3D 1 2R 1 2



Codeforces 327D Block Tower (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.