UVA 10336 Rank the Languages

38 篇文章 0 订阅
12 篇文章 0 订阅

You might have noticed that English and Spanish are spoken in many areas all over the world. Now it would be nice to rank all languages according to the number of states where they are spoken.
You’re given a map which shows the states and the languages where they are spoken. Look at the following map:
ttuuttdd
ttuuttdd
uuttuudd
uuttuudd
The map is read like this: Every letter stands for a language and states are defined as connected areas with the same letter. Two letters are “connected” if one is at left, at right, above or below the other one. So in the above map, there are three states where the language “t” is spoken, three where “u” is spoken and one state where people speak “d”.
Your job is to determine the number of states for each language and print the results in a certain order.
Input
The first line contains the number of test cases N. Each test case consists of a line with two numbers H and W, which are the height and the width of the map. Then follow H lines with a string of W letters. Those letters will only be lowercase letters from ‘a’ to ‘z’.
Output
For each test case print ‘World #n’, where n is the number of the test case. After that print a line for each language that appears in the test case, which contains the language, a colon, a space and the number of states, where that language is spoken. These lines have to be ordered decreasingly by the number of states. If two languages are spoken in the same number of states, they have to appear alphabetically, which means language ‘i’ comes before language ‘q’, for example.
Sample Input
2
4 8
ttuuttdd
ttuuttdd
uuttuudd
uuttuudd
9 9
bbbbbbbbb
aaaaaaaab
bbbbbbbab
baaaaacab
bacccccab
bacbbbcab
bacccccab
baaaaaaab
bbbbbbbbb
Sample Output
World #1
t: 3
u: 3
d: 1
World #2
b: 2
a: 1 c: 1

题意:
给定一个地图,按出现次数从大到小输出图中的相连的字母块出现的次数,若次数相同,则按字母序输出

思路:
dfs循环搜索图中每一个位置,判断其上下左右四个方向的字母是否与它一致,若一致,则继续搜索新位置


#include <iostream>
#include <cstring>
using namespace std;
int dx[]={1,0,-1,0};//列出四个方向
int dy[]={0,1,0,-1};
char map[100][100];//储存数组
int kind[30];//储存每个字母块出现的次数
int m,n;
void initial(int m,int n)
{
    for(int k=0;k<m;k++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>map[k][j];
            }

        }
}
void dfs(int x,int y)
{
    int i,nx,ny;
    char ch=map[x][y];
    map[x][y]='.';//将访问过的位置标记为‘.‘
    for(i=0;i<4;i++)
    {
        nx=x+dx[i];
        ny=y+dy[i];//判断当前位置上下左右四个方向
        if(nx>=0&&nx<m&&ny>=0&&ny<n&&map[nx][ny]==ch)//若在边界之内切与ch相同
            dfs(nx,ny);
    }
    return;
}
int main()
{
    int T,c,j,k,max;
    char ch='\0';
    cin>>T;
    for(c=1;c<=T;c++)
    {
        cin>>m>>n;
        max=0;
        memset(kind,0,sizeof(kind));//每次都初始化为0
        initial(m,n);
        for(k=0;k<m;k++)
        {
            for(j=0;j<n;j++)
            {
                ch=map[k][j];//记录当前的字母
                if(ch>='a'&&ch<='z')
                {
                    dfs(k,j);//循环搜索每个位置,将连在一起的相同字母全都置为'.',表示已经访问过
                    kind[ch-'a'+0]++;//推出dfs后将该字母块出现次数+1
                }
            }
        }
        cout<<"World #"<<c<<endl;
       while(1)//循环按字母序输出最大值
       {
        for(int i=0;i<26;i++)
        {
            if(kind[i]>max)
            {
                max=kind[i];
                ch=i+'a';
            }
        }
           if(max==0)
               break;
           else
           {
           cout<<ch<<": "<<max<<endl;
               kind[ch-'a']=0;
               max=0;
           }
       }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值