zoukankan      html  css  js  c++  java
  • poj1979【基础bfs/dfs】

    挑战习题搜索-1
    题意:
    给定起点,然后求一个可以到达的数量,位置“.”都可以走。每次应该是上下左右都可以走。
    思路:
    这题应该DFS更好写,但是BFS也可以写吧。
    好久没写了…
    dfs挫代码……..

    #include<cstdio>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define eps 1e-8
    typedef __int64 LL;
    
    const int N=25;
    
    int n,m;
    char ma[N][N];
    int dx[4]={0,0,-1,1};
    int dy[4]={1,-1,0,0};
    int ans;
    
    bool Judge(int x,int y)
    {
        if(x<0||y<0||x>=n||y>=m||ma[x][y]=='#')
            return 0;
        return 1;
    }
    
    void dfs(int x,int y)
    {
        ma[x][y]='#';//到了就标记,避免重复;
        ans++;      //计数++;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(Judge(xx,yy))//如果符合就继续搜索;
                dfs(xx,yy);
        }
    }
    int main()
    {
        while(~scanf("%d%d",&m,&n))
        {
            if(!n&&!m) break;
    
            int x,y;
            for(int i=0;i<n;i++)
            {
                scanf("%s",ma[i]);
                for(int j=0;j<m;j++)
                {
                    if(ma[i][j]=='@')//起点;
                    {
                        x=i;
                        y=j;
                    }
                }
            }
            ans=0;
            dfs(x,y);//从起点开始搜索;
            printf("%d
    ",ans);
        }
        return 0;
    }

    bfs挫code………..
    //利用bfs将可以到达的点塞进队列并标记,当出队列的时间就计数。

    #include<cstdio>
    #include<iostream>
    #include<queue>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define eps 1e-8
    typedef __int64 LL;
    
    const int N=25;
    
    struct asd{
        int x,y;
    };
    asd q[N*N];
    
    int n,m;
    char ma[N][N];
    int dx[4]={0,0,-1,1};
    int dy[4]={1,-1,0,0};
    
    int ans;
    
    bool Judge(int x,int y)
    {
        if(x<0||y<0||x>=n||y>=m||ma[x][y]=='#')
            return 0;
        return 1;
    }
    
    queue<asd>que;      //建议还是要多写手写队列
    
    void bfs(int x,int y)
    {
        while(!que.empty())
            que.pop();
        asd now,next;
        now.x=x;
        now.y=y;
        ma[x][y]='#';           //标记
        que.push(now);
        while(!que.empty())
        {
            asd now;
            now=que.front();que.pop();      //出队列
            ans++;
            for(int i=0;i<4;i++)
            {
                int xx=now.x+dx[i];
                int yy=now.y+dy[i];
                if(Judge(xx,yy))
                {
                    next.x=xx;
                    next.y=yy;
                    ma[next.x][next.y]='#'; //标记
                    que.push(next);
                }
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d",&m,&n))
        {
            if(!n&&!m) break;
    
            int x,y;
            for(int i=0;i<n;i++)
            {
                scanf("%s",ma[i]);
                for(int j=0;j<m;j++)
                {
                    if(ma[i][j]=='@')
                    {
                        x=i;
                        y=j;
                    }
                }
            }
            ans=0;
            bfs(x,y);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    项目发展规划 题解
    善意的投票&小M的作物 题解
    方格取数加强版 题解
    BZOJ1001 狼抓兔子 题解
    a
    一个搬运
    代码“小白”的温故而知新(一)-----OA管理系统
    工作流-----WorkFlow
    温习SQL语句
    浅谈MVC基础
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934889.html
Copyright © 2011-2022 走看看