M - Find a way ~~~ [kuangbin带你飞]专题一 简单搜索

hsj和lsh最近迷上了pokemon go的游戏。在双十一大物期中考试来临之前,他们想抓一只稀有土拨鼠来攒攒人品(因为土拨鼠的刷新地点最近来到了哈工程)
但是由于土拨鼠过于强大,他的雷霆半月斩以及惊天浪涛沙都可以轻松的将他们两击败,但是他们两的合击必杀技流影电光闪以及天羽屠鼠舞可以将土拨鼠打至昏迷状态,并可将其捕获。
但是因为这是款按时间付费的游戏,他们需要尽快捕捉到土拨鼠(即他们两到土拨鼠的时间之和需要最少),因此他们找到了你来帮他们解决这个问题。 规定每走一步需要花费11分钟。

Input

输入存在多组(需使用!=EOF)
每组的第一行有两个整数n,m(2<=n,m<=200)
接下来n行,每行包括m个字符
‘Y’表示hsj所在的位置
‘M’表示lsh所在的位置
‘.’表示可以通过的地方
‘#’表示教学楼即不能走的地方
‘@’表示稀有土拨鼠刷新的地方(地图中存在多只稀有土拨鼠)

Output

对于每组样例输出他们到达土拨鼠刷新点的最小时间总和。
保证每组样例都存在一个土拨鼠刷新点,他们两都能到达

Sample Input

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

思路 : 遍历全图寻找能走到的点需要的时间;

逐个找的话会超时;

下面是TLE代码

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#define ll long long
using namespace std;
char a[205][205];
int book[205][205];
struct Node
{
	int x,y,s;
	Node() {}
	Node(int xx,int yy,int ss) : x(xx) , y(yy) ,s(ss){}
};
int n,m;
int yx,yy,mx,my,ex,ey;
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
int bfs(int x,int y,int s)
{
	memset(book,0,sizeof(book));
	queue<Node> Q;
	Q.push(Node(x,y,s));
	book[x][y] = 1;
	while(!Q.empty())
	{
		Node u = Q.front() ;
		Q.pop() ;
		if(u.x == ex && u.y == ey)
		{
			return u.s;
		}
		for(int i=0 ; i<4 ; i++)
		{
			int xx = u.x + dx[i] ;
			int yy = u.y + dy[i] ;
			int ss = u.s + 1 ;
			if(xx>=0 && yy>=0 && xx<n && yy<m && a[xx][yy] != '#' && book[xx][yy] == 0)
			{
				book[xx][yy] = 1 ;
				Q.push(Node(xx,yy,ss)) ;
			}
		}
		
	}
	return -1 ;
} 
int main()
{
	while(cin>>n>>m)
	{
		for(int i=0 ; i<n ; i++)
		{
			for(int j=0 ; j<m ; j++)
			{
				cin>>a[i][j] ;
				if(a[i][j] == 'Y')
				{
					yx = i ;
					yy = j ;
				}
				if(a[i][j] == 'M')
				{
					mx = i ;
					my = j ;
				}
			}
		}
		int ans1,ans2;
		int ans = 99999 ;
		for(int i=0 ; i<n ; i++)
		{
			for(int j=0 ; j<m ; j++)
			{
				if(a[i][j] == '@')
				{
					ex = i ;
					ey = j ;
				}
				ans1 = bfs(yx,yy,0) ;
				ans2 = bfs(mx,my,0) ;
				int data = ans1 + ans2 ;
				ans = min(ans,data) ;
			}
		}
		cout<<ans*11<<endl;
	
	}

	return 0;
}

经过zhy  和  zcr 学长的思路提供 我写出来了不TLE的代码

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#define ll long long
using namespace std;
char a[205][205];
int book[205][205];
int book1[205][205];
struct Node
{
	int x,y,s;
	Node() {}
	Node(int xx,int yy,int ss) : x(xx) , y(yy) ,s(ss){}
};
int n,m;
int yx,yy,mx,my,ex,ey;
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
int bfs(int x,int y,int s)
{
	memset(book,0,sizeof(book));
	queue<Node> Q;
	Q.push(Node(x,y,s));
	book[x][y] = -1;
	while(!Q.empty())
	{
		Node u = Q.front() ;
		Q.pop() ;
		for(int i=0 ; i<4 ; i++)
		{
			int xx = u.x + dx[i] ;
			int yy = u.y + dy[i] ;
			int ss = u.s + 1 ;
			if(xx>=0 && yy>=0 && xx<n && yy<m && a[xx][yy] != '#' && book[xx][yy] == 0)
			{
				book[xx][yy] = ss ;
				Q.push(Node(xx,yy,ss)) ;
			}
		}
		
	}
	return -1 ;
} 
int bfs1(int x,int y,int s)
{
	memset(book1,0,sizeof(book1));
	queue<Node> Q;
	Q.push(Node(x,y,s));
	book1[x][y] = -1;
	while(!Q.empty())
	{
		Node u = Q.front() ;
		Q.pop() ;

		for(int i=0 ; i<4 ; i++)
		{
			int xx = u.x + dx[i] ;
			int yy = u.y + dy[i] ;
			int ss = u.s + 1 ;
			if(xx>=0 && yy>=0 && xx<n && yy<m && a[xx][yy] != '#' && book1[xx][yy] == 0)
			{
				book1[xx][yy] = ss ;
				Q.push(Node(xx,yy,ss)) ;
			}
		}
		
	}
	return -1 ;
} 
int main()
{
	while(cin>>n>>m)
	{
		for(int i=0 ; i<n ; i++)
		{
			for(int j=0 ; j<m ; j++)
			{
				cin>>a[i][j] ;
				if(a[i][j] == 'Y')
				{
					yx = i ;
					yy = j ;
				}
				if(a[i][j] == 'M')
				{
					mx = i ;
					my = j ;
				}
			}
		}
		bfs(yx,yy,0);
		bfs1(mx,my,0);
		int ans1,ans2;
		int ans = 99999 ;
		for(int i=0 ; i<n ; i++)
		{
			for(int j=0 ; j<m ; j++)
			{
				if(a[i][j] == '@' && book[i][j] != 0 && book1[i][j] != 0)
				{
					int data = book[i][j] + book1[i][j] ;
					
					ans = min(ans,data) ;
				}

			}
		}
		cout<<ans*11<<endl;

/*		for(int i=0 ; i<n ; i++)
		{
			for(int j=0 ; j<m ; j++)
			{
				cout<<book[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;
		for(int i=0 ; i<n ; i++)
		{
			for(int j=0 ; j<m ; j++)
			{
				cout<<book1[i][j]<<" ";
			}
			cout<<endl;
		}
	*/
	}

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值