[Gym-100625J] 搜索

题目链接:https://cn.vjudge.net/problem/Gym-100625J

具体思路:首先,具体思路是两个人一起走到一个点,然后两个人按照同样的道路走出去,听了别人的思路,还有一种特殊情况需要考虑,就是中间一堵墙,两个人都直接在边界上,这个时候可以新加一个点,然后两个人到这个点的开门数都是0,然后三个搜索,新加的点到图里面所有能走的点的最小路径,两个人分别到达图内的点和新加的点的最小开门数,然后枚举每一个点,找最小就可以了。

AC代码:

#include<iostream>
#include<stack>
#include<iomanip>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<map>
#include<string>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
# define inf 100000
# define ll long long
# define maxn 100+100
int dis[maxn][maxn];// 计算从外面的点到里面每个点的最小开门数
int num1[maxn][maxn];// 计算第一个人到达某一个点的最小开门数
int num2[maxn][maxn];//计算第二个人到达某一个点的最小开门数
int vis[maxn][maxn];//dfs的过程中标记
char Map[maxn][maxn];
int n,m,k;
int f[2][4]= {{1,-1,0,0},{0,0,1,-1}};
bool judge(int t1,int t2)
{
    if(t1>=0&&t2>=0&&t1<=n+1&&t2<=m+1)return true;
    return false;
}
struct node
{
    int x,y,num;
    node(int xx,int yy,int zz)
    {
        x=xx;
        y=yy;
        num=zz;
    }
    friend bool operator < (node t1,node t2)
    {
       return t2.num<t1.num;//把想要的放在后面,符号方向不变
    }
};
void bfs1(int x,int y)
{
    priority_queue<node>q;
    q.push(node(x,y,0));
    vis[x][y]=1;
    dis[x][y]=0;
    while(!q.empty())
    {
        node temp=q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int t1=temp.x+f[0][i];
            int t2=temp.y+f[1][i];
            if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
            {
                vis[t1][t2]=1;
                if(Map[t1][t2]=='#')
                {
                    q.push(node(t1,t2,temp.num+1));
                    dis[t1][t2]=dis[temp.x][temp.y]+1;
                }
                else
                {
                    q.push(node(t1,t2,temp.num));
                    dis[t1][t2]=dis[temp.x][temp.y];
                }
            }
        }
    }
}
void bfs2(int x,int y)
{
    priority_queue<node>q;
    q.push(node(x,y,0));
    vis[x][y]=1;
    num1[x][y]=0;
    while(!q.empty())
    {
        node temp=q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int t1=temp.x+f[0][i];
            int t2=temp.y+f[1][i];
            if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
            {
                vis[t1][t2]=1;
                if(Map[t1][t2]=='#')
                {
                    q.push(node(t1,t2,temp.num+1));
                    num1[t1][t2]=num1[temp.x][temp.y]+1;
                }
                else
                {
                    q.push(node(t1,t2,temp.num));
                    num1[t1][t2]=num1[temp.x][temp.y];
                }
            }
        }
    }
}
void bfs3(int x,int y)
{
    priority_queue<node>q;
    q.push(node(x,y,0));
    vis[x][y]=1;
    num2[x][y]=0;
    while(!q.empty())
    {
        node temp=q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int t1=temp.x+f[0][i];
            int t2=temp.y+f[1][i];
            if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
            {
                vis[t1][t2]=1;
                if(Map[t1][t2]=='#')
                {
                    q.push(node(t1,t2,temp.num+1));
                    num2[t1][t2]=num2[temp.x][temp.y]+1;
                }
                else
                {
                    q.push(node(t1,t2,temp.num));
                    num2[t1][t2]=num2[temp.x][temp.y];
                }
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    int x1,y1,x2,y2;
    while(T--)
    {
        int flag=1;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%s",Map[i]+1);
        }
        for(int i=0; i<=m+1; i++)
        {
            Map[0][i]='.';
            Map[n+1][i]='.';
        }
        for(int i=0; i<=n+1; i++)
        {
            Map[i][0]='.';
            Map[i][m+1]='.';
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(Map[i][j]!='$')continue;
                if(flag==1)
                {
                    x1=i;
                    y1=j;
                    flag=0;
                }
                else if(flag==0)
                {
                    x2=i;
                    y2=j;
                    break;
                }
            }
        }
        for(int i=0; i<=n+1; i++)
            for(int j=0; j<m+1; j++)
            {
                dis[i][j]=inf;
                num1[i][j]=inf;
                num2[i][j]=inf;
            }
        memset(vis,0,sizeof(vis));
        bfs1(0,0);
        memset(vis,0,sizeof(vis));
        bfs2(x1,y1);
        memset(vis,0,sizeof(vis));
        bfs3(x2,y2);
        int minn=inf;
        minn=min(minn,dis[0][0]+num1[0][0]+num2[0][0]);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(Map[i][j]=='#')
                    minn=min(minn,dis[i][j]+num1[i][j]+num2[i][j]-2);
                else
                    minn=min(minn,dis[i][j]+num1[i][j]+num2[i][j]);
            }
        }
        printf("%d\n",minn);
    }
    return 0;
}
/*
5 9
****#****
*..#.#..*
****.****
*$#.#.#$*
*********
5 11
*#*********
*$*...*...*
*$*.*.*.*.*
*...*...*.*
*********.*
9 9
*#**#**#*
*#**#**#*
*#**#**#*
*#**.**#*
*#*#.#*#*
*$##*##$*
*.#.#.#.*
*********
*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值