#include<stdio.h>
#include<iostream>
#include <strstream>
using namespace std; #define MC 1010
#define MR 999 struct Node
{
int r;
int c;
int total;
int hl;
Node* aN;
Node pop()
{
hl--;
return aN[hl];
}
void push(Node n)
{
aN[hl] = n;
hl++;
}
}; void parse(string str, Node* node);
void printNode(int num, int c);
void print(Node** a, int r, int c);
void dfs(Node** a, Node node); int main()
{
freopen("d:\\1.txt", "r", stdin);
int t;
cin >> t;
while (t--)
{
int c, r;
cin >> c >> r;
Node** a = new Node*[r];
for(int i = 0; i < r; i++)
{
a[i] = new Node[c + 1];
int num;
for(int j = 1; j <= c; j++)
{
cin >> num;
Node* node = new Node;
node->hl=0;
node->total=0;
node->r = i;
node->c = j;
if(cin.fail())
{
cin.clear();
string str;
cin >> str;
node->aN = new Node[100];
parse(str, node);
}
else
{
node->total = num;
}
a[i][j] = *node;
}
}
print(a, r, c);
// cout << endl;
}
} void printNode(int num, int c)
{
if(c == 1)
{
cout << num;
}
else
{
cout << " " << num;
} } void parse(string str, Node* node)
{
char c1;
int c = 0, r = 0;
int length = str.length();
for(int i = 0; i <length; i++)
{
c1 = str.at(i);
if(c1 == '=')
{
continue;
}
if(c1 == '+')
{
//new node
//列从1开始
Node* a = new Node;
a->c = c;
a->r = r - 1;
node->push(*a);
c = 0, r = 0;
continue;
}
if('A' <= c1 && c1 <= 'Z')
{
int xx = c1 - 'A';
c = c * 26 + xx + 1;
}
if('0' <= c1 && c1 <= '9')
{
int yy = c1 - '0';
r = r * 10 + yy;
}
}
Node* a = new Node;
a->c = c;
a->r = r - 1;
node->push(*a);
}
void dfs(Node** a, Node node)
{
while (a[node.r][node.c].hl)
{
Node n = a[node.r][node.c].pop();
dfs(a, n);
//if(node.r!=n.r||node.c!=n.c)
a[node.r][node.c].total += a[n.r][n.c].total;
}
}
void print(Node** a, int r, int c)
{
for(int i = 0; i < r; i++)
{
for(int j = 1; j <= c; j++)
{
if(a[i][j].hl != 0)
{
dfs(a, a[i][j]);
}
printNode(a[i][j].total, j);
}
cout << endl;
}
}

  附上一组测试用例:

in:

3
10 10
=J2+E3+J1 =E1 80 =J6 77 =E5 73 =I9+F1+D7 =C9+I10+D5 =J10
31 =I6 =F8 =D1+F7 =F3+A3 58 73 66 =B2+G3 =C5+B4
0 =G8+A3+J10 65 48 94 18 =A3+D6+E10 =C9 40 98
=G1+A10 =F2+C1+F2 =A3+F2+F8 30 84 70 =G10 17 =J6+C4+E6 =G8+E6
44 81 43 =I9+B8 19 =H4+I9 82 91 70 79
=F7+A2+I9 =C3+I10+J10 =J8 83 3 =F7+D3+G4 =G10+D10+F7 =J2+C9+G1 21 =C1+J5
=D5 26 =G10+D5+I9 35 31 =D8+I9+H4 79 61 96 =B3+A6+D8
43 =I9 30 77 9 =D8+H4 92 84 =J1+A1 32
95 46 =I9 =G7 =F2+G6+B1 =G1 20 =G1+F7+D8 10 14
9 71 =D8+C9 72 =C9+C1 =I3+D5+E6 3 63 =C5+B8+F2 92
8 10
38 =E7+C9+F10 66 68 =A1+A1+C7 91 =H5+H1+A1 =G10+C9
1 5 =B2+B5+A10 =F6+A9+B9 42 =A1+A4+B8 82 =B2+C7+G8
29 70 95 =B2+C3 45 66 0 =H4+H5+F4
=G7+G8 =A7 =H6+E7+A4 54 =E2+D10+D6 4 =G10+A7 67
=B4 15 =G2+A7+F10 20 78 52 =E8+H9 0
=H4+E1 87 8 63 45 =B5+C9 =C10+B7+H2 =A7
=C9+C9+C9 89 =C9+F5+C5 45 67 42 =B5 =C5+H9
=C9+A7 =C9+C10 0 =G6+D1+D6 =B2 75 =B2+C7 =C9+C9+C9
=F6+A2+H8 65 76 =H1 =D10+E6 68 70 33
40 =G4 70 97 55 76 47 =H6
10 10
51 =A5+C1 =D9+G1+H4 =I1+G8+H7 59 =J1+B1+E4 =E6+J3+E6 98 =A1+H4+I9 4
96 34 =B3+H6 59 =A5+F5+A1 =D6+A1+E8 =F5 =E7 =I6 =F2+I1
=H2 38 =D10 28 =F5 90 86 =J3+C7+E8 =E8+D8 89
91 22 =E2 =E7+B5 =C4+C6 56 =J1 =D8+A1 83 71
36 =J8+J9+G10 84 12 32 =F9+A3 60 =J3 =H9 27
=I9+F2 =E8 27 97 4 79 =B1+E7 =G1+H2+E10 =B1+E2+E10 =A5+F3+D9
62 15 =I3+G1 =H8+F4 13 41 82 =E10+A3 72 =J3
99 =H2 =I1 83 46 =D5 =C6 85 58 14
=H2+F9 =E1 81 =H8+H6 72 99 42 =F5+H10 =J3 =H8
=A8 =C3 =E1 43 =H2+D8+E7 =D2+E7+J7 =H4+I9+E10 69 =H8 =H9+H10+C5

out:

425 77 80 159 77 19 73 64 141 92
31 21 94 263 18 58 73 66 194 239
0 184 65 48 94 18 173 10 40 98
82 196 152 30 84 70 3 17 314 95
44 81 43 20 19 27 82 91 70 79
145 268 32 83 3 155 179 322 21 159
20 26 33 35 31 104 79 61 96 406
43 10 30 77 9 94 92 84 517 32
95 46 10 79 314 73 20 254 10 14
9 71 87 72 90 63 3 63 111 92
38 219 66 68 590 91 161 123
1 5 60 476 42 718 82 1038
29 70 95 100 45 66 0 71
534 228 829 54 202 4 275 67
228 15 386 20 78 52 38 0
657 87 8 63 45 91 1197 228
228 89 514 45 67 42 15 419
304 146 0 1328 5 75 519 228
320 65 76 123 142 68 70 33
40 275 70 97 55 76 47 228
51 571 535 423 59 801 97 98 274 4
96 34 257 59 199 194 112 13 879 468
13 38 43 28 112 90 86 361 129 89
91 22 199 444 226 56 4 134 83 71
36 431 84 12 32 112 60 89 181 27
283 46 27 97 4 79 584 219 879 430
62 15 226 141 13 41 82 122 72 89
99 13 274 83 46 12 27 85 58 14
112 59 81 304 72 99 42 181 89 85
99 43 59 43 109 161 332 69 85 334

UVA196的更多相关文章

随机推荐

  1. android复制包需要修改的几个地方

    1.要看什么情况,若是在同一个eclipse下,那么就需要修改包名.若不在的话,那就可以不用了. 2.这个app_name也是一样. 3.先说下情况,这是我修改好的.原本这个R的是引之前包的,必须要改 ...

  2. Sublime 下配置vim模式 + VintageEx-master下载地址

       VintageEx-master下载地址: 官方地址:https://github.com/SublimeText/VintageEx 百度云链接: http://pan.baidu.com/s ...

  3. HDU 2111:Saving HDU(贪心)

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. Spring学习--静态工厂方法、实例工厂方法创建 Bean

    通过调用静态工厂方法创建 bean: 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节. 要声明 ...

  5. 初学者必读原生AJAX-异步的javaScript和XML

    1.前台页面 <form action="#" method="post"> 姓名<input type="text" n ...

  6. day26 python学习 对象的接口,封装,私用属性 property

    # 抽象类和接口类 #** #不崇尚接口类 #python本身支持多继承,没有接口专用的语法.但是我知道接口的概念 # 接口类:# 是规范子类的一个模板,只要接口类中定义的,就应该在子类中实现# 接口 ...

  7. jmeter测试TCP服务器/模拟发送TCP请求

    jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响应. jmeter模拟发送TCP请求的方法: 1. ...

  8. hadoop大数据处理平台与案例

    大数据可以说是从搜索引擎诞生之处就有了,我们熟悉的搜索引擎,如百度搜索引擎.360搜索引擎等可以说是大数据技处理技术的最早的也是比较基础的一种应用.大概在2015年大数据都还不是非常火爆,2015年可 ...

  9. idea引入svn

    刚想在idea看一个svn的项目代码,结果发现导入项目后,idea在右下角弹出了Event Log窗口,里面的红色小字 Can't use Subversion command line client ...

  10. ASP 三十二条精华代码 (1)

    ASP 三十二条精华代码 (1) 2009-08-10 09:53:03  www.hackbase.com  来源:互联网 1. oncontextmenu="window.event.r ...