This page is still under construction.

Parts of this page are still being built. What you see may change.

Spreadsheet

Interview

Time limit1sMemory limit128 MB

Summary
Evaluate each spreadsheet cell, treating formula cells as sums of other cells, and mark any cell involved in a dependency cycle as undefined.
Level

Medium5 of 10

Topics
Graph, DFS, Topological sort, Simulation
Solved
No attempts yet

Problem

A spreadsheet is made up of many "cells" arranged in a rectangular grid. Each cell is addressed by one letter from AA to JJ (the row) and one digit from 11 to 99 (the column). Thus the top-left cell is A1A1 and the bottom-right cell is J9J9.

Every cell has a value, which is given in one of two ways:

  1. an integer from 00 to 10001000;
  2. the sum of the values of up to 1010 other cells.

Cell values may depend on one another (for instance, A1A1's sum may depend on B6B6, which in turn depends on C9C9). However, a cell whose value depends on itself, directly or indirectly, is undefined (for example, A1A1 depending on G8G8 which depends on A1A1). A cell that depends on an undefined cell is itself undefined as well. Given the specification of every cell in the spreadsheet, compute and output the value of each cell.

Input

The input consists of 1010 lines, one per spreadsheet row. Each line contains 99 cell descriptions. Each description is either an integer between 00 and 1 0001\,000, or the sum of 11 to 1010 distinct cell names joined by a + symbol (e.g. A1+B5+D3).

Output

Print 1010 lines with 99 numbers per line, giving the value of every cell in the spreadsheet. If a cell is undefined, print an asterisk (*) instead of its value. No cell's final value exceeds 1 000 000 0001\,000\,000\,000.

Examples1

  1. Example 1

    Input
    1 2 3 A1+A2+A3 A3+A4 A1+A4+A5 A8+A9 A9 A8
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    
    Expected output
    1 2 3 6 9 16 * * *
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0