浙江理工2015.12校赛-F Landlocked
来源:互联网 发布:max饰品数据更新中 编辑:程序博客网 时间:2024/04/29 23:18
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 longusing 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
- 浙江理工2015.12校赛-A
- 浙江理工2015.12校赛-B 七龙珠
- 浙江理工2015.12校赛-G Jug Hard
- 【ZSTU4215 2015年12月浙理工校赛 F】【Dijkstra最短路】Landlocked 到达海洋最少跨越国家数
- 浙江理工3881
- 浙江理工3882
- 浙江理工校赛
- 浙江理工邀请赛 后感
- 2016-浙江理工新生赛-部分题解
- 极度需要RP __ 浙江理工邀请赛 总结
- 浙江理工ZSTU 2853 计算表达式的值
- 2015浙江理工12月比赛A题
- 2015浙江理工校赛B 七龙珠 (水题)
- 2015浙江理工校赛C String Game
- 浙江理工大学校赛 JugHard 4216(gcd)
- 2015浙江理工校赛 E Power Eggs (DP)
- 2017浙江理工大学校赛D题
- __attribute__
- nodejs npm 安装报错 无法安装 behind a proxy
- 从啥也不会开始学习Java-----第四篇
- 获取plist文件信息
- Android RadioButton与ListView的混合使用
- 浙江理工2015.12校赛-F Landlocked
- React native编程风格
- iOS中定时器和RunLoop的设定
- 震动活塞式柱状取样器的使用
- QT qthread详解
- virtualbox实现linux双网卡通信
- 虚拟机上ubuntu10.04上网设置
- C++小题(一)
- [Android][Fatal Exception] android.content.res.Resources$NotFoundException 问题及解决方法