Landlocked

Time Limit: 5 Sec Memory Limit: 128 MB

Submit: 288 Solved: 39

Description

Canada is not a landlocked country: the country touches at least one ocean (in fact, it touches three).

There are 46 countries (including Bolivia and Mongolia, for example) which are landlocked. That is, they do not touch an ocean, but by going through one other country, an ocean can be reached. For example, a person in Mongolia can get to an ocean by passing through Russia.

Liechtenstein and Uzbekistan are the only two countries in the world which are land-landlocked. That is, not only are they land-locked, but all countries which surround these two countries are land-locked countries. Thus, one would have to pass through at least two different countries when leaving Uzbekistan before arriving at an ocean.

Your task is to determine how landlocked each country is on a given map. We say that a country is not landlocked (recorded as 0) if it touches water in any adjacent cell in either a horizontal, vertical, or diagonal direction. If a country is landlocked, you must calculate the minimum number of borders that one must cross in order to travel from the country to water. Each step of such a journey must be to a cell that is adjacent in either a horizontal, vertical, or diagonal direction. Crossing a border is defined as taking a step from a cell in one country to an adjacent cell in a different country.

Note that countries may not be connected to themselves (as in a country formed of islands). In this case, the landlocked value for the country is the minimal of each connected region of the country.

Input

The first line contains N and M (1 ≤ N, M ≤ 1000).

On each of the next N lines, there are M capital letters. Each country will be represented by a unique letter, with the exception that the letter W is reserved to indicate the water in the oceans or seas that will be used to determine the how landlocked each country is.

Output

The output consists of the country letter followed by a space, followed by the landlockedness for that particular country. Output should be in alphabetical order

Sample Input

7 10

WWWWWCCDEW

WWWWCCEEEW

WTWWWCCCCW

WWFFFFFFWW

WWFAAAAFWW

WWFABCAFFW

WWFAAAAFWW

Sample Output

A 1

B 2

C 0

D 1

E 0

F 0

T 0

采用从外到内的方式,由海洋到城市,进行BFS搜索,相同的城市进行DFS搜索。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long using namespace std; const int INF = 0x3f3f3f3f; int Dir[][2]={{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; typedef struct node
{
int x,y,dis;
}Node; char str[1100][1100]; bool vis[1100][1100]; int dis[1100][1100]; int Dis[300]; int n,m; queue<Node>Q; bool Ok(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y])
{
return true;
}
return false;
} void dfs(int x,int y,int num)
{
int Fx,Fy;
Node s;
// printf("%d %d\n",x,y);
for(int i=0;i<8;i++)
{
Fx=x+Dir[i][0];
Fy=y+Dir[i][1];
if(Ok(Fx,Fy))
{
if(str[Fx][Fy]==str[x][y])
{
vis[x][y]=true;
s.x=Fx;
s.y=Fy;
s.dis=num;
Q.push(s);
dfs(Fx,Fy,num);
}
}
}
} void bfs(int x,int y,int num)
{
dis[x][y]=num; int Fx,Fy; Node s; for(int i=0;i<8;i++)
{
Fx=x+Dir[i][0]; Fy=y+Dir[i][1]; if(Ok(Fx,Fy))
{
vis[Fx][Fy]=true; if(str[Fx][Fy]!=str[x][y])
{
s.x=Fx;
s.y=Fy;
s.dis=num+1;
Q.push(s);
dfs(Fx,Fy,num+1);
}
}
}
} int main()
{
Node s;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
}
memset(vis,false,sizeof(vis)); memset(dis,INF,sizeof(dis)); memset(Dis,INF,sizeof(Dis)); for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]=='W')
{
vis[i][j]=true; bfs(i,j,-1);
}
}
}
while(!Q.empty())
{
s=Q.front();
Q.pop(); bfs(s.x,s.y,s.dis);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]!='W') Dis[(int)str[i][j]]=min(Dis[(int)str[i][j]],dis[i][j]);
}
} for(int i=0;i<300;i++)
{
if(Dis[i]!=INF)
{ printf("%c %d\n",i,Dis[i]); }
}
}
return 0;
}

浙江理工2015.12校赛-F Landlocked的更多相关文章

  1. 浙江理工2015.12校赛-A

    孙壕请一盘青岛大虾呗 Time Limit: 5 Sec Memory Limit: 128 MB Submit: 577 Solved: 244 Description 话说那一年zstu与gdut ...

  2. 浙江理工2015.12校赛-G Jug Hard

    Jug Hard Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1172 Solved: 180 Description You have two e ...

  3. 浙江理工2015.12校赛-B 七龙珠

    七龙珠 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 781 Solved: 329 Description 话说孙壕请吃了青岛大虾后,一下子变穷了,就 ...

  4. 校赛F

    问题描述 例如对于数列[1 2 3 4 5 6],排序后变为[6 1 5 2 4 3].换句话说,对于一个有序递增的序列a1, a2, a3, ……, an,排序后为an, a1, an-1, a2, ...

  5. 2015 GDUT校赛

    周末打了个GDUT的校赛,也是作为SCAU的一场个人排位. 比赛中竟然卡了个特判,1个半钟就切了5条了,然后一直卡. 还有其他两条可以做的题也没法做了,性格太执着对ACM来说也是错呀. 讲回正题 . ...

  6. 校赛F 比比谁更快(线段树)

    http://acm.cug.edu.cn/JudgeOnline/problem.php?cid=1153&pid=5 题意:给你一个字符串,各两个操作: ch=0,[l,r]降序 ch=1 ...

  7. 2017年浙江理工大学程序设计竞赛校赛 题解&源码(A.水, D. 简单贪心 ,E.数论,I 暴力)

    Problem A: 回文 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1719  Solved: 528 Description 小王想知道一个字 ...

  8. 线性期望(BUPT2015校赛.F)

    将整体期望分成部分期望来做. F. network 时间限制 3000 ms 内存限制 65536 KB 题目描述 A social network is a social structure mad ...

  9. 2015 多校赛 第三场 1002 (hdu 5317)

    Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...

随机推荐

  1. JAVA 往jar包添加class文件

    (1) jar -uf jarfile.jar yourclasses (2) 右击要打包的文件夹,选择“添加到压缩文件”,弹出对话框: 把压缩文件格式改为zip,再把压缩文件名中的反缀改为.jar, ...

  2. unity3d插件Daikon Forge GUI 中文教程3-基础控件Button和Sprite的使用

    2.2添加一个按钮Button 来看看特有的属性:Button Properties Data 显示的文本 Behavior 中的几个: Aoto Size 选中时就是按钮的背景会根据Data中的文本 ...

  3. vbox下Oracle Enterprise liunx5.4虚拟机安装10G RAC实验(四)

    接第3篇 http://www.cnblogs.com/myrunning/p/4003527.html 5.安装配置数据库 5.1安装数据库软件 5.2配置监听 5.3创建ASM磁盘 5.4创建服务 ...

  4. 【iCore3 双核心板_FPGA】例程三:GPIO输入实验——识别按键输入

    实验指导书及代码包下载: http://pan.baidu.com/s/1dEaDr37 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  5. an alternative to symmetric multiprocessing

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION 17.5 CLUSTERSAn impor ...

  6. C/C++相对论——C++中为什么要使用异常(跳转语句会造成对象没有被析构)

    C++中为什么要使用异常? 很多人也许知道C++中的异常机制,很多人也许不知道.很多人知道C中常用的assert,也知道在编译时候指定NODEBUG来忽略它. 对于C语言,使用正常的if-else即是 ...

  7. Ubuntu 14.04 升级gcc 4.8到gcc 5.x

    简介 有些软件比较新,需要更高的gcc版本,所以需要升级gcc.编译安装比较耗时,所以直接选择bin包就好. 步骤 添加源 sudo add-apt-repository ppa:ubuntu-too ...

  8. Java学习-001-JDK安装配置

    本节主要讲述在 Win7 64bit 系统下安装.配置 JDK8u25,敬请参阅.详细步骤如下: 一.JDK下载 您可到 官方网站 或 我的云盘 下载,对应的JDK8u25的安装程序,下载过程不再赘述 ...

  9. Qt字符串类——1.字符串常用的几种操作

    字符串有如下几个操作符: (1)QString提供了一个二元的"+"操作符用于组合两个字符串,并提供了一个"+="操作符用于将一个字符串追加到另一个字符串的末尾 ...

  10. zabbix监控交换机

    zabbix可以通过snmp协议监控交换机 前提: 交换机需要开启snmp协议,通过snmpwalk 可以抓取到数据就可以了 snmpwalk -v 2c -c public *.*.*.* 1.创建 ...