数细胞-swust oj
数细胞(0964)
一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。编程需要用到的队列及其相关函数已经实现,你只需要完成count函数以及主函数即可。
第一行输入两个整数,分别代表矩阵的行和列 输入m*n的矩阵,由数字0到9组成。
细胞个数。
#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的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- [Swust OJ 797]--Palindromic Squares(回文数水题)
题目链接:http://acm.swust.edu.cn/problem/797/ Time limit(ms): 1000 Memory limit(kb): 10000 Description ...
- [Swust OJ 610]--吉祥数
题目链接:http://acm.swust.edu.cn/problem/610/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- [Swust OJ 137]--波浪数(hash+波浪数构造)
题目链接:http://acm.swust.edu.cn/problem/137/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- [Swust OJ 566]--开N方数(牛顿切线法解高次方程)
题目链接:http://acm.swust.edu.cn/problem/0566/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 403]--集合删数
题目链接:http://acm.swust.edu.cn/problem/403/ Time limit(ms): 5000 Memory limit(kb): 65535 Description ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
随机推荐
- FreeType in OpenCASCADE
FreeType in OpenCASCADE eryar@163.com Abstract. FreeType is required for text display in the 3D view ...
- 脚本div实现拖放功能
脚本div实现拖放功能 网页上有很多拖曳的操作,比如拖动树状列表,可拖曳的图片等. 1.原生拖放实现 <!doctype html> <html lang="en" ...
- PHP的Session机制
客户端浏览器和服务器之间通信使用的http协议是一种无状态的协议,在它看来,客户端发起的每个请求都是独立.没有关联的.然而,在实际的Web应用开发中,服务器却经常需要根据用户以往的一些状态或数据对请求 ...
- left join 条件位置问题
表一:
- [译]ASP.NET Core 2.0 路由引擎之网址生成
问题 如何在ASP.NET Core 2.0中由路由引擎来生成网址? 答案 新建一个空项目,修改Startup.cs文件,添加MVC服务和中间件: public void ConfigureServi ...
- 前端菜鸟学习之DOM事件处理
一.事件处理程序 1.DOM0级事件处理程序:就是将一个函数赋值给一个事件处理程序属性,至今仍为现代所有浏览器所支持,主要得益于其跨浏览器的优势,要使用DOM0级事件 首先要得到操作对象的引用,具体实 ...
- 采访 Lua 发明人的一篇文章
采访 Lua 发明人的一篇文章 来源 https://blog.codingnow.com/2010/06/masterminds_of_programming_7_lua.html <Mast ...
- HDU 5791 Two(训练题002 F)
Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and ...
- c++学习笔记---01---C++语言与OO思想介绍
C++语言与OO思想介绍 C++的特点与OO思想 C语言有一个优点,即它的速度可以很快.写出来的程序可以很精练.简单.小巧,不用为了解决某个问题环绕太平洋一大圈. 但如果将C和C++相比较,C++就经 ...
- AngularJS学习篇(二十三)
AngularJS 路由 AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web应用(single page web applica ...