二维矩阵存储的着色地图方格里,求出总的国家数。
来源:互联网 发布:苹果电池容量检测软件 编辑:程序博客网 时间:2024/05/05 12:40
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int resolution(vector< vector<int> > &A)
{
//self-defined construct
struct Point
{
int line;
int col;
Point(int X, int Y) :
line(X), col(Y)
{
}
~Point()
{
}
};
//allocate bitArray for Nodes' visited 0-1 state
int N = A.size();
int M = A.at(0).size();
char *isChecked = new char[(N * M + 8) / 8];
if (isChecked == NULL)
{
return -1;
}
//initialize all node with all unvisited state.
for (int i = 0; i < (N * M + 8) / 8; ++i)
{
isChecked[i] = 0;
}
int currColor = -1;
stack<Point> currS;// diagram's depth-first traversal algorithm
int totalCn = 0;
for (int ii = 0; ii < N; ++ii)
{
for (int jj = 0; jj < M; ++jj)
{
if ((isChecked[(ii * M + jj) / 8] & (1 << ((ii * M + jj) % 8))) == 0)
{ // if current Node not yet been visited, set visited.
isChecked[(ii * M + jj) / 8] |= (1 << ((ii * M + jj) % 8));
currS.push(Point(ii, jj));// a new 4-branch tree's rootNode
++totalCn;
currColor = A[ii][jj];
//Start merging all Neighboring Same color Node whose value equals currColor
while (!currS.empty())
{
Point tmp = currS.top();
currS.pop();
int i = tmp.line;
int j = tmp.col;
//current node's right neighbor if exists
if ((j + 1 < M)
&& ((isChecked[(i * M + j + 1) / 8]
& (1 << ((i * M + j + 1) % 8))) == 0)
&& (A[i][j+1] == currColor))
{
isChecked[(i * M + j + 1) / 8] |= (1 << ((i * M + j + 1) % 8));
currS.push(Point(i, j + 1));
}
//current node's down neighbor if exists
if ((i + 1 < N)
&& ((isChecked[((i+1) * M + j) / 8]
& (1 << (((i+1) * M + j) % 8))) == 0)
&& (A[i+1][j] == currColor))
{
isChecked[((i+1) * M + j ) / 8] |= (1 << (((i+1) * M + j ) % 8));
currS.push(Point(i+1, j ));
}
//current node's left neighbor if exists
if ((j - 1 >=0)
&& ((isChecked[(i * M + j - 1) / 8]
& (1 << ((i * M + j - 1) % 8))) == 0)
&& (A[i][j-1] == currColor))
{
isChecked[(i * M + j - 1) / 8] |= (1 << ((i * M + j - 1) % 8));
currS.push(Point(i, j - 1));
}
//current node's upper neighbor if exists
if ((i - 1 >=0 )
&& ((isChecked[((i-1) * M + j ) / 8]
& (1 << (((i-1) * M + j ) % 8))) == 0)
&& (A[i-1][j] == currColor))
{
isChecked[((i-1) * M + j) / 8] |= (1 << (((i-1) * M + j ) % 8));
currS.push(Point(i-1, j));
}
}
}
}
}
delete[] isChecked;
isChecked = NULL;
return totalCn;
}
int main(int argc, char** argv)
{
cout << solution(A);
return 0;
}
- 二维矩阵存储的着色地图方格里,求出总的国家数。
- 二维矩阵存储的世界地图里,每个小方格里着色不同,求所有的国家总数
- 一个二维矩阵存储的世界地图里,每个小方格区域被染上不同的颜色,求此图中的国家总数(上下左右不可斜对角)。
- 寻找二维矩阵里的一个数
- 二维矩阵里的路径问题
- CSU_1508_地图的四着色
- csu_1508_地图的四着色
- 求二维字符串数组里的元数个数
- 使用limit查询的同时取得总的记录数:
- 修改栈,使得能O(1)的求出栈里最大的数和最小的数
- 二维地图的制作
- 二维矩阵的乘法
- X*Y的矩阵,求出路径总和
- 求出excel里列的表示方法
- CSU 1508 地图的四着色
- 地图的四着色 中南1508
- 数据结构-二维数组-特殊矩阵压缩存储
- 数据结构-二维数组-三角矩阵压缩存储
- Android面试——APP性能优化
- NYOJ 248 BUYING FEED (贪心)
- 归档
- 网络攻防平台第二关
- HBase Region 的分裂
- 二维矩阵存储的着色地图方格里,求出总的国家数。
- [kuangbin带你飞]专题七 线段树 ABCDE 题解,持续更新
- 蛇形填数
- Deep Learning(深度学习)学习笔记整理系列之(八)
- bd之星资格赛Problem C(字典树(带删除))
- POJ 1204 AC自动机
- 项目上线(一,加密)
- 汇编学习(一)8086汇编拾遗(1)
- 网络攻防平台第三关