Problem UVA1103-Ancient Messages
Accept: 1176 Submit: 6103
Time Limit: 3000 mSec
Problem Description
Input
Output
Sample Input
100 25 0000000000000000000000000
0000000000000000000000000
...(50 lines omitted)...
00001fe0000000000007c0000
00003fe0000000000007c0000
...(44 lines omitted)...
0000000000000000000000000
0000000000000000000000000
150 38
00000000000000000000000000000000000000
00000000000000000000000000000000000000
...(75 lines omitted)...
0000000003fffffffffffffffff00000000000
0000000003fffffffffffffffff00000000000
...(69 lines omitted)...
00000000000000000000000000000000000000
00000000000000000000000000000000000000
0 0
Sample output
Case 1: AKW
Case 2: AAAAA
题解:这个题重在脑洞,看出来联通块的个数与字母之间的关系的确不容易(我是没看出来)。开了脑洞之后问题就简单多了,先对最外面的白色区域dfs一下,然后就是dfs各个黑色区域,先把他们包围的联通块的范围划出来,然后计数里面的联通块个数,说的时候这划区域与计数是分开的,其实实现的时候是在一起的,划区域的时候如果碰到了白格,就停下来先对这个白格所在的联通块进行dfs,顺便记个数,区域划完了,计数也就搞定了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; const int maxn = +,maxm = +;
const int converse[][] =
{
{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,}
}; char gra[maxn][maxm];
int buf[maxn][maxm<<];
int n,m,cnt;
int dir[][] = {{,},{,},{-,},{,-}}; inline bool Judge(int x,int y){
if(<=x && <=y && x<n && y<m) return true;
else return false;
} void dfsw(int x,int y,int num){
buf[x][y] = num;
int xx,yy;
for(int i = ;i < ;i++){
xx = x+dir[i][],yy = y+dir[i][];
if(Judge(xx,yy) && buf[xx][yy] == ){
dfsw(xx,yy,num);
}
}
} void dfsn(int x,int y,int num){
buf[x][y] = num;
int xx,yy;
for(int i = ;i < ;i++){
xx = x+dir[i][],yy = y+dir[i][];
if(Judge(xx,yy)){
if(buf[xx][yy] == ) dfsn(xx,yy,num);
else if(buf[xx][yy] == ){
cnt++;
dfsw(xx,yy,-);
}
}
}
} void map_print(){
for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
printf("%d ",buf[i][j]);
}
printf("\n");
}
} int sum[];
char q[] = {'W','A','K','J','S','D'};
int iCase = ; int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%d%d",&n,&m) && (n||m)){
memset(buf,,sizeof(buf));
for(int i = ;i <= n;i++){
scanf("%s",gra[i]+);
for(int j = ;j <= (int)strlen(gra[i]+);j++){
int y = (j-)<<;
if('a'<=gra[i][j] && gra[i][j]<='z'){
for(int k = ;k < ;k++){
buf[i][y+k+] = converse[gra[i][j]-'a'+][k];
}
}
else{
for(int k = ;k < ;k++){
buf[i][y+k+] = converse[gra[i][j]-''][k];
}
}
}
}
n = n+,m = *m+;
//map_print();
dfsw(,,-);
int flag = ;
memset(sum,,sizeof(sum));
for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
if(buf[i][j] == ){
cnt = ;
dfsn(i,j,flag);
flag++;
sum[cnt]++;
}
}
}
//map_print();
printf("Case %d: ",iCase++);
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'A');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'D');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'J');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'K');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'S');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'W');
printf("\n");
}
return ;
}