Problem D
独轮车
时限:1000ms 内存限制:10000K 总时限:3000ms
描述:
独轮车的轮子上有红、黄、蓝、白、绿(依顺时针序)5种颜色,在一个如下图所示的20*20的迷宫内每走一个格子,轮子上的颜色变化一次。独轮车只能向前推或在原地转向。每走一格或原地转向90度均消耗一个单位时间。现给定一个起点(S)和一个终点(T),求独轮车以轮子上的指定颜色到达终点所需的最短时间。
输入:
本题包含一个测例。测例中分别用一个大写字母表示方向和轮子的颜色,其对应关系为:E-东、S-南、W-西、N-北;R-红、Y-黄、B-蓝、W-白、G-绿。在测试数据的第一行有以空格分隔的两个整数和两个大写字母,分别表示起点的坐标S(x,y)、轮子的颜色和开始的方向,第二行有以空格分隔的两个整数和一个大写字母,表示终点的坐标T(x,y)和到达终点时轮子的颜色,从第三行开始的20行每行内包含20个字符,表示迷宫的状态。其中'X'表示建筑物,'.'表示路.
输出:
在单独的一行内输出一个整数,即满足题目要求的最短时间。
输入样例:
3 4 R N
15 17 Y
XXXXXXXXXXXXXXXXXXXX
X.X...XXXXXX......XX
X.X.X.....X..XXXX..X
X.XXXXXXX.XXXXXXXX.X
X.X.XX....X........X
X...XXXXX.X.XX.X.XXX
X.X.XX....X.X..X.X.X
X.X.X..XX...XXXX.XXX
X.X.XX.XX.X....X.X.X
X.X....XX.X.XX.X.X.X
X.X.X.XXXXX.XX.X.XXX
X.X.X.XXXXX....X...X
X.X.......X.XX...X.X
X.XXX.XXX.X.XXXXXXXX
X.....XX.......X...X
XXXXX....X.XXXXXXX.X
X..XXXXXXX.XXX.XXX.X
X.XX...........X...X
X..X.XXXX.XXXX...XXX
XXXXXXXXXXXXXXXXXXXX
输出样例:
56
#include <iostream>
#include <queue>
using namespace std;
struct rec {
int x, y, color, dir;
};
queue <rec> q;
rec st, ed;
char s[21][21];
int d[21][21][4][5];
int start_x, start_y, end_x, end_y;
char colour_st, colour_ed, dire;
int ds[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};//北东南西 四个方向
bool can(int x, int y) {
if (x < 1 || y < 1 || y > 20 || x > 20) {
return false;
}
if (s[x][y] == 'X') {
return false;
}
return true;
}
void init() {
//初始方向
if (dire == 'N') {
st.dir = 0;
}
if (dire == 'E') {
st.dir = 1;
}
if (dire == 'S') {
st.dir = 2;
}
if (dire == 'W') {
st.dir = 3;
}
//初始颜色
if (colour_st == 'R') {
st.color = 0;
}
if (colour_st == 'Y') {
st.color = 1;
}
if (colour_st == 'B') {
st.color = 2;
}
if (colour_st == 'W') {
st.color = 3;
}
if (colour_st == 'G') {
st.color = 4;
}
//终点颜色
if (colour_ed == 'R') {
ed.color = 0;
}
if (colour_ed == 'Y') {
ed.color = 1;
}
if (colour_ed == 'B') {
ed.color = 2;
}
if (colour_ed == 'W') {
ed.color = 3;
}
if (colour_ed == 'G') {
ed.color = 4;
}
}
rec nextStep(rec now, int i) {
rec next;
if (i == 0) {
next.x = now.x + ds[now.dir][0];
next.y = now.y + ds[now.dir][1];
next.color = (now.color + 1) % 5;
next.dir = now.dir;
} else if (i == 1) { //向右转
next.x = now.x;
next.y = now.y;
next.color = now.color;
next.dir = (now.dir + 1) % 4;
} else if (i == 2) { //向右转
next.x = now.x;
next.y = now.y;
next.color = now.color;
next.dir = (now.dir + 3) % 4;
}
return next;
}
int main() {
cin >> start_x >> start_y >> colour_st >> dire;
cin >> end_x >> end_y >> colour_ed;
for (int i = 1; i <= 20; i++) {
cin >> s[i] + 1;
}
init();
memset(d, -1, sizeof(d));
d[start_x][start_y][st.dir][st.color] = 0;
st.x = start_x, st.y = start_y;
ed.x = end_x, ed.y = end_y;
q.push(st);
while (q.size()) {
rec now = q.front();
q.pop();
for (int i = 0; i < 3; i++) {
rec next = nextStep(now, i);
if (!can(next.x, next.y)) {
continue;
}
if (d[next.x][next.y][next.dir][next.color] == -1) {
d[next.x][next.y][next.dir][next.color] = d[now.x][now.y][now.dir][now.color] + 1;
q.push(next);
if (next.x == ed.x && next.y == ed.y && next.color == ed.color) {
cout << d[next.x][next.y][next.dir][next.color] << endl;
return 0;
}
}
}
}
return 0;
}