题意:

n*m的迷宫,有一些格能走(“.”),有一些格不能走(“#”)。起始点为“@”。

有K个物体。(K<=4),每个物体都是放在“.”上。

问最少花多少步可以取完所有物体。

思路:

BFS+状压,看代码。

代码:

struct node{
int x,s;
node(int _x,int _s){
x=_x, s=_s;
}
}; int n,m,k,sx,sy;
char graph[105][105];
int px[5],py[5];
int dp[105][105][35]; int bfs(){
queue<node> q;
mem(dp,-1);
int s=0;
rep(i,1,k) if(px[i]==sx&&py[i]==sy) s=(1<<(i-1));
dp[sx][sy][s]=0;
q.push(node(sx*1000+sy,s));
if(s==((1<<k)-1))
return dp[sx][sy][s];
while(!q.empty()){
node f=q.front(); q.pop();
rep(i,0,3){
int nx=f.x/1000+uu[i], ny=f.x%1000+vv[i];
int s=f.s;
if(nx<0||ny<0||nx>=n||ny>=m) continue;
if(graph[nx][ny]=='#') continue;
rep(j,1,k) if(nx==px[j]&&ny==py[j]){
s|=(1<<(j-1));
}
if(dp[nx][ny][s]!=-1) continue;
dp[nx][ny][s]=dp[f.x/1000][f.x%1000][f.s]+1;
q.push(node(nx*1000+ny,s));
if(s==((1<<k)-1))
return dp[nx][ny][s];
}
}
return -1;
} int main(){
//freopen("test.in","r", stdin);
while(scanf("%d%d",&n,&m),n||m){
rep(i,0,n-1){
scanf("%s",graph[i]);
rep(j,0,m-1) if(graph[i][j]=='@'){
sx=i, sy=j;
}
}
scanf("%d",&k);
rep(i,1,k) { scanf("%d%d",&px[i],&py[i]); --px[i], --py[i]; }
printf("%d\n",bfs());
}
//fclose(stdin);
}

hdu 4771 Stealing Harry Potter's Precious (BFS+状压)的更多相关文章

  1. HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  2. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  3. HDU 4771 Stealing Harry Potter's Precious

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  4. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

  5. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

  6. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

  7. hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)

    题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...

  8. Stealing Harry Potter's Precious BFS+DFS

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. 机器学习——K-Means算法

    1 基础知识 相似度或距离 假设有 $m$ 个样本,每个样本由 $n$ 个属性的特征向量组成,样本合集 可以用矩阵 $X$ 表示 $X=[x_{ij}]_{mn}=\begin{bmatrix}x_{ ...

  2. ecshop增加调用字段问题汇总

    一.ecshop文章列表页调用缩略图.网页描述等 打开includes/lib_article.php文件,大约在69行 添加 $arr[$article_id]['description'] = $ ...

  3. Orchard Core入门配方和主题

    包含Orchard Core入门配方和主题 可以通过两个不同的NuGet包使用Orchard Core. OrchardCore.Application.Cms.Core.Targets Orchar ...

  4. Java基础系列(8)- 数据类型

    数据类型 强类型语言 要求变量的使用合乎规定,所有的变量都必须先定义才能使用.Java是强类型语言. 弱类型语言 变量定义比较随意,比如"12"+3,可以是int型123,也可以是 ...

  5. 牛客练习赛84F-牛客推荐系统开发之下班【莫比乌斯反演,杜教筛】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/11174/F 题目大意 给出\(n,k\)求 \[\sum_{i_1=1}^n\sum_{i_2=1}^n.. ...

  6. mybatis: No enum constant org.apache.ibatis.type.JdbcType."VARCHAR"

    mybatis 插入的时候一句sql报错如下. org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ib ...

  7. python3之工程中必须依赖的__init__.py

    1.  __init__.py 1.1  什么是__init__.py 在Python3工程里,当python3检测到一个目录下存在__init__.py文件时,Python3就会把它当成一个模块(m ...

  8. C语言日记① 初识C

    概念 c语言是一种计算机语言 也就是人与计算机打交道的语言 在早期,因为计算机使用的二进制 所以早期写代码都是科学家来写的使用对应的功能二进制代码 需要用到手册,所以开发不方便 在后来,人们发明了汇编 ...

  9. JS 开发中数组常用的方法

    大家有没有想过,js数组为什么会有这么多的方法,没错,就是为了不同场景下处理数据的需要,就像设计模式一样,都是为了能更好的处理当前场景的需要. 首先怎么创建一个数组呢, // 两种方式 // 1,构造 ...

  10. Django基础1

    一,web框架的本质 web应用的本质就是一个socket的服务端.而用户的浏览器就是一个客户端,具体事例如下: import socket sk = socket.socket() sk.bind( ...