UVA10336 Rank the Languages(DFS)





Problem A

Rank the Languages

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

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.

Problem

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,只是先从(0,0)这个位置开始遍历,并且对此语言计数器加一,没有访问过的就进入DFS,然后把四周字母相同的置为访问,否则什么也不做。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>

using namespace std;

struct node{
    char lang;
    int cnt;
    node(char lang0 ='0',int cnt0=0){lang=lang0,cnt=cnt0;}
}langNumber[26];

int n,m;
bool visited[1000][1000];
string mymap[1000];

bool cmp(node a,node b){
    if(a.cnt!=b.cnt) return a.cnt>b.cnt;
    else return a.lang<b.lang;
}

void initial(){
     memset(visited,false,sizeof(visited));
     for(int i=0;i<26;i++){
        langNumber[i].lang=i+'a';
        langNumber[i].cnt=0;
     }
}

void input(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) cin>>mymap[i];
}

void dfs(char c,int pi,int pj){
    if (!visited[pi][pj]&&pi>=0&&pi<n&&pj>=0&&pj<m){
        if (mymap[pi][pj] == c){
            visited[pi][pj] = true;
            dfs(c, pi + 1, pj);
            dfs(c, pi - 1, pj);
            dfs(c, pi, pj + 1);
            dfs(c, pi, pj - 1);
        }
    }
}

void output(){
    sort(langNumber,langNumber+26,cmp);
    for(int i=0;i<26;i++){
        if(langNumber[i].cnt>0)
            printf("%c: %d\n",langNumber[i].lang,langNumber[i].cnt);
    }
}

int main(){
    int t,casen=0;
    scanf("%d",&t);
    while(t--){
        initial();
        input();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(!visited[i][j]){
                    langNumber[mymap[i][j]-'a'].cnt++;
                    dfs(mymap[i][j],i,j);
                }
            }
        }
        printf("World #%d\n",++casen);
        output();
    }
    return 0;
}




Problem A

Rank the Languages

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

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.

Problem

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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值