TopcoderArchive
Archive
Algorithm problem

PastingPaintingDivTwo

Problem statement, definition, constraints, and public examples.

Problem Statement

Cucumber Boy likes drawing pictures. Today, he plans to draw a picture using a very simple graphics editor.

The editor has the following functions:

  • The canvas is an infinite two-dimensional grid of pixels.
  • There are only two colors: black, and transparent. These are denoted 'B' and '.' (a period), respectively.
  • The editor has a clipboard that contains a rectangular picture.
  • The editor can take the picture in the clipboard and paste it onto any corresponding rectangle of the canvas. The user just has to select the pixel of the canvas where the upper left corner of the clipboard will be pasted.
  • When pasting the picture, the black pixels of the picture in the clipboard will overwrite their corresponding pixels on the canvas. The pixels that are transparent in the clipboard picture do not change the canvas.

At this moment, all pixels on the infinite canvas are transparent. Cucumber Boy has already stored a picture in the clipboard. You are given this picture as a String[] clipboard.

Cucumber Boy now wants to paste the clipboard picture onto the canvas exactly T times in a row.

For each i, when pasting the clipboard for the i-th time, he will choose the pixel (i,i) as the upper left corner of the pasted picture.

You are given the String[] clipboard and the int T. Return the number of black pixels on the canvas after all the pasting is finished.

Definition

Class:
PastingPaintingDivTwo
Method:
countColors
Parameters:
String[], int
Returns:
long
Method signature:
long countColors(String[] clipboard, int T)
(be sure your method is public)

Constraints

  • clipboard will contain between 1 and 50 elements, inclusive.
  • Each element of clipboard will contain between 1 and 50 characters, inclusive.
  • Each element of clipboard will contain the same number of characters.
  • Each character of each element of clipboard will be 'B' or '.'.
  • T will be between 1 and 1,000,000,000, inclusive.

Examples

  1. {
    "..B",
    "B..",
    "BB."
    }
    3
    Returns: 10
  2. {
    "B...",
    "....",
    "....",
    "...B"
    }
    2
    Returns: 4
  3. {"BBB"}
    10000
    Returns: 30000
  4. {"."}
    1000000000
    Returns: 0
  5. {
    "BB.",
    ".B."
    }
    100
    Returns: 201
  6. {
    "..........B..........",
    ".........B.B.........",
    "........B...B........",
    ".......B.....B.......",
    "......B..B.B..B......",
    ".....B...B.B...B.....",
    "....B...........B....",
    "...B...B.....B...B...",
    "..B.....BBBBBB....B..",
    ".B..........BB.....B.",
    "BBBBBBBBBBBBBBBBBBBBB"
    }
    1000000000
    Returns: 21000000071
    Note that the answer may overflow a 32-bit integer variable. This is the image of clipboard in this example.
Back to all problems