-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1034162_UVA196_20170510.cpp
74 lines (74 loc) · 1.35 KB
/
A1034162_UVA196_20170510.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
#include<iostream>
#include<vector>
#include<cstdio>
#include<string.h>
using namespace std;
struct pts
{
int row,col;
};
vector<pts> sheet[1010][1010];
int value[1010][1010];
int dfs(int r,int c)
{
if(value[r][c] != -1) return value[r][c];
int ans = 0,l = sheet[r][c].size();
for(int i = l - 1;i >= 0;i--)
{
ans += dfs(sheet[r][c][i].col,sheet[r][c][i].row);
sheet[r][c].pop_back();
}
return value[r][c] = ans;
}
void input(int i,int j)
{
char s[5000];
int k;
pts p;
scanf("%s",s);
if(s[0] >= '0' && s[0] <= '9')
{
value[i][j] = 0;
for(k = 0;s[k] != '\0';k++)
value[i][j] = value[i][j]*10 + s[k] - '0';
}
else
{
p.row = 0,p.col = 0;
for(k = 1;k <= strlen(s);k++)
{
if(s[k] >= 'A' && s[k] <= 'Z')
p.row = p.row*26 + s[k] - 'A' + 1;
else if(s[k] >= '0' && s[k] <= '9')
p.col = p.col*10 + s[k] - '0';
else
{
sheet[i][j].push_back(p);
value[i][j] = -1,p.col = p.row = 0;
}
}
}
}
int main()
{
int n,r,c,i,j,k;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&c,&r);
for(i = 1;i <= r;i++)
for(j = 1;j <= c;j++)
input(i,j);
for(i = 1;i <= r;i ++)
for(j = 1;j <= c;j++)
dfs(i,j);
for(i = 1;i <= r;i++)
{
printf("%d",value[i][1]);
for(j = 2;j <= c;j++)
printf(" %d",value[i][j]);
puts("");
}
}
return 0;
}