暑期学习-搜索及习题

由于前段时间在考驾照所以耽误了一些时间没写博客。今天开始写
搜索虽然在学校学过的,但因为没刷题所以现在回顾刷下题

广搜(BFS)

利用队列,查询到一个分支,如果有一个能走就往下走,不能走就往上一个。
HDU1312
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
按书上所言,相当于扩散,一开始是1,然后扩散至2,3,然后2和3分别接着扩散
有个二维数组,之前这样的用法没用过,下面的dir数组,用来储存坐标。然后自己也复习了一遍队列,因为用的不多,其他的倒没什么,就是好久不写生疏了。

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long  ll;
#define pi acos(-1.0)
#define INF 0x7fffffff
#define t() cin>>t; while(t--)
#define mem(x) memset(x,INF,sizeof(x))
#define rep(m,n,h) for(int m=n;m<=h;m++)
#define fo(m,n,h) for(int m=n;m<h;m++)
#define cls(x) memset(x,0,sizeof(x))
#define IOS  std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int N = 1e6 + 10;
char room[21][21];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
struct node{
int x,y;
};
int Long,Wide;
queue<node>q;
/*char judge(int xx,int xy)
{
        if(xx<Long&&xx>=0&&xy<Wide&&xy>=0)
            return true;
}*/
#define judge(xx,xy)(xx<Long&&xx>=0&&xy<Wide&&xy>=0)
int num;
void BFS(int xx,int xy)
{
    num=1;
    node start,next;
    start.x=xx;
    start.y=xy;
    q.push(start);
    while(!q.empty())
    {
        start=q.front();
        q.pop();
        for(int i=0;i<4;i++)
            {
                next.x=start.x+dir[i][0];
                next.y=start.y+dir[i][1];
        if(judge(next.x,next.y)&&room[next.x][next.y]=='.')
        {
            num++;
            room[next.x][next.y]='#';
            q.push(next);
        }
    }
   }
}
int main()
{
    //freopen("C:\\Users\\lenovo\\Desktop\\in.txt","r",stdin);
    IOS
    int dx,dy;
    while(cin>>Long>>Wide)
    {
        if(Long==0&&Wide==0)
            break;
        for(int j=0;j<Wide;j++)
            for(int i=0;i<Long;i++)
            {
                cin>>room[i][j];
                if(room[i][j]=='@')
                {
                    dx=i;
                    dy=j;
                }
            }
        BFS(dx,dy);
        cout<<num<<endl;
    }
    return 0;
}

深搜(DFS)

HDU1312也可用深搜
深搜是利用递归,只有把这一层遍历完后才会来到下一层
我感觉,目前对我来说深搜比广搜容易理解一些

void DFS(int xx,int xy)
{
    room[xx][xy]='#';
    num++;
    for(int i=0;i<4;i++)
    {
         int nextx=xx+dir[i][0];
         int nexty=xy+dir[i][1];
        if(judge(nextx,nexty)&&room[nextx][nexty]=='.')
        {
            DFS(nextx,nexty);
        }
    }
}

今天刚开始看那本书,熟悉不起来,看的很慢,理解也慢,写代码都慢下来了,这才第一天,慢慢来吧,明天刷搜索的题,学习并查集。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值