// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子。每个筛子里又包含两种字符,其中'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. LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)

    题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...

  2. json化表单数据

    /** * josn化表单数据 * @name baidu.form.json * @function * @grammar baidu.form.json(form[, replacer]) * @ ...

  3. BrowserSync,调试利器--自动刷新(转

    ---恢复内容开始--- 请想象这样一个场面:你开着两个显示器,一边是IDE里的代码,另一边是浏览器里的你正在开发的应用.此时桌上还放着你的手机,手机里也是这个开发中的应用.然后,你新写了一小段代码, ...

  4. liunx上运行mybase

    mybase 是个人知识管理软件,国内用他的人很多,尤其是程序员.我也是mybase的忠实使用者,有大量的积累.    以前一直用Windows,mybase使用完全没有问题,后来转投ubuntu阵营 ...

  5. android bin目录下的.ap_是神马文件?

    resources.ap_ resources翻译过来是资源的意思 应该就是一种中间文件,可以改成rar.zip等压缩文件的类型,里面包含res.AndroidMainfest.xml.resourc ...

  6. C# winform 登录 单例模式(转)

    主界面配置代码: frmLogin Codz program.cs 代码 static class Program { public static EventWaitHandle ProgramSta ...

  7. 关于“心脏出血”漏洞(heartbleed)的理解

    前阵子“心脏出血”刚发生的时候读了下源代码,给出了自己觉得比较清楚的理解.   -------------------------穿越时空的分割线--------------------------- ...

  8. 利用ASP.NET MVC源代码调试你的应用程序[转]

    由于项目需要,最近学起asp.net mvc.昨天遇到ViewData和TempData他们之间的分别这样让我纠结的问题.有园友强烈建议我去看ASP.NET MVC的源代码.所以,我想到如何在调试AS ...

  9. linux 用 SSH2协议远程连接并控制 linux

    [参考链接](http://php.net/manual/zh/ssh2.installation.php) ssh2_exec 并不能打印所有的命令的提示信息 如果有返回的字符串信息,可以打印,或重 ...

  10. android string[] arraylist<string>互转

    string[] to arraylist<string>:eg:string[] str; arraylist<string> arr; for(int i=0;i<s ...