【CSU 1780】简单的图论问题?

来源:互联网 发布:软件测试实践报告 编辑:程序博客网 时间:2024/05/16 15:39

给一个 行 列的迷宫,每个格子要么是障碍物要么是空地。每个空地里都有一个权值。你的 任务是从找一条(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 27 10 3 9
* 45 6 2
* 8 14 *
21 1 * * 2 4 1 1 1 41 2 3 49 * * 92 4 1 1 1 41 * 3 49 9 * 9 
Sample Output
Case 1: 41 49Case 2: 10 -1Case 3: -1 -1


利用两个bfs不断搜索最短路

AC代码:

#include<string>#include<math.h>#include<stdio.h>#include<iostream>#include<queue>#include<algorithm>#include<vector>#include<string.h>#include <stdlib.h>#include<iterator>using namespace std;typedef unsigned long long ll;const int maxn = 505;const int inf = 100000;int vis1[maxn][maxn];int vis2[maxn][maxn][4];int a[maxn][maxn];int n,m,r1,r2,c1,c2;struct node{    int x,y,dir;    int step;};bool operator<(node a , node b){    return a.step > b.step;}int to[4][2] = {1,0,-1,0,0,1,0,-1};int bfs1(){    if(c1 == c2&& r1 == r2)        return 0;    priority_queue<struct node> que;    memset(vis1,0,sizeof(vis1));    node no,nxt,now;    no.x = r1 ; no.y = c1 ; no.step = a[r1][c1];    que.push(no);    vis1[r1][c1] = 1;    while(!que.empty()){        now = que.top();        que.pop();        if(now.x == r2&&now.y == c2)            return now.step;        for(int i = 0 ; i < 4; i ++){            nxt.x = now.x + to[i][0];            nxt.y = now.y + to[i][1];            if(nxt.x>0&&nxt.x<=n&&nxt.y>0&&nxt.y<=m&&a[nxt.x][nxt.y]!=0&&!vis1[nxt.x][nxt.y]){                vis1[nxt.x][nxt.y] = 1;                nxt.step = now.step + a[nxt.x][nxt.y];                que.push(nxt);            }        }    }    return -1;}int bfs2(){    if(c1 == c2&& r1 == r2)        return 0;    priority_queue<struct node> que;    memset(vis2,0,sizeof(vis2));    node no,nxt,now;    no.x = r1 ; no.y = c1 ; no.step = a[r1][c1];    no.dir = -1;    que.push(no);    vis2[r1][c1][0] = vis2[r1][c1][1] = vis2[r1][c1][2] =vis2[r1][c1][3] = 1;    while(!que.empty()){        now = que.top();        que.pop();        if(now.x == r2&&now.y == c2)            return now.step;        for(int i = 0 ; i < 4; i ++){            nxt.x = now.x + to[i][0];            nxt.y = now.y + to[i][1];            if(now.dir==i)                continue;            if(nxt.x>0&&nxt.x<=n&&nxt.y>0&&nxt.y<=m&&a[nxt.x][nxt.y]!=0&&!vis2[nxt.x][nxt.y][i]){                vis2[nxt.x][nxt.y][i] = 1;                nxt.step = now.step + a[nxt.x][nxt.y];                nxt.dir = i;                que.push(nxt);            }        }    }    return -1;}int main(){    int i,j;    char ch[5];    int k = 1;    while(~scanf("%d%d%d%d%d%d",&n,&m,&r1,&c1,&r2,&c2)){        for(i=1;i<=n;i++){            for(j=1;j<=m;j++){                cin>>ch;                if(ch[0] == '*')                    a[i][j] = 0;                else                    a[i][j] = atoi(ch);            }        }        cout<<"Case "<<k++<<": ";        int res1 = bfs1();        int res2 = bfs2();        printf("%d %d\n",res1,res2);    }    return 0;}