// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子。每个筛子里又包含两种字符,其中'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. 省常中模拟 Test3 Day2

    matrix 找规律 题意:给定一个 N*N 的只有 0 和 1 的矩阵,有 Q 个操作,分三种:1. 将某行上的所有数字取反:2. 将某列上的所有数字取反:3. 输出 sum{ a[i][j]*a[ ...

  2. 【C#学习笔记】LinkedList容器使用

    using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { stati ...

  3. ffmpeg/ffplay vc6 源码剖析

    ffmpeg/ffplay是当今多媒体领域的王者,很多很多的人想研究学习ffmpeg/ffplay,但苦于ffmpeg/ffplay庞大的代码量,令人望而生畏.为帮助更多的人研习ffmpeg/ffpl ...

  4. TCP/IP详解学习笔记(1)-基本概念

    为什么会有TCP/IP协议 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样 ...

  5. ECshop 每个数据库表结构说明

    ecs_account_log // 用户账目日志表 ecs_activity // 活动表(代码,名称,开始,结束,描述) ecs_ad // 广告表(位置,类型,名称,链接,图片,开始,结束,广告 ...

  6. golang语言部分保留字的举例

    golang和c的代码有很大不同的,一不小心就会误用. 1 /* go保留字: */ /* break default func interface select case defer go map ...

  7. windows远程连接linux桌面---使用tightvnc或者tigervnc

    一.安装tightvnc: tightvnc的安装在安装包中有详细的说明(README文件) 首先你要确保linux已经安装jpeg和zlib库, 2.编译 执行如下两个命令: [root@local ...

  8. Procdure for wanfo business report

    CREATE OR REPLACE PROCEDURE PROC_TZ_EXEC_N_YEARREPORT(ssrq varchar2 ) as -----声明变量 v_raise EXCEPTION ...

  9. bjfu1211 推公式,筛素数

    题目是求fun(n)的值 fun(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])C[n][k] ...

  10. [Web API] 如何让 Web API 统一回传格式以及例外处理[转]

    [Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...