HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429
思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件;
考虑一般的搜索问题的解答思路:
搜索算法即在解空间中搜索满足要求的答案,可以看做一棵不断生长的状态树,状态之间不断扩展出新的状态,直到找出所需要的状态,即答案;
<1>定义状态:对于该问题,由于存在门与锁的约束条件,所以状态应该包括3个元素,即人所在的坐标 x 和 y 以及含有锁的种类;
<2>剪枝方法:因为在bfs搜索中会出现相同的状态,所以需要判重,使用vis数组即可;另外需要根据下一状态的位置已经该位置的字符判断能否拓展;
<3>状态压缩:对于拥有的钥匙进行状态压缩,使用一个10个位进行标记是否拥有钥匙 A~J ,将该10个bit压缩为一个整数即为状态压缩;
<4>位运算获得锁X与判断是否拥有锁X: 对于获得锁,可以使用或运算使第i个位为1,;判断是否拥有锁,可以使用与运算判断;
代码如下:
#include <queue>
#include <iostream>
using namespace std; #define MAX_N 21
#define KEY_N (1 << 10)
bool vis[MAX_N][MAX_N][KEY_N];
char map[MAX_N][MAX_N];
int dir[][] = {, -, , , -, , , };
int map_x, map_y; struct State
{
int x, y;
int step, key_value;
State() {}
State(int i, int j, int s, int k){ x = i; y = j; step = s; key_value = k; }
}; int GetKey(int key, char ch) { return key | ( << (ch - 'a')); }
bool OpenDoor(int key, char ch) { return key & ( << (ch - 'A')); } int Bfs(int x, int y, int game_times)
{
queue<State> state_queue;
State start(x, y, , ); vis[x][y][] = true;
state_queue.push(start);
while (!state_queue.empty())
{
State now = state_queue.front();
State next;
state_queue.pop(); if (now.step + >= game_times)
continue;
for (int i = ; i < ; ++i)
{
int n_x, n_y, n_step, n_key_value; n_x = now.x + dir[i][];
n_y = now.y + dir[i][];
n_step = now.step + ;
n_key_value = now.key_value;
if (n_x < || n_x >= map_x || n_y < || n_y >= map_y)
continue;
if (vis[n_x][n_y][n_key_value] || map[n_x][n_y] == '*')
continue;
if (map[n_x][n_y] == '^')
return n_step;
if ('a' <= map[n_x][n_y] && map[n_x][n_y] <= 'z')
{
n_key_value = GetKey(n_key_value, map[n_x][n_y]);
if (vis[n_x][n_y][n_key_value])
continue;
}
if ('A' <= map[n_x][n_y] && map[n_x][n_y] <= 'Z')
{
if (!OpenDoor(n_key_value, map[n_x][n_y]))
continue;
}
next.x = n_x;
next.y = n_y;
next.step = n_step;
next.key_value = n_key_value;
state_queue.push(next);
vis[n_x][n_y][n_key_value] = true;
}
}
return -;
} int main()
{
int game_times;
int start_x, start_y; while (scanf("%d %d %d\n", &map_x, &map_y, &game_times) != EOF)
{
int ans = ; memset(map, , sizeof(map));
memset(vis, , sizeof(vis));
for (int i = ; i < map_x; ++i)
{
scanf("%s", &map[i]);
for (int j = ; j < map_y; ++j)
{
if (map[i][j] == '@')
start_x = i, start_y = j;
}
} ans = Bfs(start_x, start_y, game_times);
if (ans == -)
printf("-1\n");
else
printf("%d\n", ans);
}
return ;
}
HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)的更多相关文章
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】
题目:pid=1429">hdoj 1429 胜利大逃亡(续) 同样题目: 题意:中文的,自己看 分析:题目是求最少的逃亡时间.确定用BFS 这个题目的难点在于有几个锁对于几把钥匙.唯 ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDOJ 1429 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
随机推荐
- GCD的使用和面试题集锦
GCD 分为异步和同步 异步: dispatch_async ( 参数1 , { } 同步: dispatch_sync( 参数1 , { } 参数1 :队列 队列分为两种: dispatch_get ...
- 关于"cin>>"输入成功或失败时的“返回值”(转载)
今天在看c++primer的时候,读到其中这样一段话: When we use an istream as a condition, the effect is to test the state o ...
- c#实现pdf另存为功能
c#实现pdf另存为功能 /// <summary> /// PDF另存为效果 /// </summary> /// <param name="fileName ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- XSS CSRF
XSS CSRF XSS 参考 https://zh.wikipedia.org/wiki/%E8%B7%A8%E7%B6%B2%E7%AB%99%E6%8C%87%E4%BB%A4%E7%A2%BC ...
- Angular form
参考 http://blog.xebia.com/2013/10/15/angularjs-validating-radio-buttons/ http://stackoverflow.com/que ...
- Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9831 Accepted: 4779 De ...
- unity3d插件Daikon Forge GUI 中文教程-5-高级控件listbox和progress bar的使用
(游戏蛮牛首发)大家好我是孙广东.官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上,要观看是须要FQ的. 只是教 ...
- python 类属性、对象属性
类的普通属性: dir(Myclass), 返回一个key列表: Myclass.__dir__,返回一个字典: 1.类的数据属性: 2.类的方法: 类的特殊属性: 1.Myclass.__name_ ...
- mysql 数据备份
一.备份数据库并下载到本地[db_backup.php] php代码: <?php // 数据库参数(此处测试,直接给定,项目中使用配置文件) $cfg_dbname = 'blog'; $cf ...