欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

搜索专题(复习)

程序员文章站 2022-05-30 21:25:39
...

hdu1241
搜索专题(复习)

DFS

#include <bits/stdc++.h>

using namespace std;
const int MAXN=105;
//char m[MAXN][MAXN];

char ma[MAXN][MAXN];
int n,m;
void dfs(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||ma[x][y]=='*')
        return;
    if(ma[x][y]=='@')
        ma[x][y]='*';
    dfs(x+1,y);
    dfs(x-1,y);
    dfs(x-1,y-1);
    dfs(x+1,y+1);
    dfs(x-1,y+1);
    dfs(x+1,y-1);
    dfs(x,y+1);
    dfs(x,y-1);

}
int main()
{
    //cout << "Hello world!" << endl;


    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            return 0;
        int cnt=0;

        for(int i=0;i<n;i++)
        {
            cin>>ma[i];
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(ma[i][j]=='@')
                {
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

BFS

#include <bits/stdc++.h>

using namespace std;
const int MAXN=105;
char ma[MAXN][MAXN];
int n,m;
int x[]={-1,-1,0,1,1,1,0,-1};
int y[]={0,1,1,1,0,-1,-1,-1};
struct Node
{
    int x,y;
}node;
int check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||ma[x][y]=='*')
        return 0;
    return 1;
}

void bfs()
{
    queue<Node>que;
    que.push(node);
    Node now,next;
    while(!que.empty())
    {
        now=que.front();
        que.pop();
        for(int i=0;i<8;i++)
        {
            next.x=now.x+x[i];
            next.y=now.y+y[i];
            if(check(next.x,next.y))
            {
                ma[next.x][next.y]='*';
                que.push(next);
            }
        }

    }

}


int main()
{
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            return 0;
        int cnt=0;
        for(int i=0;i<n;i++)
            cin>>ma[i];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(ma[i][j]=='@')
                {
                    cnt++;
                    node.x=i;
                    node.y=j;
                    bfs();
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

POJ 3278Catch That Cow
搜索专题(复习)
BFS采用结构体写的话怎么剪枝都超时,采用int型的话可以过,没弄懂哪里出了问题

AC代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;

int visit[100005] = {0};
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        memset(visit,0,sizeof(visit));
        queue<int> q;
        q.push(n);int t;
        while(!q.empty())
        {
            t = q.front();
            q.pop();
            if(t == m)
            {
                break;
            }
            if(t > 0 && !visit[t-1])
            {
                int z;z = t-1; visit[z] = visit[t]+1;
                q.push(z);
            }
            if(t < m &&!visit[t+1])
            {
                  int z;z = t+1; visit[z] = visit[t]+1;
                q.push(z);
            }
            if(t*2 < 100005 && !visit[t*2])
            {
                int z;z = t*2; visit[z] = visit[t]+1;
                q.push(z);
            }
        }
        printf("%d\n",visit[m]);
    }
    return 0;
}

卡了的代码:

#include <iostream>
#include<queue>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdio.h>
using namespace std;

const int MAXN=200005;
bool vis[MAXN];
int n,k;
int st=MAXN;




struct Node
{
    int x;
    int step;
}node;

void bfs()
{
    queue<Node>que;
    que.push(node);
    vis[node.x]=1;
    Node now,next;
    node.step=0;
    while(!que.empty())
    {
        now=que.front();
        que.pop();
        if(now.x==k)
        {
            st=min(st,now.step);
            return;
        }
        if(now.x>0&&!vis[now.x-1])
        {
            next.x=now.x-1;next.step=now.step+1;que.push(next);

        }
        if(now.x<k&&!vis[now.x+1])
        {
            next.x=now.x+1;next.step=now.step+1;que.push(next);
        }

        if(now.x*2<=100005&&!vis[now.x*2])
        {
            next.x=now.x*2;next.step=now.step+1;que.push(next);
        }
    }

}
int main()
{
    cin>>n>>k;
    node.x=n;
    bfs();
    cout<<st<<endl;

    return 0;
}

HDU2612 Find a way

搜索专题(复习)

两次BFS,分别计算每个人到达每个KFC需要的最少时间

#include<bits/stdc++.h>
using namespace std;
const int MAXN=205;
const int INF=0x3f3f3f3f;
char ma[MAXN][MAXN];
int kfc[MAXN][MAXN];
int len[3][MAXN*MAXN];
bool vis[MAXN][MAXN];

int dirx[]={0,0,1,-1};
int diry[]={1,-1,0,0};

int n,m;

struct Node
{
    int x,y,step;
}node;

bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||ma[x][y]=='#'||vis[x][y])
        return 0;
    return 1;
}

void bfs(int x,int y,int p)
{
    node.x=x,node.y=y;node.step=0;
    queue<Node>que;
    que.push(node);

    vis[node.x][node.y]=1;


    Node now,next;
    while(!que.empty())
    {
        now=que.front();
        que.pop();
       //cout<<now.x<<" "<<now.y<<endl;
        if(ma[now.x][now.y]=='@')
        {
            len[p][kfc[now.x][now.y]]=now.step;
            //return;
        }
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dirx[i];
            next.y=now.y+diry[i];
            next.step=now.step+1;
            if(check(next.x,next.y))
            {
                que.push(next);
                vis[next.x][next.y]=1;
            }
        }


    }



}

int main()
{
    while(cin>>n>>m)
    {
        //memset(len,40005,sizeof(len));
        memset(vis,0,sizeof(vis));
        int yx,yy,mx,my;
        int cnt=0,ans=INF;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>ma[i][j];
                if(ma[i][j]=='Y')yx=i,yy=j;
                if(ma[i][j]=='M')mx=i,my=j;
                if(ma[i][j]=='@')kfc[i][j]=cnt++;
            }
        }
        bfs(yx,yy,0);
        memset(vis,0,sizeof(vis));
        bfs(mx,my,1);
        for(int i=0;i<cnt;i++)
        {
            ans=min(ans,(len[0][i]+len[1][i]));
        }
        cout<<ans*11<<endl;
    }


}

相关标签: 搜索