2013
12-23

# HDU 1885 Key Task-状态压缩-[解题报告] C++

The Czech Technical University is rather old ― you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places.

The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.

The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.

You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.

The input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:

Note that it is allowed to have

• more than one exit,
• no exit at all,
• more doors and/or keys of the same color, and
• keys without corresponding doors and vice versa.
• You may assume that the marker of your position (“*”) will appear exactly once in every map.

There is one blank line after each map. The input is terminated by two zeros in place of the map size.

输出:

For each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead.

One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.

样例输入:

1 10
*........X

1 3
*#X

3 20
####################
#XY.gBr.*.Rb.G.GG.y#
####################

0 0

样例输出:

Escape possible in 9 steps.
The poor student is trapped!
Escape possible in 45 steps.

题目链接：hdu1885

刚刚做了hdu1429那道题，这两道题一样的方法

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<queue>
#define MAXN 105
using namespace std;
char map[105][105];
bool v[105][105][20];
const int d[4][2] = {{0,-1},{1,0},{0,1},{-1,0}};
int n,m,flag;
int sx,sy;
struct node
{
int x,y;
int step;
int key;
};
int deal(char c)
{
int n,j = 1;
if(c >= 'a' && c <= 'z') n = c - 'a';
else n = c - 'A';
while(n--)
j <<= 1;
return j;
}
void bfs()
{
memset(v,0,sizeof(v));
queue <node> q;
node s,temp;
s.x = sx;
s.y = sy;
s.step = 0;
s.key = 0;
q.push(s);
v[s.x][s.y][s.key] = 1;
while(!q.empty())
{
temp = q.front();
q.pop();
if(map[temp.x][temp.y] == 'X')
{
flag = 1;
printf("Escape possible in %d steps.\n",temp.step);
return;
}
for(int i = 0; i < 4; i ++)
{
s.x = temp.x + d[i][0];
s.y = temp.y + d[i][1];
s.key = temp.key;
if(v[s.x][s.y][s.key] || s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || map[s.x][s.y] == '#')
continue;

s.step = temp.step + 1;
v[s.x][s.y][s.key] = 1;
if(map[s.x][s.y]>='a'&&map[s.x][s.y]<='d')
{
int num = deal(map[s.x][s.y]);
if((s.key&num) == 0)
s.key |= num;
q.push(s);
}
else if(map[s.x][s.y]>='A'&&map[s.x][s.y]<='D')
{
int num = deal(map[s.x][s.y]);
if(s.key&num)
q.push(s);
}
else q.push(s);
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m),(m+n))
{
for(i = 0; i < n; i ++)
{
scanf("%s",map[i]);
for(j = 0; j < m; j ++)
{
if(map[i][j] == '*')
sx = i, sy = j, map[i][j] = '.';
else if(isalpha(map[i][j]))
{
switch(map[i][j])
{
case 'G': map[i][j] = 'A'; break;
case 'g': map[i][j] = 'a'; break;
case 'Y': map[i][j] = 'C'; break;
case 'y': map[i][j] = 'c'; break;
case 'R': map[i][j] = 'D'; break;
case 'r': map[i][j] = 'd'; break;
default: break;
}
}
}
}
flag = 0;
bfs();
if(!flag) printf("The poor student is trapped!\n");
}
return 0;
}

解题报告转自：http://blog.csdn.net/u010728156/article/details/16950685

1. 我没看懂题目
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
第二个是7 0 6 -1 1 -6 7输出是14 7 7
不知道题目例子是怎么得出来的