HDU 4771 Stealing Harry Potter's Precious (BFS)2013杭州现场赛
来源:互联网 发布:淘宝首页添加热点 编辑:程序博客网 时间:2024/05/16 15:57
题目链接:点击打开链接
题意:输入一个n*m的矩阵,然后输入k个坐标,求从@位置出发访问完所有k个坐标需要走的最小步数。
分析:考虑k的值较小,那么访问的顺序可以用全排列全部BFS走一遍然后取最小值即可。
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include<malloc.h>#include <map>#include <stack>#include <queue>#include <numeric>#include <iomanip>#include <bitset>#include <sstream>#include <fstream>#include <limits.h>#define debug "output for debug\n"#define pi (acos(-1.0))#define eps (1e-4)#define inf (1<<28)#define sqr(x) (x) * (x)using namespace std;typedef long long ll;typedef unsigned long long ULL;int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};char mp[105][105];int vis[105][105];int a[5];int n,m;int k,cnt;struct node{ int x,y,sp;} ed[15],st,sel[25][5],fnal;queue<node>q;int bfs(node s,node e){ q.push(s); node tmp,hd; while(!q.empty()) { hd=q.front(); q.pop(); if(hd.x==e.x&&hd.y==e.y) { return hd.sp; } for(int i=0; i<4; i++) { int xx=hd.x+dir[i][0]; int yy=hd.y+dir[i][1]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]!='#'&&!vis[xx][yy]) { vis[xx][yy]=1; tmp.x=xx; tmp.y=yy; tmp.sp=hd.sp+1; q.push(tmp); } } } return -1;}int main(){ while(scanf("%d%d",&n,&m)) { if(m+n==0) break; for(int i=1; i<=n; i++) { scanf("%s",mp[i]+1); for(int j=1; j<=m; j++) { if(mp[i][j]=='@') { st.x=i; st.y=j; st.sp=0; } } } scanf("%d",&k); for(int i=1; i<=k; i++) { int u,v; scanf("%d%d",&u,&v); ed[i].x=u; ed[i].y=v; } for(int i=1; i<=k; i++) a[i]=i; cnt=0; do { for(int i=1; i<=k; i++) { sel[cnt][i]=ed[a[i]]; } cnt++; } while(next_permutation(a+1,a+1+k)); int ans=inf; for(int i=0; i<cnt; i++) { int sum=0,num; sel[i][0]=st; for(int j=0; j<k; j++) { while(!q.empty()) q.pop(); memset(vis,0,sizeof vis); num=bfs(sel[i][j],sel[i][j+1]); sum+=num; } ans=min(ans,sum); if(num==-1) { ans=-1; break; } } printf("%d\n",ans); } return 0;}
0 0
- HDU 4771 Stealing Harry Potter's Precious (BFS)2013杭州现场赛
- hdu 4771 Stealing Harry Potter's Precious(BFS+DFS)
- HDU 4771 Stealing Harry Potter's Precious(BFS)
- HDU 4771 Stealing Harry Potter's Precious(BFS)
- hdu 4771 Stealing Harry Potter's Precious(DFS+BFS)
- hdu 4771 Stealing Harry Potter's Precious(bfs+dfs)
- hdu 4771 Stealing Harry Potter's Precious (2013 杭州 onsite B题)bfs预处理后,dfs枚举或tsp
- HDU 4771 Stealing Harry Potter's Precious 2013亚洲区域赛杭州赛区 B 题题解
- hdu 4771 Stealing Harry Potter's Precious(bfs预处理&TSP)
- HDU-4771 Stealing Harry Potter's Precious DFS + BFS
- hdu 4771 Stealing Harry Potter's Precious(bfs)
- HDU 4771 Stealing Harry Potter's Precious(BFS + DFS)
- HDU 4771 Stealing Harry Potter's Precious【BFS+DFS】
- hdu 4771 状态压缩+bfs Stealing Harry Potter's Precious
- HDU-4771 Stealing Harry Potter's Precious 状压DP+BFS
- HDU 4771 Stealing Harry Potter's Precious(bfs+dfs)
- hdu 4771 Stealing Harry Potter's Precious (bfs+状态压缩)
- hdu 4771 Stealing Harry Potter's Precious(bfs+枚举)
- gns3 1.4.0b3 使用笔记,一些报错的处理
- LeetCode Plus One
- 测试人员绩效评价方法
- excel 统计字符出现的次数
- python unix 时间戳
- HDU 4771 Stealing Harry Potter's Precious (BFS)2013杭州现场赛
- 警惕自增的陷阱
- 17.1.1.8 Setting Up Replication with Existing Data
- 合成一个新的dateframe
- iOS开发:绘制像素到屏幕
- SQL Server 复制:事务发布
- 【IOS 开发学习总结-OC-43】★ios开发——UIButton(按钮)与 UISwitch(开关按钮)
- Javascript获取属性的值以及比较值
- hdoj Conturbatio 5480 (数组模拟+技巧) 好题