The Die Is Cast(poj 1481简单的双dfs)
2024-10-20 08:54:05
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.
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.
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.
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,
最新文章
- css引入方式
- C#深入.NET平台的软件系统分层开发
- 旅图——UI测试
- http相关概念在iOS中的使用介绍
- table创建固定表头
- DWZ (JUI) 教程 国际化问题(多语言/语言切换)
- setImageResource和setImageDrawable区别
- 在SQL SERVER中查找用分隔符分隔的数据
- css font简写规则
- Gym 100917C Constant Ratio 数论+暴力
- (转)Java线程:新特征-原子量,障碍器
- [Swift]LeetCode805. 数组的均值分割 | Split Array With Same Average
- Boostnote 为程序员的开源式记事本
- 2.Linux基础命令
- 对象Object
- marathon 测试
- (java项目)坦克大战 2.0
- [转]2017年最具价值的十大开源项目!GitHub 年度报告~
- Keil uVision4 for ARM 下增加支持C51,C5x
- MyBatis各种类型的入参使用方式
热门文章
- QT开发之旅二TCP调试工具
- wmsys.wm_concat结果长度限制的问题
- Spark学习笔记--Linux安装Spark集群详解
- VS自动添加头部注释
- MySQL的ALTER变更、正则查询、分组查询、排序查询以及事务查询的概
- 【转】UTF16和UTF8什么区别?
- FileStream实现多线程断点续传(已封装)
- Python的一个命名空间冲突,关于from-import机制
- 计蒜客 30994 - AC Challenge - [状压DP][2018ICPC南京网络预赛E题]
- 11.21 CSS学习-上午