HNNU 11657 简单的图论问题?【湖南省第十一届大学生计算机程序设计竞赛,双BFS】



原题链接:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11657&courseid=0

正如好手所说,搜索玩得就是标记

第一种方式稍微简单一点,第二种方式需要一个三维的标记,增加一个方向的标记,就和迷宫问题转弯次数一样。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int a[505][505];
bool vis1[505][505];
bool vis2[505][505][4];
int n,m,sx,sy,ex,ey;
struct node
{
    int x,y,sum;
    int dir;
    bool friend operator <(node a,node b)
    {
        return a.sum>b.sum;
    }
};
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
bool OK(int x,int y)
{
    if(x<1||x>n||y<1||y>m)
        return false;
    return a[x][y]!=0;
}
int BFS1()
{
    priority_queue<node>q;
    node now,nextt;
    now.x=sx;
    now.y=sy;
    now.sum=a[sx][sy];
    q.push(now);
    memset(vis1,false,sizeof(vis1));
    vis1[sx][sy]=true;
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.x==ex&&now.y==ey)
            return now.sum;
        for(int i=0; i<4; i++)
        {
            nextt.x=now.x+dir[i][0];
            nextt.y=now.y+dir[i][1];
            if(OK(nextt.x,nextt.y)&&!vis1[nextt.x][nextt.y])
            {
                vis1[nextt.x][nextt.y]=true;
                nextt.sum=now.sum+a[nextt.x][nextt.y];
                q.push(nextt);
            }
        }
    }
    return -1;
}
int BFS2()
{
    priority_queue<node>q;
    node now,nextt;
    now.x=sx;
    now.y=sy;
    now.sum=a[sx][sy];
    now.dir=-1;
    q.push(now);
    memset(vis2,false,sizeof(vis2));

    vis2[sx][sy][0]=true;
    vis2[sx][sy][1]=true;
    vis2[sx][sy][2]=true;
    vis2[sx][sy][3]=true;

    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.x==ex&&now.y==ey)
            return now.sum;
        for(int i=0; i<4; i++)
        {
            nextt.x=now.x+dir[i][0];
            nextt.y=now.y+dir[i][1];
            if(now.dir==i)
                continue;
            if(OK(nextt.x,nextt.y)&&!vis2[nextt.x][nextt.y][i])
            {
                vis2[nextt.x][nextt.y][i]=true;
                nextt.sum=now.sum+a[nextt.x][nextt.y];
                nextt.dir=i;
                q.push(nextt);
            }
        }
    }
    return -1;
}
int main()
{
    //freopen("e.txt","r",stdin);
    int kase=0;
    while(cin>>n>>m>>sx>>sy>>ex>>ey)
    {
        char ch[10];
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%s",ch);
                if(!strcmp(ch,"*"))
                    a[i][j]=0;
                else
                    a[i][j]=atoi(ch);
            }
        }
        printf("Case %d: ",++kase);
        cout<<BFS1()<<" "<<BFS2()<<endl;

    }
    return 0;
}
/**
2 4 1 1 1 4
1 * 3 4
9 9 * 9
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
*/


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值