uva 657 - The die is cast
2012-07-25 16:31
274 查看
The die is cast |
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的区域数目,并按照升序输出。
找区域可以用dfs或bfs为了不和后面的bfs搞混就用dfs。
统计区域数目时,找到一个X后很明显需要用bfs将与之相连的X做标记避免重复计算。
当然标记数组有2个,dfs和bfs各一个
1.找到一个s[X][Y]‘*’,dfs(X,Y)
2.四个方向继续dfs
遇到为bfs数组未标记的‘X’ 则数量+1,bfs标记与之相连的X
只要dfs数组为标记且不是‘.'就要继续dfs(可能一个区域内‘*’较少‘X'很多,
10 2 ..X**X***X ..XXXX....
中间的只有X连接,没有’*‘连接
3.特殊情况,没有”*“;
2 2 XX XX
只要在完成上述步骤最好对全图bfs标记数组未标记的点如果存在X,则总的个数+1,X的区域数为1,并bfs与之相连的点。
#include <stdio.h> #include <string.h> int top,tail,h,w,sum=0,num, a[2500],vdfs[100][100],vbfs[100][100],qx[2500],qy[2500],move[4][2]={1,0,-1,0,0,1,0,-1}; char s[100][100]; void bfs() {int X,Y,i; for (i=0;i<4;i++) {X=qx[top]+move[i][0]; Y=qy[top]+move[i][1]; if ((X>=0)&&(X<h)&&(Y>=0)&&(Y<w)&&(vbfs[X][Y]==1)&&(s[X][Y]=='X')) {++tail; qx[tail]=X; qy[tail]=Y; vbfs[X][Y]=0; } } ++top; if (top<=tail) bfs(); }; void dfs(int x,int y) {int i,X,Y; for (i=0;i<4;i++) {X=x+move[i][0]; Y=y+move[i][1]; if ((X>=0)&&(X<h)&&(Y>=0)&&(Y<w)&&(vdfs[X][Y]==1)) {if ((s[X][Y]=='X')&&(vbfs[X][Y]==1)) {vbfs[X][Y]=0; qx[1]=X; qy[1]=Y; top=1; tail=1; //每次使用bfs队列初始化 ++a[num]; bfs(); //标记相连的X } if (s[X][Y]!='.') {vdfs[X][Y]=0;dfs(X,Y);//开始没有把所有未标记的不是’.'的点dfs wrong了,n 次,就算为’X‘也要dfs } } }; int main() {int i,j; while (scanf("%d %d",&w,&h),w+h) {getchar(); ++sum; for (i=0;i<h;i++) gets(s[i]); for (i=0;i<h;i++) for (j=0;j<w;j++) {vdfs[i][j]=1;vbfs[i][j]=1;} num=0; a[0]=0; for (i=0;i<h;i++) for (j=0;j<w;j++) if ((vdfs[i][j]==1)&&(s[i][j]=='*')) //找到可能存在x的区域,以此为起点dfs {++num; a[num]=0; dfs(i,j); } for (i=0;i<h;i++) for (j=0;j<w;j++) {if ((vbfs[i][j]==1)&&(s[i][j]=='X')) //遇到没有’*‘的情况,对全图搜索’X‘,遇到后bfs与之相连的X,这种特殊情况X区域数一定为1 {++num; a[num]=1; top=1; tail=1; qx[1]=i; qy[1]=j; vbfs[i][j]=0; bfs(); } } printf("Throw %d\n",sum); for (i=1;i<num;i++) for (j=i+1;j<=num;j++) if (a[i]>a[j]) {a[0]=a[i];a[i]=a[j];a[j]=a[0];} for (i=1;i<num;i++) printf("%d ",a[i]); printf("%d\n\n",a[num]); } return 0; }
[/code]
相关文章推荐
- UVA 657 - The die is cast
- UVa 657 - The die is cast
- UVA - 657 The die is cast
- UVa657 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 ——bfs+dfs
- uva657 The die is cast(搜索)
- UVa 657 & POJ 1481 - The die is cast
- UVA 657-The die is cast(双重BFS)
- UVA 657 - The die is cast 深度搜索
- uva 657 The die is cast(DFS遍历图)
- UVA 657 The die is cast
- uva 657 - The die is cast
- UVa 657 - The die is cast
- The die is cast UVA 657