空格样例HDU1241:Oil Deposits

每日一贴,今天的内容关键字为空格样例

    

Problem Description

    

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.  

    

 

    

Input

    

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.

    

 

    

Output

    

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

    

 
    每日一道理
巴尔扎克说过“不幸,是天才的进升阶梯,信徒的洗礼之水,弱者的无底深渊”。风雨过后,眼前会是鸥翔鱼游的天水一色;走出荆棘,前面就是铺满鲜花的康庄大道;登上山顶,脚下便是积翠如云的空蒙山色。 在这个世界上,一星陨落,黯淡不了星空灿烂,一花凋零,荒芜不了整个春天。人生要尽全力度过每一关,不管遇到什么困难不可轻言放弃。

    

Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
 

    

Sample Output
0 1 2 2


 

    原来第四个样例的第二个五后还有个空格,难怪输出不对劲,去掉空格就能够了,或者把%*C改成\n

#include <stdio.h>
#include <string.h>
char map[105][105],mat[105][105];
int vis[105][105];

void dfs(int i,int j)
{
    if(vis[i][j] || mat[i][j] == '*')
    return ;
    vis[i][j] = 1;
    dfs(i-1,j-1);
    dfs(i-1,j);
    dfs(i-1,j+1);
    dfs(i,j-1);
    dfs(i,j+1);
    dfs(i+1,j-1);
    dfs(i+1,j);
    dfs(i+1,j+1);
}

int main()
{
    int n,m;
    while(~scanf("%d%d%*c",&n,&m))
    {
        memset(map,0,sizeof(map));
        memset(mat,'*',sizeof(mat));
        memset(vis,0,sizeof(vis));
        int i,j,cnt = 0;
        if(!m&&!n)
        break;
        for(i = 0;i<n;i++)
        {
            for(j = 0;j<m;j++)
            {
                scanf("%c",&map[i][j]);
                mat[i+1][j+1] = map[i][j];
            }
            getchar();
        }
        for(i = 1 ;i<=n;i++)
        {
            for(j = 1;j<=m;j++)
            {
                if(!vis[i][j] && mat[i][j] == '@')
                {
                    dfs(i,j);
                    cnt++;
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

    
 

文章结束给大家分享下程序员的一些笑话语录: Borland说我很有前途,Sun笑了;Sun说我很有钱,IBM笑了;IBM说我很专业,Sybase笑了;Sybase说我数据库很牛,Oracle笑了;Oracle说我是开放的,Linux笑了;Linux说我要打败Unix,微软笑了;微软说我的系统很稳定,我们都笑了。

--------------------------------- 原创文章 By
空格和样例
---------------------------------

原文地址:https://www.cnblogs.com/jiangu66/p/3100704.html