第十六周:[Sicily]1321. Robot

来源:互联网 发布:win7删除启动软件 编辑:程序博客网 时间:2024/05/22 12:57

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.
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算法求单源最短路。


#include<iostream>#include<vector>using namespace std;#define INF 10000int map[101][101],dis[101][101],c,r,s_y,s_x,e_y,e_x;struct node{    int x,y;    node(int xx,int yy){        x = xx;        y = yy;    }};void init_dis(){    for(int i = 1;i <= r;i++)        for(int j = 1;j <= c;j++)            dis[i][j] = INF;    dis[s_y][s_x] = 0;}int dijkstra(){    init_dis();    bool visited[101][101] = {false};    vector<node> path_set;    path_set.push_back(node(s_x,s_y));    visited[s_y][s_x] = true;    dis[s_y][s_x] = map[s_y][s_x];    while(!visited[e_y][e_x]){        node cur_min(0,0);        int min_weight = INF;        for(int i = 0;i < path_set.size();i++){            node cur_n = node(path_set[i].x,path_set[i].y);            if(cur_n.x - 1 > 0 && !visited[cur_n.y][cur_n.x - 1]){                if(dis[cur_n.y][cur_n.x] + map[cur_n.y][cur_n.x - 1] < dis[cur_n.y][cur_n.x - 1])                    dis[cur_n.y][cur_n.x - 1] = dis[cur_n.y][cur_n.x] + map[cur_n.y][cur_n.x - 1];                if(dis[cur_n.y][cur_n.x - 1] < min_weight){                    cur_min.y = cur_n.y;                    cur_min.x = cur_n.x - 1;                    min_weight = dis[cur_n.y][cur_n.x - 1];                }            }            if(cur_n.x + 1 <= c && !visited[cur_n.y][cur_n.x + 1]){                if(dis[cur_n.y][cur_n.x] + map[cur_n.y][cur_n.x + 1] < dis[cur_n.y][cur_n.x + 1])                    dis[cur_n.y][cur_n.x + 1] = dis[cur_n.y][cur_n.x] + map[cur_n.y][cur_n.x + 1];                if(dis[cur_n.y][cur_n.x + 1] < min_weight){                    cur_min.y = cur_n.y;                    cur_min.x = cur_n.x + 1;                    min_weight = dis[cur_n.y][cur_n.x + 1];                }            }            if(cur_n.y + 1 <= r && !visited[cur_n.y + 1][cur_n.x]){                if(dis[cur_n.y][cur_n.x] + map[cur_n.y + 1][cur_n.x] < dis[cur_n.y + 1][cur_n.x])                    dis[cur_n.y + 1][cur_n.x] = dis[cur_n.y][cur_n.x] + map[cur_n.y + 1][cur_n.x];                if(dis[cur_n.y + 1][cur_n.x] < min_weight){                    cur_min.y = cur_n.y + 1;                    cur_min.x = cur_n.x;                    min_weight = dis[cur_n.y + 1][cur_n.x];                }            }            if(cur_n.y - 1 > 0 && !visited[cur_n.y - 1][cur_n.x]){                if(dis[cur_n.y][cur_n.x] + map[cur_n.y - 1][cur_n.x] < dis[cur_n.y - 1][cur_n.x])                    dis[cur_n.y - 1][cur_n.x] = dis[cur_n.y][cur_n.x] + map[cur_n.y - 1][cur_n.x];                if(dis[cur_n.y - 1][cur_n.x] < min_weight){                    cur_min.y = cur_n.y - 1;                    cur_min.x = cur_n.x;                    min_weight = dis[cur_n.y - 1][cur_n.x];                }            }        }        if(cur_min.y != 0 && cur_min.x != 0){            visited[cur_min.y][cur_min.x] = true;            path_set.push_back(node(cur_min.x,cur_min.y));        }    }    return dis[e_y][e_x];}int main(){    int n;    cin>>n;    while(n--){        int i,j;        cin>>r>>c;        for(i = 1;i <= r;i++)            for(j = 1;j <= c;j++)                cin>>map[i][j];        cin>>s_y>>s_x>>e_y>>e_x;        cout<<dijkstra()<<endl;    }    return 0;}
原创粉丝点击