给定一个大小为X * Y * Z的 3D 矩阵 mat[ ][ ][ ] 。任务是将这个矩阵转换成一个全连接的循环链表。每个节点将有 6 个指针,即:左、右、上、下、前、后。
例子:
Input: X = 2, Y = 2, Z = 2
mat[0][0][0] = 10 mat[0][0][1] = 11
mat[0][1][0] = 12 mat[0][1][1] = 13
mat[1][0][0] = 14 mat[1][0][1] = 15
mat[1][1][0] = 16 mat[1][1][1] = 17
Output:
element front back left right up down
10 14 14 11 11 12 12
11 15 15 10 10 13 13
12 16 16 13 13 10 10
13 17 17 12 12 11 11
14 10 10 15 15 16 16
15 11 11 14 14 17 17
16 12 12 17 17 14 14
17 13 13 16 16 15 15
Explanation:
For mat[0][0][0] having value 10:
Its back pointer points to cell mat[1][0][0] having value 14.
It doesn’t have any element in front of it. In order to maintain the cyclic order, the front pointer points to cell mat[1][0][0] having value 12.
Its right pointer points to cell mat[0][0][1] having value 11.
It doesn’t have any element to its left. In order to maintain the cyclic order, the left pointer points to cell mat[0][0][1] having value 11.
Its bottom pointer points to cell mat[0][1][0] having value 12.
It doesn’t have any element above it. In order to maintain the cyclic order, the top pointer points to cell mat[0][1][0] having value 12.
In this way, all the pointers of each cell are connected to their respective cells.
Input: X = 1, Y = 3, Z = 2
mat[0][0][0] = 1 mat[0][0][1] = 2 mat[0][0][0] = 3
mat[0][1][0] = 4 mat[0][1][1] = 5 mat[0][0][0] = 6
Output:
element front back left right up down
1 4 4 3 2 1 1
2 5 5 1 3 2 2
3 6 6 2 1 3 3
4 1 1 6 5 4 4
5 2 2 4 6 5 5
6 3 3 5 4 6 6
方法:
- 对于矩阵的每个单元格,创建一个由 6 个指针组成的节点,分别为Up、Down、Left、Right、Back 和 Front 。
- 链接所有节点的 Down 指针。对于给定的单元格,如果其下方不存在单元格,则将其向下指针指向行的最顶部单元格以保持循环顺序。
- 链接所有节点的 Up 指针。对于给定的单元格,如果其上方不存在单元格,则将其向上指针指向行的最底部单元格以保持循环顺序。
- 链接所有节点的右指针。对于给定的单元格,如果其右侧不存在单元格,则将其右指针指向该行最左侧的单元格以保持循环顺序。
- 链接所有节点的左指针。对于给定的单元格,如果其左侧不存在单元格,则将其左指针指向该行最右侧的单元格以保持循环顺序。
- 链接所有节点的 Front 指针。对于给定的单元格,如果它前面不存在单元格,则将其前面的指针指向该行最后面的单元格以保持循环顺序。
- 链接所有节点的 Back 指针。对于给定的单元格,如果它后面没有单元格,则将其后指针指向该行最前面的单元格以保持循环顺序。
- 因此以循环方式链接所有指针。遍历如此形成的整个链表,对于链表的每个节点,使用其 6 个指针打印它指向的所有值。
下面是上述方法的实现:
CPP
// C++ code for the above program
#include
using namespace std;
// Node structure of the
// 3D-linked list
class ThreeDLinkedListNode {
public:
int val;
ThreeDLinkedListNode* front;
ThreeDLinkedListNode* back;
ThreeDLinkedListNode* up;
ThreeDLinkedListNode* right;
ThreeDLinkedListNode* down;
ThreeDLinkedListNode* left;
ThreeDLinkedListNode(
int x,
ThreeDLinkedListNode* f = NULL,
ThreeDLinkedListNode* b = NULL,
ThreeDLinkedListNode* u = NULL,
ThreeDLinkedListNode* r = NULL,
ThreeDLinkedListNode* d = NULL,
ThreeDLinkedListNode* l = NULL)
{
// initialization of 3D
// linkedlist Node
val = x;
front = f;
back = b;
up = u;
right = r;
down = d;
left = l;
}
vector extractNeighbors()
{
// extracting all front,
// back, up, down, left,
// right neighbours of
// the node
vector neighbour;
// front back left right up down
neighbour.push_back(front->val);
neighbour.push_back(back->val);
neighbour.push_back(left->val);
neighbour.push_back(right->val);
neighbour.push_back(up->val);
neighbour.push_back(down->val);
return neighbour;
}
};
class Solution {
public:
void setDown(
vector > > a,
int layer, int row, int column,
ThreeDLinkedListNode* node)
{
// setting the down pointers
// of all nodes in a
// 3D linked list
bool flag = false;
ThreeDLinkedListNode* head = node;
// loop to traverse the layers
for (int i = 0;
i < layer; i++) {
ThreeDLinkedListNode*
start_layer_head
= node;
// Loop to traverse the
// columns of each layer
for (int j = 0;
j < column; j++) {
ThreeDLinkedListNode*
start
= node;
// Loop to traverse
// each row
for (int k = 0;
k < row; k++) {
// Check if its the
// last cell of the
// current row
if (k == row - 1)
node->down = start;
else
node->down
= new ThreeDLinkedListNode(
a[i][k + 1][j]);
node = node->down;
}
// Check if it is the last
// element of the current
// column
if (j == column - 1) {
node->right = start_layer_head;
// Check if it it is the
// last element of the
// current layer
if (i < layer - 1) {
start_layer_head->back
= new ThreeDLinkedListNode(
a[i + 1][0][0]);
node = start_layer_head->back;
}
else {
start_layer_head->back = head;
node = start_layer_head->back;
}
}
else {
node->right
= new ThreeDLinkedListNode(
a[i][0][j + 1]);
node = node->right;
}
}
}
}
void setUp(
vector > > a,
int layer, int row, int column,
ThreeDLinkedListNode* node)
{
// setting the up pointers of all
// nodes in a 3d linked list
ThreeDLinkedListNode* head = node;
// loop to traverse the layers
for (int i = 0;
i < layer; i++) {
ThreeDLinkedListNode*
start_layer_head
= node;
// Loop to traverse the
// columns
for (int j = 0;
j < column; j++) {
ThreeDLinkedListNode*
start
= node;
// Loop to traverse
// the rows
for (int k = 0;
k < row; k++) {
node->down->up = node;
node = node->down;
}
// Check if it is the
// last element of
// the column
if (j == column - 1) {
node
= start_layer_head->back;
}
else {
node = node->right;
}
}
}
}
void setRight(
vector > > a,
int layer, int row, int column,
ThreeDLinkedListNode* node)
{
// setting the Right pointers of
// all nodes in a 3d linked list
ThreeDLinkedListNode* head = node;
// loop to traverse the layers
for (int i = 0;
i < layer; i++) {
ThreeDLinkedListNode*
start_layer_head
= node;
// loop to traverse the
// columns
for (int j = 0;
j < column; j++) {
ThreeDLinkedListNode*
start
= node;
// loop to traverse
// the rows
for (int k = 0;
k < row; k++) {
node->down->right
= node->right->down;
node = node->down;
}
// Check if it is the
// last element of the
// column
if (j == column - 1) {
node
= start_layer_head->back;
}
else {
node = node->right;
}
}
}
}
void setLeft(
vector > > a,
int layer, int row, int column,
ThreeDLinkedListNode* node)
{
// setting the Left pointers of
// all nodes in a 3d linked list
ThreeDLinkedListNode* head = node;
// loop to traverse the layers
for (int i = 0;
i < layer; i++) {
ThreeDLinkedListNode*
start_layer_head
= node;
// loop to traverse the
// columns
for (int j = 0;
j < column; j++) {
ThreeDLinkedListNode*
start
= node;
// loop to traverse
// the rows`
for (int k = 0;
k < row; k++) {
node->right->left = node;
node = node->down;
}
// Check if it is the
// last element of the
// column
if (j == column - 1) {
node
= start_layer_head->back;
}
else {
node = node->right;
}
}
}
}
void setBack(
vector > > a,
int layer, int row, int column,
ThreeDLinkedListNode* node)
{
// setting the Back pointers of
// all nodes in a 3d linked list
ThreeDLinkedListNode* head = node;
// loop to traverse the layers
for (int i = 0;
i < layer; i++) {
ThreeDLinkedListNode*
start_layer_head
= node;
// loop to traverse the
// columns
for (int j = 0;
j < column; j++) {
ThreeDLinkedListNode*
start
= node;
// loop to traverse
// the rows
for (int k = 0;
k < row; k++) {
node->down->back
= node->back->down;
node = node->down;
}
// Check if it is the
// last element of the
// column
if (j == column - 1) {
node = start_layer_head->back;
}
else {
node = node->right;
node->back = start->back->right;
}
}
}
}
void setFront(
vector > > a,
int layer, int row, int column,
ThreeDLinkedListNode* node)
{
// setting the Front pointers of
// all nodes in a 3d linked list
ThreeDLinkedListNode* head = node;
// loop to traverse the layers
for (int i = 0;
i < layer; i++) {
ThreeDLinkedListNode*
start_layer_head
= node;
// loop to traverse the
// columns
for (int j = 0;
j < column; j++) {
ThreeDLinkedListNode*
start
= node;
// loop to traverse
// the rows
for (int k = 0;
k < row; k++) {
node->back->front = node;
node = node->down;
}
// Check if it is the
// last element of the
// column
if (j == column - 1) {
node = start_layer_head->back;
}
else {
node = node->right;
node->back = start->back->right;
}
}
}
}
ThreeDLinkedListNode*
ThreeDimensionalMatrixToLinkedList(
vector > > a,
int layer, int row, int column)
{
ThreeDLinkedListNode* head
= new ThreeDLinkedListNode(
a[0][0][0]);
ThreeDLinkedListNode* temp = head;
// setting down pointers
// of all the nodes
setDown(a, layer,
row, column, temp);
// setting up pointers
// of all the nodes
setUp(a, layer,
row, column, temp);
// setting right pointers
// of all the nodes
setRight(a, layer,
row, column, temp);
// setting left pointers
// of all the nodes
setLeft(a, layer,
row, column, temp);
// setting back pointers
// of all the nodes
setBack(a, layer,
row, column, temp);
// setting front pointers
// of all the nodes
setFront(a, layer,
row, column, temp);
return head;
}
};
void print_2d_matrix(
vector > > mat,
ThreeDLinkedListNode* head,
int rows, int cols, int layer)
{
ThreeDLinkedListNode* rest;
// loop to traverse
// the rows
for (int i = 0;
i < rows; ++i) {
rest = head->down;
// loop to traverse
// the columns
for (int j = 0;
j < cols; ++j) {
cout << mat[layer][i][j]
<< "\t";
vector neighbors
= head->extractNeighbors();
for (auto ne : neighbors)
cout << ne << "\t";
cout << "\n";
head = head->right;
}
head = rest;
}
}
void printOutput(
vector > > mat,
ThreeDLinkedListNode* head,
int layer, int row, int column)
{
ThreeDLinkedListNode* rest;
// loop to traverse the layers
for (int i = 0;
i < layer; i++) {
rest = head->back;
print_2d_matrix(mat, head,
row, column, i);
head = rest;
}
}
// Driver Code
int main()
{
// Initialise the count
// of layers, columns and
// rows
int layer = 3;
int row = 3;
int column = 3;
vector > >
mat(layer,
vector >(
row, vector(column)));
mat = { { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } },
{ { 10, 11, 12 },
{ 13, 14, 15 },
{ 16, 17, 18 } },
{ { 19, 20, 21 },
{ 22, 23, 24 },
{ 25, 26, 27 } } };
// Function Call to form
// the linked list for the
// given 3D matrix
ThreeDLinkedListNode*
head
= Solution()
.ThreeDimensionalMatrixToLinkedList(
mat, layer, row, column);
cout << "element\t"
<< "front\tback\t"
<< "left\tright\t"
<< "up\tdown\n";
// Function to print the
// linked list elements
// and thevalues pointed
// by corresponding pointers
printOutput(mat, head,
layer, row, column);
return 0;
}
element front back left right up down
1 19 10 3 2 7 4
2 20 11 1 3 8 5
3 21 12 2 1 9 6
4 22 13 6 5 1 7
5 23 14 4 6 2 8
6 24 15 5 4 3 9
7 25 16 9 8 4 1
8 26 17 7 9 5 2
9 27 18 8 7 6 3
10 1 19 12 11 16 13
11 2 20 10 12 17 14
12 3 21 11 10 18 15
13 4 22 15 14 10 16
14 5 23 13 15 11 17
15 6 24 14 13 12 18
16 7 25 18 17 13 10
17 8 26 16 18 14 11
18 9 27 17 16 15 12
19 10 1 21 20 25 22
20 11 2 19 21 26 23
21 12 3 20 19 27 24
22 13 4 24 23 19 25
23 14 5 22 24 20 26
24 15 6 23 22 21 27
25 16 7 27 26 22 19
26 17 8 25 27 23 20
27 18 9 26 25 24 21
时间复杂度: O(X * Y * Z),其中 X、Y、Z 是 3D 矩阵的维度。
辅助空间: O(X * Y * Z),其中 X、Y、Z 是 3D 矩阵的维度。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live