您的位置:首页 > 其它

【HDOJ】1484 Basic wall maze

2015-02-21 12:10 260 查看
BFS。

/* 1484 */
#include <iostream>
#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef struct {
int x, y;
string s;
} node_t;

bool m[8][8][4];
bool visit[8][8];
char ds[5] = "NSWE";
int dir[4][2] = {
-1,0,1,0,0,-1,0,1
};

const int n = 6;
int bx, by, ex, ey;
string ans;

void bfs() {
int x = bx, y = by;
int i, j, k;
queue<node_t> Q;
string s;
node_t nd, tmp;

memset(visit, false, sizeof(visit));
visit[x][y] = true;
nd.x = x;
nd.y = y;
nd.s = "";
Q.push(nd);

while (!Q.empty()) {
nd = Q.front();
Q.pop();
for (i=0; i<4; ++i) {
if (m[nd.x][nd.y][i])
continue;
x = nd.x + dir[i][0];
y = nd.y + dir[i][1];
if (x<=0 || x>6 || y<=0 || y>6 || visit[x][y])
continue;
visit[x][y] = true;
tmp.x = x;
tmp.y = y;
tmp.s = nd.s + ds[i];
if (x==ex && y==ey) {
ans = tmp.s;
return ;
}
Q.push(tmp);
}
}
}

int main() {
int i, j, k;
int x, y;
int a, b, c, d;

#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif

while (scanf("%d %d",&by,&bx)!=EOF && (by||bx)) {
scanf("%d %d", &ey, &ex);
memset(m, false, sizeof(m));
for (i=0; i<3; ++i) {
scanf("%d%d%d%d", &a,&b,&c,&d);
if (b == d) {
// north & south need to mask
for (y=a+1; y<=c; ++y) {
m[b][y][1] = true;
m[b+1][y][0] = true;
}
} else {
// west & east need to mask
for (x=b+1; x<=d; ++x) {
m[x][a][3] = true;
m[x][a+1][2] = true;
}
}
}
bfs();
printf("%s\n", ans.c_str());
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: