关闭

sicily 1321. Robot

715人阅读 评论(0) 收藏 举报
分类:

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;
  }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27650次
    • 积分:1290
    • 等级:
    • 排名:千里之外
    • 原创:104篇
    • 转载:0篇
    • 译文:1篇
    • 评论:2条
    文章分类
    最新评论