Minesweeper

Time limit1sMemory limit128 MB

Summary
Given an n×n mine layout and which cells are currently opened, print the board: mine counts for opened safe cells, asterisks for all mines if any mine was opened, and dots elsewhere.
Level

Easy3 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

Problem

Minesweeper is played on an n×nn \times n grid. mm mines are hidden, one per cell, on distinct cells.

The player repeatedly picks a cell and opens it.

  • If the player opens a cell that contains a mine, the player loses.
  • If the player opens a cell with no mine, that cell shows a number between 0 and 8: the count of mines among the 8 cells adjacent to it horizontally, vertically, or diagonally.

The player wins by opening every safe cell until only the mm mined cells remain unopened. Those remaining mm cells must all be mines.

Your task is to read the state of a partially played game and print the corresponding current state of the grid.

Input

The first line contains a positive integer nn, the size of the grid (1≤n≤101 \le n \le 10).

The next nn lines describe the mine layout. Each line represents one row using nn characters: a period (.) is a cell with no mine, and an asterisk (*) is a cell with a mine.

The following nn lines each contain a string of length nn describing which cells have been opened. A lowercase x marks an already-opened cell, and a period (.) marks a cell that has not been opened.

Output

Print the current state of the grid on nn lines.

  • For each opened cell that contains no mine, print the number of adjacent mines (between 0 and 8).
  • If at least one mined cell has been opened, print every mined cell as an asterisk (*).
  • Print every other cell as a period (.).

Examples2

  1. Example 1

    Input
    8
    ...**..*
    ......*.
    ....*...
    ........
    ........
    .....*..
    ...**.*.
    .....*..
    xxx.....
    xxxx....
    xxxx....
    xxxxx...
    xxxxx...
    xxxxx...
    xxx.....
    xxxxx...
    
    Expected output
    001.....
    0013....
    0001....
    00011...
    00001...
    00123...
    001.....
    00123...
    
  2. Example 2

    Input
    1
    .
    x
    
    Expected output
    0