Ancient Messages UVA - 1103(dfs求联通块)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/86750508

把整个地图在最外面再加一圈这样外面的空白就变成了联通的了然后就能用求联通块的方法求

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

char map[222][111];
int map1[222<<2][111<<2];
int color[222<<2][111<<2];
char d[20][5]={"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"};
int mx[4]={1,0,-1,0};
int my[4]={0,1,0,-1};
char code[] = "WAKJSD";
int h,w;
void dfs(int x,int y,int cont,int t)
{
    if(x<0||x>=h||y<0||y>w||color[x][y]!=0||map1[x][y]!=t)
        return ;
    color[x][y]=cont;
    for(int i=0;i<4;i++)
    {
        dfs(x+mx[i],y+my[i],cont,t);
    }
}
int main()
{
    int kase=1;
    while(scanf("%d%d",&h,&w)==2&&h&&w)
    {
        for(int i=0;i<h;i++)
            scanf("%s",map[i]);
        memset(map1,0,sizeof(map1));
        memset(color,0,sizeof(color));
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                int t;
                if(map[i][j]>='0'&&map[i][j]<='9')
                    t=map[i][j]-'0';
                else
                    t=map[i][j]-'a'+10;
                for(int k=0;k<4;k++)
                    map1[i+1][1+j*4+k]=d[t][k]-'0';
            }
        }
        h+=2;
        w=w*4+1;
        // for(int i=1;i<=h;i++)
        // {
        //     for(int j=1;j<=w;j++)
        //     {
        //         cout<<map1[i][j];
        //     }
        //     cout<<endl;
        // }
        vector<int> vt1;
        int cont=0;
        for(int i=1;i<h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                if(!color[i][j])
                {
                    dfs(i,j,++cont,map1[i][j]);
                    if(map1[i][j])
                    {
                        vt1.push_back(cont);
                    }
                }
            }
        }
        vector<set<int>> vt2(cont+1);
        for(int i=1;i<h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                if(map1[i][j])
                {
                    int x,y;
                    for(int k=0;k<4;k++)
                    {
                        x=i+mx[k],y=j+my[k];
                        if(x>=1&&x<h&&y>=1&&y<=w&&color[x][y]!=1&&map1[x][y]==0)
                            vt2[color[i][j]].insert(color[x][y]);
                    }
                }
            }
        }
        vector<char> ans;
        for(int i=0;i<vt1.size();i++)
        {
            ans.push_back(code[vt2[vt1[i]].size()]);
        }
        sort(ans.begin(),ans.end());
        printf("Case %d: ",kase++);
        for(int i=0;i<ans.size();i++)
        {
            cout<<ans[i];
        }
        cout<<endl;
        // for(int i=1;i<=h;i++)
        // {
        //     for(int j=1;j<=w;j++)
        //     {
        //         cout<<color[i][j];
        //     }
        //     cout<<endl;
        // }
    }   
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/86750508