数细胞(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. MySQL基础函数

    MySQL数据库提供了很多函数包括: 数学函数: 字符串函数: 日期和时间函数: 条件判断函数: 系统信息函数: 加密函数: 格式化函数: 一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ...

  2. memset和fill_n区别

    1. 函数名: memset 所属头文件:<string.h> 用法:void *memset(void *s, char ch, unsigned n); 对于对int之类的数组,只能用 ...

  3. Yii2之行为

    Yii三大特性:属性.事件.行为.前面两篇文章已经分别讲解了属性和事件,本文接着讲讲yii的行为,分析yii行为的实现原理. 在yii中,一个对象绑定了行为之后,就拥有了所绑定行为拥有的所有事件,而且 ...

  4. LeetCode 533. Lonely Pixel II (孤独的像素之二) $

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  5. idea中的汉语注释出现乱码的解决方案

    日记 - idea中的汉语注释出现乱码的解决方案 我是个idea的忠实用户,新公司的项目都是用eclipse做的,通过svn拉下代码后发现,注释的内容里,中文内容都是乱码.问过项目负责人,说可能是GB ...

  6. Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能

    1 环境搭建 1.1 Python安装 1.2 MySQL环境搭建 1.3安装MySQLdb  2 具体实现 2.1 登陆界面 2.2 注册界面 2.3 具体实现部分代码   1 环境搭建 1.1 P ...

  7. swift 之SnapKit 动画

    这个问题纠结了我挺长时间的.一直以为把约束直接添加到动画里面就可以了.但是并没那么简单.-.-其实还是挺简 class ViewController: UIViewController { @IBOu ...

  8. c++连接数据库 在vc6.0

    配置相关环境 我的mysql安装路径为E:\mysql-5.5.28-win32所以要在VC中设置include路径和lib的路径. 添加MySql的include目录到VC工作台中Project-& ...

  9. 《mysql必知必会》读书笔记--安全管理及数据库维护

    安全管理 mysql自带的mysql数据库中的user表可查看用户所有资料 创建用户帐号 CREATE USER ben IDENTIFIED BY 'p@$$wOrd' 重命名用户帐号 RENAME ...

  10. C++ UI资源

    最近又来搞界面了,现把这几天收集到的资料汇总下,方便今后慢慢学习! Duilib: Duilib是一个Windows下免费开源的DirectUI界面库,由于简约易扩展的设计以及稳定高效的实现被各大互联 ...