菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#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

posted on 2017-05-07 13:38  好吧,就是菜菜  阅读(334)  评论(0编辑  收藏  举报