poj 2935 Basic Wall Maze

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
struct node {

    int x, y;
    string str;
};

struct Node {

    int x1, y1;
    int x2, y2;
}wall[3];

int dir[4][2] = {{0, -1},{0, 1},{-1, 0},{1, 0}};
char ch[4] = {'N', 'S', 'W', 'E'};
int vist[8][8];

bool judge(int x,int y) {

    if(x >= 1 && x <= 6 && y >= 1 && y <= 6)
        return true;
    return false;
}

bool ok(node p1, node p2, int i) {  //对墙的处理;

    for(int j = 0; j < 3; j++) {
    if(i < 2) {



            if(wall[j].y1 == wall[j].y2 && wall[j].x1 <p1.x && wall[j].x2 >= p1.x) {

                if(i == 0 && p2.y == wall[j].y1)
                  return false;
                if(i == 1 && p1.y == wall[j].y1)
                    return false;

            }
     }else {

            if(wall[j].x1 == wall[j].x2 && wall[j].y1 < p1.y && wall[j].y2 >= p1.y) {

                if(i == 2 && p2.x == wall[j].x1)
                    return false;
                if(i == 3&&p1.x == wall[j].x1)
                    return false;
            }


      }
    }
    return true;
}
void dfs(int sa, int sb, int ea, int eb) {

    node cur, next;
    memset(vist, 0, sizeof(vist));
    queue<node>que;
    cur.x = sa;
    cur.y = sb;
    cur.str = "";
    vist[sa][sb] = 1;
    que.push(cur);
    while(!que.empty()) {

    cur = que.front();
      que.pop();

      if(cur.x == ea&&cur.y == eb) {

        cout<<cur.str<<endl;
        return;
      }
     for(int  i= 0; i < 4; i++) {

         next.x = cur.x + dir[i][0];
        next.y = cur.y + dir[i][1];
        if(!vist[next.x][next.y] && judge(next.x, next.y) && ok(cur, next, i)) {

            next.str = cur.str + ch[i];
            vist[next.x][next.y] = 1;
            que.push(next);
        }
     }
    }
}

int main()
{
    int sa, sb, ea, eb;
    while(scanf("%d%d", &sa, &sb) != EOF) {

        if(sa == 0 && sb == 0)
            break;
        scanf("%d%d", &ea, &eb);
        for(int i = 0; i < 3; i++)
            scanf("%d%d%d%d", &wall[i].x1, &wall[i].y1, &wall[i].x2, &wall[i].y2);
        dfs(sa, sb, ea, eb);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值