国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

hdu 2612 Find a way【BFS】

2019-11-08 03:17:14
字體:
來源:轉載
供稿:網友

題目鏈接:http://acm.hdu.edu.cn/showPRoblem.php?pid=2612 題意:y和m要在kfc見面,問你能否選擇一家kfc使得y和m到那里的距離最短 解析:兩個人都跑一遍bfs,然后枚舉每家kfc,找出最優的即可

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;const int inf = 0x7fffffff;int dx[] = {0,1,-1,0};int dy[] = {1,0,0,-1};int vis[205][205];int step1[205][205];int step2[205][205];int n,m;char a[205][205];struct node{ int x,y; int step; node() {} node(int _x,int _y,int _step) { x = _x; y = _y; step = _step; }};void bfs(int sx,int sy,int step[205][205]){ memset(vis,0,sizeof(vis)); queue<node>q; q.push(node(sx,sy,0)); while(!q.empty()) { node now = q.front(); q.pop(); if(a[now.x][now.y]=='@') { if(step[now.x][now.y]) step[now.x][now.y] = min(step[now.x][now.y],now.step); else step[now.x][now.y] = now.step; } for(int i=0;i<4;i++) { int tx = now.x+dx[i]; int ty = now.y+dy[i]; if(tx<0 || tx>=n || ty<0 || ty>=m) continue; if(a[tx][ty]=='#' || vis[tx][ty]) continue; vis[tx][ty] = 1; q.push(node(tx,ty,now.step+1)); } }}int main(){ while(~scanf("%d %d",&n,&m)) { int x1,y1,x2,y2; for(int i=0;i<n;i++) { scanf("%s",a[i]); for(int j=0;j<m;j++) { if(a[i][j]=='Y') { x1 = i; y1 = j; } if(a[i][j]=='M') { x2 = i; y2 = j; } } } memset(step1,0,sizeof(step1)); memset(step2,0,sizeof(step2)); bfs(x1,y1,step1); bfs(x2,y2,step2); int ans = inf; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j] == '@') { if(step1[i][j] && step2[i][j]) ans = min(ans,step1[i][j]+step2[i][j]); } } } printf("%d/n",ans*11); } return 0;}
上一篇:遞歸求組合

下一篇:Unity-Networking

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富川| 乌拉特后旗| 册亨县| 天水市| 德兴市| 泸定县| 运城市| 金溪县| 屏东市| 丹棱县| 城口县| 宁海县| 隆尧县| 龙州县| 凤台县| 沙洋县| 康定县| 乐陵市| 雅江县| 丰镇市| 芦溪县| 灵宝市| 庆安县| 印江| 城口县| 五家渠市| 高台县| 太仓市| 淮北市| 大田县| 合川市| 宁陕县| 尚义县| 新河县| 察隅县| 安阳市| 和政县| 嘉义市| 宁明县| 竹溪县| 佛坪县|