您的位置:首页 > 其它

UVa 657 The die is cast

2010-03-16 15:26 357 查看
/*
coder: ACboy
date: 2010-3-16
result: AC
description: UVa 657 The die is cast
*/

#include <iostream>
#include <algorithm>
using namespace std;

int data[100][100];
int temp[100][100];
int vis[100][100];
int m, n;
int dx[] = {0, 0, 1, -1, 0};
int dy[] = {0, 1, 0, 0, -1};

void transfer(int x, int y)
{
for (int i = 0; i < 5; ++i)
{
int newx = x + dx[i];
int newy = y + dy[i];
if (newx >= 0 && newx < n && newy >= 0 && newy < m && data[newx][newy])
{
temp[newx][newy] = data[newx][newy];
data[newx][newy] = 0;
transfer(newx, newy);
}
}
}

void dfs(int x, int y)
{
for (int i = 0; i < 5; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
if (!vis[newx][newy] && temp[newx][newy] == 2)
{
vis[newx][newy] = 1;
dfs(newx, newy);
}
}
}

int findAns() {
memset(vis, 0, sizeof(vis));
int count = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; ++j) if (temp[i][j] == 2 && !vis[i][j])
{
dfs(i, j);
count++;
}
}
return count;
}

int main()
{
int k = 0;
#ifndef ONLINE_JUDGE
freopen("657.txt", "r", stdin);
#endif
while (cin >> m >> n)
{
if (m == n && n == 0) break;
int i, j;
int ans[2500];
int count = 0;
memset(data, 0, sizeof(data));
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
char temp;
cin >> temp;
if (temp == '*') {
data[i][j] = 1;
} else if (temp == 'X') {
data[i][j] = 2;
}
}
}

for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
if (data[i][j] == 1) {
memset(temp, 0, sizeof(temp));
transfer(i, j);
int a = findAns();
ans[count++] = a;
}
}
}

cout << "Throw " << ++ k << endl;
sort(ans, ans + count);
for (i = 0; i < count - 1; i++)
{
cout << ans[i] << " ";
}
cout << ans[i] << endl;
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  2010