宽度优先搜索
来源:互联网 发布:怎么恢复u盘数据 编辑:程序博客网 时间:2024/05/08 12:45
#include <stdio.h>#include <iostream>#include <cstring>#include <stdlib.h>#include <algorithm>#include <queue>#include <math.h>#define MAX 300#define INF 1e8using namespace std;typedef pair<int,int> P;int N,M;char maze[MAX][MAX];int yx,yy;//y的起始坐标int mx,my;//m的起始坐标int d1[MAX][MAX],d2[MAX][MAX];int move1[4] = {1,0,-1,0},move2[4] = {0,1,0,-1};int ax[40005],ay[40005];void dfs(int x,int y,int x1,int y1){ queue<P> que; P b; que.push(P(x,y)); for (int i = 0;i < N;i++) { for (int j = 0;j < M;j++) { d1[i][j] = INF; d2[i][j] = INF; } } d1[x][y] = 0; d2[x1][y1] = 0; while (!que.empty()) { b = que.front(); que.pop(); int nx,ny; for (int i = 0;i < 4;i++) { ny = b.second + move1[i]; nx = b.first +move2[i]; if((nx>=0&&nx<N)&&(ny>=0&&ny<M)&&maze[nx][ny]!='#'&&d1[nx][ny]==INF) { que.push(P(nx,ny)); d1[nx][ny] = d1[b.first][b.second] + 1; } } } que.push(P(x1,y1)); while (!que.empty()) { b = que.front(); que.pop(); int nx,ny; for (int i = 0;i < 4;i++) { ny = b.second + move1[i]; nx = b.first +move2[i]; if((nx>=0&&nx<N)&&(ny>=0&&ny<M)&&maze[nx][ny]!='#'&&d2[nx][ny]==INF) { que.push(P(nx,ny)); d2[nx][ny] = d2[b.first][b.second] + 1; } } }}int main(void){ int k; int ans; while(scanf("%d %d",&N,&M) != EOF) { if(N==0&&M==0) { break; } k = 0; for (int i = 0;i < N;i++) { scanf("%s",maze[i]); } for (int i = 0;i < N;i++) { for (int j = 0;j < M;j++) { if(maze[i][j] == 'Y') yx = i,yy = j; else if(maze[i][j] == 'M') mx = i,my = j; else if(maze[i][j] == '@') { ax[k] = i,ay[k] = j; k++; } } } dfs(yx,yy,mx,my); ans = d1[ax[0]][ay[0]] + d2[ax[0]][ay[0]]; for (int i = 1;i < k;i++) { ans = min(ans,d1[ax[i]][ay[i]] + d2[ax[i]][ay[i]]); /*cout<<d2[ay[i]][ax[i]]<<endl; cout<<d1[ay[i]][ax[i]]<<endl;*/ } cout<<ans*11<<endl; } return 0;}
0 0
- 宽度优先搜索 BFS
- 宽度优先搜索BFS
- 宽度优先搜索
- 宽度优先搜索图
- 宽度优先搜索
- 宽度优先搜索
- 宽度优先搜索
- 宽度优先搜索bfs
- 宽度优先搜索
- 宽度优先搜索算法
- 宽度优先搜索
- 宽度优先搜索
- 宽度优先搜索套路
- 宽度优先搜索
- 深度优先搜索与宽度优先搜索
- 广度优先搜索(宽度优先搜索)
- 宽度优先搜索与深度优先搜索
- 【宽度优先搜索】poj3414 Pots
- uva 11384 正整数序列
- 运输层多路复用与多路分解
- Abelssoft.SSD.Fresh.2017.v6.1-DVT
- Android官方培训课程中文版
- powershell的语音识别
- 宽度优先搜索
- Android 的提权 (Root) 原理
- TokuDB的特点验证 - billy鹏
- Mac OS搭建简单的Java开发环境
- 使用SVN实现项目代码分享
- 阻塞线程--通过线程池实现
- 51nod-1424 零树
- 六大设计原则
- 简单的MYSQL语法