POJ-3026-Borg Maze
来源:互联网 发布:矩阵论 清华 编辑:程序博客网 时间:2024/04/29 13:53
这个题属于BFS+最小生成树,题目要求是求出从起点到同化图中所有的敌人所需要的最少的时间。
解法:
1、先将图中的起点('S')和敌人('A')的点提取出来,作为最小生成树的点。
2、对起点进行BFS得到新图中每个点的权值。
3、用Prim算法求出最小生成树即可。
注意:不能用getchar,一旦用 getchar会一直WA,我在这WA了很多次~伤不起~
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int maxn=102;const int inf=1<<28;struct node{ int x; int y; int ans;};int n,m,x[maxn],y[maxn],num[maxn][maxn],dist[maxn];int movex[4]={1,-1,0,0},movey[4]={0,0,-1,1};char map[maxn][maxn];bool vis[maxn][maxn];bool isborder(int x,int y){ if(x<0||x>=m||y<0||y>=n)return true; return false;}queue<node> q;void BFS(int x,int y){ q.push((node){x,y,0}); memset(vis,0,sizeof(vis)); vis[x][y]=1; while(!q.empty()) {node ita=q.front();q.pop();for(int i=0;i<4;i++){ int itx=ita.x+movex[i]; int ity=ita.y+movey[i]; if(isborder(itx,ity)||vis[itx][ity]||map[itx][ity]=='#')continue; if(map[itx][ity]=='A'||map[itx][ity]=='S')dist[num[itx][ity]]=min(dist[num[itx][ity]],ita.ans+1); vis[itx][ity]=1; q.push((node){itx,ity,ita.ans+1});} } }int main(){ int T; scanf("%d",&T); while(T--) {scanf("%d%d",&n,&m);int res=0,st=0;gets(map[0]);for(int i=0;i<m;i++){ gets(map[i]); for(int j=0;j<n;j++) {if(map[i][j]=='S'||map[i][j]=='A'){ x[res]=i; y[res]=j; num[i][j]=res++; if(map[i][j]=='S')st=res-1; } }}for(int i=0;i<=101;i++) dist[i]=inf;BFS(x[st],y[st]);int ans=0;dist[st]=-1;for(int i=1;i<res;i++){ int mini=inf,pos=0; for(int j=0;j<res;j++)if(dist[j]!=-1&&dist[j]<mini){ mini=dist[j]; pos=j;} dist[pos]=-1; ans+=mini; BFS(x[pos],y[pos]);}printf("%d\n",ans); } return 0;}
- poj 3026 Borg Maze
- POJ 3026 Borg Maze
- POJ 3026 Borg Maze
- poj 3026 Borg Maze
- poj-3026-Borg Maze
- POJ 3026 Borg Maze
- POJ 3026 Borg Maze
- poj 3026 Borg Maze
- POJ-3026-Borg Maze
- poj 3026 Borg Maze
- POJ 3026 Borg Maze
- POJ 3026 Borg Maze
- poj 3026 Borg Maze
- poj 3026 Borg Maze
- POJ:3026 Borg Maze
- poj 3026 Borg Maze
- POJ 3026-Borg Maze
- POJ 3026 Borg Maze
- CSS框架列表
- java 唯一码生成
- Java基础复习:Map接口
- 黑马程序员---正则表达式
- LeetCode —— Sort Colors
- POJ-3026-Borg Maze
- CSS常用浮出层的写法
- 黑马程序员---工具类和设计模式
- 队列管理算法及策略总结
- nesC学习备忘录
- poj1934-打印LCS全部路径+搜索
- 六级之听力攻略1
- 流行趋势:27个引领时尚的网页设计作品【上篇】
- poj 2492 A Bug's Life