数细胞(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. Android基础知识04—Activity活动之间传递数据

    ------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...

  2. (七)php运算符

    一:算数运算符 +(加).-(减).*(成)./(除) %(取模,求余的意思) <?php $a=7/3; echo $a; //2.3333333333333.因为float类型的最大精度为1 ...

  3. asp中日志方法

    代码文件log.asp中内容如下: <% Function getPath() getPath = request.servervariables("APPL_PHYSICAL_PAT ...

  4. Java基础总结--变量、运算符总结

    ---变量基本概述----变量作用:就是用来存储数据变量的声明:变量类型 变量名;变量类型---区分不同类型的数据,变量名--按名访问变量变量名(规则):见名识意,第一个字母小写后面驼峰规则变量的作用 ...

  5. jstl 处理字符串

    1.引入 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> ...

  6. MongoDB高级索引

    1. 索引数组字段 创建和使用跟普通字段的索引一样. 2. 索引子文档字段: 创建索引: db.users.ensureIndex({"address.city":1," ...

  7. 简易RPC框架-上下文

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. java对象类型转换和多态性

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  9. JDBC的基本用法

    一.编程步骤 1.加载驱动 Class forName("com.mysql.jdbc.Driver"):mysql驱动 Class forName("oralce.jd ...

  10. python实现进度条和百分比同时显示

    python中同时打印进度条和百分比 仅打印进度条: import sys,time for i in range(100): sys.stdout.write('>') sys.stdout. ...