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缩点+拓扑排序)
题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...
随机推荐
- Myeclipse 添加插件
配置方式添加插件,添加一个反编译插件示例 1.官网下载(http://java-decompiler.github.io/) 2.下载到本地之后,删除多余的文件(其他的插件也是如此,例如SVN) 3. ...
- 1; XHTML 基本知识
万维网是我们这个时代最重要的信息传播手段.几乎任何人都可以创建自己的网站,然后把它发布在因特网上.一些网页属于企业,提供销售服务:另一些网页属于个人,用来分享信息.你可以自己决定网页的内容和风格.所有 ...
- 初识HTTP协议web开发
HTTP协议 HTTP协议简介 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的 ...
- Implemented the “Importance Sampling of Reflections from Hair Fibers”
Just the indirect specular pass by importance sampling. With all layers. Manually traced by 3D Ham ...
- 5步告诉你QQ音乐的完美音质是怎么来的,播放器的秘密都在这里
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由QQ音乐技术团队发表于云+社区专栏 一.问题背景与分析 不久前,团队发现其Android平台App在播放MV视频<凤凰花开的路口 ...
- 使用C#+PowerShell进行Windows系统间文件传输
新的winserver2016支持了一种nano模式,像以前的core模式,只能远程管理,只支持x64,只有610M,不让CentOS mini版独美. 这个nano版,默认只开启WinRM,所以只能 ...
- python安装pbkdf2 遇到错误TypeError: __call__() takes exactly 2 arguments (1 given)
python安装模块时遇到如下错误, import packaging.requirements File "/usr/lib/python2.7/site-packages/packagi ...
- 第一篇-Html标签中head标签,body标签中input系列,textarea和select标签
第十四周课程(1-12章节) HTML 裸体 CSS 穿华丽衣服 Javascript 动起来 一 HTML (20个标签) 1.我们的浏览器是socket客户端 2.一套规则,浏览器认识的规则 ...
- virtualbbox centos7 NAT模式外网 Host-only Adapter模式联网 双网卡
1.下载oracle VM virtualbox centos7 1.1. 下载地址:https://www.virtualbox.org/wiki/Downloads https://www.ce ...
- SQL Server Alert发送告警邮件少了的原因
最近突然发现我们部署在数据库上面的告警(Alert),当错误日志里面出现错误时,并不是每个错误日志都会发送邮件出来.如下所示,设置了告警"SQL Server Severity Event ...