UVA196-Spreadsheet(拓扑排序)
Spreadsheet
In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet application. It became a huge success and, at that time, was the killer application for the Apple II computers. Today, spreadsheets are found on most desktop computers.
The idea behind spreadsheets is very simple, though powerful. A spreadsheet consists of a table where each cell contains either a number or a formula. A formula can compute an expression that depends on the values of other cells. Text and graphics can be added for presentation purposes.
You are to write a very simple spreadsheet application. Your program should accept several spreadsheets. Each cell of the spreadsheet contains either a numeric value (integers only) or a formula, which only support sums. After having computed the values of all formulas, your program should output the resulting spreadsheet where all formulas have been replaced by their value.
Figure: Naming of the top left cells
Input
The first line of the input file contains the number of spreadsheets to follow. A spreadsheet starts with a line consisting of two integer numbers, separated by a space, giving the number of columns and rows. The following lines of the spreadsheet each contain a row. A row consists of the cells of that row, separated by a single space.
A cell consists either of a numeric integer value or of a formula. A formula starts with an equal sign (=). After that, one or more cell names follow, separated by plus signs (+). The value of such a formula is the sum of all values found in the referenced cells. These cells may again contain a formula. There are no spaces within a formula.
You may safely assume that there are no cyclic dependencies between cells. So each spreadsheet can be fully computed.
The name of a cell consists of one to three letters for the column followed by a number between 1 and 999 (including) for the row. The letters for the column form the following series: A, B, C, ..., Z, AA, AB, AC, ..., AZ, BA, ..., BZ, CA, ..., ZZ, AAA, AAB, ..., AAZ, ABA, ..., ABZ, ACA, ..., ZZZ. These letters correspond to the number from 1 to 18278. The top left cell has the name A1. See figure 1.
Output
The output of your program should have the same format as the input, except that the number of spreadsheets and the number of columns and rows are not repeated. Furthermore, all formulas should be replaced by their value.
Sample Input
1
4 3
10 34 37 =A1+B1+C1
40 17 34 =A2+B2+C2
=A1+A2 =B1+B2 =C1+C2 =D1+D2
Sample Output
10 34 37 81
40 17 34 91
50 51 71 172 题意:给你一个电子表格,行用数字表示,列用大写字母表示,如A3表示第3行第1列,表示列的比较特殊,依次为A,B,C.....AA(27),AB(28)........有些格子直接给的数字,有的给的表达式,
如=A1+B1+C1,表示由这3个数相加。而且题目保证都有解,不会出现那种循环无解的情况。行或者是列可能达到18287,但所有的个数不会非常大,所以不能开成2维数组,开一维就可以了。最后要
打印所有的数。 解析:这题是一个拓扑排序,但是比较麻烦的是处理输入,如果某个格子是一个表达式,如(D1)=A1+B1+C1,那么把A1(1),B1(2),C1(3)这3个点分别于D1(4)连一条指向D1(4)的边,只
有先知道了A1,B1,C1,才能得到D1,设置一个数组indeg[](表示入度),如果有u->v,则indeg[v]++,然后把入度为0的点(u)加入队列,把这个点指向的其他点(v)的入度减1并v的值加上u
的值,如果其他点(v)的入度此时变0,就加入队列。跑一边拓扑排序即可。
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
const int maxn=2000005; // 开到百万即可
int row,col; // 行,列
int val[maxn]; // 数值
int GetId(int x,int y){ return (x-1)*col+y; } // 得到某个位置的id
string S[maxn];
vector<int> G[maxn]; // 用动态数组建立临接表,我比较喜欢用这种方式
int indeg[maxn]; // 入度
int Get(const string& str) // 如果格子中是数值,则调用这个函数
{
int ret=0;
for(int i=0;i<str.size();i++) ret=ret*10+str[i]-'0';
return ret;
}
void change_to_num(const string& str,int id)
{
int i;
for(i=0;i<str.size();i++)
{
if(str[i]>='0'&&str[i]<='9') break; // 找到字母和数字的分界位置
}
int R=0,C=0;
for(int j=0;j<i;j++) C=C*26+str[j]-'A'+1; // 分别计算行跟列
for(int j=i;j<str.size();j++) R=R*10+str[j]-'0';
int to=GetId(R,C);
G[to].push_back(id); // 建表
indeg[id]++;
}
void link(const string& str,int id)
{
int pre=1;
int i;
for(i=1;i<str.size();i++)
{
if(str[i]=='+')
{
change_to_num(str.substr(pre,i-pre),id); //单独分离出来如=A1+B1+C1,分离出A1,B1,
pre=i+1;
}
}
change_to_num(str.substr(pre,i-pre),id); // 最后一个还要处理,如分离出最后的C1
}
void Get_Val(const string& str,int id)
{
if(str[0]!='=') val[id]=Get(str); // 如果是数值
else link(str,id); // 要建立临接表
}
queue<int> que;
void toposort()
{
while(!que.empty()) que.pop();
for(int i=1;i<=row*col;i++) if(!indeg[i]) que.push(i); //入度为0 的加入队列
while(!que.empty())
{
int now=que.front(); que.pop();
for(int i=0;i<G[now].size();i++)
{
int to=G[now][i];
indeg[to]--;
val[to]+=val[now]; //加上数值
if(!indeg[to]) que.push(to); //变为0就加入队列
}
}
}
void ans_print()
{
for(int i=1;i<=row;i++)
{
for(int j=1;j<=col;j++)
{
if(j!=1) printf(" ");
printf("%d",val[GetId(i,j)]);
}
printf("\n");
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&col,&row);
memset(val,0,sizeof(val));
for(int i=1;i<=row*col;i++) G[i].clear();
memset(indeg,0,sizeof(indeg));
for(int i=1;i<=row;i++)
{
for(int j=1;j<=col;j++)
{
int id=GetId(i,j); // 得到他的id
cin>>S[id];
Get_Val(S[id],id); // 处理
}
}
toposort(); // 拓扑排序
ans_print(); // 打印
}
return 0;
}
UVA196-Spreadsheet(拓扑排序)的更多相关文章
- UVA215-Spreadsheet Calculator(模拟+拓扑排序)
Problem UVA215-Spreadsheet Calculator Accept:401 Submit:2013 Time Limit: 3000 mSec Problem Descript ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- 拓扑排序 - 并查集 - Rank of Tetris
Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...
随机推荐
- 使用NAT方式连网的linux服务器虚拟机搭建
从一开始我就很纠结centos服务器搭建的过程. 由于自己方向并不在运维上,但是学习开发也需要用到Linux所以就一直没认真去学. 经过自己多方面摸索与学习找到了自己的一套方法. 首先我用到的是 ce ...
- Codeforces 190E - Counter Attack
[题意]给一个无向图的反图(即给定的边实际上不存在,而未给的边是存在的),求连通块数.(点数n<=5*10^5,边数m<=10^6) 一开始我想的用DFS,枚举每一个点,跳过不存在的点,直 ...
- 请输出in.txt文件中的2 4 6 8 9 10 12行
in.txt文件: 学号 姓名 性别 年龄 1001 张三 男 18 1002 赵四 男 19 1003 李丽 女 18 1004 刘芳 女 32 1005 王五 男 54 1006 小明 男 32 ...
- codevs 1128 导弹拦截 (贪心)
/* 题目大体意思是两套系统好多导弹 怎样分配使得两个系统所拦截的最大半径之和最小 贪心:把距离1系统最远的 让2拦截 记好距离 然后按照距离1由远到近排序 对于每一个导弹 如果这之前的都给2拦截 则 ...
- poj 1125 Stockbroker Grapevine (dij优化 0ms)
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...
- DataTable数据与Excel表格的相互转换
using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...
- java获取对象属性类型、属性名称、属性值 【转】
/** * 根据属性名获取属性值 * */ private Object getFieldValueByName(String fieldName, Object o) { try { String ...
- 关于Core Data的一些整理(一)
关于Core Data的一些整理(一) 在Xcode7.2中只有Mast-Debug和Single View中可以勾选Use Core Data 如果勾选了Use Core Data,Xcode会自动 ...
- 网络编程Socket之TCP
服务端: 1. 创建 ServerSocket 对象并监听一个端口 2. 调用accept()方法等待客户端的连接(阻塞式) 3. 输入流(记取客户端发送过来的数据) 4. 输出流(响 ...
- Hive学习之二 《Hive的安装之自定义mysql数据库》
由于MySQL便于管理,在学习过程中,我选择MySQL. 一,配置元数据库. 1.安装MySQL,采用yum方式. ①yum install mysql-server,安装mysql服务端,安装服 ...