zoukankan      html  css  js  c++  java
  • poj 1164 深度优先搜索模板题

    #include<iostream>                  //用栈进行的解决;
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<stack>
    using namespace std;
    
    const int maxn = 55;
    
    int room_map[maxn][maxn];
    bool color[maxn][maxn];
    
    int RoomArea;
    int maxRoomArea;
    int RoomNum;
    
    
    struct room
    {
        int r, c;
        room(int rr, int cc):r(rr),c(cc) {};
    };
    
    void bfs(int r,int c)
    {
        stack<room> stk;
        stk.push(room(r, c));
        while (!stk.empty())
        {
            room rm = stk.top();
            int x = rm.r;
            int y = rm.c;
            if (color[x][y])
                stk.pop();
            else
            {
                RoomArea++;
                color[x][y] = true;
                if ((room_map[x][y] & 1) == 0) stk.push(room(x, y - 1));    //向西;
                if ((room_map[x][y] & 2) == 0) stk.push(room(x - 1, y));    // 向北;
                if ((room_map[x][y] & 4) == 0) stk.push(room(x, y + 1));    //向东;
                if ((room_map[x][y] & 8) == 0) stk.push(room(x + 1, y));    //向南;
            }
        }
    }
    int main()
    {
        int r, c;
        while (~scanf("%d%d", &r, &c))
        {
            for (int  i = 1; i <= r; i++)
                for (int j = 1; j <= c; j++)
                    scanf("%d", &room_map[i][j]);
            memset(color, false, sizeof(color));
            maxRoomArea = 0;
            RoomNum = 0;
            for (int i = 1; i <= r; i++)
                for (int j = 1; j <= c; j++)
                {
                    if (!color[i][j])
                    {
                        RoomNum++;
                        RoomArea = 0;
                        bfs(i, j);
                        maxRoomArea = max(maxRoomArea, RoomArea);
                    }
                }
            printf("%d
    ", RoomNum);
            printf("%d
    ", maxRoomArea);
        }
        return 0;
    }
    
    #include<algorithm>
    #include<cstring>
    #include<cstdio>                    //用栈;
    using namespace std;
    
    const int maxn = 50;
    int roommap[maxn][maxn];          //城堡地图;
    int color[maxn][maxn];           //房间染色;
    
    
    int room_num, room_area ;
    int maxroom_aera;
    
    void bfs(int x, int y)
    {
        if (color[x][y]) return ;
        room_area++;
        color[x][y] = room_num;          //给访问过的房间染色;
    
        if ((roommap[x][y] & 1) == 0) bfs(x, y - 1);           //向西;
        if ((roommap[x][y] & 2) == 0) bfs(x - 1, y);           //向北;
        if ((roommap[x][y] & 4) == 0) bfs(x, y + 1);           //向东;
        if ((roommap[x][y] & 8) == 0) bfs(x + 1, y);           //向南;
    }
    
    int main()
    {
        void bfs(int x, int y);
        int row, cross;
        while (~scanf("%d%d", &row,&cross))
        {
            memset(color, 0, sizeof(color));
            maxroom_aera = 0;
            room_area = 0;
            room_num = 0;
            for (int i = 1; i <= row; i++)
            {
                for (int j = 1; j <= cross; j++)
                {
                    scanf("%d", &roommap[i][j]);
                }
            }
    
            for (int i = 1; i <= row; i++)
            {
                for (int j = 1; j <= cross; j++)
                {
                    if (!color[i][j])
                    {
                        room_num++;
                        room_area = 0;
                        bfs(i, j);
                        maxroom_aera = max(maxroom_aera, room_area);
                    }
                }
            }
            printf("%d
    ", room_num);
            printf("%d
    ", maxroom_aera);
        }
        return 0;
    }
  • 相关阅读:
    dedecms代码研究五
    dedecms代码研究四
    判断有没有真正点击打印
    SAP中删除假脱机请求
    商品扩地点不成功
    记录一些使用的abap小程序帮助开发
    sap abap 程序 中使用 FTP . <转载>
    SM37 后台调试
    ftp上传下载| 图片上传下载
    ALV调用的几个标准函数 <转自 思微随想>
  • 原文地址:https://www.cnblogs.com/zhaoningzyn/p/6607428.html
Copyright © 2011-2022 走看看