1. 程式人生 > >HDU 2612 Find a way

HDU 2612 Find a way

題目連結:傳送門

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
[email protected]
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

題目大意:輸入Y,M表示接下來有一個Y*M的矩陣,Y和M分別表示兩個人的地點,@表示KFC,#表示牆,問兩個人到達KFC的時間相加最短是多少。

坑點:有的KFC可能在牆內,不能到達。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m[202][202];
int n[202][202];
int w[202][202];
char s[202][202];
int Y,M;
int x1,x2,y1,y2;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int a,b,c;
};
void bfs(int x,int y,int ss[][202])
{
    int i;
	node st,ed;
	queue<node>q;
	st.a=x;
	st.b=y;
	st.c=0;
	q.push(st);
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			ed.a=st.a+dir[i][0];
			ed.b=st.b+dir[i][1];
			if(ed.a<0||ed.b<0||ed.a>=Y||ed.b>=M||s[ed.a][ed.b]=='#'||w[ed.a][ed.b])
                continue;
			ed.c=st.c+1;
			w[ed.a][ed.b]=1;
		    if(s[ed.a][ed.b]=='@')
			{
				ss[ed.a][ed.b]=ed.c;
			}
			q.push(ed);
		}
	}
}
int main()
{
    int a,b,c,d,e,f,g;
    while(cin>>Y>>M)
    {
        for(a=0;a<Y;a++)
            cin>>s[a];
        for(b=0;b<Y;b++)
        {
            for(c=0;c<M;c++)
            {
                if(s[b][c]=='Y')
                {
                    x1=b;
                    y1=c;
                }
                if(s[b][c]=='M')
                {
                    x2=b;
                    y2=c;
                }
            }
        }
        memset(n,0,sizeof(n));
        memset(m,0,sizeof(m));
        memset(w,0,sizeof(w));
        w[x1][y1]=1;
        bfs(x1,y1,n);
        memset(w,0,sizeof(w));
        w[x2][y2]=1;
        bfs(x2,y2,m);
        int min=0x6ffffff;
        for(d=0;d<Y;d++)
        {
            for(e=0;e<M;e++)
            {
                if(min>n[d][e]+m[d][e]&&n[d][e]&&m[d][e])
                    min=n[d][e]+m[d][e];
            }
        }
        cout<<min*11<<endl;
    }
    return 0;
}

相關推薦

HDU 2612 Find a way 題解

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25379  &n

【hdu1241Oil Deposits】【HDU 2612 Find a way

HDU1241Oil Deposits 求聯通塊數量 Sample Input 1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5  ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0  Sample Output 0 1

HDU 2612 Find a way(雙層BFS)

傳送門 題目大概的意思就是兩個人Y和M約在KFC見面,找到一家KFC使得兩個人到達時的距離之和最短,並且Y不能經過M的出發地,M不能經過Y的出發地。 思路也很清晰,用兩次BFS,找出Y和M分別到每個KFC所需要的時間,然後把兩個人到達每個KFC需要的時間加起來,找出最小值,這個最小值就是題目

HDU 2612 Find a way

題目連結:傳送門 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifen

HDU-2612-Find a way

fin 捕獲 fir sizeof ret amp second using 時間 鏈接:https://vjudge.net/problem/HDU-2612#author=zhang95986 題意: hsj和lsh最近迷上了pokemon go的遊戲。在雙十一大物

hdu Find a way(兩路廣搜)

Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to m

Find a way (廣度優先搜索)

需要 sam namespace 比較 建立 繼續 tip total lag 題目: Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo o

Find a way(兩個BFS)

city any queue ret find 初始化 typedef 維數 opened Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo a

N - Find a way

mos inpu 標記 oid += expr them people 多次 Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one ye

M - Find a way ~~~ [kuangbin帶你飛]專題一 簡單搜尋

hsj和lsh最近迷上了pokemon go的遊戲。在雙十一大物期中考試來臨之前,他們想抓一隻稀有土撥鼠來攢攢人品(因為土撥鼠的重新整理地點最近來到了哈工程) 但是由於土撥鼠過於強大,他的雷霆半月斬以及驚天浪濤沙都可以輕鬆的將他們兩擊敗,但是他們兩的合擊必殺技流影電光閃以及天羽屠鼠舞可以將土撥鼠打至

Find a way

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to

HDU2612 Find a way(BFS簡單模板)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 24558    Accepted Sub

Find a Way (雙bfs)

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

Find a way(兩路廣搜)

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

Find a way bfs搜尋 容易出錯

題目連結:題意:給你一個圖,圖中有不能走的障礙物,和兩人,以及n個(n>=1)KFC,現在要求找到其中一個KFC,讓兩個人人走到這個KFC的時間總和最小;#include <iostream

HDU-5980 Find Small A

水題 #include<iostream> using namespace std; typedef long long ll; ll a; int main() { int n; cin>>n; int ans=0; while(n--)

HDU 2012Find a way

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22068 &

CentOS系統yum報錯Cannot find a valid baseurl for repo

val 成功 start network valid 搜索 解決 base 連接 今天在嘗試安裝apache2服務器時候遇到了問題:在使用新安裝的CentOS系統下使用yum命令行直接報錯。 搜索了一下解決方法,首先嘗試第一個,修改DNS vi /etc/resolv.co

Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.

dex parameter work put 嚴重 efi open post onerror 1、錯誤描寫敘述八月 14, 2015 4:22:45 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error

Tomcat性能調優後, 啟動出現警告問題 [did not find a matching property.]

light title lis config tar ont nbsp dsm servlet http://blog.csdn.net/dracotianlong/article/details/8963594 Tomcat性能調優後, 啟動出現警告問題 [did