hdu1241 bfs 模板

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;
}

时间: 2024-08-03 15:43:00

hdu1241 bfs 模板的相关文章

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

广度优先(BFS) ------- 模板1:-----模板2:--------模板3:

//使用数组queue[ ]存放结点队列 void BFS( ) { head=0; tail=1; queue[head]=首结点的值; while (head<tail) //队列不空 { temp=tail; for (k=head; k<=tail; k++) //对当前层扩展 { if ( 到达目的状态 ) { 输出结果; return; } for (i=1; i<=m; i++) //每个结点的m种扩展可能 if (可以扩展) { 处理每种可能情况: queue[temp+

HDU5012:Dice(bfs模板)

http://acm.hdu.edu.cn/showproblem.php?pid=5012 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face,

Knight Moves(hdu1372 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6731    Accepted Submission(s): 4059 Problem Description A friend of you is doing res

POJ:Dungeon Master(三维bfs模板题)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

poj2251Dungeon Master(bfs模板题)

题目链接:http://poj.org/problem?id=2251 可以说是bfs的模板题了. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<queue> 5 using namespace std; 6 char pic[32][32][32]; 7 int vis[32][32][32]; 8 int dir[6][3]={0,0,1,0,0,-1,1,0

PAT1076. Forwards on Weibo(标准bfs模板)

//标准的层次遍历模板 //居然因为一个j写成了i,debug半天.....解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧 #include<cstdio>#include<vector>#include<queue>#include<algorithm>using namespace std;const int maxn=1001;struct node{ int level; ve

图的遍历——DFS和BFS模板(一般的图)

关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostream> #include<unordered_map> #include<queue> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #

bfs模板

模板: #include<bits/stdc++.h> using namespace std; typedef long long ll; #define MAX 1010 #define INF 0x3f3f3f3f int n,m; int T; char g[MAX][MAX]; bool vis[MAX][MAX]; int fire[MAX][MAX]; int ans=INF; int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; int sx,sy; int