POJ 2935 Basic Wall Maze (BFS)

//poj 2935
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

typedef struct path
{
    int prex;
    int prey;
    char dir;
}path;

typedef struct node
{
    int x;
    int y;
}node;

path trace[7][7];
int vis[7][7];
node s,t;

node wall[3][2];
queue<node> Q;

int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};

bool check(node u, int k)
{
    for(int i=0;i<3;i++)
    {
        if(wall[i][0].x==wall[i][1].x)
        {
            if(u.y>wall[i][0].y && u.y<=wall[i][1].y)
            {
                if(u.x==wall[i][0].x && k==2) return false;
                if(u.x==wall[i][0].x+1 && k==0) return false;
            }
        }
        else
        {
            if(u.x>wall[i][0].x && u.x<=wall[i][1].x)
            {
                if(u.y==wall[i][0].y && k==1) return false;
                if(u.y==wall[i][0].y+1 && k==3) return false;
            }
        }
    }
    return true;
}

char trans(int i)
{
    if(i==0) return 'N';
    else if(i==1) return 'E';
    else if(i==2) return 'S';
    else return 'W';
}

void bfs()
{
    node u, v;

    vis[s.x][s.y]=1;
    Q.push(s);
    while(!Q.empty())
    {
        u=Q.front(); Q.pop();
        for(int i=0;i<4;i++)
        {
            v.x=u.x+dir[i][0]; v.y=u.y+dir[i][1];
            if(v.x<1 || v.x>6 || v.y<1 || v.y>6 || vis[v.x][v.y]) continue;
            if(!check(u,i)) continue;    //check wall;
            Q.push(v);
            trace[v.x][v.y].prex=u.x; trace[v.x][v.y].prey=u.y;
            trace[v.x][v.y].dir=trans(i);
            vis[v.x][v.y]=1;
        }
    }
}

void print(int x, int y)
{
    if(x==s.x && y==s.y) return;
    print(trace[x][y].prex,trace[x][y].prey);
    cout<<trace[x][y].dir;
}

int main()
{
    while(1)
    {
        cin>>s.y>>s.x;
        if(s.x==0 && s.y==0) break;
        cin>>t.y>>t.x;
        for(int i=0;i<3;i++)
            cin>>wall[i][0].y>>wall[i][0].x
               >>wall[i][1].y>>wall[i][1].x;
        memset(vis,0,sizeof(vis));
        bfs();
        print(t.x,t.y);
        cout<<endl;
    }

    return 0;
}

/*********
input:
1 6
2 6
0 0 1 0
1 5 1 6
1 5 3 5
0 0

output:
NEEESWW
**********/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值