Why WA 8?
Послано
李春骐 3 июл 2009 16:37
I used SPFA, but failed in test 8. Who can help me?
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
int op[] = {0, 2, 1, 5, 6, 3, 4};
int r[7][7] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 4, 5, 6, 3},
{0, 0, 0, 6, 3, 4, 5},
{0, 6, 4, 0, 1, 0, 2},
{0, 3, 5, 2, 0, 1, 0},
{0, 4, 6, 0, 2, 0, 1},
{0, 5, 3, 1, 0, 2, 0}
};
struct State {
int x, y, b, f; //表示坐标(x, y)和底面和前面上的标号
} beg, end, que[24*8*8*100];
bool inque[9][9][7][7];
int dist[9][9][7][7], a[7], path[24*8*8*2];
int ans = 1000000000, last;
char s1[4], s2[4];
void eval(State &s, int x, int y, int b, int f)
{
s.x = x, s.y = y, s.b = b, s.f = f;
}
bool check(State &s)
{
if (s.x == 0 || s.x > 8 || s.y == 0 || s.y > 8) return 0;
return 1;
}
void SPFA()
{
int closed = 0, open = 2;
memset(dist, 0x3f, sizeof(dist));
eval(que[1], beg.x, beg.y, beg.b, beg.f);
inque[beg.x][beg.y][beg.b][beg.f] = 1;
dist[beg.x][beg.y][beg.b][beg.f] = 0;
do {
closed++;
inque[que[closed].x][que[closed].y][que[closed].b][que[closed].f] = 0;
if (que[closed].x == end.x && que[closed].y == end.y && dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] < ans) {
ans = dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f];
last = closed;
}
State newst;
// forward
eval(newst, que[closed].x, que[closed].y-1, que[closed].f, op[que[closed].b]);
if (check(newst)) {
if (dist[newst.x][newst.y][newst.b][newst.f] > dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b]) {
dist[newst.x][newst.y][newst.b][newst.f] = dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b];
if (!inque[newst.x][newst.y][newst.b][newst.f]) {
inque[newst.x][newst.y][newst.b][newst.f] = 1;
que[open] = newst;
path[open] = closed;
open++;
}
}
}
// right
eval(newst, que[closed].x+1, que[closed].y, r[que[closed].b][que[closed].f], que[closed].f);
if (check(newst)) {
if (dist[newst.x][newst.y][newst.b][newst.f] > dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b]) {
dist[newst.x][newst.y][newst.b][newst.f] = dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b];
if (!inque[newst.x][newst.y][newst.b][newst.f]) {
inque[newst.x][newst.y][newst.b][newst.f] = 1;
que[open] = newst;
path[open] = closed;
open++;
}
}
}
// backward
eval(newst, que[closed].x, que[closed].y+1, op[que[closed].f], que[closed].b);
if (check(newst)) {
if (dist[newst.x][newst.y][newst.b][newst.f] > dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b]) {
dist[newst.x][newst.y][newst.b][newst.f] = dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b];
if (!inque[newst.x][newst.y][newst.b][newst.f]) {
inque[newst.x][newst.y][newst.b][newst.f] = 1;
que[open] = newst;
path[open] = closed;
open++;
}
}
}
// left
eval(newst, que[closed].x-1, que[closed].y, op[r[que[closed].b][que[closed].f]], que[closed].f);
if (check(newst)) {
if (dist[newst.x][newst.y][newst.b][newst.f] > dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b]) {
dist[newst.x][newst.y][newst.b][newst.f] = dist[que[closed].x][que[closed].y][que[closed].b][que[closed].f] + a[newst.b];
if (!inque[newst.x][newst.y][newst.b][newst.f]) {
inque[newst.x][newst.y][newst.b][newst.f] = 1;
que[open] = newst;
path[open] = closed;
open++;
}
}
}
} while (closed < open);
}
void print(int i)
{
if (que[i].x == beg.x && que[i].y == beg.y) {
printf("%d %c%d", ans+a[beg.b], beg.x+'a'-1, beg.y);
return;
}
print(path[i]);
printf(" %c%d", que[i].x+'a'-1, que[i].y);
}
int main()
{
scanf("%s%s%d%d%d%d%d%d", s1, s2, &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]);
beg.x = s1[0] - 'a' + 1;
beg.y = s1[1] - '0';
beg.b = 5;
beg.f = 1;
end.x = s2[0] - 'a' + 1;
end.y = s2[1] - '0';
SPFA();
print(last);
printf("\n");
return 0;
}