原网址:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1780
Description
给一个 n 行 m 列的迷宫,每个格子要么是障碍物要么是空地。每个空地里都有一个权值。你的 任务是从找一条(r1,c1)到(r2,c2)的路径,使得经过的空地的权值之和最小。每一步可以往上下 左右四个方向之一移动一格,但不能斜着移动,也不能移动到迷宫外面或者进入障碍物格子。
如下图,灰色格子代表障碍物。路径 A->B->D->F->E 的权值为 10+3+6+14+8=41,它是从 A 到 E 的最优路径。注意,如果同一个格子被经过两次,则权值也要加两次。
为了让题目更有趣(顺便增加一下难度),你还需要回答另外一个问题:如果你每次必须转弯 (左转、右转或者后退,只要不是沿着上次的方向继续走即可),最小权值是多少?比如,在 上图中,如果你刚刚从 A 走到 B,那么下一步你可以走到 D 或者 A,但不能走到 G。在上图 中,A 到 E 的最优路径是 A->B->D->H->D->F->E,权和为 10+3+6+2+6+14+8=49。注意,D 经 过了两次。
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
Hint
Source
湖南省第十一届大学生计算机程序设计竞赛#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
using namespace std;
const long long INF = 214748364700;
const int inf = 2147483647;
long long mapp[509][509];//用来存图,-1不能走
bool vis1[509][509]; //存第一种走法走过的点
bool vis2[509][509][4]; //存第二种走法走过的点,有四个方向
long long quan[509][509]; //存第一种走法到每个点要的最小花费
long long quan2[509][509][4];//存第二种走法到每个点要的最小花费
long long n, m, r1, c1, r2, c2 ;
char word[7];
long long step1[4] = {0,0,1,-1};
long long step2[4] = {1,-1,0,0};
struct Point{
long long x,y;
long long cnt;
int f;
friend bool operator < (Point a,Point b){
return a.cnt > b.cnt;
}
}start,over;
bool pan1(Point a)
{
if(vis1[a.x][a.y] == false && a.x>=1 && a.x<=n && a.y>=1 && a.y<=m && mapp[a.x][a.y]!=-1 && a.cnt < quan[a.x][a.y])
return true;
return false;
}
long long bfs1()//第一种走法
{
priority_queue<Point> que1;
que1.push(start);
vis1[start.x][start.y] = true;
while(!que1.empty()){
Point a = que1.top();
que1.pop();
vis1[a.x][a.y] = true;
Point temp;
for(int i = 0;i < 4;i++){
temp.x = a.x+step1[i];
temp.y = a.y+step2[i];
temp.cnt = a.cnt+mapp[temp.x][temp.y];
if(pan1(temp)){
quan[temp.x][temp.y] = temp.cnt;
if(temp.x != over.x || temp.y != over.y){
que1.push(temp);
}
else{
return quan[over.x][over.y];
}
}
}
}
return quan[over.x][over.y];
}
bool pan2(Point a,int f)
{
if(vis2[a.x][a.y][f] == false && a.x>=1 && a.x<=n && a.y>=1 && a.y<=m && mapp[a.x][a.y]!=-1 && a.cnt < quan2[a.x][a.y][f])
return true;
return false;
}
long long bfs2()//第二种走法
{
priority_queue<Point> que2;
start.f = 5;
que2.push(start);
for(int i = 0;i < 4;i++)
vis2[start.x][start.y][i] = true;
while(!que2.empty()){
Point a = que2.top();
que2.pop();
Point temp;
for(int i = 0;i < 4;i++){
if(i == a.f) continue;
temp.x = a.x+step1[i];
temp.y = a.y+step2[i];
temp.cnt = a.cnt+mapp[temp.x][temp.y];
if(pan2(temp,i)){
temp.f = i;
quan2[temp.x][temp.y][i] = temp.cnt;
vis2[temp.x][temp.y][i] = true;
if(temp.x != over.x || temp.y != over.y){
que2.push(temp);
}
}
}
}
long long minn = INF;
for(int i = 0;i < 4;i++){
minn = min(minn,quan2[over.x][over.y][i]);
}
return minn;
}
int main()
{
long long Case = 1;
while(scanf("%lld%lld%lld%lld%lld%lld",&n,&m,&r1,&c1,&r2,&c2) != EOF){
start.x = r1;
start.y = c1;
over.x = r2;
over.y = c2;
memset(mapp,0,sizeof(mapp));
for(int i = 0;i < 509;i++){
for(int j = 0;j < 509;j++){
vis1[i][j] = false;
quan[i][j] = INF;
for(int qu = 0;qu < 4;qu++){
quan2[i][j][qu] = INF;
vis2[i][j][qu] = false;
}
}
}
for(long long i = 1;i <= n;i++){
for(long long j = 1;j <= m;j++){
memset(word,NULL,sizeof(word));
scanf("%s",&word);
if(word[0] == '*')
mapp[i][j] = -1;
else{
for(long long k = 0;k < strlen(word);k++){
if(word[k] >= '0' && word[k] <= '9') //对数据进行处理
mapp[i][j] = mapp[i][j]*10+word[k]-'0';
}
}
}
}
start.cnt = mapp[start.x][start.y];
over.cnt = mapp[over.x][over.y];
long long ans1 = bfs1();
if(ans1 >= INF-200)
ans1 = -1;
long long ans2 = bfs2();
if(ans2 >= INF-200)
ans2 = -1;
printf("Case %lld: %lld %lld\n",Case++,ans1,ans2);
}
return 0;
}