栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1481 The Die Is Cast

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1481 The Die Is Cast

#include <iostream>#include <cstring>#include <algorithm>#define H   51#define W   51using namespace std;char f[H][W];char vis[H][W];char d[8][2] = {{-1,0},{0,-1},{0,1},{1,0},{1,-1},{-1,1},{-1,-1},{1,1}};int w, h, ans[W], n, num;int cmp(const void * a, const void * b){  return ( *(int*)a - *(int*)b );}void prints(){    for(int i=1; i <= h; ++i)    {        for(int j=1; j <= w; ++j) cout << (int)vis[i][j];        cout << endl;    }    cout << "-------------------------------------------------------" << endl;}void findNum(int x, int y){    if(f[x][y]!='X' || vis[x][y] != 0)        return;    vis[x][y] = 2;    for(int i=0; i < 4; ++i)    {        int dx = x + d[i][0];        int dy = y + d[i][1];        findNum(dx, dy);    }    return;}int dfs(int x, int y){    if(f[x][y] == '*' || f[x][y] == 'X')    {        if(vis[x][y] == 1) return 1;        if(f[x][y] == 'X' && vis[x][y] == 0)        { num++; findNum(x, y);        }        vis[x][y] = 1;        for(int i=0; i < 4; ++i)        { int dx = x + d[i][0]; int dy = y + d[i][1]; if(vis[x][y] == 2 && f[dx][dy] == 'X')     continue; dfs(dx, dy);        }    }    return num;}int main(){    int i=1;    //freopen("in.txt", "rb+", stdin);    //freopen("out.txt", "wb+", stdout);    while(cin >> w >> h, h!=0)    {        n = 0;        memset(ans, 0, sizeof(char)*W);        memset(f  , 0, sizeof(char)*H*W);        memset(vis, 0, sizeof(char)*H*W);        for(int i=1; i <= h; ++i) for(int j=1; j <= w; ++j)     cin >> f[i][j];        for(int i=1; i <= h; ++i) for(int j=1; j <= w; ++j)     if((f[i][j] == '*' || f[i][j] == 'X') && vis[i][j] == 0)     {         num = 0;         ans[n++] = dfs(i, j);         //prints();     }        cout << "Throw " << i++ << endl;        qsort(ans, n, sizeof(int), cmp);        for(int i=0; i < n; ++i)        { if(i!=0)     cout << " "; cout << ans[i];        }        cout << "n" << endl;    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号