hdu 2612 Find a way
来源:互联网 发布:全排列的递归实现算法 编辑:程序博客网 时间:2024/06/18 01:09
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5432 Accepted Submission(s): 1837
Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866
两次bfs。
#include<iostream>using namespace std;#include<string.h>#include<queue>char mat[205][205];int n,m,dir[4][2]={1,0,0,1,-1,0,0,-1};int s,h,vis[205][205],ans[205][205];struct node{ int x,y,sum;};void bfs(int x,int y){ int i; queue<node>q; struct node head,next; head.x=x; head.y=y; head.sum=0; vis[head.x][head.y]=1; q.push(head); while(!q.empty()) { head=q.front(); q.pop(); for(i=0;i<4;i++) { next.x=head.x+dir[i][0]; next.y=head.y+dir[i][1]; next.sum=head.sum+1; if(mat[next.x][next.y]=='#'||next.x<=0||next.y<=0||next.x>n||next.y>m)continue; if(mat[next.x][next.y]=='@'&&!vis[next.x][next.y]) { vis[next.x][next.y]=1; ans[next.x][next.y]+=next.sum; q.push(next); } if(mat[next.x][next.y]=='.'&&!vis[next.x][next.y]) {vis[next.x][next.y]=1; q.push(next); } } } return ;}int main(){ int i,j,min,i1,j1,i2,j2; while(cin>>n>>m) {min=9999999; memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); for(i=1;i<n+1;i++) for(j=1;j<m+1;j++) { cin>>mat[i][j]; if(mat[i][j]=='Y') i1=i,j1=j; if(mat[i][j]=='M') i2=i,j2=j; } bfs(i1,j1); memset(vis,0,sizeof(vis)); bfs(i2,j2); for(i=1;i<n+1;i++) for(j=1;j<m+1;j++) if(mat[i][j]=='@'&&ans[i][j]!=0)//这处坑爹,少写了ans[i][j]!=0,因为有可能有几处KFC根本无法到达,求最小值时会为0,把无法到达的也给算进去。 { if(min>ans[i][j]) {min=ans[i][j];} } cout<<min*11<<endl; } return 0;}
0 0
- HDU 2612 Find a way
- HDU-2612-Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU 2612 Find A Way
- hdu 2612 Find a way
- HDU-2612-Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU-2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- html之object标签的classid收集
- MySQL 获得当前日期时间(以及时间的转换)
- history对象back()、forward()、go()方法
- zabbix用自带模板监控mysql
- 布局-高度自适应
- hdu 2612 Find a way
- 黑马程序员—Java基础(接口,类,抽象类综合小练习)
- Android平台开发-WIFI 驱动移植 -- 详细
- 一步一步教你做ios推送
- Mysql数据库安装和配置
- 状态管理-Cookie
- Python单例模式的4种实现方法
- 黑马程序员——C语言—结构体变量存储原理
- .net数据库迁移