bfs(独轮车,UVA 10047)

几个需要注意的地方:

①向左转我用d=(d-1)%4,这是错误的,当d=0时d会变成-1,应该用d=(d+3)%4才对。

②关于坐标方面的问题,我们在程序实现的时候更喜欢用行与列的概念,而在数学上我们更喜欢用x轴与y轴的概念。但是事实上行是对应y,列是对应x,如果不注意的话很容易就混用了,混用的话会且只会搞错方向。具体解决办法也很简单,把x与y或行与列互换一下即可。或者你干脆别用xy轴的概念就好了。


解法:

在普通的迷宫问题中,每一格只有一个状态。但是在此题中,每一格有多个状态(还要考虑颜色与方向)。

那就多记录每一个格子不同状态的最短距离,然后照常跑bfs即可。

当然你也可以理解成在四维空间上的bfs。

任何bfs的本质都是状态的bfs。其中涉及状态的定义以及状态的转移。

格子的bfs的本质当然也是状态的bfs。

迷宫问题可以说是bfs最好理解的实例了。

但如果你对bfs的理解只停留在迷宫问题上,那理解就未免太狭隘了。


代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 30;
const int inf = 0x3f3f3f3f;

int dy[4]={ 0, 1, 0,-1};
int dx[4]={-1, 0, 1, 0};

struct st
{
    int x,y;
    int c;
    int d;
    int t;
    st(int x=0,int y=0,int c=0,int d=0,int t=0):x(x),y(y),c(c),d(d),t(t){}
    st f()
    {
        return st(x+dx[d],y+dy[d],(c+1)%5,d,t+1);
    }
    st l()
    {
        return st(x,y,c,(d+3)%4,t+1);
    }
    st r()
    {
        return st(x,y,c,(d+1)%4,t+1);
    }
};

st S,T;
int N,M;
char MAP[maxn][maxn];
int D[maxn][maxn][5][5];
int kase;

void Update(st now)
{
    D[now.x][now.y][now.c][now.d]=now.t;
}

bool ok(st to)
{
    bool OK=to.x>0&&to.x<=N&&to.y>0&&to.y<=M;
    return OK&&MAP[to.x][to.y]!='#'&&to.t<D[to.x][to.y][to.c][to.d];
}

bool ed(st to)
{
    return to.x==T.x&&to.y==T.y&&to.c==T.c;
}

int bfs()
{
    memset(D,inf,sizeof(D));
    queue<st>q;
    q.push(S);
    Update(S);
    while(!q.empty())
    {
        st now=q.front();
        q.pop();
        for(int i=0;i<3;i++)
        {
            st to;
            if(i==0) to=now.f();
            else if(i==1) to=now.l();
            else to=now.r();
            if(ok(to))
            {
                if(ed(to)) return to.t;
                Update(to);
                q.push(to);
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d %d",&N,&M)==2&&(N+M))
    {
        if(kase) puts("");
        for(int i=1;i<=N;i++)
        {
            scanf("%s",MAP[i]+1);
            for(int j=1;j<=M;j++)
            {
                if(MAP[i][j]=='S') S=st(i,j,0,0,0);
                if(MAP[i][j]=='T') T=st(i,j,0,0,0);
            }
        }
        int ans=bfs();
        printf("Case #%d\n",++kase);
        if(ans==-1) puts("destination not reachable");
        else printf("minimum time = %d sec\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想使用 BFS(广度优先搜索)算法来实现小车在迷宫中的路径规划,可以考虑在程序中使用坐标表示小车当前的位置。以下是一个示例: ```c //定义迷宫大小 #define MAZE_WIDTH 10 #define MAZE_HEIGHT 10 //定义迷宫地图 int maze[MAZE_HEIGHT][MAZE_WIDTH] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 0, 1, 0, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1, 0, 1}, {1, 0, 1, 1, 1, 1, 0, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; //定义坐标结构体 struct Coordinate { int x; int y; }; //定义队列结构体 struct Queue { Coordinate data[MAZE_WIDTH * MAZE_HEIGHT]; int front; int rear; }; //初始化队列 void initQueue(Queue *q) { q->front = 0; q->rear = 0; } //判断队列是否为空 int isQueueEmpty(Queue *q) { return (q->front == q->rear); } //入队 void enqueue(Queue *q, Coordinate c) { q->data[q->rear] = c; q->rear++; } //出队 Coordinate dequeue(Queue *q) { Coordinate c = q->data[q->front]; q->front++; return c; } //判断坐标是否合法 int isValidCoordinate(Coordinate c) { return (c.x >= 0 && c.x < MAZE_WIDTH && c.y >= 0 && c.y < MAZE_HEIGHT && maze[c.y][c.x] == 0); } //BFS算法 void bfs(Coordinate start, Coordinate end) { Queue q; initQueue(&q); int visited[MAZE_HEIGHT][MAZE_WIDTH] = {0}; //记录是否访问过 int distance[MAZE_HEIGHT][MAZE_WIDTH] = {0}; //记录距离 Coordinate prev[MAZE_HEIGHT][MAZE_WIDTH]; //记录路径 enqueue(&q, start); visited[start.y][start.x] = 1; while (!isQueueEmpty(&q)) { Coordinate current = dequeue(&q); if (current.x == end.x && current.y == end.y) { break; } Coordinate next; next.x = current.x + 1; next.y = current.y; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } next.x = current.x - 1; next.y = current.y; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } next.x = current.x; next.y = current.y + 1; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } next.x = current.x; next.y = current.y - 1; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } } //输出路径 Coordinate c = end; while (c.x != start.x || c.y != start.y) { Serial.print("("); Serial.print(c.x); Serial.print(", "); Serial.print(c.y); Serial.println(")"); c = prev[c.y][c.x]; } Serial.print("("); Serial.print(start.x); Serial.print(", "); Serial.print(start.y); Serial.println(")"); } ``` 此示例中,使用 `Coordinate` 结构体表示小车在迷宫中的坐标,使用 `Queue` 结构体实现 BFS 算法的队列。`isValidCoordinate` 函数判断坐标是否合法,`bfs` 函数实现广度优先搜索,并输出路径。在实际使用时,您需要根据具体情况修改和完善代码,例如根据传感器数据更新小车坐标等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值