Uvalive 2517Moving Object Recognition (analog)

Source: Internet
Author: User

The topic looks very scary, very high-end, but in fact very simple, do not be frightened, copy the formula of the problem can be.

Methods: Using BFS to find the largest block and the center of gravity, to identify the problem formula needs unknown amount, and then set into the problem formula can find the answer.

Code:

#include <iostream>#include<algorithm>#include<queue>#include<cstdio>#include<cstring>using namespacestd;#defineN 550intn,m,go[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};intVis[n][n],max;CharMaps[n][n],op[n];Doublex[n* +],y[n* +];structpos{intx, y;};BOOLIn_maps (intXinty) {    return(x>=1&& x <= n && y>=1&& y<=m);}intBFsintSxintSy) {Queue<Pos>que;  while(!que.empty ()) Que.pop ();    Pos now,nxt; now.x= SX; NOW.Y =Sy;    Que.push (now); inttot =0; Vis[sx][sy]=1;  while(!Que.empty ()) { Now=Que.front ();        Que.pop (); Tot++;  for(inti =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; }        }    }    returntot;}voidGet_max () {intNxtx,nxty,tmp,startx,starty; Max= -99999999; memset (Vis,0,sizeof(VIS));  for(inti =1; I <= N; i++)    {         for(intj =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\n", max); //printf ("SX =%d sy =%d\n", startx,starty);memset (Vis,0,sizeof(VIS)); BFS (Startx,starty);}voidGet_xy (intk) {    DoubleSUMX =0, Sumy =0;  for(inti =1; I <= N; i++)    {         for(intj =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;}intMain () {intT; DoubleAnsx,ansy;  while(~SCANF ("%d%d",&m,&N)) {if(n+m==0) Break; T=0;  while(1)        {             for(inti =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\n", t);T/=2; ANSX= Ansy =0.0;  for(inti =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\n", ANSY,ANSX); }    return 0;}

Uvalive 2517Moving Object Recognition (analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.