hdu 2612 find a way
来源:互联网 发布:masslynx数据导出 编辑:程序博客网 时间:2024/05/12 10:56
点击打开链接
找到离两个人路程和的最小值。
可以广搜一遍,把有kfc的的路中最小值,找出来。
也可以两个bfs 求两个距离和最小。
坑爹的我之前吧距离和存一个数组,竟然WA了。难道是还有情况他们有人到不了KFC!!。
#include<bits/stdc++.h>const int maxn=200+20;char ma[maxn][maxn];#define inf 0xffffffint v[maxn][maxn],dis[maxn][maxn][2];using namespace std;struct node{ int x,y,step;};int m,n,sx,sy,ex,ey;int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int judge(int x,int y){ if(x<1||y<1||x>n||y>m) return 0; if(ma[x][y]=='#'||v[x][y]) return 0; return 1;}void bfs(int x1,int y1,int f){ v[x1][y1]=1; node e,u; e.x=x1,e.y=y1;e.step=0; queue<node>que; que.push(e); while(!que.empty()){ u=que.front(); que.pop(); for(int i=0;i<4;i++){ e.x=u.x+dir[i][0]; e.y=u.y+dir[i][1]; e.step=u.step+1; if(judge(e.x,e.y)){ dis[e.x][e.y][f]=e.step; v[e.x][e.y]=1; que.push(e); } } }}int main(){ while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;i++){ getchar(); for(int j=1;j<=m;j++){ scanf("%c",&ma[i][j]); if(ma[i][j]=='Y') sx=i,sy=j; if(ma[i][j]=='M') ex=i,ey=j; dis[i][j][0]=dis[i][j][1]=inf; } } memset(v,0,sizeof(v)); bfs(sx,sy,0); memset(v,0,sizeof(v)); bfs(ex,ey,1); int ans=inf; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(ma[i][j]=='@') ans=min(ans,dis[i][j][0]+dis[i][j][1]); printf("%d\n",ans*11); } 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
- 关于Android minSdkVersion和targetSdkVersion
- 谈谈 Python 程序的运行原理
- 【Codeforces Round #389】Codeforces 752B Santa Claus and Keyboard Check
- 配置hadoop中常用的Linux(ubuntu)命令
- 【codeforces 752C】Santa Claus and Robot
- hdu 2612 find a way
- CSS--鼠标滑过、鼠标点击
- 【Codeforces Round #389】Codeforces 752C Santa Claus and Robot
- 栈帧结构
- 校赛部分web+crypto WriteUP
- shell script随笔记
- 51单片机显示时间日期
- dede封面页无法生成,模板不存在,无法解析!
- N后问题——回溯法求解