Spreadsheet
InterviewTime limit1sMemory limit128 MB
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 to (the row) and one digit from to (the column). Thus the top-left cell is and the bottom-right cell is .
Every cell has a value, which is given in one of two ways:
- an integer from to ;
- the sum of the values of up to other cells.
Cell values may depend on one another (for instance, 's sum may depend on , which in turn depends on ). However, a cell whose value depends on itself, directly or indirectly, is undefined (for example, depending on which depends on ). 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 lines, one per spreadsheet row. Each line contains cell descriptions. Each description is either an integer between and , or the sum of to distinct cell names joined by a + symbol (e.g. A1+B5+D3).
Output
Print lines with 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 .