【N - Find a way】

80 篇文章 0 订阅
80 篇文章 0 订阅

思路:

  • 正解:2次BFS
  • 我的思路:
    1. 第一次我从每个K开始,找每个K的到达两个人的时间,TLE。
    2. char mp[][] 换成 bool mp[][],TLE。
    3. 正解:从每个人开始BFS,注意KFC的存储。

代码:

  • 第一版:TLE
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 205;
const int mt[4][2] = {1,0 , -1,0 , 0,1 , 0,-1};

struct node{
	int x,y;
	int step;
	node(int x,int y,int step) : x(x) , y(y) , step(step) {} ;
};
char mp[maxn][maxn] ;
bool vis[maxn][maxn];
int ans=INF;
queue<node> Q;
int M,N;

int BFS(int m,int n,char tar){
	memset(vis,false,sizeof(vis));
	while(Q.size())
		Q.pop();
	int t=0;
	Q.push(node(m,n,0));vis[m][n] = true;
	while(Q.size()){
		node cur = Q.front();Q.pop();
		int x= cur.x;
		int y= cur.y;
		if(mp[x][y] == tar)
			return cur.step;
		for(int i=0;i<4;i++){
			int nx = x+mt[i][0];
			int ny = y+mt[i][1];
			if(vis[nx][ny] || mp[nx][ny] == '#')
				continue;
			if(nx < 1 || nx > M || ny < 1 || ny > N)
				continue;
			Q.push(node(nx,ny,cur.step+1));vis[nx][ny] = true;
		}
	}
	return INF;
}


int main(){
	while(cin>>M>>N){
		for(int i=1;i<=M;i++)
			for(int j=1;j<=N;j++){
				cin>>mp[i][j];
			}
		for(int i=1;i<=M;i++)
			for(int j=1;j<=N;j++)
				if(mp[i][j] == '@'){
					int t1 = BFS(i,j,'Y');
					int t2 = BFS(i,j,'M');
					ans = min(ans , t1 + t2);
				}
		ans*=11;
		cout<<ans<<endl;
	}
	return 0;
}
  • 第二版:TLE
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 205;
const int mt[4][2] = {1,0 , -1,0 , 0,1 , 0,-1};

struct node{
	int x,y;
	int step;
	node(int x,int y,int step) : x(x) , y(y) , step(step) {} ;
};
bool mp [maxn][maxn];
int Yx,Yy,Mx,My;
int Kx[maxn*maxn];
int Ky[maxn*maxn];
int cnt;
bool vis[maxn][maxn];
int ans;
queue<node> Q;
int M,N;

int BFS(int m,int n){
	int t1=INF,t2=INF;
	memset(vis,false,sizeof(vis));
	while(Q.size())
		Q.pop();
	Q.push(node(m,n,0));vis[m][n] = true;
	while(Q.size()){
		node cur = Q.front();Q.pop();
		int x= cur.x;
		int y= cur.y;
		if(x == Yx && y == Yy)
			t1 = cur.step ;
		if(x == Mx && y == My)
			t2 = cur.step ; 
		for(int i=0;i<4;i++){
			int nx = x+mt[i][0];
			int ny = y+mt[i][1];
			if(vis[nx][ny] || !mp[nx][ny])
				continue;
			if(nx < 1 || nx > M || ny < 1 || ny > N)
				continue;
			Q.push(node(nx,ny,cur.step+1));vis[nx][ny] = true;
		}
	}
	return t1 + t2;
}


int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	while(cin>>M>>N){
		ans = INF;
		cnt=0;
		memset(mp , false , sizeof(mp));
		for(int i=1;i<=M;i++)
			for(int j=1;j<=N;j++){
				char t;
				cin>>t;
				if(t != '#'){
					mp[i][j] = true;
					if(t == 'Y')
						Yx = i , Yy = j;
					if(t == 'M')
						Mx = i , My = j;
					if(t == '@'){
						cnt++;
						Kx[cnt] = i , Ky[cnt] = j;
					}
				}
			}
		for(int i=1;i<=cnt;i++){
			int t = BFS(Kx[i] , Ky[i]) ;
			ans = min(ans , t) ;
		}
		ans*=11;
		cout<<ans<<endl;
	}
	return 0;
}
  • 正解:62ms 1884kB
//62ms		1884kB


#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 205;
const int mt[4][2] = {1,0 , -1,0 , 0,1 , 0,-1} ;

struct node{
	int x,y;
	int s;//step
	node(int x,int y,int s) : x(x) , y(y) , s(s) {} ;
};
queue<node> Q;
int M,N;
bool mp [maxn][maxn];
bool K  [maxn][maxn];
bool vis[maxn][maxn];
int  Kt [2][maxn][maxn];
int Yx , Yy;
int Mx , My;
int ans;

void BFS(int m,int n,int id){
	while(Q.size())
		Q.pop();
	memset(vis,false,sizeof(vis));
	Q.push(node(m,n,0));
	vis[m][n] = true;
	while(Q.size()){
		node cur = Q.front() ; Q.pop();
		int x = cur.x , y = cur.y ;
		if(K[x][y])
			Kt[id][x][y] = cur.s;
		for(int i=0;i<4;i++){
			int nx = x + mt[i][0];
			int ny = y + mt[i][1];
			if(vis[nx][ny] || !mp[nx][ny])
				continue;
			if(nx < 1 || ny < 1 || nx > M || ny > N)
				continue;
			Q.push(node(nx , ny , cur.s + 1));vis[nx][ny] = true;
		}
	}
	return ;
}

int main(){
	while(cin>>M>>N){
		memset(K , false , sizeof(K));
		memset(Kt , INF , sizeof(Kt));
		ans = INF;
		memset(mp,false,sizeof(mp));
		for(int i=1;i<=M;i++){
			for(int j=1;j<=N;j++){
				char t;
				cin>>t;
				if(t != '#')//KFC也可以途经 
					mp[i][j] = true;
				if(t == '@')
					K [i][j] = true;
				if(t == 'Y')
					Yx = i , Yy = j;
				if(t == 'M')
					Mx = i , My = j;
			}
		}
		BFS(Yx , Yy , 0);
		BFS(Mx , My , 1);
		for(int i=1;i<=M;i++){
			for(int j=1;j<=N;j++){
				if(K[i][j]){
					ans = min(ans , Kt[0][i][j] + Kt[1][i][j]);
				}
			}
		}
		ans *= 11;
		cout<<ans<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值