zoukankan      html  css  js  c++  java
  • 【HDOJ】1241 Oil Deposits

    经典的BFS。

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 #define MAXNUM 105
     5 #define MAXROW 105
     6 #define MAXQUE 10005
     7 
     8 char buf[MAXROW][MAXNUM];
     9 char visit[MAXROW][MAXNUM];
    10 
    11 typedef struct {
    12     int x, y;
    13 } pos_st;
    14 
    15 pos_st queue[MAXQUE];
    16 int direct[8][2] = {{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1}};
    17 int m, n;
    18 int total;
    19 
    20 void bfs();
    21 void search();
    22 
    23 int main() {
    24     int i;
    25 
    26     while (scanf("%d%d", &m, &n)!=EOF && (m||n)) {
    27         getchar();
    28         for (i=0; i<m; ++i) {
    29             gets(buf[i]);
    30         }
    31         bfs();
    32         printf("%d
    ", total);
    33     }
    34 
    35     return 0;
    36 }
    37 
    38 void bfs() {
    39     int i, j;
    40 
    41     total = 0;
    42     memset(visit, -1, sizeof(visit));
    43 
    44     for (i=0; i<m; ++i)
    45         for (j=0; j<n; ++j)
    46             if (buf[i][j]=='@' && visit[i][j]==-1)
    47                 search(i, j);
    48 }
    49 
    50 void search(int row, int col) {
    51     int front, rear;
    52     int x, y, newx, newy;
    53     int i;
    54 
    55     front = rear = 0;
    56     total++;
    57     visit[row][col] = total;
    58 
    59     if (visit[row][col] > 0) {
    60         queue[rear].x = row;
    61         queue[rear].y = col;
    62         rear++;
    63         while (front != rear) {
    64             x = queue[front].x;
    65             y = queue[front].y;
    66             front++;
    67             for (i=0; i<8; ++i) {
    68                 newx = x+direct[i][0];
    69                 newy = y+direct[i][1];
    70                 if (newx>=0 && newx<m && newy>=0 && newy<n) {
    71                     if (buf[newx][newy] == '@' && visit[newx][newy]<0) {
    72                         queue[rear].x = newx;
    73                         queue[rear].y = newy;
    74                         rear++;
    75                         visit[newx][newy] = total;
    76                     }
    77                 }
    78             }
    79         }
    80     }
    81 
    82 }
    View Code
  • 相关阅读:
    MVC页面打印成pdf插件Rotativa
    lambda表达式嵌套
    MVC+EF五种条件查询获取数据列表方式
    自己写的通用分页存储过程
    Llinux命令大全之软件管理命令
    .net开发 chrome核心浏览器组件
    常用的SQL日期操作
    Linux各版本介绍说明
    大数据环境搭建
    markdown语法链接新页面打开
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3604736.html
Copyright © 2011-2022 走看看