<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>



<style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style>
<style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style> <style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document"><title>top coder practice room(practice room1-16)(2001 invi-semi)</title>

<style>
<!--
table.MsoTableGrid
	{border:1.0pt solid windowtext;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.0pt;
	font-family:"Times New Roman";
	}
 li.MsoNormal
	{mso-style-parent:"";
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";
	margin-left:0cm; margin-right:0cm; margin-top:0cm}
-->
</style>
<style type="text/css">
      <!--
        body { font-family: Monospaced; font-size: 12pt }
        pre { font-family: Monospaced; font-size: 12pt }
      -->
    </style><style type="text/css">
      <!--
        body { font-family: Monospaced; font-size: 12pt }
        pre { font-family: Monospaced; font-size: 12pt }
      -->
    </style><style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style></head><body rightmargin="1" bottommargin="1" marginheight="1" marginwidth="1"><br>
<br>
<font color="#ff0000" size="6"><b><span lang="en-ca">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Expensive Travel<br>
<br>
</span></b></font>//============================================================================<br>
// Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : multiFactorial.cpp<br>
// Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : nick<br>
// Version&nbsp;&nbsp;&nbsp;&nbsp; :<br>
// Copyright&nbsp;&nbsp; : Your copyright notice<br>
// Description : Hello World in C++, Ansi-style<br>
//============================================================================<br>
/****************************************************************************************************<br>
<br>
Problem Statement<br>
&nbsp;&nbsp;&nbsp; <br>
You are lost in a strange land and have to return to the base camp as
soon as possible. The land is modeled as a rectangular plane divided
into square cells, and each cell can be of one of 9 different types,
numbered from 1 to 9. The cost of a cell of type k is exactly 1/k.<br>
To get to the base camp, you can move from each cell to any of its
orthogonally adjacent cells (up, down, left or right) instantaneously.
The only limitation for the speed of your movements is the
cost-per-minute. Time will be divided into one minute intervals, i.e.,
there is an interval starting at time 0 and ending at time 1, another
interval from time 1 to time 2, etc. During each interval of time, the
sum of the costs of all the cells you occupy must not exceed 1. If you
make an instantaneous move at the exact boundary between two intervals
of time, the cells you move between during that move will count toward
both intervals' total costs. This means you can never step into or out
of a cell of type 1 because any other cell you occupy will always
exceed the maximum cost per minute.<br>
You will be given m, a vector &lt;string&gt; describing the land, where
the jth character of the ith element represents the type of the cell at
row i, column j (both 1-based). You will also be given startRow,
startCol, endRow and endCol, the 1-based indices of the row and column
of your starting point and destination, respectively. Return the
minimum number of minutes that are needed to get from (startRow,
startCol) to (endRow, endCol) following the constraints above. If it's
impossible to do so, return -1.<br>
Definition<br>
&nbsp;&nbsp;&nbsp; <br>
Class:<br>
ExpensiveTravel<br>
Method:<br>
minTime<br>
Parameters:<br>
vector &lt;string&gt;, int, int, int, int<br>
Returns:<br>
int<br>
Method signature:<br>
int minTime(vector &lt;string&gt; m, int startRow, int startCol, int endRow, int endCol)<br>
(be sure your method is public)<br>
&nbsp;&nbsp;&nbsp; <br>
<br>
Constraints<br>
-<br>
m will contain between 1 and 50 elements, inclusive.<br>
-<br>
Each element of m will contain exactly N characters, where N is an integer between 1 and 50, inclusive.<br>
-<br>
Each character of each element of m will be a digit between '1' and '9', inclusive.<br>
-<br>
startRow and endRow will each be between 1 and the number of elements in m, inclusive.<br>
-<br>
startCol and endCol will each be between 1 and the number of characters in the first element of m, inclusive.<br>
-<br>
Either startCol will be different from endCol or startRow will be different from endRow.<br>
Examples<br>
0)<br>
<br>
&nbsp;&nbsp;&nbsp; <br>
{"22334"}<br>
1<br>
1<br>
1<br>
5<br>
Returns: 3<br>
During the first minute, you can move 1 cell to the right. The two
cells that you occupy during that minute are both of type 2, so the
cost is 1/2+1/2=1. In the second minute, you can move 1 more cell to
the right. You cannot go any further because 1/2+1/3+1/3 &gt; 1. During
the third minute, you can reach your destination by moving 2 cells to
the right for a cost of 1/3+1/3+1/4 &lt; 1.<br>
1)<br>
<br>
&nbsp;&nbsp;&nbsp; <br>
{"55",<br>
&nbsp;"52",<br>
&nbsp;"55"}<br>
1<br>
2<br>
3<br>
2<br>
Returns: 1<br>
You can step on all 5 5's during the same interval, so you can get there in just 1 minute.<br>
2)<br>
<br>
&nbsp;&nbsp;&nbsp; <br>
{"251",<br>
&nbsp;"212",<br>
&nbsp;"122"}<br>
1<br>
1<br>
3<br>
3<br>
Returns: -1<br>
Since it's impossible to step into a cell of type 1, there is no way to get to your destination.<br>
3)<br>
<br>
&nbsp;&nbsp;&nbsp; <br>
{"452232",<br>
&nbsp;"287979",<br>
&nbsp;"219872",<br>
&nbsp;"928234",<br>
&nbsp;"767676"}<br>
1<br>
6<br>
3<br>
1<br>
Returns: 3<br>
<br>
4)<br>
<br>
&nbsp;&nbsp;&nbsp; <br>
{"11"}<br>
1<br>
1<br>
1<br>
2<br>
Returns: -1<br>
<br>
5)<br>
<br>
&nbsp;&nbsp;&nbsp; <br>
{"123456789987654321"}<br>
1<br>
2<br>
1<br>
16<br>
Returns: 5<br>
<br>
This problem statement is the exclusive and proprietary property of
TopCoder, Inc. Any unauthorized use or reproduction of this information
without the prior written consent of TopCoder, Inc. is strictly
prohibited. (c)2003, TopCoder, Inc. All rights reserved.<br>
<br>
***************************************************************************************************************/<br>
<br>
#include &lt;string&gt;<br>
#include &lt;vector&gt;<br>
#include &lt;iostream&gt;<br>
<br>
using namespace std;<br>
<br>
<br>
struct Context<br>
{<br>
&nbsp;&nbsp;&nbsp; int minute;<br>
&nbsp;&nbsp;&nbsp; double reachingCost;<br>
&nbsp;&nbsp;&nbsp; double cost;<br>
};<br>
typedef vector&lt;Context&gt; ContextVector;<br>
typedef vector&lt;ContextVector&gt; ContextMatrix;<br>
<br>
class ExpensiveTravel<br>
{<br>
private:<br>
<br>
&nbsp;&nbsp;&nbsp; size_t row, col;<br>
<br>
&nbsp;&nbsp;&nbsp; void initContextMatrix(ContextMatrix&amp; ctxMatrix, const vector&lt;string&gt;&amp; m)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (size_t r = 0; r &lt; row; r ++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ContextVector aRow;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (size_t c = 0; c &lt; col; c ++)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Context ctx;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ctx.minute=0;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ctx.reachingCost=1.0;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ctx.cost= 1.0 / (double)(m[r][c] - '0');<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; aRow.push_back(ctx);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ctxMatrix.push_back(aRow);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; bool canMove(size_t r, size_t c)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return r &gt;= 1 &amp;&amp; r &lt;= row &amp;&amp; c &gt;=1 &amp;&amp; c &lt;= col;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
<br>
&nbsp;&nbsp;&nbsp; bool testMove(ContextMatrix&amp; ctxMatrix, size_t curRow, size_t curCol, size_t nxtRow, size_t nxtCol)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!canMove(nxtRow, nxtCol))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return false;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Context&amp; curCtx = ctxMatrix[curRow-1][curCol-1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Context&amp; nxtCtx = ctxMatrix[nxtRow-1][nxtCol-1];<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // if next minute is 0, create it<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (nxtCtx.minute == 0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // if we can reach it within this minute<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (curCtx.reachingCost + nxtCtx.cost &lt; 1.0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // update the cost<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCtx.minute=curCtx.minute;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; nxtCtx.reachingCost = curCtx.reachingCost +
nxtCtx.cost;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // but we will stuck if next has cost of 1<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (nxtCtx.cost == 1.0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return false;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // then we have to start it in next minute;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCtx.minute = curCtx.minute + 1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCtx.reachingCost = nxtCtx.cost;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // or if it is an old-reachable cell but we can do better<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if
(nxtCtx.minute == curCtx.minute &amp;&amp; curCtx.reachingCost +
nxtCtx.cost &lt; nxtCtx.reachingCost)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; nxtCtx.reachingCost = curCtx.reachingCost +
nxtCtx.cost;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // can reach
it within this minute // or within same minute, but lower cost<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (nxtCtx.minute &gt; curCtx.minute)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // first case, we can reach it within same minute<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if ((curCtx.reachingCost + nxtCtx.cost) &lt; 1.0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCtx.minute = curCtx.minute;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCtx.reachingCost =
curCtx.reachingCost + nxtCtx.cost;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; // we can always reach it in next minute, because it
is not "trap"(since somebody already reach it before<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // case one: it should be reachable next minute<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if ((nxtCtx.minute &gt; curCtx.minute + 1)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // case two:
it is next minute, but it should be a brand new start<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
||((nxtCtx.minute == curCtx.minute + 1) &amp;&amp; nxtCtx.reachingCost
&gt; nxtCtx.cost))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCtx.minute = curCtx.minute + 1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCtx.reachingCost = nxtCtx.cost;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return true;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return false;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; void expandCell(ContextMatrix&amp; ctxMatrix, size_t curRow, size_t curCol)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //debug<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // sanity test<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (ctxMatrix[curRow-1][curCol-1].cost == 1.0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; size_t nxtRow, nxtCol;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // move up<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtRow = curRow -1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCol = curCol;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (testMove(ctxMatrix, curRow, curCol, nxtRow, nxtCol))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // we have new updates, so expand more<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; expandCell(ctxMatrix, nxtRow, nxtCol);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // move down<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtRow = curRow + 1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCol = curCol;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (testMove(ctxMatrix, curRow, curCol, nxtRow, nxtCol))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // we have new updates, so expand more<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; expandCell(ctxMatrix, nxtRow, nxtCol);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // move left<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtRow = curRow;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCol = curCol-1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (testMove(ctxMatrix, curRow, curCol, nxtRow, nxtCol))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // we have new updates, so expand more<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; expandCell(ctxMatrix, nxtRow, nxtCol);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // move right<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtRow = curRow;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nxtCol = curCol+1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (testMove(ctxMatrix, curRow, curCol, nxtRow, nxtCol))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // we have new updates, so expand more<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; expandCell(ctxMatrix, nxtRow, nxtCol);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
<br>
<br>
public:<br>
&nbsp;&nbsp;&nbsp; int minTime(vector&lt;string&gt; m, int startRow, int startCol, int endRow, int endCol)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; row = m.size();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; col = m[0].size();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ContextMatrix ctxMatrix;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; initContextMatrix(ctxMatrix, m);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Context&amp; startCtx = ctxMatrix[startRow-1][startCol-1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; startCtx.minute=1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; startCtx.reachingCost = startCtx.cost;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //start expanding from starting point<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; expandCell(ctxMatrix, startRow, startCol);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // now check the result<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Context&amp; ctx = ctxMatrix[endRow - 1][endCol - 1];<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (ctx.minute == 0)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // it cannot be reached...<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return -1;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return ctx.minute;<br>
&nbsp;&nbsp;&nbsp; }<br>
};<br>
<br>
int main()<br>
{<br>
&nbsp;&nbsp;&nbsp; ExpensiveTravel travel;<br>
&nbsp;&nbsp;&nbsp; vector&lt;string&gt; m;<br>
&nbsp;&nbsp;&nbsp; /*<br>
&nbsp;&nbsp;&nbsp; m.push_back(string("452232"));<br>
&nbsp;&nbsp;&nbsp; m.push_back(string("287979"));<br>
&nbsp;&nbsp;&nbsp; m.push_back(string("219872"));<br>
&nbsp;&nbsp;&nbsp; m.push_back(string("928234"));<br>
&nbsp;&nbsp;&nbsp; m.push_back(string("767676"));<br>
&nbsp;&nbsp;&nbsp; */<br>
&nbsp;&nbsp;&nbsp; m.push_back(string("123456789987654321"));<br>
&nbsp;&nbsp;&nbsp; cout &lt;&lt; travel.minTime(m, 1, 2, 1, 16) &lt;&lt; endl;<br>
}<br>
<br>
<br>
<br>
</body></html>