码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1241 bfs 模板

时间:2016-04-26 00:14:19      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22543    Accepted Submission(s): 12980


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

 
对于我个人来说很重要的一题吧,每次bfs tle还是mle那么多次后,我终于发现症结所在,
应该是在每个step入队的时候i标记,而非出队的时候标记,原因很简单,考虑相邻节点吧,如果S->A&&S->B&&A->B,这种情况就非常尴尬了,B会两次入队,感觉以后会有题目会要考虑这种的,拭目以待吧
以前得了数据量小的便宜,晚上发现这个错误非常高兴
回到这题,找同类吧,题目还是非常简单,感觉和省赛那题差不多,,,
技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>

const int inf = (1<<31)-1;
const int MAXN = 1e2+10;
const int MMAXN = 5e4+10;
using namespace std;

struct step{
    int x;
    int y;
};

int n,m;
queue<step>q;
/*step q[MMAXN];
int l,r;*/
char G[MAXN][MAXN];
int mov[8][2]={0,1,0,-1,1,0,-1,0,1,1,-1,-1,-1,1,1,-1};
/*int mov[4][2]={0,1,1,0,1,-1,1,1};*/

int  check(int x,int y){
    if(x<0||y<0||x>=n||y>=m)return 0;
    if(G[x][y]==*)return 0;
    else return 1;
}


int main()
{
    int tk;
    step t,fro;
    int nx,ny;
    while(scanf("%d%d",&n,&m),n){
        tk = 0;
        for(int i=0;i<n;i++){
            scanf("%s",G[i]);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(G[i][j]==@){
                        tk++;
                        t.x = i;
                        t.y = j;
                        q.push(t);
                        G[i][j]=*;
                        while(!q.empty()){
                            fro = q.front();
                            q.pop();
                           // G[fro.x][fro.y] = ‘*‘;
                            for(int i=0;i<8;i++){
                                nx = fro.x+mov[i][0];
                                ny = fro.y+mov[i][1];
                                if(check(nx,ny)){
                                    t.x = nx;
                                    t.y = ny;
                                    q.push(t);
                                    G[nx][ny] = *;
                                }
                            }
                        }
                      /* G[i][j] = ‘*‘;
                        l = r = 0;
                        q[r++] = t;
                        while(l<r){
                            fro = q[l];
                            l++;
                           // G[fro.x][fro.y] = ‘*‘;
                            for(int i=0;i<8;i++){
                                nx = fro.x+mov[i][0];
                                ny = fro.y+mov[i][1];
                                if(check(nx,ny)){
                                    t.x = nx;
                                    t.y = ny;
                                    q[r++] = t;
                                    G[nx][ny] = ‘*‘;
                                }
                            }
                        }*/
                }
            }
        }
        cout<<tk<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}
View Code

 

 
 
 
 
 
 
 
 

hdu1241 bfs 模板

标签:

原文地址:http://www.cnblogs.com/EdsonLin/p/5433311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!