AOJ 0558 BFS
来源:互联网 发布:智汇诚网络 编辑:程序博客网 时间:2024/05/02 04:29
题意:
一个地图,需要先从起点到1再到2,,,,以此类推,问最短路程
题解:
明显最短路程就是bfs,每一次把起点和目的地换了,多次bfs
注意:输入地图时,可以使用如下方法,避免每一个字符输入时对‘\n’还要处理
cin>>h>>w>>n; getchar(); for(int i = 0;i<h;i++){ scanf("%s",&maze[i]); }
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>#define pr(x) cout<<#x<<" "<<x;#define pl(x) cout<<#x<<" "<<x<<endl;#include<math.h>#include<algorithm>using namespace std;char maze[1005][1005];int dx[] = {-1,1,0,0};int dy[] = {0,0,-1,1};int d[1005][1005];const int INF = 0x7f7f7f7f;typedef pair <int ,int > p;queue <p> que;int h,w;void chushihua(){ for(int i = 0;i<1005;i++){ for(int j = 0;j<1005;j++){ d[i][j] = INF; } }}int bfs(int x,int y,int gx,int gy){ chushihua(); d[x][y] = 0; que.push(p(x,y)); while(que.size()){ p cp = que.front(); que.pop(); if(cp.first==gx&&cp.second==gy){break;} for(int i = 0;i<4;i++){ int nx = cp.first + dx[i]; int ny = cp.second + dy[i]; if(nx>=0&&nx<h&&ny>=0&&ny<w&&maze[nx][ny]!='X'&&d[nx][ny]==INF){ que.push(p(nx,ny)); d[nx][ny] = d[cp.first][cp.second] + 1; } } } while(que.size()){que.pop();} return d[gx][gy];}int main(){ int n; cin>>h>>w>>n; int indexmap[n+5][4]; getchar(); for(int i = 0;i<h;i++){ scanf("%s",&maze[i]); } for(int i = 0;i<h;i++){ for(int j = 0;j<w;j++){ if(maze[i][j]=='S'){ indexmap[0][0] = i; indexmap[0][1] = j; } else if((int)maze[i][j]>=49&&(int)maze[i][j]<=57){ int index = maze[i][j]-'0'; indexmap[index][0] = i; indexmap[index][1] = j; } } } int ans = 0; for(int i=0;i<n;i++){ ans += bfs(indexmap[i][0],indexmap[i][1],indexmap[i+1][0],indexmap[i+1][1]); } cout<<ans<<endl;}
0 0
- AOJ-0558 Cheese BFS
- [bfs]AOJ 0558 Cheese
- AOJ 0558:Cheese(bfs)
- AOJ 0558 BFS
- AOJ 0558 Cheese (bfs)
- Aoj 0558 Cheese【bfs分段处理】
- AOJ 0558 Cheese (多次bfs)
- AOJ 0121 bfs
- AOJ 6 bfs
- AOJ 0118 Property Distribution (BFS)
- Seven Puzzle (AOJ 0121 bfs)
- Aoj 0121 Seven Puzzle【bfs】
- AOJ 866 飞越原野 【BFS】
- Aoj 0558
- AOJ 0558
- AOJ 0121 Seven Puzzle (bfs)
- 搜索 Seven Puzzle (AOJ 0121 bfs)
- AOJ 0121:Seven Puzzle(bfs)
- toggle 切换
- AOJ 0033 DFS
- Java中访问权限控制
- 内存映射原理
- Session入Redis
- AOJ 0558 BFS
- Codevs 4600 [NOI2015]程序自动分析
- Hygieia 部署指南(1)介绍篇
- 除法
- javascript中eval解析JSON字符串
- Android第二周(第二部分)-listview
- 关于静态变量,成员变量,静态方法,实例方法的用法及区别
- 递归算法
- ubuntu下code blocks安装全过程