标签:
Time Limit: 2000MS | Memory Limit: 32768K | |
Total Submissions: 4004 | Accepted: 1504 |
Description
Input
Output
Sample Input
2 3 3 ### #.# ### 7 6 ####### #.#.### #.#.### #.#.#.# #.....# #######
Sample Output
Maximum rope length is 0. Maximum rope length is 8.
题意:一段绳子,可以在迷宫内使用,要求这段绳子可以从迷宫内任意一点到任意的另一点,求绳子的最小长度
题解:求迷宫内最长路径的长度,利用树的直径的求法,进行两次bfs
#include<stdio.h> #include<string.h> #include<queue> #define MAX 1010 using namespace std; char map[MAX][MAX]; bool vis[MAX][MAX]; int n,m; int ans,sum,x1,y1,x2,y2; struct node { int x,y; int ong; }; void getmap() { int i,j; for(i=0;i<n;i++) scanf("%s",map[i]); for(i=0;i<n;i++) for(j=0;j<m;j++) { if(map[i][j]==‘.‘) { x1=i; y1=j; return ; } } } bool judge(int c,int r) { if(c>=0&&c<n&&r>=0&&r<m&&map[c][r]!=‘#‘&&!vis[c][r]) return true; return false; } void bfs(int x1,int y1) { int i,j; int move[4][2]={0,1,0,-1,1,0,-1,0}; memset(vis,false,sizeof(vis)); node beg,end; queue<node>q; beg.x=x1; beg.y=y1; beg.ong=0; vis[x1][y1]=true; q.push(beg); while(!q.empty()) { end=q.front(); q.pop(); for(i=0;i<4;i++) { beg.x=end.x+move[i][0]; beg.y=end.y+move[i][1]; if(judge(beg.x,beg.y)) { vis[beg.x][beg.y]=true; beg.ong=end.ong+1; if(ans<beg.ong) { ans=beg.ong; x2=beg.x; y2=beg.y; } q.push(beg); } } } } int main() { int t; scanf("%d",&t); while(t--) { getchar(); scanf("%d%d",&m,&n); ans=0; getmap(); bfs(x1,y1); bfs(x2,y2); printf("Maximum rope length is %d.\n",ans); } return 0; }
poj 1383 Labyrinth【迷宫bfs+树的直径】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4738965.html