POJ2935 Basic Wall Maze bfs记录路径

链接:   POJ2935

题意:

6 X 6的地图   格子和格子可能有墙      整个地图中有三道墙      求起点起点到终点的路径

本题中的墙可以理解为某a位置的X方向不能走   即用一个三维数组map[x][y][z]表示(x,y)的Z方向不能走

关于记录路径可以用一个pre数组记录每个坐标的前一个坐标的复合值  最后倒序输出方向即可

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node{
    int x,y;
} head;
int pre[10][10];
int vis[10][10];
bool map[7][7][4];               //实现墙的功能
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
char fx[3][3];                  //存方向名称
int startx,starty,endx,endy;
void bfs()
{
    queue<node>q;
    vis[head.x][head.y]=1;
    q.push(head);
    while(!q.empty())
    {
        head=q.front();
        q.pop();
        if(head.x==endx&&head.y==endy)
        {return;}
        int tx,ty;
        for(int i=0; i<4; i++)
        {
            tx=head.x+dir[i][0];
            ty=head.y+dir[i][1];
            if(tx<1||ty<1||tx>6||ty>6||vis[tx][ty]||map[head.x][head.y][i])
                continue;
            vis[tx][ty]=1;
            pre[tx][ty]=head.x*7+head.y;   //前一个坐标的负荷坐标
            q.push( {tx,ty});
        }
    }
}
void output()
{
    int i,j=0;
    int tx,ty;
    char q[49];
    fx[1][0]='E';fx[0][1]='S';fx[1][2]='W';fx[2][1]='N';         //x y 加1避免越界
    while(endx!=startx&&endy!=starty)
    {
        tx=pre[endx][endy]/7;
        ty=pre[endx][endy]%7;          //解密得前一个坐标
        q[j++]=fx[tx-endx+1][ty-endy+1];
        endx=tx;endy=ty;
    }
    for(j=j-2;j>=0;j--)
        cout<<q[j];
        cout<<endl;
    return ;
}
int main()
{
    while(scanf("%d%d",&head.y,&head.x)&&(head.x!=0))
    {
        scanf("%d%d",&endy,&endx);
        memset(map,false,sizeof(map));
        memset(pre,0,sizeof(pre));
        memset(vis,0,sizeof(vis));
        int i,j=3,ax,ay,bx,by,t;
        while(j--)                      //实现墙的功能
        {
            scanf("%d%d%d%d",&ay,&ax,&by,&bx);
            if(ay>by)
            {t=ay;ay=by;by=t;}
            if(ax>bx)
            {t=ax;ax=bx;bx=t;}
            if(ay==by)
            {
                for(i=ax+1; i<=bx; i++)
                {
                    map[i][ay][1]=1;
                    map[i][ay+1][3]=1;
                }
            }
            if(ax==bx)
            {
                for(i=ay+1; i<=by; i++)
                {
                    map[ax][i][0]=1;
                    map[ax+1][i][2]=1;
                }
            }
        }
        bfs();
        output();
    }
    return 0;
}
时间: 2024-08-25 08:40:18

POJ2935 Basic Wall Maze bfs记录路径的相关文章

22 poj2935 Basic Wall Maze --- bfs

对于每个点有四个方向可以走,把墙两边的点对应墙的方向能不能走给预处理一下,用vis[x][y][0..3]表示点(x,y)的该方向可不可以走. 然后bfs就好了,记录路径就把每个结点的前一个点记录下来,递归输出就可以了. #include<cstdio> #include<queue> #include<cstring> using namespace std; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; bool vis[7][

HDU 1484 Basic wall maze (dfs + 记忆化)

Basic wall maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168    Accepted Submission(s): 52 Special Judge Problem Description In this problem you have to solve a very simple maze consisti

POJ 2935 Basic Wall Maze

http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2794   Accepted: 1271   Special Judge Description In this problem you have to solve a very simple maze consisting of: a 6 by 6 grid of unit sq

HDU1026--Ignatius and the Princess I(BFS记录路径)

Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrint

SDUT2465其实玩游戏也得学程序(BFS记录路径问题)

BFS记录路径第一炮 题目连接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2465 思路:搜索找路径问题,DFS进行,调用     DFS(当前->当前点父节点)->起点,思想,以栈为储存结构,保存路径. 和以往BFS不一样的是地图储存在结构体里...注意 在DFS时候,BLF0是当前点的父节点,BLF1是其子节点,因为初始时cost是无限大,每次进出队列的过程都保证当前的cost是较小

poj 3984 迷宫问题 (BFS+记录路径)

题目连接:http://poj.org/problem?id=3984 题解:简单的BFS+记录路径,具体题解看代码注释. #include <iostream> #include <queue> #include <cstdio> using namespace std; struct point { int x; int y; }; queue<point>q; int map[5][5]; int vis[5][5];//标记走过的路 int g[4]

学霸的迷宫(BFS+记录路径)

1 //求从(sx.sy)到(gx.gy)的最短距离; 2 3 #include<iostream> 4 #include<cstdio> 5 #include<cstdio> 6 #include<queue> 7 #include<cstring> 8 #define INF 99999999 9 10 using namespace std; 11 12 typedef pair<int, int > P; //数对,记录位置

poj_3984_迷宫问题_(bfs+记录路径)

前两天状态一直很不好,不知道为什么,感觉什么都没有掌握,很想回家.还好,今天调整过来啦,acm,try my best. 前两天一道题都没有ac,不能再这样了,每天都必须有ac. 嗯,说说这道题. 这道题明明就很水,结果我做了一个下午,囧. 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description 定义一个二维数组: int maze[5]

hdu 1226 BFS + bfs记录路径

http://acm.hdu.edu.cn/showproblem.php?pid=1226 为了省空间,可以用vis数组初始化的时候初始化为-1, 发现一个BFS容易错的地方 开始一直WA在这里:就是我int tp=q.front();之后马上q.pop():了,然后才去判断是不是符合条件以break,这样就不能根据q.empty()==1认为没有找到ans 因为这里WA了 其实也可以vis[0] == -1来判断 比较不理解的是 当n==0的时候 %n==0的时候怎么处理 //#pragma