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 ...
随机推荐
- svg学习笔记(二)
SMIL animation演示代码集锦 <svg width="1400" height="1600" xmlns="http://www.w ...
- 禁止选择文本和禁用右键 v3.0
禁止选中字体(注:在火狐浏览器没有起到效果作用) <script> function disableselect(e) { var omitformtags = ["input& ...
- jquery右下角返回顶部
实现的效果也就是,当有滚动条是,滚动条未动或与顶部距离小于多少像素是,返回顶部按钮处于隐身状态,当滚动条与顶部距离大于一定像素时,返回顶部按钮出现,实现点击‘返回按钮’后,从当前位置回到等不位置.要先 ...
- 【 java版坦克大战--事件处理】 让坦克动起来--事件处理的准备
要能够控制坦克运动,必须用到事件处理的知识. 事件处理的一个demo. /** * 事件处理机制:委派事件模型.指当事件发生的时候,产生事件的对象(事件源),会把此 * "消息"传 ...
- html5写的一个时钟
看到的一个html5写的时钟 <!doctype> <html> <head> <script> window.onload=function(){ v ...
- Delphi实用小function
Write Log // strLog : the log content need write to log file; // strFile: the compliete file path(in ...
- URL传参中不能带特殊的字符以及处理方案
有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.编码的格式为:%加字符的ASCII码,即一个百分号%,后面跟对应字符的ASCII(16进制)码值.例如 ...
- 跨域Ajax请求WebService方法
一.允许跨域Ajax请求,更改如下配置: 在要调用的WebService上面添加特性标签: 二.以如下返回用户信息的WebService方法为例 三.在另一个网站上通过Ajax访问webService ...
- Object.defineProperty
属性类型ECMA-262第5版在定义只有内部才用的特性(attribute)时,描述了属性(property)的各种特征.ECMA-262定义这些特性是为了实现JavaScript引擎用的,因此在Ja ...
- DataGrid点击上下一页loading效果
js添加显示loading和取消loading方法 function showtbloading() { var target = $("#GridView1"); if (tar ...