HDU 2605 Snake(DFS+BFS+技巧+细节)(好题)
来源:互联网 发布:汽车行业概况知乎 编辑:程序博客网 时间:2024/05/18 15:08
Snake
Time Limit: 10000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 222 Accepted Submission(s): 45
Problem Description
Snake is a popular game , and I believe most of us had played it . The Original game is that you can control a snake to eat the magic bean and after the snake eat one magic bean , the length of the snake’s body will get longer .But today we are talking about a new game. These are the rules of the new Snake Game :
1. The length of the snake’s body won’t change even though it eat a magic bean.
2. Some pairs of the beans have a relation , that is, one of them can not be eaten until another one had been eaten . We call the latter “the key bean” . For example , if A can’t be eaten until B had been eaten ,we say “B is the key bean of A”. (That means when A can’t be eaten , the snake can not move into the grid where A is.)
3. The snake could not move to a wall or its body.Befor it move,it will chooses an adjacent vacant square of its head,which is neither a stone nor occupied by its body.
Figure 1 and figure2 shows how the snake move
Figure 1
Figure 2
1. The length of the snake’s body won’t change even though it eat a magic bean.
2. Some pairs of the beans have a relation , that is, one of them can not be eaten until another one had been eaten . We call the latter “the key bean” . For example , if A can’t be eaten until B had been eaten ,we say “B is the key bean of A”. (That means when A can’t be eaten , the snake can not move into the grid where A is.)
3. The snake could not move to a wall or its body.Befor it move,it will chooses an adjacent vacant square of its head,which is neither a stone nor occupied by its body.
Input
The first line contain a integer T (T <= 10).Followed by T cases. Each case contain five parts.
The first part: six integers ,H,W,L,K,R,N,(H <= 20 , W <= 20 , L <= 8 , K <= 7 ) means the height of the map , the width of the map , the length of the snake’s body, the number of the magic beans . the number of the relations , the number of the wall respectively.
The second part: L lines , each line contain two integer hi ,wi, indicating the original position of each block of snake's body, from B1(h1,w1) to BL(hL,wL) orderly, where 1<=hi<=H, and 1<=wi<=W,1<=i<=L.
The third part: K lines ,each line contain two integer hi ,wi , indicating the position of each magic bean , from MB1(h1,w1) to MBK(hK,wK) orderly, where 1<=hi<=H, and 1<=wi<=W,1<=i<=K.
The fourth part : R lines , each line contain two integer A ,B means “A is the key bean of B ”. The A and B may appear several times , but “one bean will have only one key bean”.
The fifth part: N lines , each line contain two integer hi ,wi , indicating the position of each wall , from W1(h1,w1) to WN(hN,wN) orderly, where 1<=hi<=H, and 1<=wi<=W,1<=i<=N.
The first part: six integers ,H,W,L,K,R,N,(H <= 20 , W <= 20 , L <= 8 , K <= 7 ) means the height of the map , the width of the map , the length of the snake’s body, the number of the magic beans . the number of the relations , the number of the wall respectively.
The second part: L lines , each line contain two integer hi ,wi, indicating the original position of each block of snake's body, from B1(h1,w1) to BL(hL,wL) orderly, where 1<=hi<=H, and 1<=wi<=W,1<=i<=L.
The third part: K lines ,each line contain two integer hi ,wi , indicating the position of each magic bean , from MB1(h1,w1) to MBK(hK,wK) orderly, where 1<=hi<=H, and 1<=wi<=W,1<=i<=K.
The fourth part : R lines , each line contain two integer A ,B means “A is the key bean of B ”. The A and B may appear several times , but “one bean will have only one key bean”.
The fifth part: N lines , each line contain two integer hi ,wi , indicating the position of each wall , from W1(h1,w1) to WN(hN,wN) orderly, where 1<=hi<=H, and 1<=wi<=W,1<=i<=N.
Output
For each case , if the snake could eat all the magic beans , output the minimum step it took. If the snake could not , just output “-1” (without the quotation marks) .
Sample Input
18 95 21 85 26 26 36 46 54 22 62 12 53 54 44 54 65 65 76 7
Sample Output
21
Author
Teddy
Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
题目链接:
点击打开链接
题解:
这是个好题啊。贪食蛇。爆搜。
注意一下一些细节和技巧就可以。
可能需要你花很多时间慢慢写....
反正我写了一个晚上....
很考验自己的的代码能力,我写了7K...300+行...
注意:
豆子也要成环...dfs判断一下即可。
然后再BFS,判断是否越界或者撞墙,还要判断豆子能不能吃。还要判断蛇头不能碰到自己。
注意MLE,利用一下位运算就可以了。用bitset大法应该也是可以的。
阅读全文
1 0
- HDU 2605 Snake(DFS+BFS+技巧+细节)(好题)
- BFS+DFS(hdu 1044)
- hdu 1983 Kaitou Kid - The Phantom Thief (2) (dfs+bfs+01三维判重 好题)
- HDU 4090 GemAnd Prince (DFS+BFS)/(DFS+DFS)
- hdu 4601 (bfs + dfs + trie)
- HDU-#1241 Oil Deposits(BFS & DFS)
- HDU 1254 - 推箱子(BFS + DFS)
- HDU 3313 Key Vertex(dfs + bfs)
- HDU 1242 -- Rescue (bfs,dfs)
- hdu 5335 Walk Out(dfs+bfs)
- 模板:HDU 1242: Rescue: (BFS DFS)
- HDU 1254推箱子(bfs+dfs)
- HDU 1728 逃离迷宫(BFS+DFS)
- hdu 1254(dfs+bfs+优先队列)
- HDU 1254 推箱子(BFS + DFS)
- 【hdu 1241】 Oil Deposits (dfs+bfs)
- hdu 5546(DFS或BFS)
- hdu 4605(离线 + dfs + 树状数组(好题))
- 结构体实现通讯录文件流版
- Virtual-Box Ubuntu 16.04 你应该这样来安装Chrome google 浏览器
- CSS即将具备的6种处理器特性
- java.io.CharConversionException: isHexDigit
- Codeforces 762C Two strings (前缀与后缀)
- HDU 2605 Snake(DFS+BFS+技巧+细节)(好题)
- Mysql优化4-合适的索引
- [LeetCode]202. Happy Number(判断正整数是不是Happy Number)
- Android-service之aidl
- 编写qt程序的流程
- x86 中32位寻址模式
- Paint的高级渲染
- 什么是微信小程序?
- Failed to execute goal org.apache.maven.plugins:maven-war-plugin 解决办法