Uva-657-the Die is cast (two times dfs!! )

Source: Internet
Author: User

UVA-657

The die is cast
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description


The die is cast

Intergames is a High-tech startup company This specializes in developing technology the allows users to the play games over T He Internet. A Market analysis have alerted them to the fact that games of chance is pretty popular among their potential customers. Is it Monopoly, Ludo or backgammon, most of these games involve throwing dice at some stage of the game.

Of course, it would is unreasonable if players were allowed to throw their dice and then enter the result into the compute R, since cheating would be-to-easy. So, instead, Intergames have decided to supply their users and a camera that takes a picture of the thrown dice, analyzes The picture and then transmits the outcome of the throw automatically.

For this they desperately need a, given an image containing several dice, determines the numbers of dots on t He dice.

We make the following assumptions about the input images. The images contain only three dif-ferent pixel values:for the background, the dice and the dots on the dice. We consider the pixels connected if they share an edge-meeting at a corner are not enough. The figure, pixels A and B is connected, but B and C is not.

A set S of pixels is connected if for every pair (a,b) of pixels in S, there is a sequ ence in S such that a = a1 and b = ak , and ai and ai+1 is connected for.

We consider all maximally connected sets consisting solely of non-background pixels to be dice. ' maximally connected ' means that's cannot add any other non-background pixels to the set without making it dis-connected . Likewise we consider every maximal connected set of dot pixels to form a dot.

InputThe input consists of pictures of several dice throws. Each picture description starts with a line containing-numbers W and H, the width and height of the picture, Respectiv Ely. These values satisfy.

The following h lines contain w characters each. The characters can be: "." For a background pixel, "*" for a pixel of a die, and "X" for A pixel of a die ' s dot.

Dice May has different sizes and not being entirely square due to optical distortion. The picture would contain at least one die, and the numbers of dots per die are between 1 and 6, inclusive.

The input is terminated by a picture of starting with w = h = 0, which should isn't be processed.

OutputFor each throw of dice, first output its number. Then output the number of dots on the dice in the picture, sorted in increasing order.

Print a blank line after each test case.

Sample Input

30 15...........................................................................*.................*****......** **...............*x***.....**x***..............*****....***x**...............***x*.....****................*** **.......*....................................................***........******............**x****.....*x**x* ...... *******......******..........****x**.......*x**x*.............***........****** ................................... 0 0

Sample Output
Throw 11 2 2 4


Miguel Revilla
2000-05-22

Source

Root:: Competitive programming:increasing The Lower Bound of programming contests (Steven & Felix Halim):: Chapter 4. Graph:: Depth First Search:: Finding Connected Components/flood Fill
Root:: AOAPC i:beginning algorithm Contests (Rujia Liu):: Volume 2. Data Structures:: Graphs
Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: Graph:: Graph traversal:: Flood fill/finding Connected components

Root:: Western and Southwestern European Regionals:: 1998-ulm
Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Graph:: Grap H Traversal:: Flood fill/finding Connected components




Test instructions: Probably to find the number of dots connected together in each dice (each of the two dots connected to their side), output from small to large output, and note that after each set of data to have a blank line


Idea: two times Dfs, first find dice, and then go to dice inside the number of dots connected together


AC Code:


#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <algorithm  >using namespace Std;int dir[4][2]={{1,0},{0,-1},{-1,0},{0,1}}; const int MAXN = 55;char map[maxn][maxn];int vis[maxn][maxn];int ans[1300];int num, W, h;void dfs2 (int x, int y) {if (map[x] [Y] = = ' * ' | | Map[x][y] = = '. ' | |          Vis[x][y]) Return;vis[x][y] = 1;for (int i=0; i<4; i++) {int xx=x+dir[i][0];          int yy=y+dir[i][1]; if (xx>=h| | yy>=w| | xx<0| | yy<0| |          map[xx][yy]== ' * ')//did not determine whether the overflow array, re a continue;      DFS2 (xx, yy); }}void dfs (int x, int y) {if (map[x][y] = = '. ' | | vis[x][y]) return;if (!vis[x][y] && map[x][y] = = ' x ')//Find Dice One of the dots is then performed two times with Dfs {ans[num]++;d fs2 (x, y);}          Vis[x][y] = 1;for (int i=0; i<4; i++) {int xx=x+dir[i][0];          int yy=y+dir[i][1]; if (xx>=h| | yy>=w| | xx<0| | yy<0| |    map[xx][yy]== '. ')          Do not judge whether to overflow array, re once continue;   DFS (XX,YY);   }}int Main () {int cas = 1;while (scanf ("%d%d", &w, &h), W | | h) {char a[55];for (int i=0; i

Uva-657-the Die is cast (two times dfs!! )

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.