hdu 1241 Oil Deposits,hdudeposits

來源:互聯網
上載者:User

hdu 1241 Oil Deposits,hdudeposits

題意:用廣度優先搜尋


//c++寫輸入時有問題

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>/*map數組是用來裝字元的n,m提高範圍,使訪問的許可權變高dir方便廣度優先搜尋,因為要搜尋8個方向,這樣做最方便*/char map[101][101];int n,m;int dir[8][2]={{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};int main(){    void breadthFirstSearch(int x,int y);    bool isWithinMap(int x,int y);    while(scanf("%d%d",&m,&n)!=EOF,n+m){        /*        m是行指標        n是列指標        */        getchar();        for(int i=0;i<m;i++){            scanf("%s",map[i]);        }        int count=0;        for(int i=0;i<m;i++){            for(int j=0;j<n;j++){                if(map[i][j]=='@'){                    /*                    擦掉當前點                    */                    breadthFirstSearch(i,j);                    count++;                }            }        }        printf("%d\n",count);    }    return 0;}/*px-pointxpy-pointybreadthFirstSearch廣搜優先搜尋*/void breadthFirstSearch(int x,int y){    bool isWithinMap(int x,int y);    int px,py;    for(int i=0;i<8;i++){        px = x+dir[i][0];        py = y+dir[i][1];        if(isWithinMap(px,py)&&map[px][py]=='@'){            /*            1)擦掉原來的@            2)廣搜該點            */            map[px][py] = '*';            breadthFirstSearch(px,py);        }    }}bool isWithinMap(int x,int y){    if(x<0||x>=m||y<0||y>=n){        return false;    }    return true;}






Oil Deposits石油儲量
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18046    Accepted Submission(s): 10399


Problem DescriptionThe GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp地質調查公司負責檢測地下石油資源儲量。
GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. GeoSurvComp每次在一個大矩形地區中工作,建立一個網格,將土地劃分為許多方形耕地。
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.
 你的工作是確定有多少不同的油田被包含在一個網格。
InputThe 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. 每一個網格的第一行,分別是m和n,表示網格是m行n列,每兩個數字用一個空格隔開。If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. 如果m=0著輸入結束否著1 <= m <= 100 and 1 <= n <= 100。Following this are m lines of n characters each (not counting the end-of-line characters). 每一行有n個字元(不包括斷行符號符);Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 每一個字元都要相對應的油田,“*”表示沒有油井,“@”表示一個油井。
OutputFor 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.
 一塊油田最多包含100塊油井。
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0 
 
Sample Output
0122
 
SourceMid-Central USA 1997 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.