您的位置:首页 > 其它

UVA_196_Spreadsheet

2016-04-02 14:31 429 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: