zoukankan      html  css  js  c++  java
  • [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
    ",minn);
        }
        return 0;
    }
    /*
    5 9
    ****#****
    *..#.#..*
    ****.****
    *$#.#.#$*
    *********
    5 11
    *#*********
    *$*...*...*
    *$*.*.*.*.*
    *...*...*.*
    *********.*
    9 9
    *#**#**#*
    *#**#**#*
    *#**#**#*
    *#**.**#*
    *#*#.#*#*
    *$##*##$*
    *.#.#.#.*
    *********
    */
    
  • 相关阅读:
    ubuntu右上角时间不显示
    树、森林和二叉树之间的转换(转)
    机器学习——支持向量机(SVM)之拉格朗日乘子法,KKT条件以及简化版SMO算法分析
    Java数据结构——平衡二叉树的平衡因子(转自牛客网)
    机器学习——梯度下降算法
    ubuntu下gedit闪退,遇到问题:ERROR:../../gi/pygi-argument.c:1583:_pygi_argument_to_object: code should not be reached 已放弃 (核心已转储)
    机器学习——Logistic回归
    机器学习——基于概率论的分类方法:朴素贝叶斯
    ubuntu安装simplejson模块
    Java数据结构——树的三种存储结构
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10262816.html
Copyright © 2011-2022 走看看