一道算法题,求大佬过目我的代码,看看哪不对。

qq_42412933 2018-10-15 06:24:05
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 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
原题链接:https://vjudge.net/contest/221801#problem/N

求大佬指导一下我的代码哪里不对,我测试的全通过了,但提交就错了。以下我的代码:

#include <stdio.h>
#include <string.h>
int next[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
char map[205][205];
int visit[205][205];
int timeY[205][205];
int timeM[205][205];
int n,m;
int head,tail;
int starta,startb,startc,startd;
struct node{
int x;
int y;
}que[40010];
void bfs(int,int,int);
int result();

int main()
{
int i,j;
while(scanf("%d %d",&n,&m) != EOF){
for(i = 0;i < n;i++){
scanf("%s",map[i]);
for(j = 0;j < m;j++){
if(map[i][j] == 'Y'){
starta = i;
startb = j;
}
if(map[i][j] == 'M'){
startc = i;
startd = j;
}
}
}
memset(timeY,0,sizeof(timeY));
memset(timeM,0,sizeof(timeM));
bfs(starta,startb,0);
bfs(startc,startd,1);
printf("%d\n",result());
}
return 0;
}

void bfs(int startx,int starty,int flag)
{
int i,tx,ty;
tail = 1;
head = 1;
memset(visit,0,sizeof(visit));
que[tail].x = startx;
que[tail].y = starty;
visit[startx][starty] = 1;
if(!flag)
timeY[startx][starty] = 0;
else
timeM[startx][starty] = 0;
tail++;
while(head < tail){
for(i = 0;i < 4;i++){
tx = que[head].x + next[i][0];
ty = que[head].y + next[i][1];
if(tx < 0 || ty < 0 || tx >= n || ty >= m)
continue;
if(map[tx][ty] != '#' && visit[tx][ty] == 0){
visit[tx][ty] = 1;
que[tail].x = tx;
que[tail].y = ty;
if(!flag)
timeY[tx][ty] = timeY[que[head].x][que[head].y] + 11;
else
timeM[tx][ty] = timeM[que[head].x][que[head].y] + 11;
tail++;
}
}
head++;
}
}

int result()
{
int i,j;
int min = 11 * 40000;
for(i = 0;i < n;i++){
for(j = 0;j < m;j++){
if(map[i][j] == '@'){
if(timeY[i][j] + timeM[i][j] < min)
min = timeY[i][j] + timeM[i][j];
}
}
}
return min;
}
...全文
122 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

33,009

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧