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缩点+拓扑排序)
题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...
随机推荐
- codeM美团编程大赛初赛B轮D题(考验你的数学思维!)
[编程题] 模 时间限制:1秒空间限制:32768K 给定四个正整数a,b,c,k,回答是否存在一个正整数n,使得a*n在k进制表示下的各位的数值之和模b为c.输入描述:第一行一个整数T(T < ...
- [leetcode](4.21)3. 最长重复子串
给定字符串 S,找出最长重复子串的长度.如果不存在重复子串就返回 0. 示例 1: 输入:"abcd" 输出:0 解释:没有重复子串. 示例 2: 输入:"abbaba& ...
- (6)Jquery1.8.3快速入门_过滤选择器
一.Jquery的基本过滤选择器: 基本过滤选择器: 1. :first 选取第一个元素 2. :last 选取最后一个元素 3.:not(selector) 去除所有与给定的选择器匹配的元素 4.: ...
- json接口返回值
SpringBoot响应客户端渲染数据 1.SpringBoot构造并且返回一个json对象 首先,我们在父包下新建一个pojo的包,并新建一个User类 IDEA自动生成对应的get/set,然后 ...
- Spring Boot(Spring的自动整合框架)
Spring Boot 是一套基于Spring框架的微服务框架,由于Spring是一个轻量级的企业开发框架,主要功能就是用于整合和管理其他框架,想法是将平时主流使用到的框架的整合配置预先写好,然后通过 ...
- Java并发编程-Semaphore
基于AQS的前世今生,来学习并发工具类Semaphore.本文将从Semaphore的应用场景.源码原理解析来学习这个并发工具类. 1. 应用场景 Semaphore用来控制同时访问某个特定资源的操作 ...
- 在html中使用特殊字体
目的:一首诗,要求从右往左读,垂直排列,类似古文 效果图: html内容: <!doctype html><html lang="en"><head& ...
- Neutron中插件与代理的总结
1.总结:
- css文本超出隐藏显示省略号
p style="width: 300px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;"> 如 ...
- html + css3 demo
最近,在做一个比较大的网站,主要服务于欧美地区,全站为英文版本,因为是电子产品,因此,要展示产品内在美(扯个蛋!)仿照小米.錘子.苹果等网站,着重于css3动效效果,搜集整理了一些网站中用到的动效图, ...