Problem Statement

     Little Bonnie has taken a vacation to Ha Long Bay. There are a few thousand stone islands in the bay, making it one of the most beautiful natural scenes in Vietnam. Bonnie is so impressed by this fact that she has decided to visit all of the islands. She bought a map of all the islands in the bay. This map is given in the String[] islandMap, which is a two-dimensional matrix where each cell is either '.' or lowercase 'x'. '.' cells represent sea and 'x' cells represent land. Two cells are connected if they have a point in common, so each cell may connect to at most 8 other cells. An island is defined as a maximal connected group of 'x' cells. A trip between two islands is defined as a connected group of '.' cells where, for each of the two islands, there is at least one '.' cell in the trip which is connected to a cell in that island. If there is no such connected group of '.' cells between two islands, then there is no trip between them. Note that an island can be nested inside another island. A tour is a sequence of islands where there is a trip between every pair of consecutive islands in the sequence. Little Bonnie wants to visit every island exactly once, and she wants to do this using the minimum possible number of tours. Return the number of tours she will have to take.
 

Definition

    
Class: MinimumTours
Method: getMinimumTours
Parameters: String[]
Returns: int
Method signature: int getMinimumTours(String[] islandMap)
(be sure your method is public)
    
 
 

Notes

- It is possible for a tour to have only one island.
- Bonnie cannot leave the mapped area at any time.
- It is assumed that Bonnie has another way to travel from the last island of one tour to the first island of another tour, so you don't have to take this into account when solving the problem.
 

Constraints

- islandMap will contain between 1 and 50 elements, inclusive.
- Each element of islandMap will contain between 1 and 50 characters, inclusive.
- Each element of islandMap will contain the same number of characters.
- Each character in islandMap will be either '.' or lowercase 'x'.
- There will be at least one island in the map.
 

Examples

0)  
    
{
"..x..x..x..",
"..x..x..x..",
"..x..x..x..",
"..x..x..x.."
}
Returns: 1
Only one tour is needed. Bonnie can just go from the leftmost island through the middle one to the rightmost island. Or she can choose to go in the reverse way.
1)  
    
{
"x....x....x",
".....x.....",
".....x.....",
".....x.....",
"xxxxxxxxxxx",
".....x.....",
".....x.....",
".....x.....",
"x....x....x"
}
Returns: 3
At least three tours are required to cover the five islands. One possible set of three tours is to go from the small island in the top-left corner through the big cross-shaped island to the top-right island, and each of the two other islands makes up a one-island tour.
2)  
    
{
"x....x....x",
".....x.....",
".....x.....",
"....x.x....",
"xxxx...xxxx",
"....x.x....",
".....x.....",
".....x.....",
"x....x....x"
}
Returns: 1
There is always a trip between any two islands. So only one tour is enough to visit all five islands.
3)  
    
{
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"x............................x",
"x..xxxxxxxxxx....xxxxxxxxxx..x",
"x..x........x....x........x..x",
"x..x..xxxx..x....x.xxxxxx.x..x",
"x..x........x....x.x....x.x..x",
"x..xxxxxxxxxx....x.x.x..x.x..x",
"x................x.x....x.x..x",
"x................x.xxxxxx.x..x",
"x..xxxxxxxxxx....x........x..x",
"x..x........x....x........x..x",
"x..x..xxxx..x....x.xxxxxx.x..x",
"x..x........x....x........x..x",
"x..xxxxxxxxxx....xxxxxxxxxx..x",
"x............................x",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Returns: 2
An island can be nested inside another one. And two tours are needed in this example.
4)  
    
{
"xxxxxxxxxxxxx....xxxxxxxxxxxxxxxxxxxxxx",
"x...........x....x.....................",
"x.xxxxxxxxx.x....x.xxxxxxxxxxxxxxxxxxx.",
"x.x.......x.x....x.x.................x.",
"x.x.xxxxx.x.x....x.x.xxxxxxxxxxx.....x.",
"x.x...x...x.x....x.x.x..........x....x.",
"x.x...x...x.x....x.x.x.xxx..xxx...x..x.",
"x.x.......x.x....x.x.x.x....x..x..x..x.",
"x.xxxxxxxxx.x....x.x.x.xxx..x..x..x..x.",
"x...........x....x.x.x.x....x..x..x..x.",
"xxxxxxxxxxxxx....x.x.x.xxx..xxx...x..x.",
"x................x.x.x......xx....x..x.",
"x................x.x.x......x.x...x..x.",
"x................x.x.x......x..x..x..x.",
"x................x.x.x...........x...x.",
"x................x.x.xxxxxxxxxxxx....x.",
"x................x.x.................x.",
"x................x.xxxxxxxxxxxxxxxxxxx.",
"x................x.....................",
"x................xxxxxxxxxxxxxxxxxxxxxx"
}
Returns: 1

传送门

代码

//将每一片海和岛看做一个点,构图发现这是一棵树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int dx[]={-1,-1,-1,0,0,1,1,1};
    const int dy[]={-1,0,1,-1,1,-1,0,1};
    string s[60];
    int a[1000][1000];
    int n,m,cnt,wt,ans;
    int head[100000];
    bool used[3000][3000],is[100000],gone[100000];
    struct edge{
          int next,to;
    }e[100000];
class MinimumTours
{
public:
    //TC的格式
    void ff(int x,int y,int col,int k){
          if(a[x][y]!=k)return;
          a[x][y]=col;
          for(int i=0;i<8;i++)
             ff(x+dx[i],y+dy[i],col,k);
    }
    void add(int u,int v){
          e[++wt].next=head[u];
          head[u]=wt;
          e[wt].to=v;
          e[++wt].next=head[v];
          head[v]=wt;
          e[wt].to=u;
    }
    int work(int v){
          /*返回值含义:
            0--对答案无贡献
            1--有一条链,可向上走
            2--是一个单点
          */
          int i,k,cnt1=0,cnt2=0;
          gone[v]=1;
          for(i=head[v];i;i=e[i].next){
               k=e[i].to;
             if(!gone[k]){
                 k=work(k);
                 if(k==1)cnt1++;
                 if(k==2)cnt2++;
             }
          }
          if(is[v]){
              if(cnt1){
                  ans+=cnt1-1;
                  if(cnt1==1)return 1;
                    else return 0;
              }
              else {
                  if(cnt2)return 0;
                  return 2;
              }
          }
          else {
              if(cnt1==0)return cnt2>0;
              ans+=cnt1/2;
              return ((cnt1&1)?1:2);
          }
    }
    int getMinimumTours(vector<string>islandMap)
    {     //freopen("1.in","r",stdin);
          int i,j,k;
          cnt=0;wt=0;ans=0;
          memset(head,0,sizeof(head));
          n=islandMap.size();
          m=islandMap[0].size();
          for(i=0;i<n;i++)
             for(j=0;j<m;j++)
                a[i][j]=(islandMap[i][j]=='.'?-2:-1);
          memset(used,0,sizeof(used));
          memset(is,0,sizeof(is));
          //构图
          for(i=0;i<n;i++)
             for(j=0;j<m;j++)
                if(a[i][j]==-1){
                  ff(i,j,++cnt,-1);
                  is[cnt]=1;
                }   
          for(i=0;i<n;i++)
             for(j=0;j<m;j++)
                if(a[i][j]==-2)ff(i,j,++cnt,-2);
          for(i=0;i<n;i++)
             for(j=0;j<m;j++)
                for(k=0;k<8;k++){
                    int x1=i,x2=i+dx[k],y1=j,y2=j+dy[k];
                    int p=a[x1][y1],q=a[x2][y2];
                    if(p!=q&&q>0&&!used[p][q]){
                        used[p][q]=used[q][p]=1;
                        add(p,q);
                    }
                }
          memset(gone,0,sizeof(gone));
          if(work(1)&&is[1])ans+=1;
          return ans;
    }
};

MinimumTours TopCoder - 7620的更多相关文章

  1. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  2. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  3. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  4. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  5. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  6. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  9. Topcoder Arena插件配置和训练指南

    一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...

随机推荐

  1. [原创]java WEB学习笔记31:会话与状态管理 session机制 概述(定义,session机制,session的声明周期,保存session的方式,Session的创建与删除)

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  2. mysql管理工具之pt-heartbeat

    之前我一直用Seconds_behind_master来衡量主从的延迟,今天看到文档,才觉得多么不可靠!以下是官方文档的描述: In essence, this field measures the ...

  3. pt-table-checksum检验主从数据不一致

    测试环境:主从架构,操作系统liunx 运行pt-table-checksum需要先安装以下依赖包: yum install perl-IO-Socket-SSL perl-DBD-MySQL per ...

  4. Spark Structured Streaming框架(1)之基本用法

     Spark Struntured Streaming是Spark 2.1.0版本后新增加的流计算引擎,本博将通过几篇博文详细介绍这个框架.这篇是介绍Spark Structured Streamin ...

  5. hd acm1425

    给你n个整数,请按从大到小的顺序输出其中前m大的数. 先看代码: #include<stdio.h>#include<string.h>#define MAX 1100000i ...

  6. 大话设计模式--原型模式 Prototype -- C++实现

    1. 原型模式: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象... 注意: 拷贝的时候是浅拷贝 还是 深拷贝, 来考虑是否需要重写拷贝构造函数. 关键在于: virtual Pro ...

  7. BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊:分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意: 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆 ...

  8. .dhpcd导致cpu飙升问题

    因公司有业务服务器在阿里云上面,阿里云后台报警说,“有恶意程序在挖矿”,引起了高度重视,于是我登陆服务器进行排查. 登陆云服务器:系统centos7.5 第一步使用top查看资源情况. top 可以清 ...

  9. python习题-判断输入字符串是不是小数类型

    写一个能判断输入的字符串是不是个小数类型的1,判断小数点的个数是否为1 count2,判断是否小数右边是整数 isdigit3,判断小数点左边的1,整数 isdigit ,2如果是负整数,取负号右边, ...

  10. hadoop_学习_01_入门准备

    一.入门准备 1.零基础学习Hadoop 2.大数据初学者应该知道的知识