uva 657 The die is cast(DFS遍历图)
来源:互联网 发布:软件测试的原则是什么 编辑:程序博客网 时间:2024/06/05 22:46
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 11 2 2 4题目大意:判断色子的数值(所给出的个数不一定,色子的值有从小到大输出。
解题思路:和油田的做法差不多就是要注意一点
*X*
*X*
*X*
是1点。(遍历的时候标记将一个色子变成两个)
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 55char map[N][N];int n, m, num[N], cnt;const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};void DFS(int a, int b){map[a][b] = '*';for (int i = 0; i < 4; i++){int p = a + dir[i][0], q = b + dir[i][1];if (p < 0 || p >= n)continue;if (q < 0 || q >= m)continue;if (map[p][q] != 'X')continue;DFS(p, q);}}int find(int a, int b){int sum = 0;if (map[a][b] == 'X'){DFS(a, b);sum++;sum += find(a, b);}else{map[a][b] = '.';for (int i = 0; i < 4; i++){int p = a + dir[i][0], q = b + dir[i][1];if (p < 0 || p >= n)continue;if (q < 0 || q >= m)continue;if (map[p][q] == '.')continue;sum += find(p, q);}}return sum;}int main(){int t = 1;while (scanf("%d%d%*c", &m, &n), n || m){// Init.memset(map, 0, sizeof(map));memset(num, 0, sizeof(num));cnt = 0;// Read.for (int i = 0; i < n; i++)gets(map[i]);// Handle.for (int i = 0; i < n; i++)for (int j = 0; j < m; j++){if (map[i][j] == '*' || map[i][j] == 'X'){num[cnt++] = find(i, j);}}// Printf.printf("Throw %d\n", t++);sort(num, num + cnt);for (int i = 0; i < cnt - 1; i++)printf("%d ", num[i]);printf("%d\n\n", num[cnt - 1]);}return 0;}
- uva 657 The die is cast(DFS遍历图)
- UVa 657 The die is cast(DFS)
- UVa 657 The die is cast(DFS*2)
- UVA - 657 The die is cast(二重dfs)
- UVA - 657 - The die is cast (两次DFS!!)
- UVA 657 The die is cast (DFS*2)
- 657 - The die is cast(dfs)
- UVA 657 The die is cast ——bfs+dfs
- UVA 657-The die is cast(dfs*2)
- UVa-657 The die is cast(dfs嵌套)
- The die is cast (DFS 双重)
- UVa 657 The die is cast
- uva 657 - The die is cast
- uva 657 - The die is cast
- uva 657 - The die is cast
- UVa 657 - The die is cast
- UVA 657 - The die is cast
- UVA 657 The die is cast
- Ural 1204. Idempotents 扩展欧几里得算法
- spring mvc + concurrent blockingQueue + @PostConstruct
- CodeForces 109A
- 正则-10<善恶<10 判断
- 有向图 MST 最小树形图 刘朱算法
- uva 657 The die is cast(DFS遍历图)
- HTML语义和前端架构
- UVA 152 (13.07.30)
- 和自己赛跑的人 —不要怕、不后悔!
- hdu 1248
- 从0开始学quantum之1:Python高级特性--装饰器
- VIJOS P1028 魔族密码
- 笔记
- 内核编译的步骤及遇到的问题