POJ 3083 Children of the Candy Corn

14 篇文章 0 订阅
4 篇文章 0 订阅

明白题意,S->E,第一个输出左偏到达的步数,第二个输出右偏到达的步数,第三个输出最短路用BFS

左偏的意思是,开始方向任意定义并且在后面都始终不会变,先看左边的格子是否能走,然后逆时针(就是从左边的第一个格子往右偏).

右偏,开始跟左一样,先看右边的格子是否能走,然后顺时针(说白了就是从右边的第一个格子往左偏),

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

struct node
{
    int x;
    int y;
    int step;
}que[2100];

int map[50][50];//能走为1  不能为0
int book[50][50];//标记
int flag;//标记变量,是否能找到
int ansl, ansr, ans;//l的步数,r的步数,最短步数
int ex, ey, sx, sy;//S,E的位置
int n, m;//行 列,输入的时候反着

void DfsL(int x,int y, int step, int face)//1,2,3,4,W,N,E,S
{
    if(flag==1) return;
    if(x==ex&&y==ey){
        flag = 1;
        ansl = step;
        return ;
    }
    if(face == 1){
        if(map[x+1][y])     DfsL(x+1,y,step+1,4);
        if(map[x][y-1])     DfsL(x,y-1,step+1,1);
        if(map[x-1][y])     DfsL(x-1,y,step+1,2);
        if(map[x][y+1])     DfsL(x,y+1,step+1,3);
    }else if(face == 2){
        if(map[x][y-1])     DfsL(x,y-1,step+1,1);
        if(map[x-1][y])     DfsL(x-1,y,step+1,2);
        if(map[x][y+1])     DfsL(x,y+1,step+1,3);
        if(map[x+1][y])     DfsL(x+1,y,step+1,4);
    }else if(face == 3){
        if(map[x-1][y])     DfsL(x-1,y,step+1,2);
        if(map[x][y+1])     DfsL(x,y+1,step+1,3);
        if(map[x+1][y])     DfsL(x+1,y,step+1,4);
        if(map[x][y-1])     DfsL(x,y-1,step+1,1);
    }else {
        if(map[x][y+1])     DfsL(x,y+1,step+1,3);
        if(map[x+1][y])     DfsL(x+1,y,step+1,4);
        if(map[x][y-1])     DfsL(x,y-1,step+1,1);
        if(map[x-1][y])     DfsL(x-1,y,step+1,2);
    }
}
void DfsR(int x, int y, int step, int face)
{
    if(flag == 1)   return;
    if(x == ex && y == ey){
        flag = 1;
        ansr = step;
        return;
    }
    if(face == 1){
        if(map[x-1][y])     DfsR(x-1,y,step+1,2);
        if(map[x][y-1])     DfsR(x,y-1,step+1,1);
        if(map[x+1][y])     DfsR(x+1,y,step+1,4);
        if(map[x][y+1])     DfsR(x,y+1,step+1,3);
    }else if(face == 2){
        if(map[x][y+1])     DfsR(x,y+1,step+1,3);
        if(map[x-1][y])     DfsR(x-1,y,step+1,2);
        if(map[x][y-1])     DfsR(x,y-1,step+1,1);
        if(map[x+1][y])     DfsR(x+1,y,step+1,4);
    }else if(face == 3){
        if(map[x+1][y])     DfsR(x+1,y,step+1,4);
        if(map[x][y+1])     DfsR(x,y+1,step+1,3);
        if(map[x-1][y])     DfsR(x-1,y,step+1,2);
        if(map[x][y-1])     DfsR(x,y-1,step+1,1);
    }else {
        if(map[x][y-1])     DfsR(x,y-1,step+1,1);
        if(map[x+1][y])     DfsR(x+1,y,step+1,4);
        if(map[x][y+1])     DfsR(x,y+1,step+1,3);
        if(map[x-1][y])     DfsR(x-1,y,step+1,2);
    }
}
void BFS()
{
    memset(book,0,sizeof(book));
    que[0].x = sx;
    que[0].y  = sy;
    que[0].step = 1;
    book[sx][sy] = 1;
    int tail = 1;
    int head = 0;
    int xx, yy, step;
    while(head < tail){
        xx = que[head].x;
        yy = que[head].y;
        step = que[head].step;
        head++;
        if(xx==ex&&yy==ey){
            ans = step;
            return;
        }
        if(!book[xx][yy-1]&&map[xx][yy-1]){
            book[xx][yy-1] = 1;
            que[tail].x = xx;
            que[tail].y = yy-1;
            que[tail].step = step+1;
            tail++;
        }
        if(!book[xx][yy+1]&&map[xx][yy+1]){
            book[xx][yy+1] = 1;
            que[tail].x = xx;
            que[tail].y = yy+1;
            que[tail].step = step+1;
            tail++;
        }
        if(!book[xx-1][yy]&&map[xx-1][yy]){
            book[xx-1][yy] = 1;
            que[tail].x = xx-1;
            que[tail].y = yy;
            que[tail].step = step+1;
            tail++;
        }
        if(!book[xx+1][yy]&&map[xx+1][yy]){
            book[xx+1][yy];
            que[tail].x = xx+1;
            que[tail].y = yy;
            que[tail].step = step+1;
            tail++;
        }
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    char s[50];
    int i, j;
    while(t--){
        memset(map,0,sizeof(map));
        scanf("%d %d", &m, &n);
        for(i = 1;i <=n;i++){
            cin >> s;
            for(j = 0;j < m;j++){
                if(s[j] == '.'){
                    map[i][j+1] = 1;
                }else if(s[j] =='S'){
                    map[i][j+1] = 1;
                    sx = i;
                    sy = j+1;
                }else if(s[j] == 'E'){
                    map[i][j+1] = 1;
                    ex = i;
                    ey = j+1;
                }
            }
        }
        flag = 0;
        DfsL(sx,sy,1,1);
        flag = 0;
        DfsR(sx,sy,1,1);
        BFS();
        printf("%d %d %d\n", ansl, ansr, ans);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值