UVA215-Spreadsheet Calculator(模拟+拓扑排序)
Problem UVA215-Spreadsheet Calculator
Accept:401 Submit:2013
Time Limit: 3000 mSec
Problem Description
A spreadsheet is a rectangular array of cells. Cells contain data or expressions that can be evaluated to obtain data. A “simple” spreadsheet is one in which data are integers and expressions are mixed sums and differences of integers and cell references. For any expression, if each cell that is referenced contains an integer, then the expression can be replaced by the integer to which the expression evaluates. You are to write a program which evaluates simple spreadsheets.
Input
Input consists of a sequence of simple spreadsheets. Each spreadsheet begins with a line specifying the number of rows and the number of columns. No spreadsheet contains more than 20 rows or 10 columns. Rows are labeled by capital letters A through T. Columns are labeled by decimal digits 0 through 9. Therefore, the cell in the first row and first column is referenced as A0; the cell in the twentieth row and fifth column is referenced as T4.
Following the specification of the number of rows and columns is one line of data for each cell, presented in row-major order. (That is, all cells for the first row come first, followed by all cells for the second row, etc.) Each cell initially contains a signed integer value or an expression involving unsigned integer constants, cell references, and the operators + (addition) and - (subtraction). If a cell initially contains a signed integer, the corresponding input line will begin with an optional minus sign followed by one or more decimal digits. If a cell initially contains an expression, its input line will contain one or more cell references or unsigned integer constants separated from each other by + and - signs. Such a line must begin with a cell reference. No expression contains more than 75 characters. No line of input contains leading blanks. No expression contains any embedded blanks. However, any line may contain trailing blanks.
The end of the sequence of spreadsheets is marked by a line specifying 0 rows and 0 columns.
Output
For each spreadsheet in the input, you are to determine the value of each expression and display the resulting spreadsheet as a rectangular array of numbers with the rows and columns appropriately labeled. In each display, all numbers for a column must appear right-justified and aligned with the column label.
Operators are evaluated left to right in each expression; values in cells are always less than 10000 in absolute value. Since expressions may reference cells that themselves contain expressions, the order in which cells are evaluated is dependent on the expressions themselves.
If one or more cells in a spreadsheet contain expressions with circular references, then the output for that spreadsheet should contain only a list of the unevaluated cells in row-major order, one per line, with each line containing the cell label, a colon, a blank, and the cell’s original expression.
A blank line should appear following the output for each spreadsheet.
Sample Input
A1+B1
5
3
B0-A1
3 2
A0
5
C1
7
A1+B1
B0+A1
0 0
Sample Ouput
A 3 5
B 3 -2
B0: C1
C1: B0+A1
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <cstdio>
- using namespace std;
- const int Row = ,Cloumn = ;
- const int maxl = ;
- int vis[Row][Cloumn];
- bool is_circle[Row][Cloumn];
- int n,m;
- struct Point{
- bool is_num;
- int num;
- char ss[];
- Point(bool is_num = false,int num = ) :
- is_num(is_num),num(num) {}
- };
- Point gra[Row][Cloumn];
- bool dfs(int x,int y){
- if(is_circle[x][y]) return false;
- if(vis[x][y] == -){
- gra[x][y].is_num = false;
- is_circle[x][y] = true;
- return false;
- }
- if(vis[x][y] == ) return true;
- vis[x][y] = -;
- int ans = ;
- char *p = &gra[x][y].ss[];
- int flag = ;
- //bool IsNum = true;
- for(int i = ;i < strlen(p);){
- if(p[i] == '+'){
- i++;
- flag = ;
- continue;
- }
- else if(p[i] == '-'){
- i++;
- flag = -;
- continue;
- }
- if(isdigit(p[i])){
- int temp;
- sscanf(p+i,"%d",&temp);
- ans += temp*flag;
- while(isdigit(p[i])) i++;
- }
- else{
- //IsNum = false;
- int r = p[i]-'A',c = p[i+]-'';
- i += ;
- if(gra[r][c].is_num) ans += flag*gra[r][c].num;
- else{
- if(dfs(r,c)){
- ans += flag*gra[r][c].num;
- }
- else{
- gra[x][y].is_num = false;
- is_circle[x][y] = true;
- vis[x][y] = ;
- return false;
- }
- }
- }
- }
- gra[x][y].is_num = true;
- gra[x][y].num = ans;
- vis[x][y] = ;
- return true;
- }
- void output(){
- printf(" ");
- for(int i = ;i < m;i++){
- printf("%6d",i);
- }
- printf("\n");
- for(int i = ;i < n;i++){
- printf("%c",i+'A');
- for(int j = ;j < m;j++){
- printf("%6d",gra[i][j].num);
- }
- printf("\n");
- }
- }
- int main()
- {
- //freopen("input.txt","r",stdin);
- //freopen("output.txt","w",stdout);
- while(~scanf("%d%d",&n,&m) && (n||m)){
- char str[];
- memset(vis,,sizeof(vis));
- memset(is_circle,false,sizeof(is_circle));
- for(int i = ;i < n;i++){
- for(int j = ;j < m;j++){
- scanf("%s",str);
- gra[i][j].is_num = false;
- strncpy(gra[i][j].ss,str,sizeof(str));
- }
- }
- for(int i = ;i < n;i++){
- for(int j = ;j < m;j++){
- if(gra[i][j].is_num) continue;
- dfs(i,j);
- }
- }
- bool ok = true;
- for(int i = ;i < n;i++){
- int j;
- for(j = ;j < m;j++){
- if(is_circle[i][j]){
- ok = false;
- break;
- }
- }
- if(j != m) break;
- }
- if(ok){
- output();
- }
- else{
- for(int i = ;i < n;i++){
- for(int j = ;j < m;j++){
- if(is_circle[i][j]){
- printf("%c%d: %s\n",i+'A',j,gra[i][j].ss);
- }
- }
- }
- }
- printf("\n");
- }
- return ;
- }
UVA215-Spreadsheet Calculator(模拟+拓扑排序)的更多相关文章
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28399 Accepted: 9684 De ...
- Codeforces 909 substr用法 思维合并线段目标最少 Py语句逆推DP vecrtor缩点删不同颜色点模拟 拓扑排序处理任务
A str.substr(i,j) 从str[i]开始起取j个字符作为返回的字符串 /* Huyyt */ #include <bits/stdc++.h> using namespace ...
- Day1:T1 模拟 T2 拓扑排序
T1:模拟 自己第一天的简直跟白痴一样啊...模拟都会打错.. 当时貌似在更新最大值的时候打逗比了... if((sum[x]==max && x<maxh) || sum[x] ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- 【noip模拟赛4】找啊找啊找BF 拓扑排序
描述 sqybi上次找GF的工作十分不成功,于是依旧单身的他在光棍节前的某天突发奇想,要给自己找一个BF(这里指的是男性的好朋友……),这样既可以和人分享内心的压抑(路人甲:压抑还分享么……),也可以 ...
- [JZOJ 5905] [NOIP2018模拟10.15] 黑暗之魂(darksoul) 解题报告 (拓扑排序+单调队列+无向图基环树)
题目链接: http://172.16.0.132/senior/#main/show/5905 题目: oi_juruo热爱一款名叫黑暗之魂的游戏.在这个游戏中玩家要操纵一名有 点生命值的无火的余灰 ...
- [NOIP2015模拟10.27] 挑竹签 解题报告(拓扑排序)
Description 挑竹签——小时候的游戏夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏.挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签.如果动了其他的竹签,就要换对手来挑.在所有 ...
- UVA - 12263 Rankings 模拟(拓扑排序)
题意:1~n这n个数,给你一个初始的顺序,再告诉你那两个数的大小关系发生了变化,求变化后的 顺序,不存在则输出IMPOSSIBLE 思路:这题很遗憾没在比赛的时候过掉,结束后加了一行就AC了.题目真的 ...
- 【2019.7.26 NOIP模拟赛 T3】化学反应(reaction)(线段树优化建图+Tarjan缩点+拓扑排序)
题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...
随机推荐
- String为什么是不可变的?
前几天一个面试被问到String为什么是不可变的?, 自我感觉当时回答的不太理想, 事后总结一下 不可变的是什么 我们谈论的String不可变, 指的是字符串的值不可变 例: String s = & ...
- confidence interval
95%置信区间.置信区间的两端被称为置信极限.对一个给定情形的估计来说,置信水平越高,所对应的置信区间就会越大. 对置信区间的计算通常要求对估计过程的假设(因此属于参数统计),比如说假设估计的误差是成 ...
- Android Studio 学习(四) 数据库
文件存储 写数据 String data = "Data ti save"; FileOutputStream out =null; BufferedWriter writer = ...
- Retrofit2 原理解析
Retrofit是什么 官网介绍是A type-safe HTTP client for Android and Java,是一个 RESTful 的 HTTP 网络请求框架的封装,但网络请求不是Re ...
- spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错
springboot 在使用thymeleaf 作为模板时,当出现未关闭标签时,如下所示代码,标签没有关闭. <link href="plugin/layui/css/layui.cs ...
- Windows下git设置代理服务器
SVN中,使用TortoiseSVN来进行版本控制时,设置代理非常简单,只需要在设置里面添加代理的信息即可.而 git 在GUI(v0.17.GITGUI)中却无法找到类似的设置,只能求助 git b ...
- Spring 切入点配置
有关各种拦截的切入点配置举例 (1)只对返回值为String的方法进行拦截 @Pointcut("execution (java.lang.String com.zzdr.spring.se ...
- 官网下载的Struts 2解压后缺少xwork-core.jar文件
为Eclipse配置Struts-2.5.10所需最少jar文件: 缺少的文件已被合并在struts2-core-2.5.10.jar文件中.我下的是最新版的,如果你下的找不到就是这个原因啦.
- git 入门教程之忽略文件
忽略文件 "并不是所有的牛奶都叫特仑苏",在版本控制系统中也有相似的表达,那就是"并不是所有的文件都需要提交". 有的是因为没必要提交,比如日志文件,系统缓存文 ...
- git 入门教程之github 教程
github 教程 github 是一个基于 git 的代码托管平台,是平时工作学习的好帮手,学会如何用好 github 网站能够帮助我们更好分享代码或者与其他开发人员合作. 注册 github 账号 ...