poj 2935 Basic Wall Maze

来源:互联网 发布:mvc数据传到前台 编辑:程序博客网 时间:2024/06/05 23:58
#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
原创粉丝点击