Codeforces 327D Block Tower【思维+Bfs】

本文介绍了一款名为“BlockTowers”的计算机游戏,并提出了一个算法解决方案,该方案旨在通过合理的建筑和拆除操作来实现城市人口容量的最大化。

D. Block Tower
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with n rows and m columns (it contains n × m cells). The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any building. The rest of cells are empty. In some empty cell Iahub can build exactly one tower of two following types:

  1. Blue towers. Each has population limit equal to 100.
  2. Red towers. Each has population limit equal to 200. However, it can be built in some cell only if in that moment at least one of the neighbouring cells has a Blue Tower. Two cells are neighbours is they share a side.

Iahub is also allowed to destroy a building from any cell. He can do this operation as much as he wants. After destroying a building, the other buildings are not influenced, and the destroyed cell becomes empty (so Iahub can build a tower in this cell if needed, see the second example for such a case).

Iahub can convince as many population as he wants to come into his city. So he needs to configure his city to allow maximum population possible. Therefore he should find a sequence of operations that builds the city in an optimal way, so that total population limit is as large as possible.

He says he's the best at this game, but he doesn't have the optimal solution. Write a program that calculates the optimal one, to show him that he's not as good as he thinks.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 500). Each of the next n lines contains m characters, describing the grid. The j-th character in the i-th line is '.' if you're allowed to build at the cell with coordinates (i, j) a tower (empty cell) or '#' if there is a big hole there.

Output

Print an integer k in the first line (0 ≤ k ≤ 106) — the number of operations Iahub should perform to obtain optimal result.

Each of the following k lines must contain a single operation in the following format:

  1. «B x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a blue tower at the cell (x, y);
  2. «R x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a red tower at the cell (x, y);
  3. «D x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — destroying a tower at the cell (x, y).

If there are multiple solutions you can output any of them. Note, that you shouldn't minimize the number of operations.

Examples
Input
2 3
..#
.#.
Output
4
B 1 1
R 1 2
R 2 1
B 2 3
Input
1 3
...
Output
5
B 1 1
B 1 2
R 1 3
D 1 2
R 1 2

题目大意:


现在给你一个N*M大小的矩阵,其中点表示空地(可建筑),井号表示不可建筑位子。


现在一共有两种建筑,一种是蓝色的,一种是红色的,蓝色的可以居住100人,红色的可以居住200人。

蓝色的建筑没有建筑要求,红色的建筑需要建立在蓝色建筑的边上(红色建筑的上下左右一个位子要有蓝色建筑)。

我们也可以对建筑进行拆除,问怎样建筑能够使得居住的人最多。


思路:


贪心的去想,我们肯定是想要多建立起来红色的建筑才行啊。

所以我们一开始对于整个地图都进行建筑蓝色建筑。

然后通过拆除一些蓝色建筑来重新建筑红色建筑。

以此来得到最多的居住人数的建设方案。


很显然,对于一个联通块来讲,只要最后剩下一个蓝色建筑即可,其他建筑均可拆除之后建立其红色建筑。


那么这里我们Bfs一下,记录一个数组step【i】【j】,表示走到(i,j)这个点需要的步数。

那么我们从大的step【i】【j】开始拆除,最终拆除到step【i】【j】==1为止。

那么我们对于每个联通块都进行一次Bfs,然后用优先队列维护一下即可。。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
    int op,x,y;
} ans[1000050];
struct node2
{
    int x,y;
} now,nex;
struct node3
{
    int x,y,val;
    bool friend operator <(node3 a,node3 b)
    {
        return a.val<b.val;
    }
};
int n,m,output;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int step[505][505];
char a[505][505];
void Bfs(int sx,int sy)
{
    queue<node2>s;
    now.x=sx;
    now.y=sy;
    s.push(now);
    step[sx][sy]=1;
    while(!s.empty())
    {
        now=s.front();
        s.pop();
        for(int i=0; i<4; i++)
        {
            int xx=now.x+fx[i];
            int yy=now.y+fy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
            {
                if(a[xx][yy]=='B'&&step[xx][yy]==0)
                {
                    step[xx][yy]=step[now.x][now.y]+1;
                    nex.x=xx;
                    nex.y=yy;
                    s.push(nex);
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        output=0;
        memset(step,0,sizeof(step));
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a[i]+1);
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(a[i][j]=='.')
                {
                    ans[output].op=1;
                    ans[output].x=i;
                    ans[output++].y=j;
                    a[i][j]='B';
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(step[i][j]==0&&a[i][j]=='B')
                {
                    Bfs(i,j);
                }
            }
        }
        priority_queue<node3>s;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='B')
                {
                    node3 tmp;
                    tmp.x=i;
                    tmp.y=j;
                    tmp.val=step[i][j];
                    s.push(tmp);
                }
            }
        }
        while(!s.empty())
        {
            node3 tmp=s.top();
            s.pop();
            if(tmp.val==1)continue;
            int x=tmp.x;
            int y=tmp.y;
            a[x][y]='R';
            ans[output].op=3;
            ans[output].x=x;
            ans[output++].y=y;
            ans[output].op=2;
            ans[output].x=x;
            ans[output++].y=y;
        }
        printf("%d\n",output);
        for(int i=0; i<output; i++)
        {
            if(ans[i].op==1)printf("B ");
            if(ans[i].op==2)printf("R ");
            if(ans[i].op==3)printf("D ");
            printf("%d %d\n",ans[i].x,ans[i].y);
        }

    }
}











【电力系统】单机无穷大电力系统短路故障暂态稳定Simulink仿真(带说明文档)内容概要:本文档围绕“单机无穷大电力系统短路故障暂态稳定Simulink仿真”展开,提供了完整的仿真模型与说明文档,重点研究电力系统在发生短路故障后的暂态稳定性问题。通过Simulink搭建单机无穷大系统模型,模拟不同类型的短路故障(如三相短路),分析系统在故障期间及切除后的动态响应,包括发电机转子角度、转速、电压和功率等关键参数的变化,进而评估系统的暂态稳定能力。该仿真有助于理解电力系统稳定性机理,掌握暂态过程分析方法。; 适合人群:电气工程及相关专业的本科生、研究生,以及从事电力系统分析、运行与控制工作的科研人员和工程师。; 使用场景及目标:①学习电力系统暂态稳定的基本概念与分析方法;②掌握利用Simulink进行电力系统建模与仿真的技能;③研究短路故障对系统稳定性的影响及提高稳定性的措施(如故障清除时间优化);④辅助课程设计、毕业设计或科研项目中的系统仿真验证。; 阅读建议:建议结合电力系统稳定性理论知识进行学习,先理解仿真模型各模块的功能与参数设置,再运行仿真并仔细分析输出结果,尝试改变故障类型或系统参数以观察其对稳定性的影响,从而深化对暂态稳定问题的理解。
本研究聚焦于运用MATLAB平台,将支持向量机(SVM)应用于数据预测任务,并引入粒子群优化(PSO)算法对模型的关键参数进行自动调优。该研究属于机器学习领域的典型实践,其核心在于利用SVM构建分类模型,同时借助PSO的全局搜索能力,高效确定SVM的最优超参数配置,从而显著增强模型的整体预测效能。 支持向量机作为一种经典的监督学习方法,其基本原理是通过在高维特征空间中构造一个具有最大间隔的决策边界,以实现对样本数据的分类或回归分析。该算法擅长处理小规模样本集、非线性关系以及高维度特征识别问题,其有效性源于通过核函数将原始数据映射至更高维的空间,使得原本复杂的分类问题变得线性可分。 粒子群优化算法是一种模拟鸟群社会行为的群体智能优化技术。在该算法框架下,每个潜在解被视作一个“粒子”,粒子群在解空间中协同搜索,通过不断迭代更新自身速度与位置,并参考个体历史最优解和群体全局最优解的信息,逐步逼近问题的最优解。在本应用中,PSO被专门用于搜寻SVM中影响模型性能的两个关键参数——正则化参数C与核函数参数γ的最优组合。 项目所提供的实现代码涵盖了从数据加载、预处理(如标准化处理)、基础SVM模型构建到PSO优化流程的完整步骤。优化过程会针对不同的核函数(例如线性核、多项式核及径向基函数核等)进行参数寻优,并系统评估优化前后模型性能的差异。性能对比通常基于准确率、精确率、召回率及F1分数等多项分类指标展开,从而定量验证PSO算法在提升SVM模型分类能力方面的实际效果。 本研究通过一个具体的MATLAB实现案例,旨在演示如何将全局优化算法与机器学习模型相结合,以解决模型参数选择这一关键问题。通过此实践,研究者不仅能够深入理解SVM的工作原理,还能掌握利用智能优化技术提升模型泛化性能的有效方法,这对于机器学习在实际问题中的应用具有重要的参考价值。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值