• The Die Is Cast(poj 1481简单的双dfs)


    http://poj.org/problem?id=1481

    The Die Is Cast
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 860   Accepted: 358

    Description

    InterGames is a high-tech startup company that specializes in developing technology that allows users to play games over the Internet. A market analysis has alerted them to the fact that games of chance are pretty popular among their potential customers. Be it Monopoly, ludo or backgammon, most of these games involve throwing dice at some stage of the game.  Of course, it would be unreasonable if players were allowed to throw their dice and then enter the result into the computer, since cheating would be way to easy. So, instead, InterGames has decided to supply their users with 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 program that, given an image containing several dice, determines the numbers of dots on the dice. 
    We make the following assumptions about the input images. The images contain only three different pixel values: for the background, the dice and the dots on the dice. We consider two pixels connected if they share an edge - meeting at a corner is not enough. In the figure, pixels A and B are connected, but B and C are not. 
    A set S of pixels is connected if for every pair (a,b) of pixels in S, there is a sequence a1, a2, ..., ak in S such that a = a1 and b = ak , and ai and ai+1 are connected for 1 <= i < k. 
    We consider all maximally connected sets consisting solely of non-background pixels to be dice. `Maximally connected' means that you 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.

    Input

    The input consists of pictures of several dice throws. Each picture description starts with a line containing two numbers w and h, the width and height of the picture, respectively. These values satisfy 5 <= w, h <= 50. 
    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 have different sizes and not be entirely square due to optical distortion. The picture will contain at least one die, and the numbers of dots per die is between 1 and 6, inclusive. 
    The input is terminated by a picture starting with w = h = 0, which should not be processed. 

    Output

    For 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 1
    1 2 2 4

    我很贱,这题我没读题直接看了输入输出就开始做了,主要自己真的是英语水平有限,题目很简单,主要是自己有个点没想起来,最后是看了别人的题解才A的。
    要开两个v[]标记数组,一个用来搜索投影,一个用来记录X的个数。
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    using namespace std;
    int n,m,ans1;
    int cmp(const void *a,const void *b)
    {
        return *(int *)a-*(int *)b;
    }
    char map[101][101];
    int v[101][101];
    int v2[101][101];
    int ans[1000001];
    int jx[]={1,-1,0,0};
    int jy[]={0,0,1,-1};
    void dfs2(int x,int y)
    //用来标记遍历X的个数。
    { v2[x][y]
    =1;
    int tx,ty; for(int i=0;i<4;i++) { tx=x+jx[i]; ty=y+jy[i]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]=='X'&&v2[tx][ty]==0) { dfs2(tx,ty); } } } void dfs(int x,int y) //用来搜索投影
    {
    if(map[x][y]=='X'&&v2[x][y]==0) { ans1++; dfs2(x,y); } v[x][y]=1; int tx,ty; for(int i=0;i<4;i++) { tx=x+jx[i]; ty=y+jy[i]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]!='.'&&v[tx][ty]==0) { dfs(tx,ty); } } } int main() { int k,KK=0; while(scanf("%d%d",&m,&n)!=EOF) { KK++; if(n==0&&m==0) break; k=0; for(int i=0;i<n;i++) { scanf("%*c%s",map[i]); } memset(v,0,sizeof(v)); memset(v2,0,sizeof(v2)); memset(ans,0,sizeof(ans)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(map[i][j]!='.'&&v[i][j]==0) { ans1=0; dfs(i,j); ans[k++]=ans1; } } } qsort(ans,k,sizeof(ans[0]),cmp); printf("Throw %d ",KK); for(int i=0;i<k;i++) { if(i==0) printf("%d",ans[i]); else printf(" %d",ans[i]); } printf(" "); } return 0; }
    如果两个dfs函数只用一个v[]数组,那么像这种情况整个图便不能全部遍历。
    XXXXX
    XXXXX//dfs走到这里就停了
    *****
    XXXXX
    XXXXX
    正确结果应该是2,
    
    
  • 相关阅读:
    codeforce666A_dp
    杭电1789_贪心
    杭电2059_记忆化搜索
    杭电1503_输出最长公共子序列
    杭电1501_dfs和记忆化搜索
    杭电1081_二维dp
    杭电1078_dfs
    coderforce 675C(贪心)
    杭电2571_01背包
    杭电1069_01背包
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/3916943.html
Copyright © 2020-2023  润新知