ZSTUOJ 4215 Landlocked
来源:互联网 发布:mac虚拟机win10分辨率 编辑:程序博客网 时间:2024/05/18 00:40
4215: Landlocked
Time Limit: 5 Sec Memory Limit:128 MBSubmit: 276 Solved:35
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 WWWWWCCDEWWWWWCCEEEWWTWWWCCCCWWWFFFFFFWWWWFAAAAFWWWWFABCAFFWWWFAAAAFWW
Sample Output
A 1B 2C 0D 1E 0F 0T 0
HINT
题意:给出一个图你,让你判断每一个国家到海的距离,一个格子为一个单位距离。搜索的时候要从上下左右+对角线,这8个方向搜索。
#include<bits/stdc++.h>using namespace std;#define maxn 1005char ma[maxn][maxn];int vis[maxn][maxn];int d[maxn][maxn];int dd[30];int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};int n,m;queue<int>q[10];int ok(int x,int y){ if(x<0||y<0||x>=n||y>=m||vis[x][y]==1) return 0; return 1;}void dfs(int x,int y,char ch,int val,int p){ vis[x][y]=1; d[x][y]=val; for(int i=0; i<8; i++) { int tx=x+dir[i][0]; int ty=y+dir[i][1]; if(ok(tx,ty)==0) continue; if(ma[tx][ty]!=ch) { q[p].push(tx); q[p].push(ty); q[p].push(val+1); } else { dfs(tx,ty,ch,val,p); } }}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,0,sizeof(vis)); memset(d,-1,sizeof(d)); memset(dd,0x3f3f3f3f,sizeof(dd)); for(int i=0; i<n; i++) { scanf("%s",ma[i]); } for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(ma[i][j]=='W') { q[0].push(i); q[0].push(j); q[0].push(-1); } } } for(int i=0; !q[i].empty(); i++) { while(!q[i].empty()) { int x=q[i].front(); q[i].pop(); int y=q[i].front(); q[i].pop(); int val=q[i].front(); q[i].pop(); if(vis[x][y]==1) continue; dfs(x,y,ma[x][y],val,i); } } /*for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { printf("%d ",d[i][j]); } printf("\n"); }*/ for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { dd[ma[i][j]-'A']=min(dd[ma[i][j]-'A'],d[i][j]); } } for(int i=0;i<30;i++) { if(dd[i]==0x3f3f3f3f) continue; if('W'-'A'==i) continue; printf("%c %d\n",i+'A',dd[i]); } }}
- ZSTUOJ 4215 Landlocked
- 【ZCMU1895】Landlocked(最短路)
- zstuoj 4355
- String Game【ZSTUOJ--4212】
- Jug Hard【ZSTUOJ--4216】
- ZSTUOJ 4212 String Game
- zstuoj 4243 牛吃草
- zstuoj 4246 萌新吃果果
- zstuoj 4243 牛吃草
- zstuoj 4274 约素
- zstuoj 4269 买iphone
- ZSTUOJ 4266 回文
- zstuoj 4270: 同源数
- ZSTUOJ 4273: 玩具
- ZSTUOJ 4270: 同源数
- ZSTUOJ 4362:不要方
- 浙江理工2015.12校赛-F Landlocked
- zstuoj 2853 算术表达式 模拟
- Redhat 6.5 安装Oracle11g
- C语言总结
- description方法到底什么时候会被调用以及description用法
- Gitlab与ssh登陆的恩怨情仇
- 03.(多线程与并发)面试题-02--Volidate的原理和指令重排序
- ZSTUOJ 4215 Landlocked
- C++单例模式实现及可能的问题
- 内存泄露从入门到精通三部曲之排查方法篇
- 华为OJ题目(十):称砝码
- python知识-函数式编程
- 如何improve一个有着复杂策略的已有项目
- 解决android sdk 无法更新
- 段错误之memset对类对象的误用
- 设计模式:单一职责原则