您的位置:首页 > 其它

【算法竞赛入门经典】7.5 路径寻找问题 例题7-9 UVa1601(1)

2018-02-07 16:38 513 查看

【算法竞赛入门经典】7.5 路径寻找问题 例题7-9 UVa1601

算法竞赛入门经典75 路径寻找问题 例题7-9 UVa1601
例题UVa1601

分析

本题储存结构

样例实现代码

结果

例题UVa1601

which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corridors. One morning, you found that the ghosts are not in the positions where they are supposed to be. Ah, yesterday was Halloween. Believe or not, paranormal spirits have moved them around the corridors in the night. You have to move them into their right positions before guests come. Your manager is eager to know how long it takes to restore the ghosts.

A floor consists of a matrix of square cells. A cell is either a wall cell where ghosts cannot move into or a corridor cell where they can.

At each step, you can move any number of ghosts simultaneously. Every ghost can either stay in the current cell, or move to one of the corridor cells in its 4-neighborhood (i.e. immediately left, right, up or down), if the ghosts satisfy the following conditions:

1. No more than one ghost occupies one position at the end of the step.

2. No pair of ghosts exchange their positions one another in the step.

For example, suppose ghosts are located as shown in the following (partial) map, where a sharp sign (‘#’) represents a wall cell and ‘a’, ‘b’, and ‘c’ ghosts.



The following four maps show the only possible positions of the ghosts after one step.



Input

The input consists of at most 10 datasets, each of which represents a floor map of a house. The format of a dataset is as follows.



w, h and n in the first line are integers, separated by a space. w and h are the floor width and height of the house, respectively. n is the number of ghosts. They satisfy the following constraints.

Subsequent h lines of w characters are the floor map. Each of cij is either:

• a ‘#’ representing a wall cell,

• a lowercase letter representing a corridor cell which is the initial position of a ghost,

• an uppercase letter representing a corridor cell which is the position where the ghost corresponding to its lowercase letter is supposed to be, or

In each map, each of the first n letters from a and the first n letters from A appears once and only once. Outermost cells of a map are walls; i.e. all characters of the first and last lines are sharps; and the first and last characters on each line are also sharps. All corridor cells in a map are connected; i.e. given a corridor cell, you can reach any other corridor cell by following corridor cells in the 4-neighborhoods. Similarly, all wall cells are connected. Any 2×2 area on any map has at least one sharp. You can assume that every map has a sequence of moves of ghosts that restores all ghosts to the positions where they are supposed to be.

The last dataset is followed by a line containing three zeros separated by a space.

Output

For each dataset in the input, one line containing the smallest number of steps to restore ghosts into the positions where they are supposed to be should be output. An output line should not contain extra characters such as spaces.

Sample Input



Sample Output

7

36

7

分析。

实际上,这就是一个BFS求最短路的问题。但是直接进行暴力的BFS容易造成超时。鉴于题目中说每一个2X2中都有障碍物,那么可以认为,实际上能走的路并不多,那么我们可以进行提前扫描,保存下来可以走的路的一个新图,那个就不需要BFS每一个情况中去判断下一个点能不能走,大大减少了时间消耗。因此,用ID编号存下每一个可以走的节点,用G[i][j]来记录每个节点i可用的下一个节点。TIP:直接使用vector等等方式也可以。

此外,还有一个双向广度优先搜索。另一文中再写吧。

本题储存结构

本题采用int变量cnt记录可使用的节点个数,采用数组nex[]记录每个节点可移动位置的数量,采用G[i][j]记录每个节点i可使用的下一个节点编号【对应cnt】

采用数组x,y记录每个节点cnt的实际坐标。(建图时使用,实际上在dfs内部已经不需要具体坐标了,因为已经在nex和G中记录了足够的信息)

样例实现代码

#include<iostream>
#include<queue>
#include<cstring>
#define maxs 20
#define maxn 150
using namespace std;
int s[3], d[3];
int nex[maxn], G[maxn][5];
int dx[] = { -1,1,0,0,0 };
int dy[] = { 0,0,-1,1,0 };
int dis[maxn][maxn][maxn];
int getID(int a, int b, int c) {
return ((a << 16) | (b << 8) | c);
}
bool conflict(int a, int b, int a2, int b2) {
if (a2 == b2 || (a == b2) && (b == a2))
return true;
return false;
}
int dfs() {
memset(dis, -1, sizeof(dis));
int pos = getID(s[0], s[1], s[2]);
queue<int>q;
q.push(pos);
dis[s[0]][s[1]][s[2]] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
int a = (u >> 16) & 0xff, b = (u >> 8) & 0xff, c = u & 0xff;
if (a == d[0] && b == d[1] && c == d[2])
return dis[a][b][c];
for (int i = 0; i < nex[a]; i++) {
int a2 = G[a][i];
for (int j = 0; j < nex[b]; j++) {
int b2 = G[b][j];
if (conflict(a, b, a2, b2))
continue;
for (int k = 0; k < nex[c]; k++) {
int c2 = G[c][k];
if (conflict(a, c, a2, c2) | conflict(b, c, b2, c2))
continue;
if (dis[a2][b2][c2] == -1) {
dis[a2][b2][c2] = dis[a][b][c] + 1;
q.push(getID(a2, b2, c2));
}
}
}
}
}
return -1;
}
int main() {
int w, h, n;
while (cin >> w >
b3ab
> h >> n) {
if(n==0)
break;
getchar();
char maze[20][20];
for (int i = 0; i < h; i++)
fgets(maze[i], 20, stdin);
int cnt = 0, x[maxn], y[maxn], ID[maxn][maxn];
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if (maze[i][j] != '#') {
ID[i][j] = cnt;
x[cnt] = i;
y[cnt] = j;
if (islower(maze[i][j])) {
s[maze[i][j] - 'a'] = cnt;
}
if (isupper(maze[i][j])) {
d[maze[i][j] - 'A'] = cnt;
}
cnt++;
}
for (int i = 0; i < cnt; i++) {
nex[i] = 0;
int nowx, nowy;
for (int j = 0; j < 5; j++) {
nowx = x[i] + dx[j];
nowy = y[i] + dy[j];
if (nowx < 0 || nowx >= h || nowy < 0 || nowy >= w)
continue;
if (maze[nowx][nowy] != '#') {
G[i][nex[i]] = ID[nowx][nowy];
nex[i]++;
}
}
}
if (n <= 2) {
nex[cnt] = 1;
G[cnt][0] = cnt;
s[2] = d[2] = cnt++;
}
if (n <= 1) {
nex[cnt] = 1;
G[cnt][0] = cnt;
s[1] = d[1] = cnt++;
}
cout << dfs() << endl;
}
return 0;
}


结果

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