zoukankan      html  css  js  c++  java
  • poj 3026 Borg Maze( Prim +BFS)

    光找bugs ,忘了注册cf 的比赛了,、、、、、

    程序一直 TLE ,好是郁闷,搜索 直接把我搞死了

    写的搜索太暴力了,改进版,代码太烂了

    关键是dist 存路径距离 使搜索 直接降低一个复杂度

    View Code
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<string.h>
    const int N =110;
    const int inf=999999999;
    using namespace std;
    struct node
    {
        int x,y;
        node(int a=0,int b=0):x(a),y(b){}
    }point[N];
    int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    int  map[N][N],dist[N][N];
    char str[N][N];
    int top, x,y;
    int prim(int n)
    {
        int dist[N],vis[N],cnt=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++) dist[i]=map[0][i];
        vis[0]=1;dist[0]=0;
        for(int i=1;i<n;i++)
        {
            int min=inf,pos=-1;
            for(int j=0;j<n;j++)
            {
                if(!vis[j]&&dist[j]<min)
                {
                   min=dist[j];pos=j;
                }
            }
            if(min==inf) return -1;
            vis[pos]=1;cnt+=min;
            for(int j=0;j<n;j++)
            {
                if(!vis[j]&&dist[j]>map[pos][j])
                dist[j]=map[pos][j];
            }
        }
        return cnt;
    }
    void  bfs(node p,int lenj,int leni)
    {
        queue <node> que;
        que.push( node(p.x,p.y ));
        dist[p.x][p.y]=0;
        int flag[N][N];
        memset(flag,0,sizeof(flag));
        flag[p.x][p.y]=1;
        while(!que.empty())
        {
            node fg;
            fg=que.front();que.pop();
            for(int i=0;i<4;i++)
            {
                int x=fg.x+dir[i][0],y=fg.y+dir[i][1];
                if(x>=0&&x<leni&&y>=0&&y<lenj&&str[x][y]!='#'&&!flag[x][y])
                {
                   que.push(node(x,y));
                   dist[x][y]=dist[fg.x][fg.y]+1;
                   flag[x][y]=1;
                }
            }
        }
    
    }
    void init()
    {
        top=1;
        cin>>x>>y; gets(str[0]);
        for(int i=0;i<y;i++)
        {
            gets(str[i]);
            for(int j=0;j<x;j++)
            if(str[i][j]=='A')
                point[top].x=i,point[top++].y=j;
            else if(str[i][j]=='S')
                point[0].x=i,point[0].y=j;
        }
    }
    void make()
    {
        for(int i=0;i<top;i++)
        {
           bfs(point[i],x,y);
           for(int j=0;j<top;j++)
             map[i][j]=dist[point[j].x][point[j].y];
        }
    }
    int main()
    {
        int cs;cin>>cs;
        while(cs--)
        {
          init();
          make();
          cout<<prim(top)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    在react-native中使用es7语法中的decorator装饰器
    chrome从版本55开始,不再支持设置网页内容编码
    react-native 获取组件的宽度和高度
    手动安装Android Support Library(23.0.1)
    ubuntu 14.04 git clone 出现 fatal: Unable to find remote helper for 'https'
    React-Native 在android写不支持gif的解决方案!
    游戏大世界场景(胡诌篇)
    多线程先判断再操作免锁设计方案
    Mysql语句优化
    JVM GC Q&A(补充ing)
  • 原文地址:https://www.cnblogs.com/skyming/p/2469045.html
Copyright © 2011-2022 走看看