当前位置: 首页 > news >正文

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

 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;
}
/*

*/

 

相关文章:

  • iOS中正确的截屏姿势
  • Android Volley框架的使用(三)
  • IP工具类-自己动手做个ip解析器
  • CSS3 变换
  • 前端之React实战:创建跨平台的项目架构
  • 驱动的加载与卸载例程(C++/C)
  • 寻找
  • Apache 文件根目录设置修改方法 (Document Root)
  • UIView之【UIViewContentMode】
  • Oracle 错误总结及问题解决 ORA
  • android之intent显式,显式学习
  • Daily Scrum - 11/19
  • 标签禁用之readonly和disabled
  • 利用shell实现批量添加用户
  • 关机--小程序
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • Bootstrap JS插件Alert源码分析
  • Django 博客开发教程 16 - 统计文章阅读量
  • HTTP中GET与POST的区别 99%的错误认识
  • JavaScript 基本功--面试宝典
  • javascript 总结(常用工具类的封装)
  • Nodejs和JavaWeb协助开发
  • PHP 7 修改了什么呢 -- 2
  • PHP的类修饰符与访问修饰符
  • 搞机器学习要哪些技能
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 聊聊directory traversal attack
  • 前端工程化(Gulp、Webpack)-webpack
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 入口文件开始,分析Vue源码实现
  • 设计模式(12)迭代器模式(讲解+应用)
  • 什么软件可以剪辑音乐?
  • 推荐一个React的管理后台框架
  • 数据库巡检项
  • 通过调用文摘列表API获取文摘
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • #14vue3生成表单并跳转到外部地址的方式
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • (4) PIVOT 和 UPIVOT 的使用
  • (done) 两个矩阵 “相似” 是什么意思?
  • (NSDate) 时间 (time )比较
  • (规划)24届春招和25届暑假实习路线准备规划
  • (实战)静默dbca安装创建数据库 --参数说明+举例
  • (数据结构)顺序表的定义
  • (四)linux文件内容查看
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (转)人的集合论——移山之道
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • ******之网络***——物理***
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .Net的C#语言取月份数值对应的MonthName值
  • .NET基础篇——反射的奥妙
  • .NET序列化 serializable,反序列化