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. ArcGIS Javascript 异常之No 'Access-Control-Allow-Origin' header

    本文只描述现象与处理措施,不讨论原理. 开发过程中遇到此异常,查询后网上说是跨域访问的问题,给出的解决方案是通过JQuery的跨域访问机制来解决, 难道我需要直接找ArcGISTiledMapServ ...

  2. BizTalk开发系列(四) 深入Map测试

    在BizTalk的开发过程中XML消息间的映射是一个很重要的内容.如果只是一般的从源节点的值复制到目标节点的话,BizTalk项目提供的 MAP测试和验证就已经可以满足需求了.但是很多时候需要在映射的 ...

  3. 关闭SSMS的事务自动提交,改为手动提交

    SQLServer 2005-2008-2012使用Oracle时,默认是手动提交.而SQLServer2005中,默认是自动提交,但是SQLServer支持配置. 方法: 用SSMS连接到SQL S ...

  4. IOS第二天多线程-01-延时执行

    **********延时执行 #import "HMViewController.h" @interface HMViewController () @end @implement ...

  5. [IT新应用]如何用好搜索引擎学习英语

    用谷歌可以学习英语,用必应也可以的. 输入如下地址:global.bing.com,如果是中文界面,就单击顶部右侧“Switch to Bing in English”. 这个界面有很多英文原版的时事 ...

  6. Memcached 笔记与总结(8)Memcached 的普通哈希分布算法和一致性哈希分布算法命中率对比

    准备工作: ① 配置文件 config.php ② 封装 Memcached 类 hash.class.php,包含普通哈希算法(取模)和一致性哈希算法 ③ 初始化 Memcached 节点信息 in ...

  7. 奥迪--A4L

    -型号:A4L -价格:30-41W -动力:2T -变速箱:7挡双离合 -长宽高:4.81,1.84,1.43 -油箱:54L -发动机:EA888 -大灯:氙气(选装LED)

  8. ngModel

    https://docs.angularjs.org/error/ngModel/numfmt?p0=sa angular.module('myApp', []) .directive('tagLis ...

  9. change和onchange触发为什么不立马生效?

    change和onchange触发了,为什么不立马生效?那是因为他们本身不是当文本改变就立马触发的事件,而是当文本改变了,blur离开了表单才触发. 如果要加上触发请结合keyup,keydown,o ...

  10. Android 线程的正确使用姿势

    进程优先级(Process Priority) 线程寄宿在进程当中,线程的生命周期直接被进程所影响,而进程的存活又和其优先级直接相关.在处理进程优先级的时候,大部分人靠直觉都能知道前台进程(Foreg ...