[分享一下]Google Code Jam 2013 入围题目 D

startstartsvip 2013-04-15 04:53:47
原题介绍:开宝箱的游戏,宝箱有锁,需要匹配类型的钥匙才可以开启,开启一次钥匙消失,宝箱打开,可取出里面的钥匙继续开启他宝箱,游戏很简单打开全部宝箱为胜利,因为宝箱是可透视的,你可以知道里面有什么钥匙。

开始时系统先会发放几个可用的钥匙,同类型的钥匙可能有多把,需要尝试去开启所有的宝箱,如果不行就说不行,行就给个正确的开启顺序。 如果说宝箱有编号 1,2,3,4,5 那么如果有多个正确的顺序则希望顺序号最小。

一楼 会有 测试例题,google 入围参与者中只有30% 作了出来,能做出来的吼一声


原题:
Problem D

Following an old map, you have stumbled upon the Dread Pirate Larry's secret treasure trove!

The treasure trove consists of N locked chests, each of which can only be opened by a key of a specific type. Furthermore, once a key is used to open a chest, it can never be used again. Inside every chest, you will of course find lots of treasure, and you might also find one or more keys that you can use to open other chests. A chest may contain multiple keys of the same type, and you may hold any number of keys.

You already have at least one key and your map says what other keys can be found inside the various chests. With all this information, can you figure out how to unlock all the chests?

For example, suppose the treasure trove consists of four chests as described below, and you began with exactly one key of type 1:

Chest Number | Key Type To Open Chest | Key Types Inside
--------------+--------------------------+------------------
1 | 1 | None
2 | 1 | 1, 3
3 | 2 | None
4 | 3 | 2
You can open all the chests in this example if you do them in the order 2, 1, 4, 3. If you start by opening chest #1 first, then you will have used up your only key, and you will be stuck.
Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a single line containing two positive integers K and N, representing the number of keys you start with and the number of chests you need to open.

This is followed by a line containing K integers, representing the types of the keys that you start with.

After that, there will be N lines, each representing a single chest. Each line will begin with integers Ti and Ki, indicating the key type needed to open the chest and the number of keys inside the chest. These two integers will be followed by Ki more integers, indicating the types of the keys contained within the chest.

Output

For each test case, output one line containing "Case #x: C1 C2 ... CN", where x is the case number (starting from 1), and where Ci represents the index (starting from 1) of the ith chest that you should open.

If there are multiple ways of opening all the chests, choose the "lexicographically smallest" way. In other words, you should choose to make C1 as small as possible, and if there are multiple ways of making C1 as small as possible, choose the one that makes C2 as small as possible, and so on.

If there is no way to open all the chests, you should instead output one line containing "Case #x: IMPOSSIBLE".

Limits

1 ≤ T ≤ 25.
1 ≤ K.
All key types will be integers between 1 and 200 inclusive.
Small dataset

1 ≤ N ≤ 20.
In each test case, there will be at most 40 keys altogether.
Large dataset

1 ≤ N ≤ 200.
In each test case, there will be at most 400 keys altogether.
Sample 示例


Input 输入示例


3 三个测试题
1 4 第1题,先给1个钥匙,总共4个箱子
1 给的1个钥匙 {钥匙类型1}
1 0 箱子编号1 需要钥匙类型1 箱子里面有 0 个钥匙
1 2 1 3 箱子编号2 需要钥匙类型1 箱子里面有 2 个钥匙 {钥匙类型1,钥匙类型3}
2 0 箱子编号3 需要钥匙类型2 箱子里面有 0 个钥匙
3 1 2 箱子编号4 需要钥匙类型3 箱子里面有 1 个钥匙 {钥匙类型2}
3 3 第2题,先给3个钥匙,总共3个箱子
1 1 1 给的3个钥匙 {钥匙类型1,钥匙类型1,钥匙类型1}
1 0 箱子编号1 需要钥匙类型1 箱子里面有 0 个钥匙
1 0 箱子编号2 需要钥匙类型1 箱子里面有 0 个钥匙
1 0 箱子编号3 需要钥匙类型1 箱子里面有 0 个钥匙
1 1 第3题,先给1个钥匙,总共1个箱子
2 给的1个钥匙 {钥匙类型2}
1 1 1 箱子编号1 需要钥匙类型1 箱子里面有 1 个钥匙 {钥匙类型1}

Output 输出示例


Case #1: 2 1 4 3 箱子编号2 1 4 3
Case #2: 1 2 3 箱子编号1 2 3
Case #3: IMPOSSIBLE 不可能
...全文
290 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
(☆随缘☆) 2013-04-15
  • 打赏
  • 举报
回复
感谢分享,
startstartsvip 2013-04-15
  • 打赏
  • 举报
回复
25 2 20 7 17 9 2 10 4 5 2 6 9 2 1 5 6 2 14 2 20 1 6 4 1 20 15 3 20 17 4 1 1 15 3 1 1 12 1 3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 2 4 4 2 3 2 4 1 1 2 1 2 3 2 2 3 4 3 4 2 2 1 2 1 2 2 4 4 1 1 2 2 1 4 3 0 3 3 1 1 1 1 0 1 0 1 0 1 1 2 2 3 1 2 3

110,477

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧