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

1321. Robot Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Karell...
• u012925008
• 2015年03月29日 11:54
• 1145

sicily 1321. Robot

• sina012345
• 2013年12月19日 21:03
• 425

第十六周：[Sicily]1321. Robot

Description Karell Incorporated has designed a new exploration robot that has the ability to explor...
• line02
• 2017年06月06日 21:40
• 76

1321. Robot

• stloztoeplitz
• 2016年12月03日 10:28
• 126

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

• simon_wwt
• 2015年01月13日 23:28
• 355

1321. Robot

TAG 最短路  把格按上下左右的连通性转换成图，即格子为顶点，边 (a,b) 的权值为格子b的花费。再用普通的 Dijkstra 算法就可以直接解。 不过这里顶点最多n=10000个，普通的 Dij...
• Dinosoft
• 2010年09月18日 02:39
• 1258

1321. Robot （dijkstra）

#include //优先级队列实现dijkstra算法很方便 #include #include using namespace std; class Path { public: ...
• luojiayu14
• 2012年03月12日 23:37
• 618

SOJ 1321. Robot

1321. Robot Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Karell Incorporat...
• chengyangyy
• 2013年08月24日 21:43
• 1074

sicily 1008. Robot

• wanzhs_cedar
• 2016年01月11日 16:22
• 317

Sicily 4875. ROBOT

2012年每周一赛第二场第三题。看完题目大家都想到的那个办法肯定会超时，复杂度是O（MN）。仔细思考可以发现，其实我们关心的只是每次移动后，在X轴和Y轴上分别靠近和远离了多少个点，两者差值就是靠近或是...
• Ederick
• 2012年03月04日 20:13
• 488

举报原因： 您举报文章：sicily 1321. Robot 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)