zoukankan      html  css  js  c++  java
  • ZOJ 1709

    主要运用BFS把一个’@‘所在的油田的‘@’都标记(既改为‘*’),

    然后不断的找’@‘,重复刚才那一步,直到找不到’@‘为止;记录找的次数;

    #include <iostream>
    #include <queue>
    #include "string.h"
    using namespace std;
    struct spot{
    public:
        int x, y;
        void init(int nx, int ny){
            x = nx;
            y = ny;
        }
    };
    int n = 0, m = 0;
    char oil[101][101];
    int step[8][2] = {{1, 1}, { -1, 1}, { -1, -1}, {1, -1}, {1, 0}, {0, 1}, { -1, 0}, {0, -1}};     //八个方向
    void bfs( int x, int y){
        int nx,ny;
        queue<spot> q;
        spot start;
        start.init(x, y);
        q.push(start);
        oil[y][x]=1;
        while (!q.empty()){
            spot t = q.front();
            q.pop();
            int x = t.x, y = t.y;
            for (int i = 0; i < 8; i++){
                nx = x + step[i][0];
                ny = y + step[i][1];
                 if(nx >= 0 && ny >= 0 && nx < m && ny < n && oil[ny][nx] == '@'){
                spot c;
                c.init(nx, ny);
                q.push(c);
                oil[ny][nx] = '*';
                }
            }
        }
    }
    int main()
    {
        while (cin>>n>>m){
            if (m == 0)break;
            int sum = 0;
            memset(oil, 0, sizeof(oil));
            for (int i = 0; i < n; i++){
                for (int j = 0; j < m; j++){
                    cin>>oil[i][j];
                }
            }
            for (int i = 0; i < n; i++){
                for (int j = 0; j < m; j++){
                    if (oil[i][j]=='@'){
                        sum ++;
                        bfs(j, i);
                    }
                }
            }
            cout << sum << endl;
        }
        return 0;
    }

  • 相关阅读:
    考研打卡_Day04
    考研打卡_Day03
    考研打卡-Day02
    吾日三省-归隐
    为什么要写博客?
    用C语言写一个Helloworld_实现第一步编译运行
    C语言中的结构体是怎么定义的_怎么使用?
    C语言的常用的数据类型有哪些_所占字节分别是多少
    Vim编辑器中查找关键词命令_查找与替换命令_多窗口命令
    Vim的基本操作命令与光标移动命令
  • 原文地址:https://www.cnblogs.com/Mr-Xu-JH/p/3843716.html
Copyright © 2011-2022 走看看