The Die Is Cast(poj 1481简单的双dfs)
http://poj.org/problem?id=1481
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 860 | Accepted: 358 |
Description
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.

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 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
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)的更多相关文章
- UVa657 The die is cast
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...
- UVA 657 The die is cast
The die is cast InterGames is a high-tech startup company that specializes in developing technolo ...
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 2492 (简单并查集) A Bug's Life
题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的 ...
- poj 3414(简单bfs)
题目链接:http://poj.org/problem?id=3414 思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态. #include<iostream> #in ...
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- 两次DFS,POJ(1481)
题目链接:http://poj.org/problem?id=1481 两次DFS,这里的思路是,没找到*,就说明,有一个骰子,因此,每搜索到一个*,深搜4个方向,并且变为'.',要是搜到'X',就是 ...
- POJ 1321 简单dfs
1.POJ 1321 棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
随机推荐
- Puppet部署
一.域名,IP规划 域名:beyond.com puppet master:puppet.sa.beyond.com 192.168.254.254 puppet client: *.beyond ...
- 【Spring源码深度解析学习系列】Bean的加载(六)
Bean的加载所涉及到的大致步骤: 1)转换对应beanName 为什么需要转换beanName呢?因为传入的参数可能是别名,也可能是FactoryBean,所以需要一系列的解析,这些解析内容包括如下 ...
- oracle数据库用户加锁和解锁
oracle数据库安装好之后,scott之类的用户默认情况下是被锁住的,无法使用scott用户登录数据库.使用有alter user数据库权限的用户登陆,角色选sysdba,执行以下命令: 解锁命令: ...
- 问题记录,如何解决confluence的office预览的时候的乱码问题
在新的服务器(ubuntu16.04)上安装confluence,预览office的附件的时候,发现中文无法正确显示 在网上搜了一下,搜到一篇官方的文档,是关于这个问题的 问题原因: 在服务器上没有安 ...
- STM32 ADC转换时间
STM32F103XX的ADC的采样时钟最快14MHz,最快采样率为1MHz. ADC时钟: 这个ADC时钟是从哪来的呢.我们看下面这个STM32的时钟结构图: 我们大多使用STM32的最快PCLK2 ...
- Linux(CentOS)安装JDK(.tar.gz)并配置
本文思路转自http://blog.sina.com.cn/s/blog_81631744010137iy.html 点击(此处)折叠或打开 1.到 甲骨文(oracle)下载jdk不用多说 tar ...
- Unity3D如何有效地组织代码?(转)
问题: Unity3D可以说是高度的Component-Based Architecture,同时它的库提供了大量的全局变量.如何来组织代码呢? 答: - Unity有一些自身的约定,譬如项目里的Ed ...
- nginx虚拟目录配置
参考文章:https://blog.csdn.net/whatday/article/details/50649461 1. location ~ ^/awstats/ { root /home/aw ...
- /usr/bin/ld: i386:x86-64 architecture of input file `command.o' is incompatible with i386 output
/usr/bin/ld: i386:x86-64 architecture of input file `command.o' is incompatible with i386 output 出现这 ...
- django之admin设置
Django自带的后台管理是Django明显特色之一,可以让我们快速便捷管理数据.后台管理可以在各个app的admin.py文件中进行控制.以下是我最近摸索总结出比较实用的配置.若你有什么比较好的配置 ...