救命!!!!!!!

题目描述

暑假即将到来,鱼大大和羊大大计划吃肯德基。他们在计划中在某一天选择了某一个肯德基吃午饭。

到了约定的日子,鱼大大和羊大大都是9点出发,向KFC前进,问他们两能否在中午12点之前到达约定的KFC?如果可以,那么他们最少花费多少分钟就能在KFC相约?

约定:

时间计算:起点到终点的距离为时间,即走1单位的距离,鱼大大和羊大大都要花费1分钟的时间。

‘@’ 是鱼大大的位置,‘#’ 是障碍,‘F’ 表示KFC,‘&’ 表示羊大大的位置。

‘.’,‘F’,‘@’ 和 ‘&’,这些位置都可以通过。

输入格式

第一行包含两个整数 n 和 m。

接下来的 n 行,每行输入 m 个字符来表示地图。(1 ≤ n ≤ m ≤ 200)

输出格式

如果能找到一条可行的路径并在在12点之前到达KFC,输出花费的最少时间,否则输出 “Meeting cancelled”(两人无法在12点之前相约在此KFC)。

样例

Input 1

4 4 @.#. … .#… F…&

Output 1

3

数据范围

见题面

样例过了,但WA 40!!!

代码:

#include <bits/stdc++.h>
using namespace std;
int n, m, yx1, yy1, yx2, yy2, fx, fy, ans, bfs(int, int);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
char mp[205][205];
struct node
{
	int x, y, step;
};
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> mp[i][j];
			if (mp[i][j] == '@')
			{
				yx1 = i;
				yy1 = j;
			}
			else if (mp[i][j] == 'F')
			{
				fx = i;
				fy = j;
			}
			else if (mp[i][j] == '&')
			{
				yx2 = i;
				yy2 = j;
			}
		}
	}
	ans = max(bfs(yx1, yy1), bfs(yx2, yy2));
	if (ans > 180)
	{
		printf("Meeting cancelled");
	}
	else
	{
		printf("%d", ans);
	}
	return 0;
}
int bfs(int sx, int sy)
{
	int vis[205][205];
	queue<node> que;
	vis[sx][sy] = 1;
	que.push({sx, sy, 0});
	while (!que.empty())
	{
		node hd = que.front();
		que.pop();
		for (int i = 0; i < 4; i++)
		{
			int x = hd.x + dx[i], y = hd.y + dy[i], step = hd.step + 1;
			if (x < 1 || x > n || y < 1 || y > m || mp[x][y] == '#' || vis[x][y])
			{
				continue;
			}
			if (x == fx && y == fy)
			{
				return step;
			}
			vis[x][y] = 1;
			que.push({x, y, step});
		}
	}
}
2 个赞

看一下这个
相约KFC TLE0 - 常规 - 信友队论坛 (xinyoudui.com)

2 个赞

错误1:
image
不是<是<=

错误2:
image
代码中无此判断

2 个赞

帮你改出来RE80分
其他自己在看看吧

#include <bits/stdc++.h>
using namespace std;

int n, m;
int yx1, yy1, yx2, yy2, fx, fy;
int ans1, ans2, maxans;
int bfs(int, int);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
char mp[205][205];
bool a = 0;

struct node {
	int x, y, step;
};

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mp[i][j];
			if (mp[i][j] == '@') {
				yx1 = i;
				yy1 = j;
			} else if (mp[i][j] == 'F') {
				fx = i;
				fy = j;
			} else if (mp[i][j] == '&') {
				yx2 = i;
				yy2 = j;
			}
		}
	}

	ans1 = bfs(yx1, yy1);
	a = 0;
	ans2 = bfs(yx2, yy2);
	maxans = max(ans1, ans2);
	if (maxans  >= 180 || a == 0)printf("Meeting cancelled");
	else printf("%d", maxans);

	return 0;
}

int bfs(int sx, int sy) {
	int vis[205][205] = {};
	queue<node> que;
	vis[sx][sy] = 1;
	que.push({sx, sy, 0});
	while (!que.empty()) {
		node hd = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int x = hd.x + dx[i], y = hd.y + dy[i], step = hd.step + 1;
			if (x == fx && y == fy) {
				a = 1;
				return step;
			}
			if (x < 1 || x > n || y < 1 || y > m || mp[x][y] == '#' || vis[x][y] == 1) {
				continue;
			}
			vis[x][y] = 1;
			que.push({x, y, step});
		}
	}
}
2 个赞

@金彦劭 还有什么问题啊,找不出来力(悲)

2 个赞

这是我的代码,应该能过,你自己看一下有哪里和上面那人的不一样

#include<bits/stdc++.h>
using namespace std;

int n,m,o[2]={40005,40005},dx[4]{0,-1,0,1},dy[4]={-1,0,1,0},sum=0;
char s[205][205];
struct node{
	int x,y,step;
};

void bfs(int x,int y,int v){
	int vis[205][205] = {};
	vis[x][y] = 1;
	queue<node> q;
	q.push(node {x,y,0});
	while(!q.empty()){
		node now = q.front();
		q.pop();
		for(int i = 0; i<4; i++){
			int xx=now.x+dx[i],yy=now.y+dy[i];
			if(s[xx][yy] == 'F'){
				sum++;
				o[v] = now.step+1; 
				return;
			}
			if(xx>=1 && xx<=n && yy>=1 && yy<=m && s[xx][yy] != '#' && vis[xx][yy] == 0){
				vis[xx][yy] = 1;
				q.push(node {xx,yy,now.step+1});
			}
		}
	}
}

int main(){
	cin>>n>>m;
	int a,b,c,d;
	for(int i = 1; i<=n; i++){
		for(int j = 1; j<=m; j++){
			cin>>s[i][j];
			if(s[i][j] == '@'){
				a = i;
				b = j;
			}else if(s[i][j] == '&'){
				c = i;
				d = j;
			}
		}
	}
	bfs(a,b,0);
	bfs(c,d,1);
	if(sum>=2 && o[0]<180 && o[1]<180){
		cout<<max(o[1],o[0]);
	}else{
		cout<<"Meeting cancelled";
	}
	return 0;
}
2 个赞