1. 程式人生 > >hdu 1241 Oil Deposits

hdu 1241 Oil Deposits

ott 由於 class parent mission desc 數字 tint 字符

題意:用廣度優先搜索


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


1)這個是深搜

#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-pointx
py-pointy
breadthFirstSearch廣搜優先搜索
*/
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;
}


2)java廣搜

import java.util.Scanner;

/*
 * 使用廣搜實現
 */
public class p1241 {
	/*
	 * 相當於全局變量,降低內存的使用
	 */
	static Plot[][] plots = new Plot[101][101];
	static Queue queue = new Queue();
	static int dir[][] = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 },
			{ 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 } };
	static int m = 0;
	static int n = 0;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int count = 0;
		while (sc.hasNext()) {
			/*
			 * m控制行
			 * n控制列
			 */
			m = sc.nextInt();
			n = sc.nextInt();
			if (m + n == 0) {
				return;
			}
			for (int i = 0; i < m; i++) {
				String str = sc.next();
				for (int j = 0; j < n; j++) {
					plots[i][j] = new Plot(i, j);
					plots[i][j].ch = str.charAt(j);
				}
			}
			count = 0;
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					if (plots[i][j].ch == '@') {
						/*
						 * 通過廣搜把相連的油田改變成"*"
						 * 這樣就能夠降低訪問次數了
						 */
						plots[i][j].ch = '*';
						queue.add(plots[i][j]);
						breadthFirthSearch();
						count++;
					}
				}
			}
			System.out.println(count);
		}
	}

	/*
	 * 
	 */
	private static void breadthFirthSearch() {
		Plot plot = null;
		int px = 0;
		int py = 0;
		while (!queue.isEmpty()) {
			plot = queue.pop();
			for (int i = 0; i < 8; i++) {
				px = plot.x + dir[i][1];
				py = plot.y + dir[i][0];
				if (isWithinMap(px, py) && plots[px][py].ch == '@') {
					plots[px][py].ch = '*';
					queue.add(plots[px][py]);
				}
			}
		}
	}

	/*
	 * x表示行值
	 * y表示列值
	 */
	private static boolean isWithinMap(int x, int y) {
		if (x < 0 || y < 0 || x >= m || y >= n) {
			return false;
		}
		return true;
	}

}

/*
 * plot油井
 */
class Plot {
	/*
	 * 這道題思想簡單化,沒有寫visited,parent
	 * 你懂得。不用的變量不寫才是高效的。
	 */
	char ch;
	int x;
	int y;

	public Plot(int x, int y) {
		this.x = x;
		this.y = y;
	}

}

/*
 * 建立自己的隊列
 */
class Queue {
	final int FRONT = 0;
	Plot[] plots;
	int end;

	/*
	 * 最多包括100塊油井
	 */
	public Queue() {
		plots = new Plot[101];
		end = 0;
	}

	/*
	 * 入棧函數
	 */
	public void add(Plot plot) {
		plots[end] = plot;
		end++;
	}

	/*
	 * 出棧函數 
	 */
	public Plot pop() {
		if (end <= 0) {
			return null;
		}
		Plot plot = plots[FRONT];
		for (int i = 0; i < end; i++) {
			plots[i] = plots[i + 1];
		}
		end--;
		return plot;
	}

	/*
	 * 推斷棧是否為空
	 */
	public boolean isEmpty() {
		if (end >= 1) {
			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 Description The 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.
你的工作是確定有多少不同的油田被包括在一個網格。


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. 每個網格的第一行,各自是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 [email protected], representing an oil pocket.
每個字符都要相相應的油田。“*”表示沒有油井。[email protected]
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.
一塊油田最多包括100塊油井。
Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 
****@
*@@*@
*@**@
@@@*@
@@**@
0 0 

Sample Output
0
1
2
2

Source Mid-Central USA 1997

hdu 1241 Oil Deposits