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[][];
int v[][];
int v2[][];
int ans[];
int jx[]={,-,,};
int jy[]={,,,-};
void dfs2(int x,int y)
//用来标记遍历X的个数。
{ v2[x][y]=;
int tx,ty;
for(int i=;i<;i++)
{
tx=x+jx[i];
ty=y+jy[i];
if(tx>=&&tx<n&&ty>=&&ty<m&&map[tx][ty]=='X'&&v2[tx][ty]==)
{
dfs2(tx,ty);
}
}
}
void dfs(int x,int y)
//用来搜索投影
{
if(map[x][y]=='X'&&v2[x][y]==)
{
ans1++;
dfs2(x,y);
}
v[x][y]=;
int tx,ty;
for(int i=;i<;i++)
{
tx=x+jx[i];
ty=y+jy[i];
if(tx>=&&tx<n&&ty>=&&ty<m&&map[tx][ty]!='.'&&v[tx][ty]==)
{
dfs(tx,ty);
}
}
}
int main()
{
int k,KK=;
while(scanf("%d%d",&m,&n)!=EOF)
{
KK++;
if(n==&&m==) break;
k=;
for(int i=;i<n;i++)
{
scanf("%*c%s",map[i]);
}
memset(v,,sizeof(v));
memset(v2,,sizeof(v2));
memset(ans,,sizeof(ans));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(map[i][j]!='.'&&v[i][j]==)
{
ans1=;
dfs(i,j);
ans[k++]=ans1;
} }
}
qsort(ans,k,sizeof(ans[]),cmp);
printf("Throw %d\n",KK);
for(int i=;i<k;i++)
{
if(i==) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n\n");
}
return ;
}
如果两个dfs函数只用一个v[]数组,那么像这种情况整个图便不能全部遍历。
XXXXX
XXXXX//dfs走到这里就停了
*****
XXXXX
XXXXX
正确结果应该是2,
												

The Die Is Cast(poj 1481简单的双dfs)的更多相关文章

  1. UVa657 The die is cast

    // 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...

  2. UVA 657 The die is cast

      The die is cast  InterGames is a high-tech startup company that specializes in developing technolo ...

  3. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  4. POJ 2492 (简单并查集) A Bug's Life

    题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的 ...

  5. poj 3414(简单bfs)

    题目链接:http://poj.org/problem?id=3414 思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态. #include<iostream> #in ...

  6. Poj(2236),简单并查集

    题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...

  7. 两次DFS,POJ(1481)

    题目链接:http://poj.org/problem?id=1481 两次DFS,这里的思路是,没找到*,就说明,有一个骰子,因此,每搜索到一个*,深搜4个方向,并且变为'.',要是搜到'X',就是 ...

  8. POJ 1321 简单dfs

    1.POJ 1321  棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...

  9. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

随机推荐

  1. 【mac】php7.1 安装swoole 扩展

    环境依赖: php- 或更高版本 gcc-4.4 或更高版本 make autoconf 下载源代码包后,在终端进入源码目录,执行下面的命令进行编译和安装 https://github.com/swo ...

  2. 【cs229-Lecture5】生成学习算法:1)高斯判别分析(GDA);2)朴素贝叶斯(NB)

    参考: cs229讲义 机器学习(一):生成学习算法Generative Learning algorithms:http://www.cnblogs.com/zjgtan/archive/2013/ ...

  3. 日记整理---->2016-11-26

    记录一些营销产品中的一些学习知识.我们在同一个时区,却有一辈子的时差. 一.关于mysql的注释问题 mysql的注释有以下三种,要注意是第二种的--后面至少要有一个空格. /*hello world ...

  4. ElasticSearch概述及Linux下的单机ElasticSearch安装

    原文链接:http://blog.csdn.net/w12345_ww/article/details/52182264 这两天在项目中要涉及到ElasticSearch的使用,就上网去搜索了一些这方 ...

  5. 安装pod

    1.ruby升级最新 sudo gem update -n /usr/local/bin --system 2. $ gem sources *** CURRENT SOURCES *** https ...

  6. 【转】Hudson插件Email-Ext邮件模板时间格式化的解决方法

    原文地址:http://www.cnblogs.com/haycco/archive/2012/03/20/3031397.html 最近因对Hudson版本进行了升级为2.2.0,所以各方面都在搞项 ...

  7. 阻止form表单提交的问题

    阻止form表单提交这种场景可能在生活中,我们经常碰到,而在我们第一印象里面可能我们用return false 去阻止表单默认行为. 但是,有中情况我们用return false 不能阻止表单提交 & ...

  8. Windows Server 2008 R2之五操作主控的管理

    一.概述 操作主控(FSMO)也称作操作主机(OM),它是指在AD中一个或多个特殊的DC,用来执行某些特殊的功能(资源标识符SID分配.架构修改.PDC选择等). 1.操作主控的分类 基于森林的操作主 ...

  9. poi 导入导出excel

    import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; i ...

  10. 反正切函数atan与atan2的区别

    atan 和 atan2 都是求反正切函数,如:有两个点 point(x1,y1), 和 point(x2,y2); 那么这两个点形成的斜率的角度计算方法分别是: float angle = atan ...