数细胞(0964)

一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。编程需要用到的队列及其相关函数已经实现,你只需要完成count函数以及主函数即可。

第一行输入两个整数,分别代表矩阵的行和列 输入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<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int m, n;
int go[][] = { { -, }, { , }, { , }, { , - } };//定义方向数组:上下左右
int map[][];
void dfs(int x, int y)//遍历
{
map[x][y] = ;//之前把这里写成了map[x][y]='0';真是傻了,调了半天才调出来
for (int i = ; i <= ; i++)//上下左右
{//之前把这里写成了八个方向,所以怎么交都不对 int gx = x + go[i-][];//左右
int gy = y + go[i-][];//上下
if (gx >= && gx < m&&gy >= && gy < n&&map[gx][gy] != )//在范围内,并且当前为细胞
{
dfs(gx, gy);//遍历某个区域
}
}
}
int main()
{ int i, j, k=;
cin >> m >> n;
{
for (i = ; i < m; i++)
{
for (j = ; j < n; j++)
{
cin >> map[i][j];//输入地图
}
}
for (i = ; i < m; i++)
{
for (j = ; j < n; j++)
if (map[i][j] != )//初始
{
k++;
dfs(i, j);//遍历
}
}
cout << k ;
}
return ;
}

这是第二种不同的写法,稍微麻烦了点:

#include<iostream>
using namespace std;
#define max 100
int m, n, str[max][max];
void Input()
{
int i, j;
cin >> m >> n;
for (i = ; i<m; i++)
{
for (j = ; j<n; j++)
{
cin >> str[i][j];
}
}
}
bool exist(int x, int y)
{
if (x >= && x<m&&y >= && y<n)
return true;
else
return false;
} void DFS(int x, int y)
{
int tx, ty, i;
str[x][y] = ;
for (i = ; i<; i++)
{
if (i == )
{
tx = x - ;
ty = y;
if (exist(tx, ty))
{
if (str[tx][ty] != )
{
DFS(tx, ty);
}
}
}
else
if (i == )
{
tx = x + ;
ty = y;
if (exist(tx, ty))
{
if (str[tx][ty] != )
{
DFS(tx, ty);
}
}
}
else
if (i == )
{
tx = x;
ty = y + ;
if (exist(tx, ty))
{
if (str[tx][ty] != )
{
DFS(tx, ty);
}
}
}
else
if (i == )
{
tx = x;
ty = y - ;
if (exist(tx, ty))
{
if (str[tx][ty] != )
{
DFS(tx, ty);
}
}
}
}
}
int main()
{
int i, j, count = ;
Input();
for (i = ; i<m; i++)
{
for (j = ; j<n; j++)
{
if (str[i][j] != )
{
count++;
DFS(i, j);
}
}
}
cout << count;
return ;
}

DFS

#include<iostream>
using namespace std;
const int maxnum = + ;
int map[maxnum][maxnum];
int visit[maxnum][maxnum];//判断是否访问过
int dis[][] = { { -, }, { , }, { , }, { , - } };//方向数组
int M, N;
void DFS(int x,int y)
{
int i;
int dx, dy;
for (i = ; i < ; i++)
{
dx = x + dis[i][];
dy = y + dis[i][];
if (dx < M&&dx >= && dy < N&&dy >= && visit[dx][dy]== )//判断是否越界 并且没访问过
{
visit[dx][dy] = ;//节点已经访问
DFS(dx,dy);//继续遍历
}
} }
int main()
{
int i,j;
int cnt = ;
memset(map, , sizeof(map));
memset(visit, , sizeof(visit));
cin >> M >> N;
for (i = ; i < M;i++)
for (j = ; j < N; j++)
{
cin >> map[i][j];
}
for (i = ; i < M; i++)
{
for (j = ; j < N; j++)
{
if (map[i][j]!=&& (visit[i][j]==))
{
visit[i][j] = ;
DFS(i, j);
cnt++;
}
}
}
cout << cnt << endl; return ;
}

数细胞-swust oj的更多相关文章

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

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

  2. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  3. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  4. [Swust OJ 797]--Palindromic Squares(回文数水题)

    题目链接:http://acm.swust.edu.cn/problem/797/ Time limit(ms): 1000 Memory limit(kb): 10000   Description ...

  5. [Swust OJ 610]--吉祥数

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

  6. [Swust OJ 137]--波浪数(hash+波浪数构造)

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

  7. [Swust OJ 566]--开N方数(牛顿切线法解高次方程)

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

  8. [Swust OJ 403]--集合删数

    题目链接:http://acm.swust.edu.cn/problem/403/ Time limit(ms): 5000 Memory limit(kb): 65535   Description ...

  9. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

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

随机推荐

  1. PHP中foreach()用法汇总

    这篇文章主要给大家详细介绍了PHP中foreach()用法以及相关的示例,十分的细致,有需要的小伙伴可以参考下. PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像.这只是一种遍 ...

  2. eclipse+fileSyncPlugin+svn+jenkins+tomcat

    实现一个想法,把项目部署在linux服务器上,在本地的eclipse代码更新后,通过svn+jenkins自动同步到服务器, 然后通过eclipse远程debug项目.这样是不是就解决了在本地跑项目, ...

  3. Tomcat 笔记-设置虚拟主机

    通过作用虚拟主机,可以使多个不同域名的网站共存于一个Tomcat中 在tomcat的server.xml文件中添加主机名: <Host name="hostname" app ...

  4. pandas.DataFrame学习系列1——定义及属性

    定义: DataFrame是二维的.大小可变的.成分混合的.具有标签化坐标轴(行和列)的表数据结构.基于行和列标签进行计算.可以被看作是为序列对象(Series)提供的类似字典的一个容器,是panda ...

  5. IE10和IE11中滑动条遮挡页面问题

    今天在开发的过程中前端项目,在小设备上会出现滑动条,这本没什么,在其他浏览器上都很正常,但是在IE10和IE11上出现了问题,发现侧边滑动条挡住了一部分页面的内容,因为侧边有要操作的按钮,这就是一个很 ...

  6. PyQt5安装目录中找不到designer.exe与pyrcc5.exe

    我安装的是PyQt5的5.9版本,在安装目录下找不到designer.exe文件.在摸索一段后发现5.9版本对库文件和相关的开发工具是分开发布的.QtDesigner是在pyqt5-tools的包里. ...

  7. dp百题大过关(第一场)

    好吧,这名字真是让我想起了某段被某教科书支配的历史.....各种DP题层出不穷,不过终于做完了orz 虽然各种手糊加乱搞,但还是要总结一下. T1 Monkey Banana Problem    这 ...

  8. AngularJS学习篇(二十一)

    AngularJS 动画 AngularJS 提供了动画效果,可以配合 CSS 使用. AngularJS 使用动画需要引入 angular-animate.min.js 库. <!doctyp ...

  9. Python datetime之timedelta

    该函数表示两个时间的间隔 参数可选.默认值都为0:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minut ...

  10. 通讯框架 t-io 学习——websocket 部分源码解析

    前言 前端时间看了看t-io的websocket部分源码,于是抽时间看了看websocket的握手和他的通讯机制.本篇只是简单记录一下websocket握手部分. WebSocket握手 好多人都用过 ...