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

2 2
A1+B1
5
3
B0-A1
3 2
A0
5
C1
7
A1+B1
B0+A1
0 0
 
 

 Sample Ouput

      0     1
A     3     5
B     3    -2
A0: A0
B0: C1
C1: B0+A1
 
题解:一道模拟题,我一开始没理解题意,WA了两发,主要是没有考虑可以计算的数字表达式,考虑了之后就没有太大问题了。
这个题的递归函数框架其实是按照拓扑排序来写的,vis数组三种状态0,1,-1,分别表示未访问,正在访问,已访问。这样就可以轻松找环(dfs过程中遇到vis为-1的点就意味着找到了环)。
把环标记一下可以大大剪枝。
 
 #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(模拟+拓扑排序)的更多相关文章

  1. POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28399   Accepted: 9684 De ...

  2. Codeforces 909 substr用法 思维合并线段目标最少 Py语句逆推DP vecrtor缩点删不同颜色点模拟 拓扑排序处理任务

    A str.substr(i,j) 从str[i]开始起取j个字符作为返回的字符串 /* Huyyt */ #include <bits/stdc++.h> using namespace ...

  3. Day1:T1 模拟 T2 拓扑排序

    T1:模拟 自己第一天的简直跟白痴一样啊...模拟都会打错.. 当时貌似在更新最大值的时候打逗比了... if((sum[x]==max && x<maxh) || sum[x] ...

  4. [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 ...

  5. 【noip模拟赛4】找啊找啊找BF 拓扑排序

    描述 sqybi上次找GF的工作十分不成功,于是依旧单身的他在光棍节前的某天突发奇想,要给自己找一个BF(这里指的是男性的好朋友……),这样既可以和人分享内心的压抑(路人甲:压抑还分享么……),也可以 ...

  6. [JZOJ 5905] [NOIP2018模拟10.15] 黑暗之魂(darksoul) 解题报告 (拓扑排序+单调队列+无向图基环树)

    题目链接: http://172.16.0.132/senior/#main/show/5905 题目: oi_juruo热爱一款名叫黑暗之魂的游戏.在这个游戏中玩家要操纵一名有 点生命值的无火的余灰 ...

  7. [NOIP2015模拟10.27] 挑竹签 解题报告(拓扑排序)

    Description 挑竹签——小时候的游戏夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏.挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签.如果动了其他的竹签,就要换对手来挑.在所有 ...

  8. UVA - 12263 Rankings 模拟(拓扑排序)

    题意:1~n这n个数,给你一个初始的顺序,再告诉你那两个数的大小关系发生了变化,求变化后的 顺序,不存在则输出IMPOSSIBLE 思路:这题很遗憾没在比赛的时候过掉,结束后加了一行就AC了.题目真的 ...

  9. 【2019.7.26 NOIP模拟赛 T3】化学反应(reaction)(线段树优化建图+Tarjan缩点+拓扑排序)

    题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...

随机推荐

  1. js实现ctrl+v上传图片

    描述:实现类似QQ截图删上传图片的功能 a.需要的js插件 paste.image.js 地址:https://github.com/iyangyuan/pasteimg b.paste.image. ...

  2. Netty实战十三之使用UDP广播事件

    1.UDP的基础知识 我们将会把重点放在一个无连接协议即用户数据报协议(UDP)上,它通常用在性能至关重要并且能够容忍一定的数据报丢失的情况下. 面向连接的传输(如TCP)管理了两个网络端点之间的连接 ...

  3. 带你使用JS-SDK自定义微信分享效果

    前言 想必各位在写wap端时都遇到过这样的场景吧 ----自定义分享标题.图片.描述 接下来小编给大家讲解下分享相关操作 预期效果 原始的分享效果: 使用微信JS-SDK的分享效果: 可以看出缩略图, ...

  4. C# 添加Windows服务,定时任务。

    源码下载地址:http://files.cnblogs.com/files/lanyubaicl/20160830Windows%E6%9C%8D%E5%8A%A1.zip 步骤 一 . 创建服务项目 ...

  5. Python date,datetime,time等相关操作总结

    date,datetime,time等相关操作总结   by:授客 QQ:1033553122 测试环境: Python版本:Python 3.3.2 代码实践: __author__ = '授客' ...

  6. Android为TV端助力 切换fragment的两种方式

    使用add方法切换时:载入Fragment1Fragment1 onCreateFragment1 onCreateViewFragment1 onStartFragment1 onResume用以下 ...

  7. <API自动化测试>Centos-Newman

    一.介绍: 在测试和开发中,有一款API测试工具一直占据着武林盟主的地位,那就是声名远播的Google公司的Postman. Postman原先是Chrome浏览器的一个插件,后面发展成了一个应用程序 ...

  8. Android自定义多宫格解锁控件

    在此之前,一直在想九宫格的实现方法,经过一个上午的初步研究终于完成了一个简单的N*N的宫格解锁组件,代码略显粗糙,仅仅做到简单的实现,界面等后期在做优化,纯粹是学习的目的,在算法上有点缺陷,如果有错误 ...

  9. 章节四、2-Switch语句

    package introduction5; public class SwitchDemo { //switch用于固定值的判断,如星期.人的性别 //if用于判断区间.范围,能够用switch进行 ...

  10. Bullet3的一些理解

    Bullet3应该是第三大物理引擎了,拥有宽松的授权方式,开源.在我的项目中将采用它. 碰撞世界(btCollisionWorld)是最基本的环境类. 动态世界(btDynamicsWorld)从碰撞 ...