Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

Description

Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……

Input

. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙，对应的门分别为A-J

Output

Sample Input

4 5 17

@A.B.

a*.*.

*..*^

c..b*

4 5 16

@A.B.

a*.*.

*..*^

c..b*

Sample Output

16

-1

Source

ACM暑期集训队练习赛（三）

AC代码：

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n, m, t;
char map[25][25];
bool vis[25][25][1 << 10];
struct node
{
int x, y, step, state;
};
node start;
queue <node> q;
const int dx[] = {0, 0, 1, -1},
dy[] = {1, -1, 0, 0};
void input()
{
for (int i = 0; i < n; i++)
scanf("%s", map[i]);
}
bool check(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == '*')
return 0;
return 1;
}
void pre()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (map[i][j] == '@')
{
start.x = i, start.y = j;
map[i][j] = '.';
}
start.step = start.state = 0;
while (!q.empty())
q.pop();
memset(vis, 0, sizeof(vis));
}
void solve()
{
pre();
q.push(start);
vis[start.x][start.y][0] = 1;
while (!q.empty())
{
node u = q.front();
q.pop();
if (u.step >= t)
break;
if (map[u.x][u.y] == '^')
{
printf("%d\n", u.step);
return;
}
node next;
for (int i = 0; i < 4; i++)
{
next.x = u.x + dx[i];
next.y = u.y + dy[i];
if (!check(next.x, next.y))
continue;
next.state = u.state;
if (map[next.x][next.y] >= 'a' && map[next.x][next.y] <= 'j')
next.state |= (1 << (map[next.x][next.y] - 'a'));
else if (map[next.x][next.y] >= 'A' && map[next.x][next.y] <= 'Z')
if (!((u.state >> (map[next.x][next.y] - 'A')) & 1))
continue;
if (vis[next.x][next.y][next.state])
continue;
vis[next.x][next.y][next.state] = 1;
next.step = u.step + 1;
q.push(next);
}
}
printf("-1\n");
}
int main()
{
while (scanf("%d%d%d", &n, &m, &t) == 3)
{
input();
solve();
}
return 0;
}

dianlujitao还在竞赛？