Light Up

면접 대비

시간 제한1초메모리 제한1024 MB

요약
전구가 배치된 n x n 라이트 업 격자가 주어질 때, 모든 빈칸이 밝혀지고 전구끼리 서로 비추지 않으며 숫자 칸마다 인접 전구 수가 정확히 맞는지 판정한다.
난이도

쉬움10점 중 3점

유형
구현, 시뮬레이션, 배열, 완전 탐색
정답자
아직 제출이 없습니다

문제

Light Up is a pencil puzzle. Your job will not be to play Light Up, but simply to judge whether a player’s solution is correct.

The game is played on a square grid. Some of the cells are blocked, and some of the blocked cells have a number from 00 to 44. The player must place light bulbs in open cells. Each light bulb can light all of the open cells above, below, left, and right (but not diagonally) until the light reaches the edge of the grid or a blocked cell. The player must place light bulbs so that:

  • Every open cell is lit.
  • No two light bulbs can shine on each other.
  • Any blocked cell with a number in it must have exactly that number of light bulbs immediately adjacent above, below, left, or right. Diagonals do not count.

The following is an example grid with its solution:

Given a grid with light bulbs placed, determine whether it is, in fact, a solution. Note that if a grid has no open cells, and does not violate any other constraints, it is trivially solved.

입력

The first line of input contains a single integer nn (1≤n≤301≤n≤30), which is the number of rows and columns in the grid.

Each of the next nn lines contains exactly nn characters from the set {‘.’,‘X’,‘?’,‘0’,‘1’,‘2’,‘3’,‘4’}. This is the grid, with ‘.’ representing an open cell, ‘X’ representing a blocked cell, ‘?’ representing a light bulb, and the numbers ‘0’,‘1’,‘2’,‘3’,‘4’ representing a blocked cell with a constraint on the number of adjacent light bulbs.

출력

Output a single integer, which is 11 if the input is a valid solution, and 00 otherwise.

예제2

  1. 예제 1

    입력
    7
    .?.0..?
    ..X.1?.
    .X.?.2.
    .....?.
    ?3?..2.
    .?3.X?.
    ..?X?..
    
    예상 출력
    1
    
  2. 예제 2

    입력
    7
    .?.0..?
    ..X.1?.
    .X...2.
    .....?.
    ?3?..2.
    .?3.X?.
    ..?X?..
    
    예상 출력
    0