FZU 2186 BFS+状压最短路
来源:互联网 发布:调节手机光线软件 编辑:程序博客网 时间:2024/06/02 05:49
一开始看宝藏只有10个,100*100的矩阵,
果断写了状压BFS,然后果断超时。。
正解:先不考虑财宝,先做BFS算出,每个财宝之间的最短路(包括起点),然后状压最短路处理即可。
注意三中情况:起点为负数,不存在财宝,存在一个财宝且财宝在原点。
果断写了状压BFS,然后果断超时。。
正解:先不考虑财宝,先做BFS算出,每个财宝之间的最短路(包括起点),然后状压最短路处理即可。
注意三中情况:起点为负数,不存在财宝,存在一个财宝且财宝在原点。
#include "stdio.h"#include "string.h"#include "algorithm"#include "queue"using namespace std;const int inf=0x3f3f3f3f;struct Mark{ int x,y;}mark[15];struct node{ int x,y,t;};int dir[4][2]={ {1,0},{-1,0},{0,1},{0,-1} };int b[15];int str[110][110];int dis[15][15];int n,m,cnt;int used[110][110];int dp[5010][15];int Min(int a,int b){ if (a<b) return a; else return b;}void bfs(int w){ queue<node>q; node cur,next; int i; memset(used,0,sizeof(used)); cur.x=mark[w].x; cur.y=mark[w].y; cur.t=0; q.push(cur); used[mark[w].x][mark[w].y]=1; while (!q.empty()) { cur=q.front(); q.pop(); for (i=0;i<4;i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; if (next.x<0 || next.y<0 || next.x>=n || next.y>=m) continue; if (str[next.x][next.y]<0) continue; if (used[next.x][next.y]==0) { used[next.x][next.y]=1; next.t=cur.t+1; if (str[next.x][next.y]>0) { dis[w][str[next.x][next.y]]=Min(dis[w][str[next.x][next.y]],next.t); dis[str[next.x][next.y]][w]=dis[w][str[next.x][next.y]]; } q.push(next); } } }}int main(){ int i,j,ans,flag,aim,l; b[0]=1; for (i=1;i<=12;i++) b[i]=b[i-1]*2; while (scanf("%d%d",&n,&m)!=EOF) { cnt=0; for (i=0;i<n;i++) for (j=0;j<m;j++) { scanf("%d",&str[i][j]); if (str[i][j]>0 && i+j!=0) { mark[++cnt].x=i; mark[cnt].y=j; str[i][j]=cnt; } } if (cnt==0) { printf("0\n"); continue; } if (cnt==1 && str[0][0]>0) { printf("0\n"); continue; } if (str[0][0]<0) { printf("-1\n"); continue; } mark[0].x=0; mark[0].y=0; memset(dis,inf,sizeof(dis)); for (i=0;i<=cnt;i++) bfs(i); flag=1; for (i=1;i<=cnt;i++) if (dis[0][i]==inf) { printf("-1\n"); flag=0; } if (flag==0) continue; memset(dp,inf,sizeof(dp)); dp[1][0]=0; aim=b[cnt+1]-1; for (l=0;l<=aim;l++) for (i=0;i<=cnt;i++) for (j=0;j<=cnt;j++) if (i!=j) { if ((b[i]&l)==0) continue; if ((b[j]&l)!=0) continue; if (dp[l][i]==inf) continue; dp[l|b[j]][j]=Min(dp[l|b[j]][j],dp[l][i]+dis[i][j]); } ans=inf; for (i=1;i<=cnt;i++) ans=Min(ans,dp[aim][i]+dis[i][0]); printf("%d\n",ans); } return 0;}
3 0
- FZU 2186 BFS+状压最短路
- bfs最短路+dp(FZU 2186)
- FZU-1295 单源最短路思想+BFS
- FZU 2186 BFS+状压(类似TSP)
- FZU 2124 吃豆人 (BFS)
- FZU 2188 BFS
- FZU 2196 Escape BFS
- FZU 2196(bfs)
- FZU 1408位图(bfs)
- FZU 2196 Escape【BFS】
- FZU 2150 BFS
- FZU 2124 吃豆人 BFS
- FZU 2180 双向BFS
- fzu Fire Game bfs
- [BFS] FZU 2150
- FZU 2186 小明迷宫寻宝 (状压Dp+bfs)
- FZU 2169 shadow(最短路)
- bfs fzu 1205 小鼠迷宫
- spring data jpa specification的封装
- Hdoj 2152 Fruit 【母函数】
- C++设计一个真正不能被继承的类
- 初识GihHub,创建仓库,使用GitHub for Windows 同步本地代码文件,创建分支
- Ruby on Rails创建项目和服务启动中的一些问题
- FZU 2186 BFS+状压最短路
- Hdoj 1719 Friend 【数学】
- LeetCode-Best Time to Buy and Sell Stock
- Android 上传图片至服务器
- PLSQL Developer用户登录时提示:ORA-28000: the account is locked
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- C++中的this指针
- Linux软件的安装和卸载
- Manacher算法(最长子回文串)