ZCMU—1895

来源:互联网 发布:编程语言 薪资 编辑:程序博客网 时间:2024/06/07 06:52

1895: Landlocked

Time Limit: 5 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]

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

【分析】

题意:给出一个地图"W'是海洋,其他字符都是国家,问你每个国家想要到达海洋最少需要经过多少国家。
看题意就猜的到是最短路...只不过这个最短路有点...考思路吧算是..
首先,可以反过来想,国家到达海洋,反过来就是海洋到达国家,因为我们如果从国家出发,那其实很难找起点...但是从海洋出发就简单了,起点全是“W”。
因为每个国家是一个联通块...所以这里需要一个dfs,剩下的就是单纯的bfs了..以所有单独的海洋块为起点开始展开,碰到新的国家就距离+1并且入队,否则就直接dfs搜索遍整个当前国家
【代码】
#include<iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#define INF 0x3f3f3f3f  using namespace std;  char a[1010][1010];  int vis[1010][1010];  int value[1010][1010];  int fx[8][2]={{1, 0},              {-1, 0},              {0, 1},               {0, -1},               {1, 1},               {1, -1},               {-1, 1},               {-1, -1}};  struct xx{    int x,y,v;}t;int deep,n,m;char now;queue<xx>q[100];  void find(int x,int y,int val)  {      vis[x][y]=1;      value[x][y]=val;      for(int i=0;i<8;i++)      {          int xx=x+fx[i][0];          int yy=y+fx[i][1];          if(xx<0||yy<0||xx>=n||yy>=m||vis[xx][yy]==1) continue;          if(a[xx][yy]==now)              find(xx,yy,val);        else        {              t.x=xx;t.y=yy;t.v=val+1;            q[deep].push(t);          }     }}  int f[30];int main()  {      while(~scanf("%d%d",&n,&m))      {          memset(vis,0,sizeof(vis));            for(int i=0;i<n;i++) scanf("%s",a[i]);          for(int i=0;i<n;i++)              for(int j=0;j<m;j++)                  if(a[i][j]=='W')                  {                      t.x=i;t.y=j;t.v=-1;                    q[0].push(t);                  }          memset(value,-1,sizeof(value));        for(int i=0;!q[i].empty();i++)          {              while(!q[i].empty())              {                  xx tt=q[i].front();                q[i].pop();                if(vis[tt.x][tt.y]==1)  continue;                  deep=i;                now=a[tt.x][tt.y];                find(tt.x,tt.y,tt.v);              }          }          memset(f,INF,sizeof(f));        for(int i=0;i<n;i++)              for(int j=0;j<m;j++)                  f[a[i][j]-65]=min(f[a[i][j]-65],value[i][j]);          for(int i=0;i<30;i++)              if (f[i]!=INF && i!=22)                printf("%c %d\n",i+65,f[i]);      }  }  


0 0
原创粉丝点击