UVa 657 Roll Dice

Source: Internet
Author: User

Serie A champions: There is a big picture. Each pixel is a lattice child can only be. * X three kinds. Represents the background, bosons, dice points.

Two the lattice is adjacent or in communication, when and only if two squares of the son * or X. and has a common edge, which is up and down about four directions, oblique, that is, four even block.

A dice. The number of connected blocks of the x in this connected block is considered to be the count of the dice.
Idea: two times deep search. The first is a deep search for each connected block by * and X. The number of connected blocks of x that are deeply searched by X when searching for each connected block. Here is the ability to use two tag array visit to indicate whether or not to visit, VISITX to indicate whether to interview deep search X. (It can also be implemented by changing the x to *, * instead of the tag array.) What I thought before was to use a tag array and then infer its value, rather than two tag arrays for simplicity and intuition.

Note: Before the two for loop of the main function is deeply searched. Notice whether the inference has been visited, otherwise a large number of dice with 0 points is generated. In addition, because this cycle every success to deep search is a deep search of a large connected block, so the number of dice should be added in this cycle, do not put in DFS ~

(Before the idea was.) Deep Search. * In the deep search encountered x points plus 1,x deep Search encountered x points do not add.

The data *xxx on 2 rows and 2 columns is found to be wrong. Because of this one, the idea is calculated two.

After looking at the online ideas, to participate in the examination. Two times deep search, learning ~)

The deep search here is marked by the interview, each time the deep search is marked with a connected block.

The number of visits is then counted, that is, the number of connected blocks: The number of dice, the amount of dice. (This is also true of the deep search that included the previous oil field.) )

Inclusive include, exclusive is exclusive ~

Here by using the character. To initialize figure G and start with 1 instead of 0, which is a circle of characters around the perimeter of the diagram. , so you can avoid inferring that the coordinates are out of bounds. ( add a stop character around the perimeter to avoid inferring that the coordinates are out of bounds )

Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXN 55int cmp_int (const void *_a, const void *_b), void dfs (int hs,int ls), void dfsx (int hs,int ls), int w,h;char g[maxn][maxn];int visit[maxn][maxn];int Visi Tx[maxn][maxn];int Dir[][4]={{-1,1,0,0},{0,0,1,-1}};//lrdu. The first x after Y, the front horizontal after the vertical int num[maxn*maxn];//corresponds to the dice of the points int cnt;//boson number int main () {//freopen ("657.in", "R", stdin);//freopen ("657.out" , "W", stdout); int thrw=1;  while (scanf ("%d%d", &w,&h) && w && h) {memset (G, '. ', sizeof (g));  memset (visit,0,sizeof (visit));  memset (visitx,0,sizeof (VISITX));   for (int i=1;i<=h;++i) {getchar ();          for (int j=1;j<=w;++j) {G[i][j]=getchar ();  }} cnt=0;  memset (num,0,sizeof (num)); for (int i=1;i<=h;++i) for (int j=1;j<=w;++j) if (g[i][j]!= '. ' &&!visit[i][j])//Pay attention to the!visit conditions here.       Otherwise the dice {dfs (I,J) with a lot of points of 0 will be played;      cnt++;//dice Number Add//Note this does not put in the DFS statement, think about the understanding of//printf ("%d%d\n", i,j); } qsort (Num,cnt,sizeoF (num[0]), cmp_int);  printf ("Throw%d\n", thrw++);   for (int i=0;i<cnt;++i) if (i==0) printf ("%d", num[i]);  else printf ("%d", num[i]);                           printf ("\ n");   } return 0;   }int cmp_int (const void *_a, const void *_b) {return * (int*) _a-* (int*) _b; }void dfsx (int hs,int ls) {//X to traverse the connected block if (g[hs][ls]!= ' X ' | | visitx[hs][ls]) return; visitx[hs][ls]=1; for (int i=0;i<4;+  +i) {int nh=hs+dir[1][i];  int nl=ls+dir[0][i];        DFSX (NH,NL); }}void dfs (int hs,int ls) {//starting from a point, by * and X to traverse the connected block if (g[hs][ls]== '. ' | | visit[hs][ls]) return; visit[hs][ls]=1; if (g[hs][l  s]== ' X ' &&!visitx[hs][ls]) {DFSX (HS,LS); num[cnt]++;//points added}//else if (g[hs][ls]== ' * ')//Note this inference condition is not added here.   Because the character x is also required to iterate over the following loop for (int i=0;i<4;++i) {int nh=hs+dir[1][i];   int nl=ls+dir[0][i];         DFS (NH,NL); }    }


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

UVa 657 Roll Dice

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.