Oil Deposits( UVA - 572 )

标签: 广度优先搜索 分支限界  队列
1人阅读 评论(0) 收藏 举报
分类:

Oil Deposits UVA - 572

#include <iostream>
#include<stdio.h>
#include<queue>
#include<stdlib.h>
#include<string.h>
using namespace std;
char a[100][100];
int vis[100][100];
int dx[8]= {0,1,1,1,0,-1,-1,-1};
int dy[8]= {1,1,0,-1,-1,-1,0,1};
typedef struct QNode
{
    int len;
    int data;
    int data1;
    struct QNode *next;
} QNode,*QueuePtr;
typedef struct
{
    QueuePtr front1;
    QueuePtr rear;
} LinkQueue;
int m,n;
void initQueue(LinkQueue &Q)
{
    Q.front1=Q.rear=(QueuePtr)malloc(sizeof(QNode));
    Q.front1->next=NULL;
    Q.rear->len=0;
}
void EnQueue(LinkQueue &Q,int e,int e1)
{
    QNode *p;
    p=(QueuePtr)malloc(sizeof(QNode));
    p->data=e;
    p->data1=e1;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    Q.rear->len++;
}
QueuePtr dequeue(LinkQueue &Q)
{
    QNode *p;
    p=Q.front1->next;
    Q.front1->next=p->next;
    if(Q.rear==p)
    {
        Q.rear=Q.front1;
    }
    free(p);
    Q.rear->len--;
    if(Q.rear!=Q.front1)
        return Q.front1->next;
}
int emptyqueue(LinkQueue &Q)
{
    if(Q.rear==Q.front1)
        return 1;
    return 0;
}
int bfs()
{
    int x,y;
    LinkQueue Q;
    initQueue(Q);
    int num=0;
    for(int i1=0; i1<m; i1++)
        for(int j1=0; j1<n; j1++)
        {
            if(vis[i1][j1]==0)
            {
                num++;
                x=i1;
                y=j1;
                EnQueue(Q,x,y);
                vis[x][y]=1;
                while(!emptyqueue(Q))
                {
                    int i;
                    int mx,my;
                    for(i=0; i<8; i++)
                    {
                  //      cout<<x<<"!"<<y<<endl;
                        mx=x+dx[i];
                        my=y+dy[i];
                        if(mx>=0&&mx<m&&my>=0&&my<n&&vis[mx][my]==0)
                        {
                    //        cout<<mx<<" "<<my<<endl;
                            EnQueue(Q,mx,my);
                            vis[mx][my]=1;
                        }
                    }
                    QueuePtr q;
                    q=dequeue(Q);
                    x=q->data;
                    y=q->data1;
                }
            }
        }
    return num;
}
int main()
{
  //  freopen("C:\\Users\\Administrator\\Desktop\\ceshi.txt","r",stdin);
    while(~scanf("%d%d",&m,&n))
    {
        int k1=0,k2=0;
        if(m==0)
            break;
        memset(vis,0,sizeof(vis));
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='*')
                {
                    vis[i][j]=1;
                }
            }
        cout<<bfs()<<endl;
    }
    return 0;
}



查看评论

UVa 572 - Oil Deposits 搜索专题

572 - Oil Deposits 11158 58.77% 5511 95.30% 题目链接: http://uva...
  • shuangde800
  • shuangde800
  • 2012-07-06 00:52:11
  • 4607

UVA 572-Oil Deposits

UVA 572-Oil Deposits题目大意:给出一个字符串的地理图,判断油田@的数目,几个@紧挨在一起算一个油田解题思路:先二维数组存好,然后将找到的油田变为*,递归调用函数#include ...
  • ZeroLH00
  • ZeroLH00
  • 2016-08-10 16:06:22
  • 133

Uva - 572 - Oil Deposits

图的连通块问题,,DFS深度优先遍历递归找出每个连通块,并标记,注意边界情况的处理。 AC代码: #include #include #include #include #include ...
  • zyq522376829
  • zyq522376829
  • 2015-06-16 12:53:26
  • 419

uva572 - Oil Deposits 油矿

Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting unde...
  • hcbbt
  • hcbbt
  • 2013-04-24 23:32:03
  • 1211

HDU 1241/UVa572 Oil Deposits(DFS&BFS)

Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota...
  • hurmishine
  • hurmishine
  • 2016-03-22 18:14:35
  • 1120

DFS入门精讲:油田(Oil Deposits UVA572)

传送门:https://vjudge.net/problem/UVA-572 具体原题看链接。 大意:输入一个m行n列字符矩阵,统计字符@组成多少个八连块(如果两个字符@所在格子横竖对角线方向相邻就...
  • qq_38121348
  • qq_38121348
  • 2017-05-23 20:02:09
  • 167

[ACM - 深度优先搜索]Oil Deposits

Problem Description   The GeoSurvComp geologic survey company is responsible for detecting undergrou...
  • sr19930829
  • sr19930829
  • 2013-11-27 09:22:55
  • 2192

Oil Deposits(基础dfs)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T...
  • tree__water
  • tree__water
  • 2016-02-28 09:46:59
  • 529

POJ 1562:Oil Deposits

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14462   ...
  • u010885899
  • u010885899
  • 2015-07-31 16:06:07
  • 446

uva 572 Oil Deposits

深度优先搜索,太简单了,不多说了。 #include #define MAX_LEN 105 char arr[MAX_LEN][MAX_LEN]; int count; int ...
  • xiaohaowudi
  • xiaohaowudi
  • 2013-10-08 17:02:53
  • 316
    个人资料
    持之以恒
    等级:
    访问量: 1351
    积分: 292
    排名: 25万+
    文章存档