暴搜 - Codeforces Round #327 (Div. 2) E. Three States

简介:  E. Three States  Problem's Link   Mean:  在一个N*M的方格内,有五种字符:'1','2','3','.

 E. Three States 

Problem's Link


 

Mean: 

在一个N*M的方格内,有五种字符:'1','2','3','.','#'.

现在要你在'.'的地方修路,使得至少存在一个块'1','2'和'3'是连通的.

问:最少需要修多少个'.'的路.

analyse:

想法题,想到了就很简单.

直接暴力枚举每个国家到每个可达的点的最小代价,然后计算总和取最小值.

dis[k][x][y]表示第k个国家到达坐标(x,y)的点的最小代价.

Time complexity: O(N)

 

view code

/*
* -----------------------------------------------------------------
* Copyright (c) 2015 crazyacking All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);

const int MAXN = 1001;
char s [ MAXN ][ MAXN ];
int dx [ 4 ] = { - 1 , 0 , 1 , 0 };
int dy [ 4 ] = { 0 , - 1 , 0 , 1 };
int dis [ 3 ][ MAXN ][ MAXN ];
int main()
{
    ios_base :: sync_with_stdio( false);
    cin . tie( 0);
    int n , m;
    while ( ~ scanf( "%d %d" , &n , & m))
    {
        for ( int i = 0; i < n; ++ i)
            scanf( "%s" , s [ i ]);
        for ( int i = 0; i < n; ++ i)
            for ( int j = 0; j < m; ++ j)
                dis [ 0 ][ i ][ j ] = dis [ 1 ][ i ][ j ] = dis [ 2 ][ i ][ j ] = ( int) 1e8;
        queue < pair < int , int > > Q;
        for ( int k = 0; k < 3; ++ k)
        {
            while ( ! Q . empty())
                Q . pop();
            for ( int i = 0; i < n; ++ i)
            {
                for ( int j = 0; j < m; ++ j)
                {
                    if (s [ i ][ j ] == k + '1')
                    {
                        Q . push( make_pair( i , j));
                        dis [ k ][ i ][ j ] = 0;
                    }
                }
            }

            while ( ! Q . empty())
            {
                pair < int , int > now = Q . front();
                Q . pop();
                int x = now . first;
                int y = now . second;
                for ( int i = 0; i < 4; ++ i)
                {
                    int xx = x + dx [ i ];
                    int yy = y + dy [ i ];
                    if ( !( xx >= 0 && xx < n)) continue;
                    if ( !( yy >= 0 && yy < m)) continue;
                    if (s [ xx ][ yy ] == '#') continue;
                    if ( dis [ k ][ xx ][ yy ] > dis [ k ][ x ][ y ] + (s [ x ][ y ] == '.'))
                    {
                        dis [ k ][ xx ][ yy ] = dis [ k ][ x ][ y ] + (s [ x ][ y ] == '.');
                        Q . push( make_pair( xx , yy));
                    }
                }
            }
        }
        int ans = 1e9;
        for ( int i = 0; i < n; ++ i)
        {
            for ( int j = 0; j < m; ++ j)
                ans = min( ans , dis [ 0 ][ i ][ j ] + dis [ 1 ][ i ][ j ] + dis [ 2 ][ i ][ j ] +(s [ i ][ j ] == '.'));
        }
        if ( ans >= 1e8)
            puts( "-1");
        else
            printf( "%d \n " , ans);
    }
    return 0;
}
/*

*/

 

目录
相关文章
|
Java 程序员 编译器
可能是把 Java 接口讲得最通俗的一篇文章(2)
可能是把 Java 接口讲得最通俗的一篇文章
123 0
可能是把 Java 接口讲得最通俗的一篇文章(2)
|
缓存 网络协议 关系型数据库
max_connect_errors参数
一次ERROR 1129 (HY000): Host 'xxx.xxx.xxx.xxx' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'报错,重新认识max_connect_errors参数。
2193 0
|
9月前
|
Dubbo 应用服务中间件 测试技术
流量路由技术
流量路由,顾名思义就是将具有某些属性特征的流量,路由到指定的目标。流量路由是流量治理中重要的一环,本节内容将会介绍流量路由常见的场景、流量路由技术的原理以及实现。我们可以基于流量路由标准来实现各种业务场景,如标签路由、金丝雀发布、同机房优先路由等。标签路由标签路由是按照标签为维度对目标负载进行划分,...
流量路由技术
|
前端开发 JavaScript 机器人
面向中后台复杂场景的低代码实践思路
现实中,业务场景多,迭代频繁,变化快到跟不上,规则可能由多人掌握,无法通过一个人了解全貌; 还有业务所在行业固有的复杂度和历史包袱,这些问题都会让我们感到痛苦。 除了逻辑问题,我们还关注易用性交互开发的问题。
面向中后台复杂场景的低代码实践思路
|
9月前
|
SQL 关系型数据库 MySQL
关系型数据库使用 TRUNCATE TABLE 语句
`TRUNCATE TABLE` SQL 语句快速删除表所有记录,不记录删除操作,通常比 `DELETE` 快。不触发 DELETE 触发器,可能重置自增字段,并产生较少日志。语法:`TRUNCATE TABLE 表名`。注意:不可回滚,不激活触发器,慎用,确保数据不可恢复。考虑使用 `DELETE` 当需保留触发器功能或删除特定条件的行。
185 1
|
9月前
|
算法框架/工具 Python 算法
Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(三)(3)
Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(三)
136 0
Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(三)(3)
idea查看过期时间
idea查看过期时间
1012 0
idea查看过期时间

热门文章

最新文章