-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspreadsheet_UVA196.cpp
116 lines (100 loc) · 2.45 KB
/
spreadsheet_UVA196.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
ifstream fin("spreadsheet_UVA196.in");
ofstream fout("spreadsheet_UVA196.out");
const int inf = INT_MAX / 2;
struct pos
{
int y;
int x;
};
pos _pos(int y, int x)
{
pos temp{y, x}; return temp;
}
int getPos_s(string s)
{
int size = s.size(), ans = 0, x = 1;
for (int i = size - 1; i >= 0; --i)
{
int toi = s[i] - 'A'; ++toi;
ans += toi * x;
x *= 26;
}
return ans;
}
pos getPos(string s)
{
int size = s.size();
string s0 = "", s1 = "";
for (int i = 0; i <= size - 1; ++i)
{
if ('A' <= s[i] && s[i] <= 'Z') s0 += s[i];
else s1 += s[i];
}
return _pos(stoi(s1), getPos_s(s0));
}
int getNum(int y, int x, vector<vector<int>> & a_int, vector<vector<string>> & a_string)
{
if (a_int[y][x] < inf)
{
return a_int[y][x];
}
else if (a_string[y][x][0] != '=')
{
a_int[y][x] = stoi(a_string[y][x]);
return a_int[y][x];
}
else
{
int size = a_string[y][x].size(), ans = 0;
string temp = "";
for (int i = 1; i <= size; ++i)
{
if (a_string[y][x][i] == '+' || i == size)
{
pos now = getPos(temp);
ans += getNum(now.y, now.x, a_int, a_string);
temp = "";
}
else
{
temp += a_string[y][x][i];
}
}
a_int[y][x] = ans;
return a_int[y][x];
}
}
int main()
{
int tcc; fin >> tcc;
for (int t = 1; t <= tcc; ++t)
{
int xn, yn; fin >> xn >> yn;
vector<vector<int>> a_int(yn + 1, vector<int>(xn + 1, inf));
vector<vector<string>> a_string(yn + 1, vector<string>(xn + 1, "-"));
for (int y = 1; y <= yn; ++y)
{
for (int x = 1; x <= xn; ++x)
{
fin >> a_string[y][x];
}
}
for (int y = 1; y <= yn; ++y)
{
for (int x = 1; x <= xn; ++x)
{
if (y == 2 && x == 702)
{
for (int __s = 0; __s == 0; ++__s);
}
if (x > 1) fout << ' ';
int now = getNum(y, x, a_int, a_string);
fout << now;
}
fout << '\n';
}
}
return 0;
}