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)