zoukankan      html  css  js  c++  java
  • poj3194

    简单题, BFS

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

    #define maxn 105

    struct Point
    {
    int x, y;
    Point()
    {
    }
    Point(int xx, int yy): x(xx), y(yy)
    {
    }
    }q[maxn * maxn];

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

    void input()
    {
    memset(map, 0, sizeof(map));
    for (int i = 1; i < n; i++)
    for (int j = 0; j < n; j++)
    {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    map[a][b] = i;
    }
    }

    int bfs(Point s)
    {
    int front, rear;
    front = rear = 0;
    q[rear++] = s;
    vis[s.x][s.y] = true;
    int ret = 1;
    while (front != rear)
    {
    Point p = q[front++];
    for (int i = 0; i < 4; i++)
    {
    Point a(p.x + dir[i][0], p.y + dir[i][1]);
    if (a.x >= 0 && a.y >= 0 && a.x < n && a.y < n && !vis[a.x][a.y] && map[a.x][a.y] == map[p.x][p.y])
    {
    vis[a.x][a.y] = true;
    q[rear++] = a;
    ret++;
    }
    }
    }
    return ret;
    }

    bool work()
    {
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
    if (!vis[i][j])
    {
    int x = bfs(Point(i, j));
    if (x != n)
    return false;
    }
    return true;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    while (scanf("%d", &n), n)
    {
    input();
    if (work())
    printf("good\n");
    else
    printf("wrong\n");
    }
    return 0;
    }

  • 相关阅读:
    用PHP对数据库数据进行删除
    用PHP向数据库中添加数据
    PHP中如何连接数据库基本语句
    数组的函数(方法)
    PHP数组的定义和遍历
    正则表达式
    HTML
    函数
    二维数组
    字符串类型||日期时间类型||数学运算
  • 原文地址:https://www.cnblogs.com/rainydays/p/2203599.html
Copyright © 2011-2022 走看看