HDU 2612 Find a way (廣搜)
【題目連結】
http://acm.hdu.edu.cn/showproblem.php?pid=2612
題目意思
兩個人約在肯德基見面,但是肯德基有多家,所以讓你找出兩人時間和最少的一家肯德基。每次移動是11分鐘,‘@’為肯德基,‘。’為路,‘#’為牆
解題思路
跑兩遍廣搜,記錄到的時間,找出兩次和最小的就可以了
程式碼部分
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 1e5+5;
struct node
{
int x,y,step;
}s1,s2;
char maps[205][205];
int vis[205][205];
int step[205][205]; ///記錄步數
int dir[4][2]={1,0,-1,0,0,-1,0,1}; ///方向
int Max = inf;
int n,m;
void bfs (node s, int k)
{
queue <node>q;
q.push(s);
vis[s.x][s.y] = 1;
while (!q.empty())
{
node t,tt;
t = q.front();
q.pop();
tt.step = t.step+11;
if (maps[t.x][t.y] == '@') ///到肯德基時加上步數
{
step[t.x][t.y] += t.step;
if (step[t.x][t.y] < Max && k == 2) ///如果是第二個人到看下是否跟新最小和
Max = step[t.x][t.y];
}
for (int i = 0; i < 4; i++)
{
tt.x = t.x+dir[i][0];
tt.y = t.y+dir[i][1];
if (tt.x >= 0 && tt.y >= 0 && tt.x < n && tt.y < m && !vis[tt.x][tt.y] && maps[tt.x][tt.y] !='#')
{
vis[tt.x][tt.y] = 1;
q.push(tt);
}
}
}
}
int main()
{
while (cin>>n>>m)
{
Max = inf;
memset(step,0,sizeof(step));
memset(vis,0,sizeof (vis));
for (int i =0; i < n; i++)
for (int j = 0; j < m;j++)
{
cin>>maps[i][j];
if (maps[i][j] == 'Y')
{
s1.x = i;
s1.y = j;
s1.step = 0;
}
if (maps[i][j] == 'M')
{
s2.x = i;
s2.y = j;
s2.step = 0;
}
}
bfs(s1,1);
memset(vis,0,sizeof(vis));
bfs(s2,2);
cout << Max << endl;
}
return 0;
}
相關文章
- HDU 2612 Find a way
- HDU2612 Find a way【BFS】
- HDU Find the hotel(RMQ)MQ
- By Recognizing These Guys, We Find Social Networks Useful(HDU-3849)
- HDU 4620 Fruit Ninja Extreme(搜尋)UIREM
- hdu 1175 連連看 搜尋
- HDU1560,迭代加深搜尋
- hdu 1728 逃離迷宮 搜尋
- HDU 6415(dp/記憶化搜尋)
- Find Any File for Mac(檔案搜尋)Mac
- 檔案搜尋工具:Find Any File for MacMac
- The Way to HappinessAPP
- 簡單檔案搜尋:Find Any File for MacMac
- Find Any File for Mac(檔案搜尋軟體)Mac
- Find Any File for Mac檔案搜尋軟體Mac
- 檔案搜尋工具:Find Any File Mac版Mac
- The Way To Go --- 切片Go
- Linux 搜尋命令總結 – whereis,which,locate,find,grepLinux
- c++ 廣度優先搜尋(寬搜)C++
- 如何使用 Find 和 Locate 在 Linux 中搜尋檔案Linux
- ybtoj:廣度優先搜尋
- bfs廣度優先搜尋
- Linux 檔案搜尋神器 find 實戰詳解,建議收藏!Linux
- hdu 2111 Saving HDU (DP)
- POJ 2891 Strange Way to Express IntegersExpress
- New way to deploy SSL VPN in LinuxLinux
- 演算法之路 - Way to Algorithm演算法Go
- The Best Way to Export an SVG from SketchExportSVG
- 搜狗搜尋推廣平臺下線
- 專業的本地檔案搜尋查詢器:Find Any File (FAF) for MacMac
- 推薦 | 通往AGI之路(Way to AGI)
- A way to represent that more realistically might be forcing users
- 聚焦科技女性 - “Her Future, Her Way”
- 圖的遍歷:深度優先搜尋與廣度優先搜尋
- js版本的(廣、深)度優先搜尋JS
- Shape of HDU
- HDU 3349
- 圖的廣度優先搜尋和深度優先搜尋Python實現Python