UVA 657 The die is cast
The die is cast |
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 dif- ferent 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 in S such that a = a1 and b = ak, and ai and ai+1 are connected for
.
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 .
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
题意: 给定一个图片。。图片上有有一些骰子的图形。。骰子点数用'X'表示,骰子背景为'*', 其余背景为'.' , 如果X是连在一起的为1点。。
思路: 嵌套的搜索。。遇到'*' 然后去搜‘X’。。把连在的'X'转换成‘*’,点数+1,再在把连在一起的*转换成‘.',这样一个骰子就搜索出来了。。注意‘X’一定要先转换成‘*'不能直接变成'.'不然假如遇到这种情况
*X*X。。第一次搜索过去 X 变成'.'就把一个骰子分割成2个骰子了。。就会错。。
#include <stdio.h>
#include <string.h> int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int n, m;
int num[7];
int i, j;
int sum;
char map[55][55];
int vis[55][55];
int ju;
void jud(int x, int y)
{
if (map[x][y] == '*')
{
ju = 1;
return;
}
if (map[x][y] == '.')
return;
int i;
vis[x][y] = 1;
for (i = 0; i < 4; i ++)
{
if (vis[x + d[i][0]][y + d[i][1]] == 0)
jud(x + d[i][0], y + d[i][1]);
}
}
void bfs2(int x, int y)
{
int i;
map[x][y] = '*';
for (i = 0; i < 4; i ++)
{
if (map[x + d[i][0]][y + d[i][1]] == 'X')
{
bfs2(x + d[i][0], y + d[i][1]);
}
}
}
void bfs(int x, int y)
{
int i;
map[x][y] = '.';
for (i = 0; i < 4; i ++)
{
if (map[x + d[i][0]][y + d[i][1]] == 'X')
{
bfs2(x + d[i][0], y + d[i][1]);
sum ++;
}
if (map[x + d[i][0]][y + d[i][1]] == '*')
{
bfs(x + d[i][0], y + d[i][1]);
}
}
}
int main()
{
int tt = 1;
while (~scanf("%d%d", &m, &n) && n && m)
{
getchar();
memset(map, '.' ,sizeof(map));
memset(num, 0 ,sizeof(num));
for (i = 1; i <= n; i ++)
{
for (j = 1; j <= m; j ++)
scanf("%c", &map[i][j]);
getchar();
}
for (i = 1; i <= n; i ++)
{
for (j = 1; j <= m; j ++)
{
if (map[i][j] == '*')
{
sum = 0;
bfs(i, j);
num[sum] ++;
}
else if (map[i][j] == 'X')
{
ju = 0;
memset(vis, 0, sizeof(vis));
jud(i, j);
if (ju == 0)
{
bfs2(i, j);
num[1] ++;
}
}
}
}
int bo = 0;
int judge = 0;
printf("Throw %d\n", tt ++);
for (i = 1; i <= 6; i ++)
{
while (num[i])
{
if (bo == 0)
{
judge = 1;
printf("%d", i);
num[i] --;
bo = 1;
}
else
{
printf(" %d", i);
num[i] --;
}
}
}
if (judge == 0)
printf("0");
printf("\n\n");
}
return 0;
}
UVA 657 The die is cast的更多相关文章
- UVa657 The die is cast
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...
- The Die Is Cast(poj 1481简单的双dfs)
http://poj.org/problem?id=1481 The Die Is Cast Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- UVa 657 掷骰子
意甲冠军:有一个大图.每个像素是格孩子只可能是 . * X 三种.代表背景.玻色子.色子点. 两格子是邻近或在通信,当且仅当两个格儿子*要么X.且具有共同的边,这是上下左右四个方向,斜过,即四连块. ...
- uva 657
很简单的题,就是题意不懂……! 就是判断每个'*'区域内‘X’区域块的个数 WA了好多次,就是太差了: 1.结果排序输出 2.因为是骰子所以不再1-6范围内的数字要舍弃 3.格式要求要空一行…… 4. ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 10409 - Die Game
Problem G: Die Game Life is not easy. Sometimes it is beyond your control. Now, as contestants of AC ...
- words2
餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘 ...
- Android解析服务器Json数据实例
Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
随机推荐
- demo_06Canvas
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CSS 响应式设计
响应式设计是指在不同分辨率的设备中,网页布局可以自适应的调整.这种弹性化的布局使网站在不同设备中的布局都比较合理,可以为不同终端的用户提供更加舒适的界面和更好的用户体验,其根本理念是使原本 PC 上的 ...
- ASP.NET注意事项
1.服务器上bin目录下面的dll备份的时候,第一个点号之前的名字不能是一样的,否则会报错. 2.
- asp.net core 认证及简单集群
众所周知,在Asp.net WebAPI中,认证是通过AuthenticationFilter过滤器实现的,我们通常的做法是自定义AuthenticationFilter,实现认证逻辑,认证通过,继续 ...
- Dom学习笔记-(一)
一.概述 DOM(文档对象模型)是针对HTML和XML文档的一个API,其脱胎于DHTML. DOM可以将任意HTML和XML文档描绘成一个由多层节点构成的结构. 每一个文档包含一个根节点-文档节点, ...
- uva 11437 - Triangle Fun
计算几何: 直线交点: #include<cstdio> using namespace std; struct node { double x,y; node(,):x(x),y(y){ ...
- Codeforces Round #198 (Div. 2) —— A
最水的题,可惜当时赶时间没有注意数据范围:暴力超时了! 其实应该用x,y的最大公约数来判断: 代码: #include<iostream> using namespace std; int ...
- Cxf + Spring3.0 入门开发WebService
转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...
- C++的try catch到底能防止什么错误?
我在.h文件里定义: LoadingWidget* w;然后.cpp文件里定义: void MyClass::ModifyTask(){ // w = new LoadingWidget( ...
- Android调用MediaScanner进行新产生的媒体文件扫描
有时候,我们拍了一张图片或录制了一段视频,图库应用默认没有将这些新产生的文件识别出来所以打开图库或视频播放器发现没有找到这些文件,需要调用MediaScanner扫描一下才会出来.从FFMPEG中找了 ...