hdu入門搜索題


 

新人們加油,就優先隊列有點難度,其它對新手很有學習價值

下面是我今晚剛剛刷的幾道基本搜索題

hdu 1010 Tempter of the Bone

經典搜索入門題,DFS,本題考查要點:剪枝,奇偶性剪枝

View Code
#include<stdio.h>
#include<stdlib.h>
int m,n,t;
char map[8][8];
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
int ex,ey,sx,sy,ok;//e表示end,終點,s表示start,出發點,ok用來判斷是否在規定時間到達

void dfs(int x,int y,int cnt)
{
    int i;
    if(cnt==t)//剪枝1:到時間了符合條件ok=1再退出,不符合條件直接退出。
    {
        if(ex==x&&ey==y)ok=1;
        return;
    }
    if(ok)return;//找到解后還有部分在繼續搜索,這條是為了讓其它搜索停止
    int temp=abs(x-ex)+abs(y-ey)-abs(cnt-t);//剪枝2:((((要點))))
    //奇偶性剪枝 ,起點和終點確定以后就可以確定走的步數是奇數還是偶數,通過這個特點來剪枝
    if(temp>0||temp&1)return;//temp&1相當於temp%2,運算位。
    for(i=0;i<4;i++)
    {
        int fx=x+dir[i][0];
        int fy=y+dir[i][1];
        if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]!='X')
        {
            map[fx][fy]='X';
            dfs(fx,fy,cnt+1);
            map[fx][fy]='.'; //回溯
        }
    }
}
int main()
{
    int i,j,wall;
    //freopen("int.txt","r",stdin);
    while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(m||n||t))
    {
        wall=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;map[i][j]!='\0';j++)
            {
                if(map[i][j]=='S')
                {
                    sx=i;sy=j;
                }
                else if(map[i][j]=='D')
                {
                    ex=i;ey=j;
                }
                else if(map[i][j]=='X')
                    wall++;
            }
        }
        if(m*n-wall<=t) printf("NO\n");
        //剪枝3:能走的格子個數比時間少的話,直接不符合,不用再搜了
        else
        {
            ok=0;
            map[sx][sy]='X';
            dfs(sx,sy,0);
            if(ok)printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

 

 hdu 1241 Oil Deposits

水題,DFS,適合新手,新人加油

View Code
#include<stdio.h>
int m,n;
char map[101][101];
int dir[8][2]={{1,0},{1,-1},{1,1},{0,-1},{0,1},{-1,0},{-1,-1},{-1,1}};//8個方向
void dfs(int x,int y)
{
    int fx,fy,i;                //該題比較水,適合新手,不解釋
    for(i=0;i<8;i++)
    {
        fx=x+dir[i][0];
        fy=y+dir[i][1];
        if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]=='@')
        {
            map[fx][fy]='*';
            dfs(fx,fy);
        }
    }
}
int main()
{
    int i,j;
    //freopen("int.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;
        getchar();
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%c",&map[i][j]);
            }
            getchar();
        }
        int cnt=0;
        for(i=0;i<n;i++)
        for(j=0;j<m;j++)
        if(map[i][j]=='@'){map[i][j]='*';cnt++;dfs(i,j);}
        printf("%d\n",cnt);
    }
    return 0;
}

 hdu 1242 Rescue

優先隊列+BFS,注意救援可以有多個,但杭電題目一個救援就可以過

View Code
#include<stdio.h>
#include<queue>
#include<string.h>
#define maxn 201
using namespace std;
int dir[4][2]={1,0,-1,0,0,-1,0,1};
int m,n,sx,sy;
int vis[maxn][maxn];
char map[maxn][maxn];
struct node
{
    int x,y,time;
    friend bool operator<(node a,node b)//優先隊列,去學學STL吧
    {
        return a.time>b.time;
    }
};
int bfs()
{
    node now,next;
    int i;
    priority_queue<node>q;
    while(!q.empty())q.pop();
    now.x=sx;now.y=sy;now.time=0;
    q.push(now);vis[now.x][now.y]=1;
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(map[now.x][now.y]=='r')return now.time;
        for(i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='#'&&!vis[next.x][next.y])
            {
                if(map[next.x][next.y]=='x')next.time=now.time+2;
                else next.time=now.time+1;
                vis[next.x][next.y]=1;
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
      int i,j;
      //freopen("int.txt","r",stdin);
      while(scanf("%d%d",&n,&m)!=EOF)
      {
        if(n==0&&m==0)break;
        getchar();
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='a'){sx=i;sy=j;}
            }
            getchar();
        }
        memset(vis,0,sizeof(vis));
        int ans=bfs();
        if(ans!=-1)printf("%d\n",ans);
        else printf("Poor ANGEL has to stay in the prison all his life.\n" );
      }
}

hdu1312 Red and Black

DFS,新手題

View Code
#include<stdio.h>
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int m,n,bx,by,cnt;
char map[22][22];
void dfs(int x,int y)
{
    int i;cnt++;
    for(i=0;i<4;i++)
    {
        int fx=x+dir[i][0];
        int fy=y+dir[i][1];
        if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]=='.')
        {
            map[fx][fy]='#' ;
            dfs(fx,fy);
        }
    }
}
int main()
{
    int i,j;
    //freopen("int.txt","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        getchar();
        if(m==0&&n==0)break;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='@'){bx=i;by=j;}
            }
            getchar();
        }
        cnt=0;map[bx][by]='#';
        dfs(bx,by);
        printf("%d\n",cnt);
    }
    return 0;
}

 hdu 1026 搜索題

BFS+優先隊列,因為要輸出路徑,比較麻煩,所以要用到結構體

View Code
#include<stdio.h>
#include<queue>
#define maxn 101
using namespace std;
int m,n;
int dir[4][2]={1,0,-1,0,0,-1,0,1};
struct node
{
    int x,y,time;
    friend int operator<(node a,node b)
    {
        return a.time>b.time;
    }
};
struct cmap
{
    char c;
    int nx,ny;
}map[maxn][maxn];
int fight[maxn][maxn],vis[maxn][maxn];
priority_queue<node>q;
int bfs()
{
    node now,next;
    int i;
    char s;
    while(!q.empty())q.pop();
    now.x=n-1;now.y=m-1;
    s=map[now.x][now.y].c;
    if(s>='1'&&s<='9')
    {
        now.time=map[now.x][now.y].c-'0';
        fight[now.x][now.y]=map[now.x][now.y].c-'0';
    }
    else now.time=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.top();q.pop();
        if(now.x==0&&now.y==0)return now.time;
        for(i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y].c!='X'&&!vis[next.x][next.y])
            {
                 vis[next.x][next.y]=1;
                 s=map[next.x][next.y].c;
                 if(s>='1'&&s<='9')
                 {
                     fight[next.x][next.y]=s-'0';
                     next.time=now.time+s-'0'+1;
                 }
                 else next.time=now.time+1;
                 map[next.x][next.y].nx=now.x;
                 map[next.x][next.y].ny=now.y;
                 q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    int i,j;
    //freopen("int.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%c",&map[i][j].c);
            }
            getchar();
        }
        memset(vis,0,sizeof(vis));
        memset(fight,0,sizeof(fight));
        int ans=bfs();
        if(ans==-1)printf("God please help our poor hero.\n");
        else
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
            int cnt=1,x=0,y=0;
            while(cnt<=ans)
            {
                int fx=map[x][y].nx,fy=map[x][y].ny;
                printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,x,y,fx,fy);
                for(int j=1;j<=fight[fx][fy];j++)
                printf("%ds:FIGHT AT (%d,%d)\n",cnt++,fx,fy);
                x=fx;y=fy;
            }
        }
        printf("FINISH\n");
    }
    return 0;
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM