hdu 2612 Find a way(bfs)

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.

?

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

?

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 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

?

Sample Output
66
88
66

?

bfs求出兩個人到各個點的距離,最后枚舉最短的距離。用c++提交WA,G++提交AC了,這是什么原因。。。

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<set>
 10 #include<bitset>
 11 #include<map>
 12 #include<vector>
 13 #include<stdlib.h>
 14 #include <stack>
 15 using namespace std;
 16 #define PI acos(-1.0)
 17 #define max(a,b) (a) > (b) ? (a) : (b)
 18 #define min(a,b) (a) < (b) ? (a) : (b)
 19 #define ll long long
 20 #define eps 1e-10
 21 #define MOD 1000000007
 22 #define N 206
 23 #define inf 1e12
 24 int n,m;
 25 char mp[N][N];
 26 struct Node{
 27    int x,y;
 28    int t;
 29 }st1,st2;
 30 int vis[N][N];
 31 int dirx[]={0,0,-1,1};
 32 int diry[]={-1,1,0,0};
 33 int dist1[N][N];
 34 int dist2[N][N];
 35 void bfs1(Node st){
 36    memset(vis,0,sizeof(vis));
 37    queue<Node>q;
 38    q.push(st);
 39    vis[st.x][st.y]=1;
 40 
 41    Node t1,t2;
 42    while(!q.empty()){
 43       t1=q.front();
 44       q.pop();
 45       for(int i=0;i<4;i++){
 46          t2.x=t1.x+dirx[i];
 47          t2.y=t1.y+diry[i];
 48          if(mp[t2.x][t2.y]=='#') continue;
 49          if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue;
 50          if(vis[t2.x][t2.y]) continue;
 51          vis[t2.x][t2.y]=1;
 52          t2.t=t1.t+1;
 53          dist1[t2.x][t2.y]=t2.t;
 54          q.push(t2);
 55       }
 56    }
 57 }
 58 void bfs2(Node st){
 59    memset(vis,0,sizeof(vis));
 60    queue<Node>q;
 61    q.push(st);
 62    vis[st.x][st.y]=1;
 63 
 64    Node t1,t2;
 65    while(!q.empty()){
 66       t1=q.front();
 67       q.pop();
 68       for(int i=0;i<4;i++){
 69          t2.x=t1.x+dirx[i];
 70          t2.y=t1.y+diry[i];
 71          if(mp[t2.x][t2.y]=='#') continue;
 72          if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue;
 73          if(vis[t2.x][t2.y]) continue;
 74          vis[t2.x][t2.y]=1;
 75          t2.t=t1.t+1;
 76          dist2[t2.x][t2.y]=t2.t;
 77          q.push(t2);
 78       }
 79    }
 80 }
 81 int main()
 82 {
 83    while(scanf("%d%d",&n,&m)==2){
 84       memset(dist1,0,sizeof(dist1));
 85       memset(dist2,0,sizeof(dist2));
 86       for(int i=0;i<n;i++){
 87          scanf("%s",mp[i]);
 88          for(int j=0;j<m;j++){
 89             if(mp[i][j]=='Y'){
 90                st1.x=i;
 91                st1.y=j;
 92                st1.t=0;
 93             }
 94             if(mp[i][j]=='M'){
 95                st2.x=i;
 96                st2.y=j;
 97                st2.t=0;
 98             }
 99          }
100       }
101 
102       bfs1(st1);
103       bfs2(st2);
104 
105       int ans=inf;
106       for(int i=0;i<n;i++){
107          for(int j=0;j<m;j++){
108             if(mp[i][j]=='@'){
109                if(dist1[i][j]!=0 && dist2[i][j]!=0){
110                   int sum=dist1[i][j]+dist2[i][j];
111                   if(sum<ans){
112                      ans=sum;
113                   }
114                }
115 
116             }
117          }
118       }
119       printf("%d\n",ans*11);
120 
121 
122    }
123     return 0;
124 }
View Code

?

?

轉載于:https://www.cnblogs.com/UniqueColor/p/4972680.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/256417.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256417.shtml
英文地址,請注明出處:http://en.pswp.cn/news/256417.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

定義類或對象

學習總結&#xff1a; 工廠方式 原始的方式&#xff08;對象創建后動態定義對象的屬性&#xff09; var oCar new Object; oCar.color "blue"; oCar.doors 4; oCar.mpg 25; oCar.showColor function() {alert(this.color); };創建對象 car&#xff0c;屬性&…

七橋問題

怎么不重復地走完連接兩座島和陸地的七座橋&#xff1f; 簡化為以下&#xff1a; 答案是不能走完的。 奇點&#xff1a;這個點有奇數條線匯聚于此 偶點&#xff1a;這個點有奇數條線匯聚于此 七橋問題——一筆畫問題 若一個圖形全部是偶點或者只有2個奇點&#xff08;沒有…

office2016打開PPT出現解決VBE6EXT.OLB不能被加載問題的解決辦法

第一步 打開路徑C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\VBA。找到VBA只要是默認安裝路徑均一樣。 第二步 打開VBA6找到VBE6EXT.OLB將其復制到VBA7.1中。 第三步 打開VBA7.1找到VBE7.DLL將其復制到VBA6中。 第四步…

20151118小問題

1.模板引擎 百度百科:模板引擎(這里特指用于Web開發的模板引擎)是為了使 用戶界面與業務數據(內容)分享而產生的,它或以生成特定格式的文檔,用于網站的模板引擎就會產生一個標準的HTML文檔. 目的:生成一個標準的HTML文檔. 概念:模板引擎不屬于特定技術領域,它是跨領域跨平臺的概…

機器學習——人工神經網絡之發展歷史(神經元數學模型、感知器算法)

目錄 一、神經元的數學模型 ? 二、感知器算法&#xff08;SVM算法前身&#xff09; 1、目的 2、流程 >>>問題1&#xff1a;下圖w和b的調整是什么意思&#xff1f; 3、算法的有效性驗證 1&#xff09;原算法 2&#xff09;增廣矩陣 3&#xff09;修改后的算法…

PHP 基礎知識-數組

PHP 的數組主要分為&#xff1a; 索引數組 - 帶有數字索引的數組關聯數組 - 帶有指定鍵的數組多維數組 - 包含一個或多個數組的數組 索引數組&#xff1a;有兩種創建索引數組的方法&#xff1a;索引是自動分配的&#xff08;索引從 0 開始&#xff09;&#xff1a; 第一…

打開word2016總是出現很抱歉,此功能看似中斷需要修復。。問題解決辦法

第一步 打開運行窗口&#xff0c;在電腦桌面左下角有個圓圈點擊進去&#xff0c;輸入regedit&#xff0c;即可進入。 第二步 打開HKEY_CURRENT_USER中的SOFTWARE 第三步 找到HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options項如圖紅色箭頭標示。然后點擊O…

機器學習——人工神經網絡之多層神經網絡(多層與三層)

目錄 一、多層神經網絡 1、多層神經網絡數學模型 2、數學模型中的非線性函數fai 1&#xff09;非線性函數fai存在的意義 2&#xff09;非線性函數fai具體是什么&#xff1f; 3、多層神經網絡與單層神經網絡的區別與改進 1&#xff09;單層神經網絡數學模型 2&#xff0…

noip2012-day2-t2

【問題描述】 在大學期間&#xff0c;經常需要租借教室。大到院系舉辦活動&#xff0c;小到學習小組自習討論&#xff0c;都需要向學校申請借教室。教室的大小功能不同&#xff0c;借教室人的身份不同&#xff0c;借教室的手續也不一樣。 面對海量租借教室的信息&#xff0c;我…

機器學習——人工神經網絡之后向傳播算法(BP算法)

目錄 一、后向傳播算法的本質——梯度下降法求局部極值 1、w迭代公式的合理性

獲取視圖的寬高

1 view.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 2 int width view.getMeasuredWidth(); 3 int height view.getMeasuredHeight(); 轉載于:https://www.cnblogs.com/cmgrass/p/4978222.html

排序算法02--冒泡排序

思路&#xff1a;冒泡排序 就是把大的數一個個沉到下面&#xff0c;當然也可以是把小的數一個個浮到上面。 在最外層需要比較n-1次&#xff0c;因為n-1個大的數被沉到了下面&#xff0c;剩下一個自然就是最小的數了。 在這n-1次的里層&#xff0c;還需要亮亮相互比較&#xff0…

機器學習——人工神經網絡之參數設置(BP算法)

目錄 一、復習(BP算法) 二、訓練模型的建議 三、參數設置內容 1、隨機梯度下降(SGD)

關于▲的各種交點

對于△ABC證明&#xff1a; ①三角形的三條中線交于一點&#xff1a; 等腰三角形&#xff1a;作中線BD、CE與AC、AB交于D、E&#xff0c;相交于O&#xff0c;連接AO并延長交BC于F&#xff1b; 證△ABD全等于△ACE&#xff0c;再證△EBO全等于△D…

javaScript獲取url中的參數

var urlTools {//獲取RUL參數值getUrlParam: function(name) { /*?videoIdidentification */var params decodeURI(window.location.search); /* 截取&#xff1f;號后面的部分 index.html?actdoctor,截取后的字符串就是?actdoctor */var reg …

機器學習——支持向量機SVMpython實現

一、SVM理論 可見以下文章&#xff1a; 《機器學習——支持向量機SVM之線性模型》 《機器學習——支持向量機SVM之非線性模型低維到高維映射》 《機器學習——支持向量機SVM之非線性模型原問題與對偶問題》 《機器學習——支持向量機SVM之非線性模型原問題轉化為對偶問題》…

瑣碎易錯點

1.font-size 設置的是字體的高 2.瀏覽器內核&#xff1a; 主流瀏覽器   內核 IE       trident Firfox     Gecko Chorme    Webkit&#xff08;原來&#xff09;/blink&#xff08;現在&#xff09; Safari     Webkit&#xff08;蘋果公司獨立研發的&a…

Python安裝Jupyter Notebook配置使用教程

原文見&#xff1a;https://blog.csdn.net/qq_27825451/article/details/84427269 一、什么是jupyter 1、簡介&#xff1a; jupyter notebook是一種 Web 應用&#xff0c;能讓用戶將說明文本、數學方程、代碼和可視化內容全部組合到一個易于共享的文檔中。它可以直接在代碼旁…

ExtJS4.2學習(10)分組表格控件--GroupingGrid(轉)

鳴謝網址&#xff1a;http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-17/179.html --------------------------------------------------------------------------------------------- 分組表格控件在我們的開發中經常被用到&#xff0c;GroupingGrid…

九個Console命令,讓js調試更簡單

一、顯示信息的命令 1: <!DOCTYPE html>2: <html>3: <head>4: <title>常用console命令</title>5: <meta http-equiv"Content-Type" content"text/html; charsetutf-8" />6: </head>7: <body>8: …