sicily 1321. Robot

原创 2015年11月19日 21:22:20

1321. Robot

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 Karell`s 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.

\epsfbox{p3481.eps}
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 < row$ \le$100 , 0 < column$ \le$100

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


题目分析

单源最短路径

直接dijkstra暴力搜索,

注意判断起点和终点相同的情况

按照dijkstra的想法,每次只更新与代价最小的点直接相连的点的权值

我尝试了每次只要更新了一个点,则从该点继续发散,递归更新

但是时间和内存都变大了

想想也是有可能的,按照dijkstra的想法,最坏每次是搜索整个盘,

而发散更新的话,每次都得搜索整个盘,时间一定会变长

#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
  • u012925008
  • 2015年03月29日 11:54
  • 1145

sicily 1321. Robot

最短路径问题,参考下网上代码 #include #include #include using namespace std; struct Path { int i, j; int dis...
  • sina012345
  • 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
  • line02
  • 2017年06月06日 21:40
  • 76

1321. Robot

最开始想用动态规划,但是水平太低,写出来居然超时了。 接下用dijkstra算法,dijkstra算主要分为初始化,找最小,修改,跳出。 这里我不知道为什么用if(tmp==INF) return...
  • stloztoeplitz
  • stloztoeplitz
  • 2016年12月03日 10:28
  • 126

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

题目描述 Karell Incorporated has designed a new exploration robot that has the ability to explore new...
  • simon_wwt
  • simon_wwt
  • 2015年01月13日 23:28
  • 355

1321. Robot

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

1321. Robot (dijkstra)

#include //优先级队列实现dijkstra算法很方便 #include #include using namespace std; class Path { public: ...
  • luojiayu14
  • 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
  • chengyangyy
  • 2013年08月24日 21:43
  • 1074

sicily 1008. Robot

求矩阵的最小路径(dijkstra算法)
  • wanzhs_cedar
  • wanzhs_cedar
  • 2016年01月11日 16:22
  • 317

Sicily 4875. ROBOT

2012年每周一赛第二场第三题。看完题目大家都想到的那个办法肯定会超时,复杂度是O(MN)。仔细思考可以发现,其实我们关心的只是每次移动后,在X轴和Y轴上分别靠近和远离了多少个点,两者差值就是靠近或是...
  • Ederick
  • Ederick
  • 2012年03月04日 20:13
  • 488
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sicily 1321. Robot
举报原因:
原因补充:

(最多只允许输入30个字)