# Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

# Description

Karell Incorporated has designed a new exploration robot that has the ability to explore new terrains, this new robot can move in all kinds of terrain, it only needs more fuel to move in rough terrains, and less fuel in plain terrains. The only problem this robot has is that it can only move orthogonally, the robot can only move to the grids that are at the North, East, South or West of its position.

The Karells robot can communicate to a satellite dish to have a picture of the terrain that is going to explore, so it can select the best route to the ending point, The robot always choose the path that needs the minimum fuel to complete its exploration, however the scientist that are experimenting with the robot, need a program that computes the path that would need the minimum amount of fuel. The maximum amount of fuel that the robot can handle is 9999 units

The Terrain that the robot receives from the satellite is divided into a grid, where each cell of the grid is assigned to the amount of fuel the robot would need to pass thought that cell. The robot also receives the starting and ending coordinates of the exploration area.

Path Example
From (1,1) to (5,5)
Fuel needed 10

# Input

The first line of the input file is the number of tests that must be examined.

The first line of the test is the number of rows and columns that the grid will contain. The rows and columns will be 0 < row100 , 0 < column100

The next lines are the data of the terrain grid

The last line of the test has the starting and ending coordinates.

# Output

One line, for each test will have the amount of fuel needed by the robot

# Sample Input

3
5 5
1 1 5 3 2
4 1 4 2 6
3 1 1 3 3
5 2 3 1 2
2 1 1 1 1
1 1 5 5
5 4
2 2 15 1
5 1 15 1
5 3 10 1
5 2 1 1
8 13 2 15
1 1 1 4
10 10
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 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 10 10



# Sample Output

10
15
19



#include <iostream>

int dir[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int row, col;
int cost[105][105];
int best[105][105];
bool visited[105][105];

void print() {
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j)
std::cout << best[i][j] << " ";
std::cout << std::endl;
}
std::cout << std::endl;
}

void refresh(int r, int c) {
int tr, tc;
for (int i = 0; i < 4; ++i) {
tr = r + dir[i][0];
tc = c + dir[i][1];
if (tr < 1 || tr > row || tc < 1 || tc > col)
continue;
if (best[tr][tc] > best[r][c]+cost[tr][tc]) {
best[tr][tc] = best[r][c]+cost[tr][tc];
//refresh(tr, tc);
}
}
// print();
}

int main()
{
int test;
std::cin >> test;
while (test--) {
std::cin >> row >> col;
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j) {
std::cin >> cost[i][j];
best[i][j] = 9999;
visited[i][j] = false;
}
}
int sr, sc, er, ec;
std::cin >> sr >> sc >> er >> ec;
if (sr == er && sc == ec) {
std::cout << cost[sr][sc] << std::endl;
continue;
}
visited[sr][sc] = true;
best[sr][sc] = cost[sr][sc];
refresh(sr, sc);
while (true) {
int inr, inc, min = 9999;
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j) {
if (!visited[i][j] && min > best[i][j]) {
min = best[i][j];
inr = i;
inc = j;
}
}
}
if (inr == er && inc == ec)
break;
visited[inr][inc] = true;
refresh(inr, inc);
}
std::cout << best[er][ec] << std::endl;
}
}`

#### Sicily 1321. Robot

2015-03-29 11:54:32

#### Sicily 1321. Robot (不一样的宽搜）

2015-01-13 23:28:26

#### 第十六周：[Sicily]1321. Robot

2017-06-06 21:40:40

#### 1321. Robot

2016-12-03 10:28:05

#### 1321. Robot

2010-09-18 02:39:00

#### 1321. Robot （dijkstra）

2012-03-12 23:37:48

#### SOJ 1321. Robot

2013-08-24 21:43:32

#### 中大SICILY分类

2011-11-03 20:18:21

#### 中山大学Sicily在线测评系统题目分类

2013-06-26 21:39:40

#### 我的ACM第一步——Sicily 1000

2011-05-07 22:01:00