Walking on a chessboard time limit:2 Seconds Memory limit:65536 KB A mini robot was put on a cell of a chessboard. The size of the chessboard is 8 * 8. The robot has four states denoted by integers from 1 to 4.
There is a integer in each cell of the chessboard, the integers was positive and not greater than 1000. The robot can walk up, off, left or right to one of the neighboring cells. The cost of the movement are the multiplication of the integer in the new cell and its original state, then the state of th E robot is altered to (cost MOD 4 + 1). The initial state of the robot is 1.
Your task is to find the path between, given cells with the minimum total cost. The sum of the costs for each walk along the path.
Input
The input contains multiple test cases. Each test case begins with a line containing the row number and column number of the the start cell and the target cell. Row number and column number would be within 1 and 8. 8 lines follow, each with 8 integers. This is the chessboard configuration.
There'll be a blank line between subsequent test cases. The last line of the input would have 4 zeros which signals the end of the input and should is not processed.
Output
For each test case, print the minimum total cost on a line.
Sample Input
1 1 2 2
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0 0 0
Sample Output
3
Author:chen, Shunbao
Source:zhejiang University Local Contest 2004
This is a question about BFS, unlike the general BFS problem is that each step, its state will change, and the topic is required from one point to another point of the shortest distance
The cost of every step he took = the original state * The value of the current point
And his status state is also updated to = Cost% 4 + 1
So we can't do it with pure BFS, we need to record the time it takes to get to each point.
I use status[8][8][5] to record the shortest distance the robot can reach a point in a certain state.
For example: status[temp.x-1][temp.y-1][temp.state] means that robot arrives at Temp.x-1,temp.y-1, and when the status changes to Temp.state, the shortest time spent
Here is my code: (I feel bad to write)
#include <iostream> #include <algorithm> #include <memory.h> #include <queue> using namespace
Std
struct point {int x, y;
int state;
int Val;
}start, temp, other;
int n1,n2,m1,m2;
int num[8][8];
int status[8][8][5];
int direction[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int cost;
void BFS () {queue<point> que;
Start.x = N1;
Start.y = n2;
Start.state = 1;
Start.val = 0;
Que.push (start);
while (!que.empty ()) {temp = Que.front ();
Que.pop ();
if (temp.x = = M1 && temp.y = = m2) {if (Temp.val < cost) cost = Temp.val;
} for (int i=0; i<4; i++) {int x = temp.x + direction[i][0];
int y = temp.y + direction[i][1];
if (x>0 && x<=8 && y>0 && y<=8) {other.x = x;
Other.y = y;
Other.val = temp.val + temp.state * num[x-1][y-1];
Other.state = (temp.state * num[x-1][y-1])% 4 + 1; if (status[x-1][y-1][other.state] = = 0 | | Other.val < STATUS[X-1][Y-1][OTHER.STate]) {status[x-1][y-1][other.state] = Other.val;
Que.push (other); }}}}} int main () {while (cin>>n1>>n2>>m1>>m2 && n1!=0 && n2!=0 &am
p;& m1!=0 && m2!=0) {for (Int. i=0; i<8; i++) for (int j=0; j<8; j + +) cin>>num[i][j];
memset (status, 0, sizeof (status));
Cost = (1 << 30);
BFS ();
cout<<cost<<endl;
} return 0; }