The Die Is Cast(poj 1481简单的双dfs)
http://poj.org/problem?id=1481
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 860 | Accepted: 358 |
Description
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.
Input
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
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[][];
int v[][];
int v2[][];
int ans[];
int jx[]={,-,,};
int jy[]={,,,-};
void dfs2(int x,int y)
//用来标记遍历X的个数。
{ v2[x][y]=;
int tx,ty;
for(int i=;i<;i++)
{
tx=x+jx[i];
ty=y+jy[i];
if(tx>=&&tx<n&&ty>=&&ty<m&&map[tx][ty]=='X'&&v2[tx][ty]==)
{
dfs2(tx,ty);
}
}
}
void dfs(int x,int y)
//用来搜索投影
{
if(map[x][y]=='X'&&v2[x][y]==)
{
ans1++;
dfs2(x,y);
}
v[x][y]=;
int tx,ty;
for(int i=;i<;i++)
{
tx=x+jx[i];
ty=y+jy[i];
if(tx>=&&tx<n&&ty>=&&ty<m&&map[tx][ty]!='.'&&v[tx][ty]==)
{
dfs(tx,ty);
}
}
}
int main()
{
int k,KK=;
while(scanf("%d%d",&m,&n)!=EOF)
{
KK++;
if(n==&&m==) break;
k=;
for(int i=;i<n;i++)
{
scanf("%*c%s",map[i]);
}
memset(v,,sizeof(v));
memset(v2,,sizeof(v2));
memset(ans,,sizeof(ans));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(map[i][j]!='.'&&v[i][j]==)
{
ans1=;
dfs(i,j);
ans[k++]=ans1;
} }
}
qsort(ans,k,sizeof(ans[]),cmp);
printf("Throw %d\n",KK);
for(int i=;i<k;i++)
{
if(i==) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n\n");
}
return ;
}
如果两个dfs函数只用一个v[]数组,那么像这种情况整个图便不能全部遍历。
XXXXX
XXXXX//dfs走到这里就停了
*****
XXXXX
XXXXX
正确结果应该是2,
The Die Is Cast(poj 1481简单的双dfs)的更多相关文章
- UVa657 The die is cast
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...
- UVA 657 The die is cast
The die is cast InterGames is a high-tech startup company that specializes in developing technolo ...
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 2492 (简单并查集) A Bug's Life
题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的 ...
- poj 3414(简单bfs)
题目链接:http://poj.org/problem?id=3414 思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态. #include<iostream> #in ...
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- 两次DFS,POJ(1481)
题目链接:http://poj.org/problem?id=1481 两次DFS,这里的思路是,没找到*,就说明,有一个骰子,因此,每搜索到一个*,深搜4个方向,并且变为'.',要是搜到'X',就是 ...
- POJ 1321 简单dfs
1.POJ 1321 棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
随机推荐
- Objective-C官方文档 值和集合
版权声明:原创作品,谢绝转载!否则将追究法律责任. 尽管Objective-c是一个面向对象的语言,是C语言的超集,这意味着你可以用任何标准的C标量(非对象)像int,float,和char在Obje ...
- 【python】flask 开启 debug 模式
方法一: 直接在run的时候添加debug from flask import Flask app = Flask(__name__) @app.route('/') def hello_world( ...
- sql management studio正则替换sql
需要把create proc xxx替换为 drop proc xxx go create proc xxx 方法,使用正则查找替换 create procedure {\[dbo\]\.[^\(]+ ...
- Repository(资源库)模式
Repository(资源库) 协调领域和数据映射层,利用类似于集合的接口来访问领域对象 定义(来自Martin Fowler的<企业应用架构模式>): Mediates between ...
- Open XML C# and Word docx documents
openxml sdk2.5 : http://download.microsoft.com/download/5/5/3/553C731E-9333-40FB-ADE3-E02DC9643B31/ ...
- 【CF860E】Arkady and a Nobody-men 长链剖分
[CF860E]Arkady and a Nobody-men 题意:给你一棵n个点的有根树.如果b是a的祖先,定义$r(a,b)$为b的子树中深度小于等于a的深度的点的个数(包括a).定义$z(a) ...
- [通信] C#多线程Socket-文件传输
FileSendClient : Form1.cs using System; using System.IO; using System.Net; using System.Net.Sockets; ...
- jQuery里面ajax请求的封装
为了避免ajax漫天飞,我们需要对jQuery的代码进行封装,封装代码: function api_request(name, params, cb, scope, async, el) { if ( ...
- ElasticSearch 安装 go-mysql-elasticsearch 同步mysql的数据
一.首先在Centos6.5上安装 go 语言环境 下载Golang语言包:https://studygolang.com/dl [hoojjack@localhost src]$ ls apache ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...