杭电2612
来源:互联网 发布:js继承面试题详解 编辑:程序博客网 时间:2024/05/29 14:53
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>#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;int n,m,vis[201][201],d[4][2]={1,0,-1,0,0,1,0,-1};char mm[201][201];int vj(int x,int y){ if(x<0||x>=n||y<0||y>=m||vis[x][y])return 0; return 1;}struct node { int x,y,t;}k[40000],a,b;int main(){ while(cin>>n>>m){ for(int i=0;i<n;i++) scanf("%s",mm[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(mm[i][j]=='Y'){ a.x=i; a.y=j; a.t=0; } } } memset(vis,0,sizeof(vis)); queue<node>q; q.push(a); int len=0; while(!q.empty()){ a=q.front(); q.pop(); if(mm[a.x][a.y]=='@'){ k[len++]=a; } for(int i=0;i<4;i++){ int xx=a.x+d[i][0]; int yy=a.y+d[i][1]; if(vj(xx,yy)&&mm[xx][yy]!='#'){ b.x=xx; b.y=yy; b.t=a.t+11; vis[xx][yy]=1; q.push(b); } } } int t[40001]; int h=0; for(int i=0;i<len;i++){ memset(vis,0,sizeof(vis)); q.push(k[i]); while(!q.empty()){ a=q.front(); q.pop(); if(mm[a.x][a.y]=='M'){ t[h++]=a.t; break; } for(int i=0;i<4;i++){ int xx=a.x+d[i][0]; int yy=a.y+d[i][1]; if(vj(xx,yy)&&mm[xx][yy]!='#'){ b.x=xx; b.y=yy; b.t=a.t+11; vis[xx][yy]=1; q.push(b); } } } } sort(t,t+h); cout<<t[0]<<endl; } return 0;}
交了之后我都觉得自己蠢
第一次思路是从一个点去找肯德基,然后再从肯德基找另一个人
当肯德基数量跟多的时候就会超时
这样走了无数次重复的路
正解是从两个点搜索两次
#include<iostream>#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;int n,m,vis[201][201],t[200][200],d[4][2]={1,0,-1,0,0,1,0,-1};char mm[201][201];int vj(int x,int y){ if(x<0||x>=n||y<0||y>=m||vis[x][y])return 0; return 1;}struct node { int x,y,t;}k[40000],a,b,c;int main(){ while(cin>>n>>m){ for(int i=0;i<n;i++) scanf("%s",mm[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ t[i][j]=99999999; if(mm[i][j]=='Y'){ a.x=i; a.y=j; a.t=0; } if(mm[i][j]=='M'){ c.x=i; c.y=j; c.t=0; } } } memset(vis,0,sizeof(vis)); queue<node>q; q.push(a); while(!q.empty()){ a=q.front(); q.pop(); if(mm[a.x][a.y]=='@'){ t[a.x][a.y]=a.t; } for(int i=0;i<4;i++){ int xx=a.x+d[i][0]; int yy=a.y+d[i][1]; if(vj(xx,yy)&&mm[xx][yy]!='#'){ b.x=xx; b.y=yy; b.t=a.t+11; vis[xx][yy]=1; q.push(b); } } } memset(vis,0,sizeof(vis)); q.push(c); while(!q.empty()){ a=q.front(); q.pop(); if(mm[a.x][a.y]=='@'){ t[a.x][a.y]+=a.t; } for(int i=0;i<4;i++){ int xx=a.x+d[i][0]; int yy=a.y+d[i][1]; if(vj(xx,yy)&&mm[xx][yy]!='#'){ b.x=xx; b.y=yy; b.t=a.t+11; vis[xx][yy]=1; q.push(b); } } } int minn=99999999; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(t[i][j]<minn)minn=t[i][j]; } } cout<<minn<<endl; } return 0;}
1 0
- 杭电2612
- 杭电2612
- 杭电
- 杭电
- 杭电
- 杭电---2612 find a way
- 杭电2612Find a way
- 【杭电】[2612]Find a way
- 杭电搜索 2612 Find a way
- 杭电 1234 和 杭电 2115
- 杭电2056之Rectangles 杭电
- 杭电ACM1061Rightmost Digit
- 杭电2099 7.11
- 杭电ACM 1003
- 杭电 ACM 2016
- 杭电ACM1466
- 杭电ACM1003
- 杭电ACM1225
- 开源CORBA OmniORB开发环境搭建及编译问题解决方法
- 贪心算法☞会场安排问题/图着色
- c++ map使用
- 将WEB PROJECT部署到tomcat
- csuoj-1729-齿轮传动
- 杭电2612
- Android JSONArray.remove ()报错
- Onethink 系统架构 开发规划图
- Noip2007普及组
- android Binder 学习
- hdoj 4268 Alice and Bob 【贪心 + set】
- 【C#语法】C#中强转和as、is的区别
- c++静态链接库和动态链接库的创建和调用
- ssh整合简单总结