zoukankan      html  css  js  c++  java
  • UVALive 2517 Moving Object Recognition(模拟)

      题目看上去很吓人,很高端,但其实很简单,不要被吓到,照搬题目的公式就可以了。

      方法:用BFS求出最大块和重心,找出题目公式需要的未知量,然后套到题目公式里就可以求出答案了。

      代码:

    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define N 550
    int n,m,go[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int vis[N][N],Max;
    char maps[N][N],op[N];
    double X[N*1000],Y[N*1000];
    struct Pos
    {
        int x,y;
    };
    bool in_maps(int x,int y)
    {
        return (x>=1 && x <= n && y>=1 && y<=m);
    }
    int bfs(int sx,int sy)
    {
        queue<Pos> que;
        while(!que.empty()) que.pop();
        Pos now,nxt;
        now.x = sx;  now.y = sy;
        que.push(now);
        int tot = 0;
        vis[sx][sy] = 1;
        while(!que.empty())
        {
            now = que.front();
            que.pop();
            tot++;
            for(int i = 0; i < 4; i++)
            {
                nxt.x = now.x + go[i][0];
                nxt.y = now.y + go[i][1];
                if(in_maps(nxt.x,nxt.y) && !vis[nxt.x][nxt.y] && maps[nxt.x][nxt.y]=='x')
                {
                    que.push(nxt);
                    vis[nxt.x][nxt.y] = 1;
                }
            }
        }
        return tot;
    }
    void get_Max()
    {
        int nxtx,nxty,tmp,startx,starty;
        Max = -99999999;
        memset(vis,0,sizeof(vis));
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(maps[i][j] == 'x' && !vis[i][j])
                {
                    tmp =  bfs(i,j);
                    if(tmp > Max)
                    {
                        Max = tmp;
                        startx = i;
                        starty = j;
                    }
                }
            }
        }
       // printf("Max = %d
    ",Max);
       // printf("sx = %d sy = %d
    ",startx,starty);
        memset(vis,0,sizeof(vis));
        bfs(startx,starty);
    }
    void get_xy(int k)
    {
        double sumx = 0,sumy = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(vis[i][j])
                {
                    sumy += (j*1.0);
                    sumx += (i*1.0);
                }
            }
        }
        sumx /= Max;
        sumy /= Max;
        X[k] = sumx;
        Y[k] = sumy;
    }
    int main()
    {
        int T;
        double ansx,ansy;
        while(~scanf("%d%d",&m,&n))
        {
            if(n+m==0) break;
            T = 0;
            while(1)
            {
                for(int i = 1; i <= n; i++)
                {
                    scanf("%s",maps[i]+1);
                }
                scanf("%s",op);
                get_Max();
                get_xy(T);
                T++;
                if(op[0]=='=') break;
            }
            //printf("T = %d
    ",T);
            T /= 2;
            ansx = ansy = 0.0;
            for(int i = 0; i < T; i++)
            {
                ansx += (X[i+T] - X[i]);
                ansy += (Y[i+T] - Y[i]);
            }
            ansx /= (T*T);
            ansy /= (T*T);
            printf("%.2lf %.2lf
    ",ansy,ansx);
        }
        return 0;
    }
  • 相关阅读:
    javascript如何处理字符串中的u
    查看postgresql的日志show queries log in PostgreSQL?
    angular7 promiss
    解决echarts的叠堆折线图数据出现坐标和值对不上的问题
    微信小程序将图片数据流添加到image标签中
    深入理解flex布局的flex-grow、flex-shrink、flex-basis
    优先级:content –> width –> flex-basis (limted by max|min-width)
    ubuntu17.10 python3.6 install plugins for AI
    tensorflow import 没找到cudnn库问题解决
    ubuntu17.10 安装ssh
  • 原文地址:https://www.cnblogs.com/jifahu/p/5723194.html
Copyright © 2011-2022 走看看