// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子。每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点
// 统计每个筛子里有多少个“X”连通块

思路:两次dfs

//思路:先dfs找包含X和*的区域,再在*的区域中dfs X的个数
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
const int N=52;
char pic[N][N];
int idx[2][N][N];
vector<int> ans;
int w, h;
int dr[]={0, 1, 0, -1};
int dc[]={1, 0, -1, 0}; //type 0 .
//type 1 . and *
bool isOk(int r, int c, int type)
{
if(type==0)
{
if(idx[type][r][c] || r <0 || r >=h || c <0 || c >=w || pic[r][c]=='.')
return false;
}
else if(type==1)
{
if(idx[type][r][c] || r <0 || r >=h || c <0 || c >=w || pic[r][c]!='X')
return false;
}
return true;
}
void dfs(int r, int c, int cnt)
{
if(!isOk(r, c, 0))
return; idx[0][r][c]=cnt;
for(int i=0;i<4;i++)
{
int nr=r+dr[i];
int nc=c+dc[i];
dfs(nr, nc, cnt);
}
} void dfs2(int r, int c)
{
if(!isOk(r, c, 1))
return; idx[1][r][c]=idx[0][r][c];
for(int i=0;i<4;i++)
{
int nr=r+dr[i];
int nc=c+dc[i];
dfs2(nr, nc);
}
} void dump(int type)
{
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
printf("%d", idx[type][i][j]);
printf("\n");
}
printf("\n");
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva657.in", "r", stdin);
#endif
int kase=0;
while(scanf("%d %d", &w, &h)!=EOF && w && h)
{
for(int i=0;i<h;i++) scanf("%s", pic[i]);
memset(idx, 0, sizeof idx);
ans.clear();
int cnt=0;
for(int i=0; i<h; i++)
for(int j=0;j<w;j++)
{
//搜索 X 和 *(即骰子), 并在 idx[0]中 标记骰子的序号
if(!idx[0][i][j] && pic[i][j]=='X')
{
cnt++;
dfs(i, j, cnt);
}
//搜索骰子中的 X ,并统计骰子的连通点的个数
if(!idx[1][i][j] && pic[i][j]=='X')
{
dfs2(i, j);
if(ans.size()<idx[0][i][j])
ans.resize(idx[0][i][j]);
ans[idx[0][i][j]-1]++;
}
} //dump(0);
//dump(1);
sort(ans.begin(), ans.end());
printf("Throw %d\n", ++kase);
int n=ans.size();
for(int i=0; i<n-1; i++)
printf("%d ", ans[i]);
if(n)
printf("%d", ans[n-1]);
printf("\n\n");
} return 0;
}

 

lrj解法:

// UVa657 The die is cast
// Rujia Liu
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子。每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点
// 统计每个筛子里有多少个“X”连通块
// 算法:首先用DFS找出两类连通块,即筛子('.'与'X')和点('X'),类别用0和1表示,然后统计0类连通块和1类连通块之间的包含关系,最后输出结果
// 在代码中,cnt[i]为第i类连通块的个数,idx[i][r][c]为格子(r,c)处第i类连通分量的编号
// enclose[i][j]表示编号为i的第0类连通块是否包含编号为j的第1类连通块
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 50; // 图片边长的最大值
const int maxd = 1000; // 筛子的最大个数
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, -1, 1}; char pic[maxn][maxn];
int h, w, idx[2][maxn][maxn], cnt[2];
int enclose[maxd][maxd*6]; bool is_type(int type, char ch) {
if(type == 0) return ch != '.'; // 筛子(包括点)
return ch == 'X'; // 点
} // DFS找第type类联通块,赋予编号id
void dfs(int r, int c, int type, int id) {
if(r < 0 || r >= h || c < 0 || c >= w) return;
if(!is_type(type, pic[r][c])) return;
if(idx[type][r][c] > 0) return;
idx[type][r][c] = id;
for(int d = 0; d < 4; d++)
dfs(r+dr[d], c+dc[d], type, id);
} // 标记(r,c)处的0类连通块包含编号为id的1类连通块
void mark(int r, int c, int id) {
if(r >= 0 && r < h && c >= 0 && c < w)
enclose[idx[0][r][c]][id] = 1;
} int main() {
int kase = 0;
while(scanf("%d%d", &w, &h) == 2 && w) {
for(int i = 0; i < h; i++)
scanf("%s", pic[i]); // DFS求连通块
memset(idx, 0, sizeof(idx));
cnt[0] = cnt[1] = 0;
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
for(int t = 0; t < 2; t++) {
if(idx[t][i][j] == 0 && is_type(t, pic[i][j])) dfs(i, j, t, ++cnt[t]);
} // 计算包含关系
memset(enclose, 0, sizeof(enclose));
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
if(pic[i][j] == 'X')
for(int d = 0; d < 4; d++)
mark(i+dr[d], j+dc[d], idx[1][i][j]); // 统计点数。注意两类连通块均从1开始编号
int ans[maxd];
for(int i = 1; i <= cnt[0]; i++) {
ans[i] = 0;
for(int j = 1; j <= cnt[1]; j++)
if(enclose[i][j]) ans[i]++;
}
sort(ans+1, ans+cnt[0]+1); // 输出。注意行末不要输出多余空格
printf("Throw %d\n", ++kase);
for(int i = 1; i < cnt[0]; i++)
printf("%d ", ans[i]);
printf("%d\n\n", ans[cnt[0]]);
}
return 0;
}

UVa657 The die is cast的更多相关文章

  1. UVA 657 The die is cast

      The die is cast  InterGames is a high-tech startup company that specializes in developing technolo ...

  2. The Die Is Cast(poj 1481简单的双dfs)

    http://poj.org/problem?id=1481 The Die Is Cast Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  3. 10409 - Die Game

    Problem G: Die Game Life is not easy. Sometimes it is beyond your control. Now, as contestants of AC ...

  4. Android解析服务器Json数据实例

    Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  7. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  8. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  9. words2

    餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘 ...

随机推荐

  1. I.MX6 默认打开 USB adb

    /***************************************************************************** * I.MX6 默认打开 USB adb ...

  2. Java [Leetcode 66]Plus One

    题目描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...

  3. noip2000提高组题解

    事实再次向我证明了RP的重要性... 第一题:进制转换 是我最没有把握AC的一道题目却是我唯一一道AC的题目,真是讽刺.看完题目几乎完全没有往正常的解法(取余倒序)去想,直接写了搜索,因为数据范围在2 ...

  4. .net-C#代码判断

    ylbtech-doc:.net-C#代码判断 C#代码判断 1.A,C#代码判断返回顶部 01.{ C#题目}public static void Main(string[] args){     ...

  5. C#中的值类型(value type)与引用类型(reference type)的区别

    ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部     C#中 ...

  6. <译>Selenium Python Bindings 1 - Installation

    Installation Introduction Selenium Python bindings 提供了一个简单的API来使用Selenium WebDriver编写使用功能/验收测试.通过Sel ...

  7. 软件测试技术(五)——Software Review

    本周的测试课上进行了一次同行评审的演练,大家讨论的很热烈,不过我也发现了一些不太理解的过程,如如何进行计划活动,走读.技术评审.正规检视是基于什么目的,并应该在何时进行.我做了一下详细的研究. 首先, ...

  8. 我喜欢的乐队-Euphoria

    来自日本的后摇乐团,001年冬天由森川裕之.佐藤昭太.木下阳辅三人于东京组建,2003年签约日本独立厂牌123Record,并发行首张EP细碟<Floral Dew>.包括EP.Singl ...

  9. 微信web调试工具

    做微信公众号开发的时候,最头疼的一件事,莫过于bug调试了. 由于有些功能涉及到权限问题,必须在微信公众号内打开,所以对开发人员来说就是遇到了一场噩梦. 这里有两种方法: 1.工具:mac.使用微信m ...

  10. 怎么用PHP在HTML中生成PDF文件

    原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...