E - 简单的图论问题?

给一个 行 列的迷宫,每个格子要么是障碍物要么是空地。每个空地里都有一个权值。你的 任务是从找一条(r1,c1)(r2,c2)的路径,使得经过的空地的权值之和最小。每一步可以往上下 左右四个方向之一移动一格,但不能斜着移动,也不能移动到迷宫外面或者进入障碍物格子。

如下图,灰色格子代表障碍物。路径 A->B->D->F->E 的权值为 10+3+6+14+8=41,它是从 到 的最优路径。注意,如果同一个格子被经过两次,则权值也要加两次。


为了让题目更有趣(顺便增加一下难度),你还需要回答另外一个问题:如果你每次必须转弯 (左转、右转或者后退,只要不是沿着上次的方向继续走即可),最小权值是多少?比如,在 上图中,如果你刚刚从 走到B,那么下一步你可以走到 或者 A,但不能走到 G。在上图 中,到 的最优路径是 A->B->D->H->D->F->E,权和为 10+3+6+2+6+14+8=49。注意,经 过了两次。

Input

输入包含不超过 10 组数据。每组数据第一行包含 6 个整数 n, m, r1, c1, r2, c2 (2<=n,m<=500, 1<=r1,r2<=n, 1<=c1,c2<=m). 接下来的 n 行每行包含 m 个格子的描述。每个格子要么是一个 1~100 的整数,要么是星号"*"(表示障碍物)。起点和终点保证不是障碍物。

Output

对于每组数据,输出两个整数。第一个整数是“正常问题”的答案,第二个整数是“有趣问 题”的答案。如果每个问题的答案是“无解”,对应的答案应输出-1。

Sample Input
4 4 1 2 3 2
7 10 3 9

* 45 6 2

* 8 14 *

21 1 * * 
2 4 1 1 1 4
1 2 3 4
9 * * 9
2 4 1 1 1 4
1 * 3 4
9 9 * 9 
Sample Output
Case 1: 41 49
Case 2: 10 -1
Case 3: -1 -1

 代码中字符存入代码转于:http://blog.csdn.net/yu_ch_sh/article/details/50582762

 思路:

          主要就是控制有趣问题的那部分代码。

 代码如下:           

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<queue>

using namespace std;

int vis[550][550];
char map[550][550];
int v[550][550][4];
int fx[]= {1,-1,0,0};
int fy[]= {0,0,-1,1};
int n,m,ex,ey,si,sj;
///存地图
char str[500010];
void fun(int i,char *str)
{
    char *p;
    int j=1;
    p=strtok(str," ");
    while(p!=NULL)
    {
        if(p[0]=='*')
            map[i][j]=-1;
        else
            map[i][j]=atoi(p);
        j++;
        p=strtok(NULL," ");
    }
}
///优先队列
struct node
{
    int x;
    int y;
    int step;
    int k;
    friend bool operator <(node a,node b)
    {
        return a.step>b.step;
    }
} s,p;

///原来的找法
int dfs_yuan(int x,int y,int step)
{
    if(x==ex&&y==ey)
        return 0;
    memset(vis,0,sizeof(vis));
    s.x = x;
    s.y = y;
    s.step = map[x][y];
    priority_queue<struct node>q;
    vis[x][y] = 1;
    q.push(s);
    while(!q.empty())
    {
        p = q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            s.x = p.x + fx[i];
            s.y = p.y + fy[i];
            if(s.x<=0 || s.x>n || s.y<=0 || s.y>m || vis[s.x][s.y]==1 || map[s.x][s.y]==-1)
                continue;
            s.step = p.step + map[s.x][s.y];
            if(s.x==ex && s.y==ey)
                return s.step;
            vis[s.x][s.y] = 1;
            q.push(s);
        }
    }
    return -1;
}
///有趣的游戏
int dfs_youqu(int x,int y,int step)
{
    if(x==ex && y==ey)
        return 0;
    memset(v,0,sizeof(v));
    priority_queue<struct node>q;
    s.x = x;
    s.y = y;
    s.step = step;
    for(int i=0; i<4; i++)
    {
        p.x = s.x + fx[i];
        p.y = s.y + fy[i];
        p.k = i;
        if(p.x<=0 || p.x>n || p.y<=0 || p.y>m || v[p.x][p.y][p.k] || map[p.x][p.y]==-1)
            continue;

        p.step = s.step+map[p.x][p.y];

        if(p.x == ex && p.y == ey)
            return p.step;
        v[p.x][p.y][p.k] = 1;
        q.push(p);
    }
    while(!q.empty())
    {
        p = q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            if(p.k==i)
                continue;
            s.x = p.x + fx[i];
            s.y = p.y + fy[i];
            s.k = i;
            if(s.x<=0 || s.x>n || s.y<=0 || s.y>m || v[s.x][s.y][s.k]==1 || map[s.x][s.y]==-1)
                continue;
            s.step = p.step + map[s.x][s.y];
            if(s.x==ex && s.y==ey)
                return s.step;
            v[s.x][s.y][s.k] = 1;
            q.push(s);
        }
    }
    return -1;
}

int main()
{
    int v=1;
    while(scanf("%d%d%d%d%d%d\n",&n,&m,&si,&sj,&ex,&ey)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            gets(str);
            fun(i,str);
        }
        int ans1 = dfs_yuan(si,sj,map[si][sj]);
        int ans2 = dfs_youqu(si,sj,map[si][sj]);
        printf("Case %d: %d %d\n",v++,ans1,ans2);
    }
    return 0;
}
posted @ 2017-08-18 11:26  让你一生残梦  阅读(334)  评论(0编辑  收藏  举报