# Include <iostream>
# Include <queue>
Using namespace STD;
# Define Max 1001 // no extra points are allowed at the end of this article. Otherwise, he defines the value of Max as "1001 ;"
Int s [10] [10];
Int T [10] [10] [5];
Int map [4] [2] = {1, 0}, {0, 1}, {-1}, {0,-1 }};
Int I, J, K, X, Y;
Struct Node
{
Int X;
Int y;
Int state;
} Start, temp, end;
// The Key to this question is that the cost value of different States leads to different minimum values, so they must be retained.
Void insert ()
{
For (I = 1; I <9; I ++)
For (j = 1; j <9; j ++)
Cin> S [I] [J];
for (I = 1; I <= 8; I ++)
{< br> for (j = 1; j <= 8; j ++)
{< br> for (k = 1; k <= 4; k ++)
T [I] [J] [k] = 100000000;
}< BR >}< br> T [start. y] [start. x] [1] = 0;
}
void BFS (queue qu)
{< br> int V, ans;
while (! Qu. empty ()
{< br> temp = Qu. front ();
Qu. pop ();
for (I = 0; I <4; I ++)
{< br> X = temp. X + map [I] [0];
Y = temp. Y + map [I] [1];
If (x> 0 & x <9 & Y> 0 & Y <9)
{
Start. x = X;
Start. Y = y;
V = T [temp. Y] [temp. x] [temp. State] + temp. State * s [y] [x];
Start. State = (temp. State * s [y] [x] % 4) + 1;
If (v <t [y] [x] [start. State])
{
Qu. Push (start );
T [y] [x] [start. State] = V;
}
// S [y] [x] = max;
}
}
}
Ans = T [end. Y] [end. x] [1];
For (I = 2; I <= 4; I ++)
{
If (ANS> T [end. Y] [end. x] [I])
Ans = T [end. Y] [end. x] [I];
}
Cout <ans <Endl;
}
// The number before the value is 128 is a large number of 16843009 and so on. After the value is 128, the number is a negative number and the value 0 is 0.
Int main ()
{
Int A [2], B [2];
While (CIN> A [0]> A [1]> B [0]> B [1] &! (A [0] = 0 & A [1] = 0 & B [0] = 0 & B [1] = 0 ))
{
// Memset (S, Max, sizeof (s); // here memset does not seem to allow the whole array s to be cleared, so it cannot save
Queue <struct node> qu;
Start. x = A [1];
Start. Y = A [0];
Start. State = 1;
Insert ();
S [A [0] [A [1] = max;
End. x = B [1];
End. Y = B [0];
Qu. Push (start );
BFS (qu );
}
Return 0;
}