hdu 1429 bfs+状态压缩
来源:互联网 发布:服务器硬件检测软件 编辑:程序博客网 时间:2024/04/29 20:57
//开一个三维标记数组,标记Ignatius有没有以拿到钥匙的状态到达该位置
//由于钥匙的个数最多十个,所以可以用状态压缩来做
//用1和0表示有没有第i种钥匙,这样对于这个人拿到的钥匙状态就可以用二进制数表示
//用bfs找到最小值
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std ;
const int maxn = 30;
const int inf = 0x7fffffff;
int vis[maxn][maxn][1<<10];
char map[maxn][maxn] ;
int ans = ans;
struct node
{
int x , y;
int step ;
int state ;
};
int st_x , st_y ;
int n , m;
int dx[4] = {-1,0,1,0} ;
int dy[4] = {0,1,0,-1} ;
queue<struct node> que;
void bfs()
{
while(que.size())
que.pop() ;
memset(vis, 0 ,sizeof(vis)) ;
struct node first = {st_x , st_y , 0 ,0} ;
vis[st_x][st_y][0] = 1;
que.push(first) ;
while(que.size())
{
struct node now = que.front() ;
que.pop() ;
if(map[now.x][now.y] == '^')
{
ans = now.step ;
break;
}
for(int i = 0;i < 4;i++)
{
struct node next ;
next.x = now.x + dx[i] ;
next.y = now.y + dy[i] ;
if(map[next.x][next.y] == '*' || next.x < 1 || next.x > n ||next.y < 1 || next.y > m)
continue ;
if(map[next.x][next.y] >= 'A' && map[next.x][next.y] <= 'J')
if(!(now.state & (1<<(map[next.x][next.y] - 'A'))))
continue ;
if(map[next.x][next.y] >= 'a' && map[next.x][next.y] <= 'j')
{
next.state = now.state | (1<<(map[next.x][next.y]-'a')) ;
next.step = now.step + 1;
if(vis[next.x][next.y][next.state])
continue ;
que.push(next) ;
vis[next.x][next.y][next.state] = 1;
continue;
}
next.state = now.state ;
next.step = now.step + 1;
if(vis[next.x][next.y][next.state])
continue ;
vis[next.x][next.y][next.state] = 1;
que.push(next) ;
}
}
}
int main()
{
// freopen("in.txt","r",stdin) ;
int time ;
while(scanf("%d%d%d", &n , &m , &time) != EOF)
{
for(int i = 1;i <= n;i++)
{
scanf("%s" , &map[i][1]);
for(int j = 1;j <= m;j++)
if(map[i][j] == '@')
st_x = i,st_y = j;
}
ans = inf ; //可能到不了终点,所以有可能bfs没有更新ans
bfs(); //所以ans每次都要初始化
if(ans < time)
printf("%d\n", ans);
else
printf("-1\n");
}
return 0;
}
//由于钥匙的个数最多十个,所以可以用状态压缩来做
//用1和0表示有没有第i种钥匙,这样对于这个人拿到的钥匙状态就可以用二进制数表示
//用bfs找到最小值
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std ;
const int maxn = 30;
const int inf = 0x7fffffff;
int vis[maxn][maxn][1<<10];
char map[maxn][maxn] ;
int ans = ans;
struct node
{
int x , y;
int step ;
int state ;
};
int st_x , st_y ;
int n , m;
int dx[4] = {-1,0,1,0} ;
int dy[4] = {0,1,0,-1} ;
queue<struct node> que;
void bfs()
{
while(que.size())
que.pop() ;
memset(vis, 0 ,sizeof(vis)) ;
struct node first = {st_x , st_y , 0 ,0} ;
vis[st_x][st_y][0] = 1;
que.push(first) ;
while(que.size())
{
struct node now = que.front() ;
que.pop() ;
if(map[now.x][now.y] == '^')
{
ans = now.step ;
break;
}
for(int i = 0;i < 4;i++)
{
struct node next ;
next.x = now.x + dx[i] ;
next.y = now.y + dy[i] ;
if(map[next.x][next.y] == '*' || next.x < 1 || next.x > n ||next.y < 1 || next.y > m)
continue ;
if(map[next.x][next.y] >= 'A' && map[next.x][next.y] <= 'J')
if(!(now.state & (1<<(map[next.x][next.y] - 'A'))))
continue ;
if(map[next.x][next.y] >= 'a' && map[next.x][next.y] <= 'j')
{
next.state = now.state | (1<<(map[next.x][next.y]-'a')) ;
next.step = now.step + 1;
if(vis[next.x][next.y][next.state])
continue ;
que.push(next) ;
vis[next.x][next.y][next.state] = 1;
continue;
}
next.state = now.state ;
next.step = now.step + 1;
if(vis[next.x][next.y][next.state])
continue ;
vis[next.x][next.y][next.state] = 1;
que.push(next) ;
}
}
}
int main()
{
// freopen("in.txt","r",stdin) ;
int time ;
while(scanf("%d%d%d", &n , &m , &time) != EOF)
{
for(int i = 1;i <= n;i++)
{
scanf("%s" , &map[i][1]);
for(int j = 1;j <= m;j++)
if(map[i][j] == '@')
st_x = i,st_y = j;
}
ans = inf ; //可能到不了终点,所以有可能bfs没有更新ans
bfs(); //所以ans每次都要初始化
if(ans < time)
printf("%d\n", ans);
else
printf("-1\n");
}
return 0;
}
0 0
- hdu 1429 BFS+状态压缩
- HDU 1429 BFS+状态压缩
- hdu~1429(bfs+状态压缩)
- HDU 1429 BFS+状态压缩
- hdu 1429 bfs+状态压缩
- hdu 1429 bfs 状态压缩
- hdu 1429 bfs+二进制状态压缩
- hdu 1429 (bfs+状态压缩)
- HDU 1429 (BFS+状态压缩)
- hdu 2209 BFS + 状态压缩
- HDU 4634 - BFS + 状态压缩
- hdu 4012 状态压缩BFS
- HDU 4771 BFS+状态压缩
- hdu 1885(状态压缩+bfs)
- HDU 1885 BFS+状态压缩
- hdu~1885(bfs+状态压缩)
- hdu~5025(bfs+状态压缩)
- hdu 5025 bfs+状态压缩
- SOJ Dollars 解题报告
- sql的左连接和右连接
- 3.2 对话框QDialog(1)-->Qt Creater 快速入门(笔记)
- 回溯法的分析及N皇后问题
- 每日一题10:在排序的二维数组中查找
- hdu 1429 bfs+状态压缩
- CentOS6.5 挂载超过16T的大容量存储空间
- Linux下配置java环境 安装eclipse
- 深入学习Objective-C(二)理解 objc 关联对象 (Associated Objects)
- 给UIView 设置透明度,不影响其他sub views
- Tomcat配置内存和远程debug端口
- 蓝桥杯 ALGO-111 明明的随机数(排序,去重)
- Html的一些常用标签
- Hadoop学习(2)——虚拟机安装