UVA 572 Oilfield connecting block--and check-set solution

Source: Internet
Author: User

Test instructions: 8 Directions If you can connect to a block, a number of connected blocks are obtained.

Analysis: The online solution is generally DFS, but today found and check set can also be resolved, in order to facilitate my own understanding of the great God template, then try to solve this problem, have not thought of ...

1#include <cstdio>2#include <iostream>3#include <sstream>4#include <cmath>5#include <cstring>6#include <cstdlib>7#include <string>8#include <vector>9#include <map>Ten#include <Set> One#include <queue> A#include <stack> -#include <algorithm> - #definell Long Long the #defineMem (M, a) memset (M, A, sizeof (m)) - #defineRepu (I, A, b) for (int i = A; I < b; i++) - #defineMAXN 1100 - Const DoublePi=-acos (-1.0); + using namespacestd; - structNode + { A     intval, x, y; at     BOOL operator< (ConstNode a)Const -     { -         returnVal >A.val; -     } -Node (intv=0,intx=0,inty=0): Val (v), X (x), Y (y) {} -} nd[maxn*MAXN]; in structQuery - { to     inth, id; +     BOOL operator< (ConstQuery a)Const -     { the         returnH>a.h; *     } $Query (intH=0,intId=0): H (h), ID (ID) {}Panax Notoginseng} q[100010]; - BOOLVIS[MAXN][MAXN]; the intN, M, pre[maxn*MAXN]; + Const intdx[]= {0,0, -1,1,1,1, -1, -1}; A Const intdy[]= {1, -1,0,0,1, -1,1, -1}; the intFind (intx) + { -     if(Pre[x] = =-1)returnx; $     returnPRE[X] =Find (pre[x]); $ } - BOOLOK (intXinty) - { the     returnX && y && x<=n && y<=m; - }Wuyi intJudgeintXinty) the { -     intv[5], cnt=0; Wu      for(intI=0; i<8; i++)///looking for the four numbers around him which are the collections of -     { About         intNx=x+dx[i], ny=y+Dy[i]; $         if(OK (NX, NY) && Vis[nx][ny])///that have been traversed. -         { -             intt = Find (m* (nx-1)+NY); -v[cnt++] =T; A         } +     } theSort (V, v+CNT); -CNT = Unique (V, v+cnt)-v; $      for(intI=0; I < CNT; i++) thePre[v[i]] = m* (X-1) +y;///all the filters are grouped into (x, y) the     return 1-CNT;///The returned value is only -1,0,1. the } the intMain () - { in     Charc[ the][ the]; the     intT, T, V; the      while(~SCANF ("%d%d", &n, &m), n +m) About     { the GetChar (); the         inttot=0; theRepu (I,1, n+1) + gets (C[i]); -Repu (I,1, n+1) the         {BayiRepu (J,0, M) the             { the                 if(C[i][j] = ='*') -Nd[tot]=node (1, I, j+1); -                 Else theNd[tot]=node (2, I, j+1); thetot++; the             } the         } -         ///It's all from big to small . theSort (ND, nd+tot); thememset (Vis,0,sizeofvis); thememset (Pre,-1,sizeofpre);94         intCnt=0, j =0; the          for(; Nd[j].val >1&& J < tot; J + +) the         { the             ///If this point is larger than the point you are querying, you can add its coordinates98CNT + =judge (nd[j].x, nd[j].y); Aboutvis[nd[j].x][nd[j].y]=1; -         }101printf"%d\n", CNT);102     }103     return 0;104 } the /*106 3 5107 *@*@*108 **@**109 *@*@* the */
View Code

with the help of UVA 1665 , the Great god template

UVA 572 Oilfield connected block-and check set resolution

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.