2

我为这个问题做了这个实现: http ://www.spoj.pl/problems/SHOP/


#include<iostream>
#include<stdio.h>
#include<queue>
#include<conio.h>
#include<string.h>
using namespace std;

struct node
{
    int x;
    int y;
    int time;
};
bool operator <(const node &s,const node &r)
{
    if(s.time>r.time)
    return true;
    else return false;
}
node beg,src,dest,tempa;
int b,a,temp;
int map[25][25];
bool vis[25][25];
int X[]={1,0,-1,0};
int Y[]={0,1,0,-1};


int djs_bfs(node src,node dest)
{
    int result=0;
    priority_queue<node>pq;
    pq.push(src);
    while(!pq.empty())
    {
        node top = pq.top();
        pq.pop();
        if(top.x==dest.x && top.y==dest.y) return result;
        if(top.x<0 || top.x>=a) continue;
        if(top.y<0 || top.y>=b) continue;
        if(vis[top.x][top.y]) continue;

        vis[top.x][top.y]=true;
        result+=map[top.x][top.y];
        for(int i=0;i<4;i++)
        {
            tempa.x=top.x+X[0];
            tempa.y=top.y+Y[0];
            tempa.time=map[top.x+X[0]][top.y+Y[0]];
            pq.push(tempa);
        }
    }
    return -1;
}

int main()
{
    memset(vis,false,sizeof(vis));
    scanf("%d %d",&a,&b);
    while(a != 0)
    {
        for(int i=0;i<a;i++)
            for(int j=0;j<b;j++)
            {
                scanf("%c",&temp);
                if(temp=='X') {map[i][j]=0;vis[i][j]=true;}
                if(temp=='S') {src.x=i;src.y=j;src.time=0;}
                if(temp=='D') {dest.x=i;dest.y=j;dest.time=0;}
                else map[i][j]=temp-'0';
            }
        cout<<djs_bfs(src,dest)<<endl;
        scanf("%d %d",&a,&b);
    }
    return 0;
    getch();
}

我不知道为什么我的代码没有为测试用例生成正确的答案。如果有人可以帮助我改进代码,请这样做:D

4

3 回答 3

4

首先,图解析代码不正确。第一行指定宽度和高度,其中宽度是每行的字符数,高度是行数。因此,在第一个scanf中交换&a和,或交换嵌套循环的顺序(但不能同时交换)。此外,我不得不在不同的地方添加虚拟调用来过滤掉换行符。像这样的简单转储将有助于确定您的地图是否被正确解析:&bforscanf("%c", &dummy);

cout << "a=" << a << endl;
cout << "b=" << b << endl;
for (int i=0; i<a; i++) {
    for(int j=0; j<b; j++) {
        cout << (char)('0' + map[i][j]) << ",";
    }
    cout << endl;
}

注意:我也将map[i][j]'S' 和 'D' 设置为 0,也将重复的if语句变成了一个if; else if; else链。这使得算法更加健壮,因为您通常可以从源或目标添加时间。

现在,到算法本身......

算法的每个循环都会result按当前地图位置权重递增。但是,该算法同时搜索多个路径(即优先级队列中的条目数),因此result最终是所有已处理节点权重的总和,而不是当前路径权重。当前路径权重为top.temp,因此您可以消除result变量并top.temp在到达目的地时简单地返回。

此外,正如其他答案所述,您需要在内部循环中使用X[i]and Y[i],否则您只会在一个方向上搜索。

现在,由于 and 的加法/减法X[i]Y[i]您可能会访问map[][]超出范围(-1 或 25)。因此,我建议将if守卫移至内部for循环以防止超出范围的访问。这也避免了用非法的可能性填充优先级队列。

这是我的算法版本,修正最少,供参考:

priority_queue<node>pq;
pq.push(src);
while(!pq.empty())
{
    node top = pq.top();
    pq.pop();

    if(top.x==dest.x && top.y==dest.y) return top.time;
    if(vis[top.x][top.y]) continue;

    vis[top.x][top.y]=true;

    for(int i=0;i<4;i++)
    {
        tempa.x=top.x+X[i];
        tempa.y=top.y+Y[i];
        if(tempa.x<0 || tempa.x>=a) continue;
        if(tempa.y<0 || tempa.y>=b) continue;
        tempa.time=top.time + map[tempa.x][tempa.y];
        pq.push(tempa);
    }
}
return -1;

我希望这有帮助。

于 2010-02-03T21:38:06.927 回答
2

您在该内部循环中使用X[0]andY[0]而不是X[i]and 。Y[i]

顺便说一句,除此之外,您的 Dijkstra 效率很低。首先,即使节点已经被访问过,您也将它们推入队列,其次,队列中可能有几个相同的节点,只是时间不同。最终,这些事情都不会影响结果,但你正在改变复杂性。

编辑:哦,tempa.time应该等于top.time加上边缘重量,而不仅仅是边缘重量。

于 2010-02-03T15:14:25.570 回答
2

为什么你有 0 个索引?

tempa.x=top.x+X[0];
tempa.y=top.y+Y[0];
tempa.time=map[top.x+X[0]][top.y+Y[0]];

挑剔:

bool operator <(const node &s,const node &r)
{
    if(s.time>r.time)
    return true;
    else return false;
}

这不是更具可读性:

bool operator <(const node &s,const node &r)
{
    return (s.time>r.time);
}
于 2010-02-03T15:16:00.580 回答