Sicily 1321. Robot

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.

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
Dijkstra
怎么优化都是0.01s,当然我才刚刚开始学Dijkstra。。。

这是最开始的:

// Problem#: 1321
// Submission#: 2776602
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <vector>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAX 105
#define INF 99999999

int h, w, mapp[MAX][MAX], si, sj, ei, ej;
bool done[MAX * MAX];

struct edge {
    int to;
    int dis;
    edge(int new_to, int new_dis): to(new_to), dis(new_dis){}
};

vector<edge> e[MAX * MAX];

void make_roads() {

    int pos_i;

    for (int i = 0; i < MAX * MAX; i++) {
        e[i].clear();
    }

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {

            pos_i = i * 100 + j;

            if (i > 0) {
                e[pos_i].push_back(edge(pos_i - 100, mapp[i - 1][j]));
            }

            if (j > 0) {
                e[pos_i].push_back(edge(pos_i - 1, mapp[i][j - 1]));
            }

            if (i < h - 1) {
                e[pos_i].push_back(edge(pos_i + 100, mapp[i + 1][j]));
            }

            if (j < w - 1) {
                e[pos_i].push_back(edge(pos_i + 1, mapp[i][j + 1]));
            }
        }
    }

    si--;
    sj--;
    ei--;
    ej--;
}

typedef pair<int, int> p;

int Dijkstra() {

    memset(done, 0, sizeof(done));
    int sp = si * 100 + sj;
    int ep = ei * 100 + ej;

    int d[MAX * MAX];
    fill(d, d + MAX * MAX, INF);
    d[sp] = 0;

    priority_queue<p, vector<p>, greater<p> > q;
    q.push(p(0, sp));
    p top;

    while (!q.empty()) {

        top = q.top();
        q.pop();

        if (top.second == ep)
            return d[ep];

        if (done[top.second] || d[top.second] < top.first)
            continue;
        done[top.second] = 1;

        for (int i = 0; i < (int)e[top.second].size(); i++) {
            if (d[e[top.second][i].to] > d[top.second] + e[top.second][i].dis) {
                d[e[top.second][i].to] = d[top.second] + e[top.second][i].dis;
                q.push(p(d[e[top.second][i].to], e[top.second][i].to));
            }
        }
    }

    return d[ep];
}

int main() {

    int case_num;
    scanf("%d", &case_num);
    while (case_num--) {
        scanf("%d%d", &h, &w);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                scanf("%d", &mapp[i][j]);
            }
        }
        scanf("%d%d%d%d", &si, &sj, &ei, &ej);

        make_roads();
        printf("%d\n", Dijkstra() + mapp[si][sj]);
    }
    return 0;
}                           
时间: 2024-10-15 20:26:02

Sicily 1321. Robot的相关文章

Sicily 1321. Robot 解题报告

1321_Robot 题目链接: http://soj.me/1321 题目大意: 给一个矩阵,每一个点上面的数字表示走到该点需要的花费,找出给定起点到终点的最小总花费 思路: 每个格子看作一个结点,花费可以看作从上一个点走到这个点的路程,那么这道题就是典型的最短路径问题,可以用Dijkstra算法解决.一开始直接套用整个算法,将每个新的结点加入到集合S中的时候,更新所有不在集合中的结点的最短路径并排序以便下一次找出路径最短的结点,结果超时. 这里使用了优先队列q来优化,q里面放入每次可以加入到

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

CodeForces 321 A - Ciel and Robot

[题目链接]:click here~~ [题目大意]:一个robot 机器人 ,可以根据给定的指令行动,给你四种指令,robot初始位置是(0,0),指令一出,robot会重复行动,判断能否在无限行动的情况下经过点(n,m). [解题思路]其实仔细模拟一下,可以发现是有周期的,判断即可,见代码吧~~ 代码: #include <iostream> #include <algorithm> #include <bits/stdc++.h> using namespace

Robot Framework自动化测试(五)--- 开发系统关键字

最近一直在用robot framework 做自动化测试项目,老实说对于习惯直接使用python的情况下,被框在这个工具里各种不爽,当然,使用工具的好处也很多,降低了使用成本与难度:当然,在享受工具带来便利的同时也会受制于工具.对于特定的需求,工具没提供相关的Library和关键字的时候,就只能放弃了. 还好robot framework提供了 Evaluate 关键字,对于Evaluate 关键字的使用等有时间再讲.当robot framework 不能解决需求,我是直接写个.py 程序,通过

Robot Framework + Selenium2环境安装

操作系统环境:Windows XP 安装包: 1. 安装Python Python是一切的基础 版本:python-2.7.6.msi 下载地址:https://www.python.org/downloads/ 2. 安装wxPython wxPython是Python语言的一套GUI图形库:ride需要wxPython的支持 版本:wxPython2.8-win32-unicode-2.8.12.1-py27.exe 下载地址:http://sourceforge.net/projects/

Robot Framework常用关键字介绍

下面关键字全部由 Builtin 库提供,Builtin 为 Robot Framework 标准类库.Builtin库提供常用的关键字 1.log log 关键字就是编程语言里的"print"一样,可以打印任何你想打印的内容. 2.定义变量 通过"Set variable"关键字来定义变量 3.连接对象 "Catenate"关键字可以连接多个信息 加上"SEPARATOR="可以对多个连接的信息进行分割. 4.定义列表 通过

在linux下搭建Robot Framework

在linux下搭建自动化测试框架Robot Framework,可以实现多用户同时登录并进行自动化脚本编写,相互之间没有影响. linux系统:fedora 21 步奏 描述 动作 note 1 Install Python yum install python yum list python  /* can view which python version will be installed */ 2 Install easy_install sudo yum install python-

hdu-5673 Robot(默次金数)

题目链接: Robot Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如果机器人的当前位置在原点右侧,它同样可以 向左移动单位距离.一系列的移动(左移,右移,原地不动)定义为一个路径.问有多少种不同的路径,使得nn秒后机器人仍然位于坐标原点? 答案可能很大,只需输出答案对1,000,00

HDU 5007 Post Robot

Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1306    Accepted Submission(s): 941 Problem Description DT is a big fan of digital products. He writes posts about technological produc