hdu2612

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21639    Accepted Submission(s): 7026


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
 

Author
yifenfei
 

Source

奋斗的年代


讲道理不难  2分钟看题  3分钟敲打码的水题 

结果tm的卡了我一个多小时

说坑点

就是Y 或者M 不能经过对方所在的点    这个坑可以在刚开始的处理掉  就是把他们变成墙 #

第2个坑点  就是  过程中可能遇到KFC的店 @  但是还要沿着这个点向四周延伸

意思就是  可以路过某个KFC 去另一个KFC去吃鸡 

刺不刺激

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define sz size()
#define cl clear()
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164
typedef long long ll;
typedef pair<int,int> pr;
const int inf = 99999999;
const double eps = 1e-8;
const int dir4[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
const int dir8[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
struct node{
	int x, y;
	int step;
	node(){
	}
	node(int x,int y,int step)
	{
		this->x = x;
		this->y = y;
		this->step = step;
	}
};
int n, m;
int ps1, pe1, ps2, pe2;
char mp[205][205];
int tim[205][205],tiy[205][205];
int vis[205][205]; 

void bfs()
{
	queue<node> q1,q2;
	q1.push(node(ps1,pe1,0));
	q2.push(node(ps2,pe2,0));
	
	mset(vis,0);
	vis[ps1][pe1] = 1;
	while(!q1.empty())
	{
		node tmp = q1.front();
		q1.pop();
		// 这么写就过不了了  也就是说竟然可以路过一个KFC去另一个KFC吃鸡   坑不吭 
//		if(mp[tmp.x][tmp.y] == '@')
//		{
//			tiy[tmp.x][tmp.y] = tmp.step;
//			continue;
//		}
		for(int i = 0;i < 4;i ++)
		{
			int dx = tmp.x + dir4[i][0];
			int dy = tmp.y + dir4[i][1];
			
			if(dx < 1 || dy < 1 || dx > n || dy > m || vis[dx][dy] || mp[dx][dy] == '#')
			continue;
			if(mp[dx][dy] == '@')
			tiy[dx][dy] = tmp.step + 1;
			q1.push(node(dx,dy,tmp.step+1));
			vis[dx][dy] = 1;
		}
	}
	
	mset(vis,0);
	vis[ps2][pe2] = 1;
	while(!q2.empty())
	{
		node tmp = q2.front();
		q2.pop();
		
		for(int i = 0;i < 4;i ++)
		{
			int dx = tmp.x + dir4[i][0];
			int dy = tmp.y + dir4[i][1];
			
			if(dx < 1 || dy < 1 || dx > n || dy > m || vis[dx][dy] || mp[dx][dy] == '#')
			continue;
			if(mp[dx][dy] == '@')
			tim[dx][dy] = tmp.step + 1;
			q2.push(node(dx,dy,tmp.step+1));
			vis[dx][dy] = 1;
		}
	}
}

int main() 
{
	while(cin >> n >> m)
	{
		mset(tim,0);
		mset(tiy,0);
		
		for(int i = 1;i <= n;i ++)
		cin >> mp[i] + 1;

		for(int i = 1;i <= n;i ++)
		{
			for(int j = 1;j <= m;j ++)
			{
				if(mp[i][j] == 'Y')
				{
					ps1 = i;
					pe1 = j;
					mp[i][j] = '#';
				}
				if(mp[i][j] == 'M')
				{
					ps2 = i;
					pe2 = j;
					mp[i][j] = '#';
				}
			}
		}
		
		bfs();
		
		int minCost = inf;
		
		for(int i = 1;i <= n;i ++)
		{
			for(int j = 1;j <= m;j ++)
			{
				if(minCost > tiy[i][j] + tim[i][j] && tiy[i][j] && tim[i][j])
				minCost = tiy[i][j] + tim[i][j];
			}
		}		
		cout << minCost * 11 << endl;
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/soul_97/article/details/80325810