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. Spring核心——设计模式与IoC

    “Spring”——每一个Javaer开发者都绕不开的字眼,从21世纪第一个十年国内异常活跃的SSH框架,到现在以Spring Boot作为入口粘合了各种应用.Spring现在已经完成了从web入口到 ...

  2. strdup strcpy 的区别

    strdup可以直接把要复制的内容复制给没有初始化的指针,因为它会自动分配空间给目的指针 strcpy的目的指针一定是已经分配内存的指针

  3. 记录一些日常windows命令或操作技巧

    一.远程连接 通常我们发布项目的时候会先发布成本地文件然后通过远程服务器连接放到测试服务器发布成站点,这里就涉及到对远程发布的一些操作. 1. 点击运行,输入 mstsc /admin (这里的adm ...

  4. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  5. MongoDB 通过配置文件启动及注册服务

    1.配置mongodb环境变量,配置完成之后就可以直接执行mong.mongod等常用命令,不用每次都到mongodb安装目录bin下去执行: 2.通过命令启动mongo服务 mongod --dbp ...

  6. Android IPC机制(二)用Messenger进行进程间通信

    Messenger可以在不同进程中传递Message对象,我们在Message中加入我们想要传的数据就可以在进程间的进行数据传递了.Messenger是一种轻量级的IPC方案并对AIDL 进行了封装, ...

  7. codeforces 632C The Smallest String Concatenation

    The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...

  8. Linux学习笔记 软链接和硬链接

    Linux 中,"everything is file".接下来给大家介绍 linux 如何通过链接,达到节省磁盘空间.共享文件等目的.链接文件有两种方式,软链接(soft lin ...

  9. [katalon] 页面切换

    UI自动化测试过程中会涉及到需要切换多个页面, 如点击一个按钮之后跳转到新的页面, 后者A站点提交信息后,B站点审核. Katalon虽然不支持控制多个浏览器,但是支持处理tab切换. 核心方法是使用 ...

  10. [20181220]使用提示OR_EXPAND优化.txt

    [20181220]使用提示OR_EXPAND优化.txt --//链接http://www.itpub.net/thread-2107240-2-1.html,http://www.itpub.ne ...