POJ 3083 Children of the Candy Corn(BFS+DFS)

题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105916#problem/H

代码:

#include<stdio.h>
#include<string.h>
#include<queue>

using namespace std;
char maps[45][45];
int book[45][45];
int fx[4]= {1,0,-1,0};
int fy[4]= {0,1,0,-1};
int n,m;
int startx,starty;
struct node
{
    int x,y;
    int step;
} ui,op;
int step;
int dl[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int dr[][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int dfs(int x, int y, int d, int step, int dir[][2])
{
    for(int i=0; i<4; i++)
    {
        int j = ((d-1+4)%4+i)%4;
        int nx = x+dir[j][0];
        int ny = y+dir[j][1];

        if(maps[nx][ny]=='E') return step+1;
        if(nx < 0 || ny < 0 || nx>m || ny>n) continue;
        if(maps[nx][ny] == '#') continue;
        //printf("%d %d\n",nx,ny);
        return dfs(nx, ny, j, step+1, dir);
    }
}

void bfs(int x,int y)
{
    memset(book,0,sizeof(book));
    ui.x=x;
    ui.y=y;
    ui.step=1;
    queue<node>q;
    book[x][y]=1;
    q.push(ui);

    while(!q.empty())
    {
        ui=q.front();
        q.pop();

        if(maps[ui.x][ui.y]=='E')
        {
            printf("%d\n",ui.step);
            return;
        }

        for(int i=0; i<4; i++)
        {
            op.x=ui.x+fx[i];
            op.y=ui.y+fy[i];

            if(op.x>=0&&op.x<m&&op.y>=0&&op.y<n)
            {
                if(maps[op.x][op.y]!='#')
                {
                    if(book[op.x][op.y]==0)
                    {
                        op.step=ui.step+1;
                        book[op.x][op.y]=1;
                        q.push(op);
                    }
                }
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(maps,0,sizeof(maps));

        for(int i=0; i<m; i++)
        {
            scanf("%s",&maps[i]);
            for(int j=0; j<n; j++)
            {
                if(maps[i][j]=='S')
                {
                    startx=i;
                    starty=j;
                }
            }
        }
        //printf("%d %d\n",startx,starty);
        int d1,d2;
        if(startx == 0)
        {
            d1 = 3;
            d2 = 3;
        }
        else if(startx == m-1)
        {
            d1 = 1;
            d2 = 1;
        }
        else if(starty == 0)
        {
            d1 = 2;
            d2 = 0;
        }
        else if(starty == n-1)
        {
            d1 = 0;
            d2 = 2;
        }
        step=0;
        printf("%d ", dfs(startx, starty, d1, 1, dl));
        //printf("\n1\n");
        //printf("%d %d\n",startx,starty);
        step=0;
        printf("%d ", dfs(startx, starty, d2, 1, dr));
        bfs(startx,starty);
        //getchar();
    }
}


深搜的代码是照网上抄的。

太难了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值