首页 > 其他分享 >E - Christmas Color Grid 1

E - Christmas Color Grid 1

时间:2024-01-13 11:12:37浏览次数:29  
标签:return Color long int Grid sij 1005 Christmas

E - Christmas Color Grid 1

https://atcoder.jp/contests/abc334/tasks/abc334_e

思路

https://www.cnblogs.com/Lanly/p/17923753.html

Code

https://atcoder.jp/contests/abc334/submissions/49243194

int h,w;
bool s[1005][1005];
int c[1005][1005]; // c - class
long long modbase = 998244353;

struct NODE{
    int x;
    int y;
};

vector<struct NODE> directions = {
    {1, 0},
    {-1, 0},
    {0, 1},
    {0, -1}
};

void dfs(int i, int j, int class_order){
    if (i<1 || i>h){
        return;
    }
    
    if (j<1 || j>w){
        return;
    }

    bool sij = s[i][j];
    if (sij == false){
        return;
    }
    
    if (c[i][j] > 0){
        return;
    }
    
    c[i][j] = class_order;
    
    for(int k=0; k<directions.size(); k++){
        struct NODE nd = directions[k];
        int ni = i + nd.x;
        int nj = j + nd.y;
        
//        cout << "ni = " << ni << ", nj=" << nj << endl;
        
        dfs(ni, nj, class_order);
    }
}

int get_nb_comp_num(int i, int j){
    if (i<1 || i>h){
        return 0;
    }

    if (j<1 || j>w){
        return 0;
    }

    bool sij = s[i][j];
    if (sij == true){
        return 0;
    }

    set<int> nbs;
    for(int k=0; k<directions.size(); k++){
        struct NODE nd = directions[k];
        int ni = i + nd.x;
        int nj = j + nd.y;

        if (ni >= 1 && ni <=h && nj >= 1 && nj <= w){
            bool sninj = s[ni][nj];
            if (sninj == true){
                nbs.insert(c[ni][nj]);
            }
        }
    }
    
    return nbs.size();
}

long long qpower(long long a, long long b) {

    long long qwq = 1;

    while (b) {

        if (b & 1)

            qwq = qwq * a % modbase;

        a = a * a % modbase;

        b >>= 1;

    }

    return qwq;

}

long long inv(long long x) { return qpower(x, modbase - 2); }

int main()
{
    cin >> h >> w;
    
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            char ch;
            cin >> ch;
            if (ch == '#'){
                s[i][j] = true;
            } else {
                s[i][j] = false;
            }
        }
    }

//    cout << "--- after input ------" << endl;

    int class_order = 0;
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            bool sij = s[i][j];
            if (sij == true && c[i][j] == 0){
//                cout << "i = " << i << ", j=" << j << endl;
                class_order++;
                dfs(i, j, class_order);
            }
        }
    }
    
//    for(int i=1; i<=h; i++){
//        for(int j=1; j<=w; j++){
//            int cij = c[i][j];
//            cout << cij << " ";
//        }
//        cout << endl;
//    }

//    cout << "--- after dfs ------" << endl;

    long long redcnt = 0;
    long long compcnt = 0;
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            bool sij = s[i][j];
            if (sij == false){
//                cout << "comp cnt for i=" << i << ", j=" << j << endl;
                redcnt++;
                
                int nbcmpnum = get_nb_comp_num(i, j);
//                cout << "nbcmpnum = " << nbcmpnum << endl;
                compcnt += class_order - nbcmpnum + 1;
            }
        }
    }
    
//    cout << "--- after compcnt ------" << endl;
//    
//    cout << "compcnt = " << compcnt << ", redcnt=" << redcnt << endl;

//    long long ret = compcnt / redcnt;
    long long ret = compcnt % modbase * inv(redcnt) % modbase;
    cout << ret << endl;

    return 0;
}

 

标签:return,Color,long,int,Grid,sij,1005,Christmas
From: https://www.cnblogs.com/lightsong/p/17962121

相关文章

  • dic = dict(zip( ["a", "b"], ["h", "i"] )) lis_color
    dic=dict(zip(["a","b"],["h","i"]))lis_color=["lightred"]forkeyindic.keys():#问题1:判断键名是字典第几个键ind=list(dic.keys()).index(key)#问题2:根据索引循环选择颜色color=lis_color[i......
  • WPF的DataGrid绑定DataTable调研小记
    公司有个项目,界面很卡,同事怀疑是DataTable刷新引起的,我写了一个小Demo测试一下这块的性能。测试的结果DataTalbe的绑定非常的耗时我的前台代码:<DataGridGrid.Row="1"AutoGenerateColumns="True"BorderBrush="LightGray"ItemsSource="{BindingItems}"......
  • 【转】C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
    原文:https://www.cnblogs.com/leavind/p/6732530.html 1usingSystem.ComponentModel;2usingSystem.Drawing;3usingSystem.Windows.Forms;4namespaceRaywindStudio.Components5{6publicclassTabCtrlX:TabControl7{8publicT......
  • Flutter组件GridView使用介绍
    介绍GridView是Flutter中用于创建网格布局的滚动小部件。它可以创建多列布局,并且每个网格单元可以包含一个小部件。GridView提供了几种构造函数来创建不同类型的网格布局:GridView:最通用的构造函数,完全自定义网格布局。GridView.builder:按需构建网格项,适用于具有大量(或无限)网格......
  • TDBGridEh表格 lookupParams 使用
    客户的:cdsCustomer:TclientDataSet的CommandText为下的,中包含了DepartmentID部门ID,这里不用关联Department表的DepartmentID字段CommandText:='selecttop10CustomerID,Code,Customer,DepartmentIDfromCustomer'; 客户显示的表格如下设置Grideh1.columns[2].FieldName......
  • Flutter中GridTile中图像上方的InkVell波纹
    Flutter中GridTile中图像上方的InkVell波纹我认为这是在图像上显示波纹效果的更好方法。Ink.image(image:AssetImage('sample.jpg'),fit:BoxFit.cover,child:InkWell(onTap:(){},),),使用Stack,我们可以将Material和InkWell带到图像上。要拉伸材......
  • VMware Tanzu Kubernetes Grid (TKG) 1.4 下载 - VMware Kubernetes 分发版
    作者:gc,主页:www.sysin.orgTanzuKubernetes集群是由VMware构建、签名和支持的开源Kubernetes容器编排平台的完整分发版。可以通过使用TanzuKubernetesGrid服务在主管集群上置备和运行TanzuKubernetes集群。主管集群是启用了vSpherewithTanzu的vSphere集群。Tanzu......
  • VMware Tanzu Kubernetes Grid Integrated Edition (TKGI) 1.12 下载
    作者:gc,主页:www.sysin.orgVMwareTanzuKubernetesGridIntegratedEdition(TKGI)使运营商能够使用BOSH和OpsManager配置、运营和管理企业级Kubernetes集群。VMwareTanzuKubernetesGridIntegratedEdition(以前称为VMwareEnterprisePKS)是基于Kubernetes的容器解决......
  • [Mac软件]ColorWell For Mac 7.4.0调色板生成器
    美丽而直观的调色板和调色板生成器是任何Web或应用程序开发人员工具包的必要补充!创建无限数量的调色板,快速访问所有颜色信息和代码生成,用于应用程序开发,非常简单。可编辑调色板数据库允许您存档和恢复任何调色板,以便稍后通过超快速搜索使用。您所有精心创建的调色板都与macOS调色板......
  • B - Christmas Trees
    B-ChristmasTreeshttps://atcoder.jp/contests/abc334/tasks/abc334_b 思路对于起始种树点A在[L,R]区间的位置情况,三种A<LA>RA>=L,A<=R Codehttps://atcoder.jp/contests/abc334/submissions/48822474LLa,m,l,r;intmain(){cin>>a>&g......