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

Dijkstra算法,但是由于是网格的缘故,在寻找最短路径时很费时,直接使用会TLE,必须使用优先队列才可以AC。另外也可以用宽搜,但要保证路程必须是最短的 (以往宽搜一般是访问过就不访问了,而在这里...
  • Ederick
  • Ederick
  • 2012年02月08日 12:42
  • 1195

第十六周:[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
  • 63

第十六周:( Sicily1321) Robot(c++)

单源最短路径,分别用Dijkstra和Bellman-Ford两种算法求解

Sicily 1321. Robot

1321. Robot Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Karell...

1321. Robot

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

1321. Robot

每个周五都深深地打击了我幼小的心灵...一个错误在考场卡一个小时,回宿舍1分钟就看出,妈的真是不可言状的伤啊...上午不会做,下午听不懂,小学期有没有这么伤人啊...1321. Robot就是将一个 ...

SOJ 1321. Robot

1321. Robot Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Karell Incorporat...

sicily 1008. Robot

求矩阵的最小路径(dijkstra算法)

md5算法描述_rfc1321.txt.pdf

  • 2011年12月05日 15:23
  • 24KB
  • 下载

迅雷5 正式版1321

  • 2008年12月28日 14:34
  • 9MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sicily 1321. Robot
举报原因:
原因补充:

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