UVA_196_Spreadsheet

来源:互联网 发布:uefi硬盘安装ubuntu 编辑:程序博客网 时间:2024/06/05 01:13
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::map;
using std::stack;
using std::queue;
int convert(const string &str)
{
int sum = -1;
for (size_t i = 0; i < str.size(); i++)
{
sum = (sum + 1) * 26 + str[i] - 'A';
}
return sum;
}
typedef struct
{
int sum;//空格的值
list<pair<int, int>>prenode;//该结点的所有前驱点
}CeilType;
void getFormula(const string&str,CeilType&ceil)
{
if (isdigit(str[0])||str[0]=='-')//这里绝对不能省str[0]=='-'
{
stringstream stream;
stream << str;
stream >> ceil.sum;
}
else
{
string row;
int column = 0;
for (size_t i = 1; i < str.size(); i++)
{
if (str[i] == '+')
{
ceil.prenode.push_back({ column - 1 ,convert(row)});
row.clear();
column = 0;
}
else if (isalpha(str[i]))
{
row += str[i];
}
else
{
column *= 10;
column += str[i] - '0';
}
}
}
}
int toposort(vector<vector<CeilType>>&sheet,pair<int,int>pos)
{
if (sheet[pos.first][pos.second].prenode.empty())
{
return sheet[pos.first][pos.second].sum;
}
while (sheet[pos.first][pos.second].prenode.size())
{
auto adjvex = *sheet[pos.first][pos.second].prenode.begin();
sheet[pos.first][pos.second].prenode.erase(sheet[pos.first][pos.second].prenode.begin());
sheet[pos.first][pos.second].sum += toposort(sheet, adjvex);
}
return sheet[pos.first][pos.second].sum;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T; cin >> T;
while (T--)
{
int row, column; cin >> column >> row;
vector<vector<CeilType>>sheet(row, (vector<CeilType>)column);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
string str; cin >> str; str.push_back('+');
getFormula(str, sheet[i][j]);
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cout << toposort(sheet, { i,j });
if (j != column - 1)
{
cout<<' ';
}
}
cout << endl;
}
}
return 0;
}

0 0