#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <queue>
#include <cassert>
using namespace std;
const int INF = 0xfffffff;
const int MAX = 64;
int a[MAX][MAX];
int n, m, ans;
int forwd[4][3][2] = {-3,0, -2,0, -1,0, 0,3, 0,2, 0,1,
3,0, 2,0, 1,0, 0,-3, 0,-2, 0,-1};
int turn[4][2] = {1,3, 0,2, 1,3, 0,2};
//int beside[4][2][2] = {0,-1, 0,1, -1,0, 1,0, 0,-1, 0,1, -1,0, 1,0};
int beside[4][2] = {0,0, 0,1, 1,0, 1,1};
bool vis[MAX][MAX][4];
struct Node {
int x, y;
int to; //朝向, 0 1 2 3
// n e s w
int step;
bool operator==(Node B)const {
return x == B.x && y == B.y;
}
} B, E; //起点,终点
inline int rest(int x, int y) {
return (abs(x-E.x)+2)/3 + (abs(y-E.y)+2)/3;
}
inline bool valid(Node f, Node p) {
bool ok = p.x >= 0 && p.y >= 0
&& p.x < n-1 && p.y < m-1
&& !vis[p.x][p.y][p.to];
if (!ok) return false;
// bool PRINT = f.x == 6 && f.y == 1 && p.x == 3 && p.y == 1;
int lx = min(f.x, p.x), rx = max(f.x, p.x);
int ly = min(f.y, p.y), ry = max(f.y, p.y);
for (int i = lx; i <= rx; ++i)
for (int j = ly; j <= ry; ++j)
for (int k = 0; k < 4; ++k) {
if (a[i+beside[k][0]][j+beside[k][1]])
return false;
}
return true;
}
int bfs(void) {
memset(vis, false, sizeof(vis));
queue<Node> Q;
Node now, next;
Q.push(B);
vis[B.x][B.y][B.to] = true;
while (!Q.empty()) {
now = Q.front(); Q.pop();
if (now == E) {
return ans = now.step;
}
// printf("\n\nFrom:%d,%d,%d,%d\n", now.x, now.y, now.to, now.step);
//if (now.step + rest(now.x, now.y) >= ans) continue;
/* 转向 */
next.x = now.x, next.y = now.y, next.step = now.step + 1;
for (int i = 0; i < 2; ++i) {
next.to = turn[now.to][i];
if (valid(now, next)) {
//printf("trun: %d,%d,%d,%d\n", next.x, next.y, next.to, next.step);
Q.push(next);
vis[next.x][next.y][next.to] = true;
}
}
/* move */
next.to = now.to;
for (int i = 0; i < 3; ++i) {
next.x = now.x + forwd[now.to][i][0];
next.y = now.y + forwd[now.to][i][1];
if (valid(now, next)) {
//printf("go: %d,%d,%d,%d\n", next.x, next.y, next.to, next.step);
Q.push(next);
vis[next.x][next.y][next.to] = true;
}
}//getchar();
}
return -1;
}
int main() {
char stat[MAX];
while (~scanf(" %d %d", &n, &m) && n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf(" %d", &a[i][j]);
}
}//in
scanf(" %d %d %d %d", &B.x, &B.y, &E.x, &E.y);
--B.x, --B.y, --E.x, --E.y;
scanf(" %s", stat);
if (strcmp(stat, "south") == 0) {
B.to = 2;
} else if (strcmp(stat, "north") == 0) {
B.to = 0;
} else if (strcmp(stat, "east") == 0) {
B.to = 1;
} else if (strcmp(stat, "west") == 0) {
B.to = 3;
}
assert(B.to >= 0 && B.to < 4);
B.step = 0;
//printf("%d\n", a[3][2]);
printf("%d\n", bfs());
}
return 0;
}
下午都贡献给这题了,坑爹.其实就是BFS水题,开始一看觉得50*50好大了....以为是神马IDA*呐那那那那那........坑爹!不过用vim写代码好辛苦,太辛苦了,暂且不用了.太累了!!!当然,前提是我是菜狗...就这样一题浪费我好多时间,泥煤泥煤泥煤...
51416/D辛苦屎了,骗纸呐o o
最新推荐文章于 2015-08-05 16:47:23 发布