现在的位置: 首页 > 综合 > 正文

[简单图论]「微软面试」找联通子图

2018年04月12日 ⁄ 综合 ⁄ 共 2928字 ⁄ 字号 评论关闭

从一个无向图里找出各个联通子图,每个子图作为一组。
如何测试。


Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4498    Accepted Submission(s): 1951

Problem Description
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes,
which is marked from A to K, as Figure 1 shows.



Figure 1

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map 

ADC
FJK
IHE

then the water pipes are distributed like 



Figure 2

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn. 

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him? 

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.

 

Input
There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the
corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
 

Output
For each test case, output in one line the least number of wellsprings needed.
 

Sample Input
2 2 DK HF 3 3 ADC FJK IHE -1 -1
 

Sample Output
2 3
 

Author
ZHENG, Lu
 

Source
 

Recommend
Ignatius.L.
就如标题说的,这题就是找出图中所有联通子图,做法和那个找油田的相似。对每个顶点做次dfs就行了,做过后在标记上。开始没有想到用ac数组标记当前不能访问位置,只标记*,结果错了。还有就是那个字符的处理,和方向有关的,这个细心点,我没什么好办法,就是手打……
复制代码
#include<stdio.h>
#include<string.h>
#define MAX 55
char map[MAX][MAX];
int ac[MAX][MAX];
int dr[4]={-1,1,0,0};
int dc[4]={0,0,-1,1};
int N,M;
int ans;
int inmap(int x,int y)
{
    if(x<0||y<0||x>=N||y>=M) return 0;
    return 1;
}
int cango(int now_x,int now_y,int dirct)
{
    int x,y;
    char ch=map[now_x][now_y];
    char next;
    x=now_x+dr[dirct];
    y=now_y+dc[dirct];
    next=map[x][y];
    if(dirct==0)
    {
        if(ch=='C'||ch=='D'||ch=='F'||ch=='I') return 0;
        if(next=='A'||next=='B'||next=='F'||next=='G') return 0;
        else return 1;
    }
    else if(dirct==1)
    {
        if(ch=='A'||ch=='B'||ch=='F'||ch=='G') return 0;
        if(next=='C'||next=='D'||next=='F'||next=='I') return 0;
        else return 1;
    }
    else if(dirct==2)
    {
        if(ch=='B'||ch=='D'||ch=='E'||ch=='J') return 0;
        if(next=='A'||next=='C'||next=='E'||next=='H') return 0;
        else return 1;
    }
    else if(dirct==3)
    {
        if(ch=='A'||ch=='C'||ch=='E'||ch=='H') return 0;
        if(next=='B'||next=='D'||next=='E'||next=='J') return 0;
        else return 1;
    }
}
int dfs(int now_x,int now_y)
{
    int i;
    if(map[now_x][now_y]=='*') return 0;
    for(i=0;i<4;i++)
    {
        int x=now_x+dr[i];
        int y=now_y+dc[i];
        if(!inmap(x,y)||map[x][y]=='*'||ac[x][y]==1) continue;
        if(!cango(now_x,now_y,i)) continue;
        ac[now_x][now_y]=1;
        dfs(x,y);
        ac[now_x][now_y]=0;
    }
    map[now_x][now_y]='*';
    return 1;
}
int main()
{
    scanf("%d%d",&N,&M);
    while(scanf("%d%d",&N,&M),N>=0&&M>=0)
    {
        int i,j;
        ans=0;
        memset(ac,0,sizeof(ac));
        for(i=0;i<N;i++)
            scanf("%s",map[i]);
        for(i=0;i<N;i++)
            for(j=0;j<M;j++)
                ans+=dfs(i,j);
        printf("%d\n",ans);
    }
    return 0;
}
复制代码

分类: ACM图论

抱歉!评论已关闭.