It is very difficult for others to answer questions... Alas
First, this question is abstracted into a directed graph. Due to the existence of magic power, this graph has a ring, so I think of the method of reducing points with strongly connected components, change this graph to a DAG (directed acyclic graph), and accumulate the weights of each vertex in each component (note that it may be 0)
Then, from the component of the vertex in the upper left corner, find the longest weighted path. There are many methods, such as topological sorting on the Internet, and Bellman-Ford (using memory-based search ), DFS and so on. Because of the small data volume, I used a simple DFS.
This question was read again. In the order from north to south then west to east, I thought it was a column-by-column scan, but in fact it was actually a row-by-row scan, despise your English...
However, after question a, there are still a lot of GAINS and I prefer the adjacent table.
Code:
# Include <iostream> <br/> # include <queue> <br/> # include <stack> <br/> using namespace STD; <br/> const int max = 1605; <br/> struct node <br/>{< br/> int V, next; <br/>} G [Max x 100], path [Max x 100]; <br/> int dfn [Max], low [Max], instack [Max], belong [Max], adj [Max], adj2 [Max], value [Max]; <br/> char map [45] [45]; <br/> int X [Max], Y [Max], mat [45] [45], vis [Max], V [Max]; <br/> int N, m, E, EE, index, CNT, Maxx; <br/> stack <int> S; <br/> V Oid add (int u, int v) <br/>{< br/> G [e]. V = V; G [e]. next = adj [u]; adj [u] = e ++; <br/>}< br/> void Add2 (int u, int V) <br/> {<br/> path [EE]. V = V; path [EE]. next = adj2 [u]; adj2 [u] = EE ++; <br/>}< br/> void DFS (int u, int sum) <br/>{< br/> Maxx = max (sum, Maxx); <br/> vis [u] = 1; <br/> for (INT I = adj2 [u]; I! =-1; I = path [I]. Next) <br/>{< br/> If (! Vis [path [I]. v]) <br/> {<br/> DFS (path [I]. v, sum + value [path [I]. v]); <br/>}< br/> vis [u] = 0; <br/>}< br/> void Tarjan (int u) <br/>{< br/> int V, I; <br/> dfn [u] = low [u] = ++ index; <br/> S. push (U); <br/> instack [u] = 1; <br/> for (I = adj [u]; I! =-1; I = G [I]. Next) <br/>{< br/> V = G [I]. V; <br/> If (! Dfn [v]) <br/>{< br/> Tarjan (V); <br/> low [u] = min (low [u], low [v]); <br/>}< br/> else if (instack [v]) <br/>{< br/> low [u] = min (low [u], dfn [v]); <br/>}< br/> If (dfn [u] = low [u]) <br/>{< br/> CNT ++; <br/> DO <br/> {<br/> V = S. top (); <br/> S. pop (); <br/> belong [v] = CNT; <br/> instack [v] = 0; <br/>}while (u! = V); <br/>}< br/> int main () <br/>{< br/> int I, J, K,, b, T; <br/> scanf ("% d", & T); <br/> while (t --) <br/>{< br/> memset (dfn, 0, sizeof (dfn); <br/> memset (instack, 0, sizeof (instack )); <br/> memset (adj,-1, sizeof (adj); <br/> memset (adj2,-1, sizeof (adj )); <br/> E = CNT = EE = Index = 0; <br/> scanf ("% d", & N, & M ); <br/> getchar (); <br/> for (I = 1; I <= N; I ++) <br/> {<br/> for (j = 1; j <= m; j ++) <br/> scanf ("% C ", & map [I] [J]); <br/> Ge Tchar (); <br/>}< br/> K = 0; <br/> memset (MAT, 0, sizeof (MAT )); <br/> for (I = 1; I <= N; I ++) <br/> {<br/> for (j = 1; j <= m; j ++) <br/> {<br/> If (Map [I] [J]> = '0' & map [I] [J] <= '9 ') <br/> V [(I-1) * m + J] = map [I] [J]-'0 '; <br/> else <br/> {<br/> If (Map [I] [J] = '*') <br/> mat [I] [J] = + + k; <br/> V [(I-1) * m + J] = 0; <br/>}< br/> for (I = 1; I <= K; I ++) <br/>{< br/> scanf ("% d", & X [I], & Y [I]); <br/> X [I] ++; <br/> Y [I] ++; <br/>}< br/> for (I = 1; I <= N; I ++) <br/>{< br/> for (j = 1; j <= m; j ++) <br/> {<br/> If (Map [I] [J] = '#') <br/> continue; <br/> K = (I-1) * m + J; <br/> If (J! = M & map [I] [J + 1]! = '#') <Br/>{< br/> Add (K, k + 1); <br/>}< br/> if (I! = N & map [I + 1] [J]! = '#') <Br/>{< br/> Add (K, K + M ); <br/>}< br/> If (Map [I] [J] = '*' & map [x [Mat [I] [J] [y [Mat [I] [J]! = '#') <Br/>{< br/> A = (X [Mat [I] [J]-1) * m + Y [Mat [I] [J]; <br/> Add (K, ); <br/>}< br/> while (! S. empty () <br/> S. pop (); <br/> for (I = 1; I <= m * n; I ++) <br/>{< br/> If (! Dfn [I]) <br/> Tarjan (I); <br/>}< br/> memset (value, 0, sizeof (value )); <br/> for (I = 1; I <= m * n; I ++) <br/> value [belong [I] + = V [I]; <br/> for (j = 1; j <= m * n; j ++) <br/> {<br/> for (I = adj [J]; i! =-1; I = G [I]. next) <br/> {<br/> A = belong [J]; <br/> B = belong [G [I]. v]; <br/> if (a = B) <br/> continue; <br/> Add2 (A, B ); <br/>}< br/> memset (VIS, 0, sizeof (VIS); <br/> Maxx =-1; <br/> DFS (belong [1], value [belong [1]); <br/> printf ("% d/N", Maxx ); <br/>}< br/> return 0; <br/>}