【九度】题目1460:Oil Deposit

来源:互联网 发布:二维码条形码扫描软件 编辑:程序博客网 时间:2024/06/05 17:07
题目地址:http://ac.jobdu.com/problem.php?pid=1460
题目描述:

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.

输入:

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.

输出:

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.

样例输入:
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 0
样例输出:
0122

题目的大意是有一块油田,如果油田中的任意部分在水平,垂直或者对角线紧挨着,
那么他们属于同一部分。
现在需要求解油田有多少小块。
bfs吧。
针对每个小块@部分,如果没有访问过,就进行宽度优先搜索,如果发现满足要求条件,
将该点标记为已经访问过。同时将总数加1。
C++ AC

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <queue>using namespace std;const int maxn = 102;int visit[maxn][maxn];char mazeArr[maxn][maxn];int stepArr[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{1,1},{1,-1},{-1,1},{-1,-1}};int m,n,startx,starty;    struct Node{    int x;    int y;    Node(int x1,int y1):x(x1),y(y1){}};    void bfs() {    Node node(startx,starty);    visit[startx][starty] = 1;    queue<Node> q ;    while(!q.empty()) q.pop();    q.push(node);    while (!q.empty()) {        node = q.front();        q.pop();        for (int i = 0; i < 8; i++) {            int x = node.x + stepArr[i][0];            int y = node.y + stepArr[i][1];            if (x >= 0 && y >= 0 && x < m && y < n                         && visit[x][y] == 0 && mazeArr[x][y] == '@') {                visit[x][y] = 1;                Node next(x, y);                q.push(next);            }        }    }}int main(){       int i,j;    while(scanf("%d%d",&m,&n) != EOF ){        if(m == 0 && n == 0){            break;        }        getchar();          for (i = 0; i < m; i++){            scanf("%s", mazeArr[i]);          }        memset(visit,0,sizeof(visit));                int count = 0;        for (i = 0; i < m; i++){            for(j = 0; j < n; j++){                if(mazeArr[i][j] != '*' && visit[i][j] == 0){                    startx = i;                    starty = j;                    bfs();                    count++;                }            }        }        printf("%d\n",count);    }          return 0;} /**************************************************************    Problem: 1460    User: wangzhenqing    Language: C++    Result: Accepted    Time:10 ms    Memory:1104 kb****************************************************************/

Java AC

import java.util.LinkedList;import java.util.Queue;import java.util.Scanner; public class Main {    /*     * 1460     */    private static char mazeArr[][];    private static int visit[][];    private static int stepArr[][] = {{-1,0},{1,0},{0,-1},{0,1},{1,1},{1,-1},{-1,1},{-1,-1}};    private static int m, n, startx, starty;    public static void main(String[] args){        Scanner scanner = new Scanner(System.in);        while (scanner.hasNext()) {            m = scanner.nextInt();            n = scanner.nextInt();            if (m == 0 && n == 0) {                break;            }            mazeArr = new char[m][n];            visit = new int[m][n];            for (int i = 0; i < m; i++) {                mazeArr[i] = scanner.next().toCharArray();            }            int count = 0;            for (int i = 0; i < m; i++) {                for (int j = 0; j < n; j++) {                    if (mazeArr[i][j] != '*' && visit[i][j] == 0) {                        startx = i;                        starty = j;                        bfs();                        count++;                    }                }            }            System.out.println(count);        }    }       private static void bfs() {        Node node = new Node(startx, starty);        visit[startx][starty] = 1;        Queue<Node> queue = new LinkedList<Node>();        queue.add(node);        while (!queue.isEmpty()) {            Node newNode = queue.poll();            for (int i = 0; i < 8; i++) {                int x = newNode.x + stepArr[i][0];                int y = newNode.y + stepArr[i][1];                if (x < 0 || y < 0 || x >= m || y >= n || visit[x][y] == 1 || mazeArr[x][y] == '*') {                    continue;                }                visit[x][y] = 1;                Node next = new Node(x, y);                queue.add(next);            }        }    }    private static class Node{        private int x;        private int y;        public Node(int x, int y) {            super();            this.x = x;            this.y = y;        }    }}/**************************************************************    Problem: 1460    User: wzqwsrf    Language: Java    Result: Accepted    Time:290 ms    Memory:27724 kb****************************************************************/
0 0