657 - The die is cast

来源:互联网 发布:2016美国经济非农数据 编辑:程序博客网 时间:2024/06/05 01:16



  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 pixelsconnected 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 inS, there is a sequence $a_1, a_2, \dots, a_k$ inS such that a = a1 andb = ak , andai and ai+1 are connected for$1 \le i < k$.

连接在一起的像素点组成集合S,集合中的每一对像素(a,b),a,b分别代表一个点,都有一条路径a1,a2,a3,a4.....ak,使得a=a1,b=ak(就是说存在一条路径从a到b)。

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.

我们把有非背景像素点连接在一起的最大的集合称为色子,同样把由点像素(X)点连接在一起的最大的集合称作一点。(其实就是各自的联通分量) 

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$5 \leŸw,h \le 50$.

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

思路1:百度搜索中有很多牛人的妙方法,比较好的是使用双重深度优先,第一重遍历色子,第二重计算色子点数。把背景点当成边界和访问标志,遇*表示进入第一连通分量,把其修改为’.‘表示已经访问,深度遍历四周四个点,如果遇到X进入第二重深度优先遍历计算点数。

思路2:这是我的做法,比较笨,先把背景像素点全部遍历一次,并标记为已经访问过,则剩下的就是连通分量块,每次把一个连通分量块复制到一个新的地图中,再对新地图进行遍历求出色子的点数。

代码:

#include <iostream>#include <string.h>#include <set>#define MAX 52using namespace std;void init_graph(char (*G)[MAX],int row,int col){//输入地图char ch;int r=0,c=0;while(cin>>ch){G[r][c++]=ch;if(c==col){++r;c=0;}if(r==row) break;}}int visited[MAX][MAX];int copy_vis[MAX][MAX];void get_graph(char (*G)[MAX],char (*dest)[MAX],int r,int c,int row,int col){//每次复制一个连通分量到新地图destif(visited[r][c] || G[r][c]=='.') return ;    visited[r][c]=1;copy_vis[r][c]=0;dest[r][c]=G[r][c];if(r-1>=0 ) {get_graph(G,dest,r-1,c,row,col); }    if(c-1>=0){    get_graph(G,dest,r,c-1,row,col);    }if(c+1<col){get_graph(G,dest,r,c+1,row,col);}if(r+1<row){get_graph(G,dest,r+1,c,row,col);}}void DFS(char (*G)[MAX],int (*vis)[MAX],char ch,int r,int c,int row,int col){//深度优先访问连接的结点    if(vis[r][c] || G[r][c]!=ch) return ;vis[r][c]=1;if(r-1>=0 ) {DFS(G,vis,ch,r-1,c,row,col);}if(c-1>=0){DFS(G,vis,ch,r,c-1,row,col);}if(c+1<col){DFS(G,vis,ch,r,c+1,row,col);}if(r+1<row){DFS(G,vis,ch,r+1,c,row,col);}}int main(){int col,row;int time=0;while(cin>>col>>row){if(col==0 && row==0) break;++time;char G[MAX][MAX];memset(G,0,sizeof(G));memset(visited,0,sizeof(visited));init_graph(G,row,col);for(int i=0;i<row;++i){//访问背景像素点,目的是求出所有的连通分量for(int j=0;j<col;++j){if(!visited[i][j] && G[i][j]=='.'){     DFS(G,visited,'.',i,j,row,col);}}}multiset<int> sets;for(int i=0;i<row;++i){//寻找连通分量for(int j=0;j<col;++j){if(!visited[i][j]) {//找到起点    char dest[MAX][MAX];memset(dest,'.',sizeof(dest));memset(copy_vis,1,sizeof(copy_vis));get_graph(G,dest,i,j,row,col);//将连通分量复制到新地图中int count=0;for(int r=0;r<row;++r){//计算色子的点数for(int c=0;c<col;++c){if(!copy_vis[r][c] && dest[r][c]=='X'){++count;                                DFS(dest,copy_vis,'X',r,c,row,col);}}}sets.insert(count);}}}cout<<"Throw "<<time<<endl;bool is_first=true;for(auto elem: sets){if(is_first){cout<<elem;is_first=false;}else{cout<<" "<<elem;}}cout<<endl;cout<<endl;}}

0 0
原创粉丝点击