• POJ 2386 Lake Counting


    题意:输入一个 n 行 m 列 的矩阵, 搜索有多少个连通 ‘W'字母块

    题解:DFS。

    有个疑惑,就是输入一个字符串AC,一个字母一个字母的输入会 wa

    找Bug找了一个小时多,甚至怀疑过自己的算法写错了,现在都没想通

    莫名其妙贡献的一发wa. 欢迎大佬看见能告诉我QAQ

    #include <iostream>
    #include <queue>
    #include <stdio.h>
    #include <vector>
    #include <string>
    #include <map>
    #include <algorithm>
    #include <cstring>
    #define INF 0x3f3f3f3f
    using namespace std;
    const int N = 110;
    char mp[N][N];
    int n,m,ans = 0;
    int dir[8][2]={{-1,1},{-1,0},{-1,-1},{0,1},{0,-1},{1,1},{1,0},{1,-1}};  //八个方向
    bool inbound(int x, int l, int r)  //判断位置是否合法
    {
        if(x < l || x >= r) return false;  //不合法
        return true;
    }
    void dfs(int x, int y)
    {
        mp[x][y] = '.';   //访问过后覆盖
        for(int i = 0; i < 8; i++)
        {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];
            if(!inbound(tx,0,n) || !inbound(ty,0,m)) continue;  //位置不合法就结束本次循环
            if(mp[tx][ty] == 'W')  //递归搜索
                dfs(tx,ty);
        }
      //  return ;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++) scanf("%s",mp[i]);
    /***
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                scanf("%c",&mp[i][j]);
                ***/
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(mp[i][j] == 'W')
                {
                    ans++;
                    dfs(i,j);
                }
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    

      

  • 相关阅读:
    dsp1
    数字信号处理中的常用方法
    近一星期的学习计划4-8 到 4-15
    近一个月的学习计划!4-8
    离散时间与系统-1
    python _列表
    2016-1-19
    fushioncharts 使用教程要点---使用JSON数据方式
    使用easeui dialog弹出框中使用CKeditor多次加载后无法编辑问题
    MVC之路随记3--Html辅助方法
  • 原文地址:https://www.cnblogs.com/Edviv/p/12250779.html
Copyright © 2020-2023  润新知