【BFS】CODE[VS] 3411 洪水 (日常刷题)

点击进入异世界


基础BFS,很水,直接宽搜,都不需要推入队列


代码:

#include 
#include 
#include 
#include 
#include 

const int maxn = 1010;

using namespace std;

int n,m,r,c;
int map[maxn][maxn];
bool came[maxn][maxn];
int dx[5] = {0,1,0,-1};
int dy[5] = {1,0,-1,0}; 

inline bool check(int xx,int yy)
{
    if(xx <= 0||xx > n||yy <= 0||yy > m) return false;  
    else return true;
}

int sum;

inline void bfs(int sx,int sy)
{
    //cout<<"motherfucker: "<
    for(int i = 0;i <= 4;i++)
    {
        //cout<
        if(check(sx+dx[i],sy+dy[i]) == true&&map[sx][sy] >= map[sx+dx[i]][sy+dy[i]]&&came[sx+dx[i]][sy+dy[i]] == 0)
        {
            came[sx+dx[i]][sy+dy[i]] = 1;
            if(map[sx][sy] >= map[sx+dx[i]][sy+dy[i]])
            {
                sum++;
                //cout<
                bfs(sx+dx[i],sy+dy[i]);
            }
        } 
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    int tot = n*m;
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++)
        {
            scanf("%d",&map[i][j]);
        }
    scanf("%d%d",&r,&c);
    came[r][c] = 1;
    sum = 1;
    bfs(r,c);
    printf("%d\n",tot-sum);
return 0;
}

你可能感兴趣的:(【NOIP2016】,【搜索-BFS】,【日常-刷题记录】)