The Die Is Cast(poj 1481简单的双dfs)
http://poj.org/problem?id=1481
The Die Is Cast
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 860 | Accepted: 358 |
Description
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 different 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 a1, a2, ..., ak in S such that a = a1 and b = ak , and ai and ai+1 are connected for 1 <= i < k.
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.
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 different 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.

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 5 <= w, h <= 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.
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.
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
我很贱,这题我没读题直接看了输入输出就开始做了,主要自己真的是英语水平有限,题目很简单,主要是自己有个点没想起来,最后是看了别人的题解才A的。
要开两个v[]标记数组,一个用来搜索投影,一个用来记录X的个数。
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> using namespace std; int n,m,ans1; int cmp(const void *a,const void *b) { return *(int *)a-*(int *)b; } char map[101][101]; int v[101][101]; int v2[101][101]; int ans[1000001]; int jx[]={1,-1,0,0}; int jy[]={0,0,1,-1}; void dfs2(int x,int y) //用来标记遍历X的个数。
{ v2[x][y]=1;
int tx,ty; for(int i=0;i<4;i++) { tx=x+jx[i]; ty=y+jy[i]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]=='X'&&v2[tx][ty]==0) { dfs2(tx,ty); } } } void dfs(int x,int y) //用来搜索投影
{ if(map[x][y]=='X'&&v2[x][y]==0) { ans1++; dfs2(x,y); } v[x][y]=1; int tx,ty; for(int i=0;i<4;i++) { tx=x+jx[i]; ty=y+jy[i]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]!='.'&&v[tx][ty]==0) { dfs(tx,ty); } } } int main() { int k,KK=0; while(scanf("%d%d",&m,&n)!=EOF) { KK++; if(n==0&&m==0) break; k=0; for(int i=0;i<n;i++) { scanf("%*c%s",map[i]); } memset(v,0,sizeof(v)); memset(v2,0,sizeof(v2)); memset(ans,0,sizeof(ans)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(map[i][j]!='.'&&v[i][j]==0) { ans1=0; dfs(i,j); ans[k++]=ans1; } } } qsort(ans,k,sizeof(ans[0]),cmp); printf("Throw %d\n",KK); for(int i=0;i<k;i++) { if(i==0) printf("%d",ans[i]); else printf(" %d",ans[i]); } printf("\n\n"); } return 0; }
如果两个dfs函数只用一个v[]数组,那么像这种情况整个图便不能全部遍历。
XXXXX
XXXXX//dfs走到这里就停了
*****
XXXXX
XXXXX
正确结果应该是2,
分类:
图论
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构