编程介的小学生 2019-08-28 22:51 采纳率: 20.5%
浏览 188

用C语言,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

  • 写回答

1条回答 默认 最新

  • 蹄法芬芳潘大仙 2019-11-08 16:28
    关注

    #include
    int m,n;
    char map[120][120];
    int fangxiang[8][2]= {{-1,-1},{-1,0},
    {-1,1},{0,1},{0,-1},{1,1},{1,0},{1,-1}
    };

    void dfs(int x,int y) {
    int i;
    map[x][y]='*';
    for(i=0; i int sex=x+fangxiang[i][0];
    int sey=y+fangxiang[i][1];
    if(sex=m||sey=n) {
    continue;
    }
    if(map[sex][sey]=='@')
    dfs(sex,sey);

    }
    

    }

    int main() {
    int i,j;
    while(~scanf("%d %d",&m,&n)) {
    int sum=0;
    if (m==0&&n==0) {
    break;
    }
    for(i=0; i<m; i++) {
    scanf("%s",map[i]);
    }
    for(i=0; i<m; i++) {
    for(j=0; j<n; j++) {
    if(map[i][j]=='@') {
    sum++;
    dfs(i,j);
    }

            }
        }
        printf("%d\n",sum);
    
    }
    
    
    
    return 0;
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)