zoukankan      html  css  js  c++  java
  • poj1164

    bfs

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 55

    struct Point
    {
    int x, y;
    }q[maxn
    * maxn];

    int n, m, ans;
    int map[maxn][maxn];
    bool vis[maxn][maxn];
    int dir[4][2] =
    {
    {
    0, -1 },
    {
    -1, 0 },
    {
    0, 1 },
    {
    1, 0 } };

    bool ok(Point &p)
    {
    if (p.x < 0 || p.y < 0 || p.x >= n || p.y >= m || vis[p.x][p.y])
    return false;
    return true;
    }

    void bfs(int cx, int cy)
    {
    int front = 0, rear = 1;
    vis[cx][cy]
    = true;
    q[
    0].x = cx;
    q[
    0].y = cy;
    int num = 0;
    while (front != rear)
    {
    Point p
    = q[front++];
    num
    ++;
    int k = 1;
    for (int i = 0; i < 4; i++)
    {
    Point temp;
    temp.x
    = p.x + dir[i][0];
    temp.y
    = p.y + dir[i][1];
    if (!(k & map[p.x][p.y]) && ok(temp))
    {
    q[rear
    ++] = temp;
    vis[temp.x][temp.y]
    = true;
    }
    k
    <<= 1;
    }
    }
    ans
    = max(ans, num);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    scanf(
    "%d", &map[i][j]);
    int num = 0;
    memset(vis,
    0, sizeof(vis));
    ans
    = 0;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    if (!vis[i][j])
    {
    bfs(i, j);
    num
    ++;
    }
    printf(
    "%d\n%d\n", num, ans);
    return 0;
    }

  • 相关阅读:
    学习笔记::有上下界的网络流
    zoj2314
    bzoj3261
    bzoj 1898
    bzoj4009
    bzoj4033
    bzoj3389
    bzoj2427
    uva 11825
    交换A与B值的四种方法
  • 原文地址:https://www.cnblogs.com/rainydays/p/2103691.html
Copyright © 2011-2022 走看看