zoukankan      html  css  js  c++  java
  • bfs-Find a way

    bfs——Find a way

    这道题的大意是有两个人一个为Y,一个为M,他们两个要到同一个地方@,求他们两到相同@的最短时间。其中‘.’代表是可以走的路,‘#’代表不能走的路,他们每走一步要花11分钟。

    这道题,个人解法是先用bfs将Y能走到的各个@的距离算出来,然后再用bfs将M能走到的各个@的距离算出来,找到最小的。其中距离用DIS二维数组存放,该数组下标为@的坐标。

    #include<cstdio>
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int maxn =205;
    const int inf=0x3f3f3f3f;
    int me[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//向上,下,左,右移动 
    int min1;//存放最短距离 
    char ah[maxn][maxn];//存放地图 
    int dp[maxn][maxn];//该点是否已经走过 
    int n;int m;//行,列 
    int ans;int cnt;//cnt代表进行几次bfs 
    int de;//代表@的个数,@不仅仅只有一个 
    int dis[maxn][maxn];//存放距离 
    struct node{
        int x;int y;int step; //step代表距离 
    }st1,st2; 
    bool ok(int x,int y){//判断(x,y)点 是否可用 
        if(x<1||x>n||y<1||y>m)return false;
        else if(dp[x][y]==1)return false;
        else if(ah[x][y]=='Y'||ah[x][y]=='M'||ah[x][y]=='#')return false;
        else if(dp[x][y]==0&&ah[x][y]=='.')return true;
        else if(dp[x][y]==0&&ah[x][y]=='@')return true;
    }
    void bfs(int x,int y){
        dp[x][y]=1;
        node we;we.step=0;
        we.x=x;we.y=y;
        queue<node>q;
        q.push(we);
        while(!q.empty()){
            node t=q.front();q.pop();
            node newnode;
            if(ah[t.x][t.y]=='@'){
                dis[t.x][t.y]=dis[t.x][t.y]+t.step;
            
                if(cnt==2&&de!=1){//cnt等于2代表Y点bfs好了,在进行M,这时要相加距离 
                    min1=min(dis[t.x][t.y],min1);
        
                }
                else if(de==1)min1=dis[t.x][t.y];
            }
            for(int i=0;i<4;i++){
                int fx=t.x+me[i][0];
                int fy=t.y+me[i][1];
                if(ok(fx,fy)){
                    
                    newnode.x=fx;
                    newnode.y=fy;
                    newnode.step=t.step+1;
                    q.push(newnode);
                    dp[fx][fy]=1;
                }
            }
        }
        cnt++;
    }
    
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            de=0;
            for(int i=1;i<=n;i++){
                getchar();
                for(int j=1;j<=m;j++){
                    scanf("%c",&ah[i][j]);
                    if(ah[i][j]=='Y'){
                        st1.x=i;st1.y=j;
                    }
                    if(ah[i][j]=='M'){
                        st2.x=i;st2.y=j;
                    }
                    if(ah[i][j]=='@')de++;
                }
            }
            min1=inf;
            memset(dis,0,sizeof dis);
            ans=0;cnt=1;
            memset(dp,0,sizeof dp);
            bfs(st1.x,st1.y);
            memset(dp,0,sizeof dp);
            bfs(st2.x,st2.y);
            printf("%d
    ",min1*11);
        }
    }
  • 相关阅读:
    Luogu P1596 [USACO10OCT]湖计数Lake Counting
    Luogu P1757 通天之分组背包
    数据建模笔记1
    单纯形算法 matlab
    有效集 matlab代码
    拟牛顿 DFP matlab
    FR共轭梯度法 matlab
    整数规划
    线性规划 Matlab
    远期、期货和互换(三)
  • 原文地址:https://www.cnblogs.com/kitalekita/p/12411643.html
Copyright © 2011-2022 走看看