本题解题方法完全按照老师ppt上整的,不是自己写的。
题目描述
一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。编程需要用到的队列及其相关函数已经实现,你只需要完成count函数以及主函数即可。

#include
#include
using namespace std;
#define max 50000

struct pos {
int x;
int y;
};

typedef struct se
{
pos data[max];
int front,rear;
} Sequeue;

void init(Sequeue *&Q)
{
Q=(Sequeue *)malloc(sizeof(Sequeue));
Q->front=-1;
Q->rear=-1;
}

int empty(Sequeue *Q)
{
if(Q->rear==Q->front)
return 1;
else
return 0;
}

void insertsequeue(Sequeue *&Q,int i,int j)
{
if((Q->rear+1)%max==Q->front)
{
cout<<“队列已满!”<<endl;
exit(0);
}
Q->rear=(Q->rear+1)%max;
Q->data[Q->rear].x=i;
Q->data[Q->rear].y=j;
}

void delsequeue(Sequeue *&Q,int &i,int &j)
{
if(Q->rear==Q->front)
{
cout<<“队列已空!”<<endl;
exit(0);
}
Q->front=(Q->front+1)%max;
i=Q->data[Q->front].x;
j=Q->data[Q->front].y;
}
int count(int a[50][50],int m,int n)
{
int x=0;
return x; //x为计算出在细胞在个数

}

void main()
{
int test[50][50];
int i,j, m,n;
cin>>m;
cin>>n;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
cin>>test[i][j];

cout<<count(test,m,n)<<endl;

}
输入
第一行输入两个整数,分别代表矩阵的行和列 输入m*n的矩阵,由数字0到9组成。

输出
细胞个数。

样例输入
4 10
1 2 3 4 5 1 1 1 6 7
1 0 3 4 5 6 1 5 1 0
2 0 4 5 6 6 1 6 7 1
0 0 6 0 6 6 1 0 8 9
样例输出
1

#include<iostream>
#include<cstdlib>
using namespace std;
#define max 50000struct pos {int x;int y;
};//某个数值的坐标 typedef struct se
{pos data[max];int front,rear;
} Sequeue;//队列 void init(Sequeue *&Q)
{Q=(Sequeue *)malloc(sizeof(Sequeue));Q->front=-1;Q->rear=-1;
}//初始化队列 int empty(Sequeue *Q)
{if(Q->rear==Q->front)return 1;elsereturn 0;
}//为空判断 void insertsequeue(Sequeue *&Q,int i,int j)
{if((Q->rear+1)%max==Q->front){cout<<"队列已满!"<<endl;exit(0);}Q->rear=(Q->rear+1)%max;Q->data[Q->rear].x=i;Q->data[Q->rear].y=j;
}//插入判断 void delsequeue(Sequeue *&Q,int &i,int &j)
{if(Q->rear==Q->front){cout<<"队列已空!"<<endl;exit(0);}Q->front=(Q->front+1)%max;i=Q->data[Q->front].x;j=Q->data[Q->front].y;
}void change(int a[50][50],int m,int n)
{int i,j;for(i=0;i<m;i++)for(j=0;j<n;j++)if(a[i][j]!=0)a[i][j]=1;      //量化,把非零数字全部转换成1
}int count(int a[50][50],int m,int n)
{int counter=0;//计数变量 int c,d;//行数列数 Sequeue *q;init(q);int i,j;for(i=0;i<m;i++)for(j=0;j<n;j++)if(a[i][j]==1){insertsequeue(q,i,j);         //自身入队列if((i-1>=0&&a[i-1][j]!=0))insertsequeue(q,i-1,j);       //  上                   if((j+1<=n&&a[i][j+1]==1))insertsequeue(q,i,j+1);       //右                if((i+1<=m&&a[i+1][j]==1))insertsequeue(q,i+1,j);       //下if((j-1>=0&&a[i][j-1]))insertsequeue(q,i,j-1);       //左a[i][j]=0;while(!empty(q)){delsequeue(q,c,d);                  //自身出队列 if((c-1>=0&&a[c-1][d]==1))insertsequeue(q,c-1,d);       //  上                 if((d+1>=0&&a[c][d+1]==1))insertsequeue(q,c,d+1);       //右                if((i+1>=0&&a[c+1][d]==1))insertsequeue(q,c+1,d);       //下if((d-1>=0&&a[c][d-1]==1))insertsequeue(q,c,d-1);       //左a[c][d]=0;}counter++;                  }        return counter;}int main()
{int test[50][50];int i,j, m,n;cin>>m;cin>>n;for(i=0; i<m; i++)for(j=0; j<n; j++)cin>>test[i][j];change(test,m,n); //int c =count(test,m,n);cout<<count(test,m,n);return 0;
}

递归方法来自一位学长

#include<iostream>
#include<stdio.h>
using namespace std;
int date[100][100],M=0;
int m,n;
void stt(int x,int y)
{if(date[x][y]!=0){date[x][y]=0;if(x+1<m)stt(x+1,y);if(y+1<n)stt(x,y+1);if(x-1>=0)stt(x-1,y);if(y-1>=0)stt(x,y-1);}}int main()
{int i,j;cin>>m>>n;for(i=0;i<m;i++)for(j=0;j<n;j++)cin>>date[i][j];for(i=0;i<m;i++)for(j=0;j<n;j++)if(date[i][j]!=0)              {M++;stt(i,j);}cout<<M;return 0;
}

SWUST.OJ 964: 数细胞相关推荐

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Descr ...

  2. swust oj#160促销计算

    SWUST OJ#160 题目描述 某百货公司为了促销,采用购物打折的优惠方法,每位顾客一次购物:在1000元以上者,按9.5折优惠:在2000以上者,按9折优惠:在3000以上者,按8.5折优惠:在 ...

  3. swust oj代码+解析_1165,0284,0074,0042,1171,0026,0189,0078,0046,0077,0209,0129

    swust oj 1165,0284(int a[n]\数字根),0074,0042,1171(矩阵相乘 输出对齐),0026/0189,0078(计算生日是星期几),0046,0077(计算员工周工 ...

  4. fafu oj 1266 数数

    http://acm.fafu.edu.cn/problem.php?id=1266 fafu oj 1266 数数 //fafu oj 1266 数数//二分,具体看代码 #include < ...

  5. SWUST OJ 954单链表的链接

    swust oj 954 题目描述 建立长度为n的单链表A和长度为m的单链表B.编程实现将B表链接在A表的尾端,形成一个单链表A.数据类型指定为字符型. 输入 输出 样例输入 样例输出 源代码 #in ...

  6. SWUST OJ 1168 喝可乐

    swust oj 1168 题目描述 小明十分喜欢喝可乐,有一次店家搞促销,用三个可乐瓶盖便可换一瓶新可乐.现在告诉你小明身上的钱和 每瓶可乐的单价,问你小明最多可以喝多少瓶可乐?(不能向老板借瓶盖) ...

  7. SWUST OJ#281逃跑的蠕虫

    swust oj 281 题目描述 装在瓶子(瓶子高度为h)的蠕虫都想从瓶子底部向瓶口处爬出去.它每分钟向上爬行u厘米,之后会休息一分钟,这一分钟它会向下滑行d厘米,当蠕虫到了瓶口或者超出瓶口后便出了 ...

  8. SWUST OJ#978 #979 #980 二叉树的遍历

    目录 深度优先遍历 输出利用先序遍历创建的二叉树的前序遍历序列 思路 代码 #978 输出利用先序遍历创建的二叉树的中序遍历序列 题目 思路 代码 #979 输出利用先序遍历创建的二叉树的后序遍历序列 ...

  9. SWUST OJ 1159 吃披萨

    swust oj 1159 题目描述 小明楼下新开了两家披萨店,价格都一样,不同的是A家披萨店的披萨是圆形,B家披萨店的披萨是三角形.为了知道 哪家披萨店的披萨面积更大一些,于是就找到你咯,你来帮帮他 ...

最新文章

  1. Struts2 项目搭建
  2. Paramiko: SSH and SFTP With Python
  3. 新课程网上选课系统V1.0—适用于中小学校本课程选课、选修课选课
  4. 11个鲜为人知的实用Linux命令 - Part 2
  5. cesium事件简单全面描述
  6. ubuntu等linux系统给windows共享文件
  7. mysql访问类型最好的_【干货满满】最全的MySQL性能指南(一):选择最佳的数据类型...
  8. npm install Saving to file: /root/.jenkins/workspace/ems-web/node_modules/chromedriver/2
  9. Docker学习总结(26)——Docker 管理工具的选择:Kubernetes 还是 Swarm?
  10. 如何区分USB接口类型
  11. 实时计算之storm
  12. Android最佳实践之流畅设计
  13. docker自动部署脚本
  14. LiveReload拓展配合Webpack实现网页自动刷新
  15. 黑群晖系统备份与恢复
  16. 使用prewitt算子分割白纸黑字图像(Matlab)
  17. layui可以动态添加div吗_js 动态添加元素(div、li、img等)及设置属性的方法
  18. 成都车展:百度车联网的“阅兵”表演
  19. Vue 合同模板_【开源】后端开发也很容易上手的前端框架模板
  20. win8/win10微信QQ邮箱可登陆,浏览器显示无网络连接

热门文章

  1. 船舶强度与结构设计大作业二matlab,船舶强度与结构设计最新版
  2. JAVA1.7 NIO.2 入门,第 2 部分: 文件系统 API
  3. 【泛微系统】OA运维小知识
  4. 2022年计算机软件水平考试系统分析师(高级)练习题及答案
  5. 诺基亚计划接入鸿蒙系统!官方回应,反转来了?
  6. Libgdx游戏编程之Touchpad摇杆控制角色行走
  7. 多标签分类的学习感悟
  8. 1-100中6的倍数有多少个
  9. 事件循环EventLoop机制
  10. 公司即将要倒闭的征兆