华为编程大赛之笨笨熊搬家交通篇答案(供参考。)

/*华为编程大赛之笨笨熊搬家交通篇答案(供参考。错误之处请指出)*/ 


#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define max 10000

void change_map(int n,int x,int y,char * map,char c)
{
if(map[(x-1)*n+y]=='-')
{
map[(x-1)*n+y]='c';
change_map(n,x-1,y,map,c);
}
if(map[(x+1)*n+y]=='-')
{
map[(x+1)*n+y]='c';
change_map(n,x+1,y,map,c);
}
if(map[(x)*n+y+1]=='-')
{
map[(x)*n+y+1]='c';

change_map(n,x,y+1,map,c);
}
if(map[(x)*n+y-1]=='-')
{
map[(x)*n+y-1]='c';
    change_map(n,x,y-1,map,c);
}
}


int main(void)
{
int M=0;
int N=0;
int m=0;
int n=0;
int i=0;
int j=0;

int lenth=0;

int bx=0;
int by=0;
int hx=0;
int hy=0;

char map1[max]={'0'};
char * map;
char * map_B;
char * map_H;
bool change=false;
 
scanf("%d",&M);
getchar();
scanf("%d",&N);
getchar();

for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%c",&map1[i*N+j]);
}
getchar();
}
lenth=strlen(map1);

if((lenth/M)!=N&&(lenth%M!=0))
return -1;
m=M+2;
n=N+2;

map=(char *)malloc(m*n*sizeof(char));
map_B=(char *)malloc(m*n*sizeof(char));
map_H=(char *)malloc(m*n*sizeof(char));

memset(map,'0',m*n);
memset(map_B,'0',m*n);
memset(map_H,'0',m*n);

for(i=1;i<n-1;i++)
{
for(j=1;j<m-1;j++)
{
map[i*n+j]=map1[(i-1)*N+(j-1)];
map_B[i*n+j]=map1[(i-1)*N+(j-1)];
map_H[i*n+j]=map1[(i-1)*N+(j-1)];
}
}

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i*n+j]=='B')
{
bx=i;
by=j;
}
if(map[i*n+j]=='H')
{
hx=i;
hy=j;
}
}
}
change_map(m,bx,by,map_B,'B');
change_map(m,hx,hy,map_H,'H');

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map_B[i*n+j]=='B'&&map_H[i*n+j]=='H')
{
change=true;
}

}
}

if(change)
printf("Y");
else
printf("N");
free(map_B);
free(map);
free(map_H);
return 0;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值