华为机试(污水污染)

给一个二维数组,污染源为1,干净水源为0,每隔一天污染源向相邻的上下左右扩散一格,求多少天全部污染?

example: 1 0

0 0

输出 : 2天

代码:

#include"stdafx.h"

#include<stdio.h>

#include<malloc.h>

//污水行

const int row=4;

//污水列

const int col=4;

//污水面积

const int ALL=row*col;

int countALL=0;

//需要多少天

int coutDay=0;

int** A=(int**)malloc(sizeof(int*)*row);

void printfA(int **A)

{

for(int i=0;i<row;i++)

{

for(int j=0;j<col;j++)

{

printf("%d",A[i][j]);

}

printf("\n");

}

}

//初始化一个二维数组全为0

void initA(int **A)

{

for(int i=0;i<row;i++)

{

A[i]=(int*)malloc(sizeof(int)*col);

for(int j=0;j<col;j++)

{

A[i][j]=0;

}

}

}

//拷贝一个二维数组

void copy(int **A,int**B)

{

for(int i=0;i<row;i++)

{

for(int j=0;j<col;j++)

{

A[i][j]=B[i][j];

}

}

}

//是否全部污染了

bool isALLSuit(int**A)

{

for(int i=0;i<row;i++)

{

for(int j=0;j<col;j++)

{

if(A[i][j]!=1)

{

return false;

}

}

}

return true;

}

//污染

void setValue(int** today,int **tommorow)

{

for(int i=0;i<row;i++)

{

for(int j=0;j<col;j++)

{

if(today[i][j]==1)

{

//上

if(i-1>0)

{

tommorow[i-1][j]=1;

}

//右

if(j-1>0)

{

tommorow[i][j-1]=1;

}

//左

if(i+1<row)

{

tommorow[i+1][j]=1;

}

//下

if(j+1<col)

{

tommorow[i][j+1]=1;

}

}

}

}

}

int calculate(int** A)

{

countALL=0;

for(int i=0;i<row;i++)

{

for(int j=0;j<col;j++)

{

if(A[i][j]==0)

{

countALL++;

}

}

}

if(countALL==ALL)

{

return -1;

}

if(isALLSuit(A))

{

return coutDay;

}

else

{

//set value

int** tommorow=(int**)malloc(sizeof(int*)*row);

initA(tommorow);

copy(tommorow,A);

printfA(tommorow);

setValue(A,tommorow);

coutDay++;

return calculate(tommorow);

}

}

int main()

{

initA(A);

//设置污染源

A[0][0]=1;

return calculate(A);

}

#华为机试题#
全部评论
这是100分的题目吧
点赞 回复 分享
发布于 2023-06-13 21:08 北京

相关推荐

07-08 13:48
门头沟学院 C++
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:16
点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
Twilight_m...:表格简历有点难绷。说说个人看法: 1.个人基本情况里好多无意义信息,什么婚姻状况、健康状况、兴趣爱好、户口所在地、身份证号码、邮政编码,不知道的以为你填什么申请表呢。 2.校内实践个人认为对找工作几乎没帮助,建议换成和测开有关的项目,实在没得写留着也行。 3.工作经历完全看不出来是干什么的,起码看着和计算机没啥关系,建议加强描述,写点你在工作期间的实际产出、解决了什么问题。 4.个人简述大而空,看着像AI生成,感觉问题最大。“Python,C,C++成为我打造高效稳定服务的得力工具”、“我渴望凭借自身技术知识与创新能力,推动人工智能技术的应用发展,助力社会实现智能化转型”有种小学作文的美感。而且你确定你个人简述里写的你都会嘛?你AI这块写的什么“深入研究”,发几篇顶会的硕博生都不一定敢这么写。而且你AI这块的能力和软测也完全无关啊。个人简述建议写你对哪些技术栈、哪些语言、哪些生产工具的掌握,写的有条理些,而且最好是和测开强相关的。
点赞 评论 收藏
分享
07-09 18:28
门头沟学院 Java
写着提前批,结果还要实习4个月以上???
程序员牛肉:这种不用看,直接投了,面试的时候问对应的HR就行。有可能他们是直接复制的暑期实习的模板。
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务