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 ...
随机推荐
- 《JavaScript高级程序设计》笔记(1):<script>元素
使用<script>元素内部的JavaScript代码将从上至下依此解释.在使用<script>嵌入的JavasCript代码时,代码任何地方不能出现"</sc ...
- jquery tab mouseover 特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- File控件杂谈
我们通常使用<input type='file'/>来实现网页中文件上传功能,用户可以通过点击file控件选择本地文件,当我们提交包含该控件的表单时,浏览器会向服务器发送用户选中的文件. ...
- smarty
模板引擎是用于把模板文件和数据内容合并在一起的程序,便于网站开发有利于代码分离和维护,了解一个模板最好知道其工作原理,以便于实现一通万通. 模板文件一般是HTML xml js等类型文件,如果不用模板 ...
- poj 3185 The Water Bowls
The Water Bowls 题意:给定20个01串(最终的状态),每个点变化时会影响左右点,问最终是20个0所需最少操作数? 水题..直接修改增广矩阵即可:看来最优解不是用高斯消元(若是有Gaus ...
- contos vsftp 530错误
只需要把/etc/pam.d/vsftpd文件中的 auth required pam_listfile.so item=user sense=deny file=/etc/vsft ...
- [BZOJ 2721] [Violet 5] 樱花 【线性筛】
题目链接:BZOJ - 2721 题目分析 题目大意:求出 1 / x + 1 / y = 1 / n! 的正整数解 (x, y) 的个数. 显然,要求出正整数解 (x, y) 的个数,只要求出使 y ...
- angularJS vs backbone
http://alistapart.com/article/javascript-mvc http://blog.nebithi.com/backbone-and-angular-demystifyi ...
- GRUB引导——menu.lst的写法
转自menu.lst的写法.menu.lst的写法 1.menu.lst的写法之一 首先我们看一下我的Fedora 4.0 中的/boot/grub/menu.lst 的内容: default=0 ...
- Unity3d 协程的注意问题(新手须注意,老手须加勉)
关于unity3d的协程,非常的好用,比如等待几秒执行,等待下一帧执行等! 但是也有潜在的问题: 1.协程是单线程的,在主线程中完成 2.如果发现yield, 那么这一帧会结束,那么等下一帧调用此脚本 ...