UVa657 The die is cast
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子。每个筛子里又包含两种字符,其中'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的更多相关文章
- UVA 657 The die is cast
The die is cast InterGames is a high-tech startup company that specializes in developing technolo ...
- 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 ...
- 10409 - Die Game
Problem G: Die Game Life is not easy. Sometimes it is beyond your control. Now, as contestants of AC ...
- Android解析服务器Json数据实例
Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- words2
餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘 ...
随机推荐
- ffmepg 指定RTSP网络连接模式UDP还是TCP
AVFormatContext *formatCtx = NULL; formatCtx = avformat_alloc_context(); AVDictionary* options = NUL ...
- ffmpeg开发指南
FFmpeg是一个集录制.转换.音/视频编码解码功能为一体的完整的开源解决方案.FFmpeg的开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用.FFmpeg支持MPEG.DivX.M ...
- Android的图片压缩并上传
Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...
- 查询MySQL锁等待的语句
select 'Blocker' role, p.id, p.user, left(p.host, locate(':', p.host) - 1) host, tx.trx_ ...
- POJ 1519 Digital Roots
题意:求数根. 解法:一个数的数根就是mod9的值,0换成9,只是没想到给的是一个大数……只好先把每位都加起来再mod9…… 代码: #include<stdio.h> #include& ...
- User Agent
Android: Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; MI-ONE Plus Build/GINGERBREAD) AppleWebKit/533 ...
- HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)
题意: 给你一个序列a[],求它的不降子序列的个数 分析: dp[i]表示以i结尾不降子序列的个数,dp[i]=sum(dp[j])+1(j<i&&a[j]<=a[i]); ...
- NEUOJ711 异星工厂 字典树+贪心
题意:你可以收集两个不相交区间的权值,区间权值是区间异或,问这两个权值和最大是多少 分析:很多有关异或求最大的题都是利用01字典树进行贪心,做这个题的时候我都忘了...最后是看别人代码的时候才想起来这 ...
- linux常用命令之--用户与用户组管理命令
linux的用户与用户组管理命令 1.用户和群组 groupadd:用于添加新的组群 其命令格式如下: groupadd [-option] 群组名 常用参数: -g GID:指定创建群组的GID(G ...
- 如何用Entity Framework 6 连接Sqlite数据库[转]
获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/do ...