C++
[C++] 5월 2일 코딩 테스트 수업
k-codestudy
2025. 5. 2. 17:49
#pragma once
#include <iostream>
#include <list>
class C_ISLAND
{
private:
struct S_NODE
{
int nType;
std::list<S_NODE*> arChild[2];
std::list<S_NODE*>::iterator iterNodeList;
};
private:
static int m_arData[10][10];
S_NODE m_arNode[10][10];
std::list<S_NODE *> m_listNode;
//std::list< std::list<S_NODE*>* > listIsland;
private:
void linkNode();
bool findType(int nY, int nX, int* pType);
void makeLand();
void nodeGroup(int nType , S_NODE* pNode, std::list<S_NODE*>& listGroup);
public:
C_ISLAND() = default;
void makeNode();
void print();
};
#include "island.h"
int C_ISLAND::m_arData[10][10]
{
{1,1,1,0,0,0,0,1,1,1},
{1,1,1,1,0,0,0,0,1,1},
{1,0,1,1,0,0,0,0,1,1},
{0,0,1,1,1,0,0,0,0,1},
{0,0,0,1,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
};
void C_ISLAND::linkNode()
{
int arWay[4][2]{ {-1,0},{0,1},{1,0},{0,-1} };
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 4; k++)
{
m_arNode[i][j].nType = m_arData[i][j];
int nType{};
int nChildY = i + arWay[k][0];
int nChildX = j + arWay[k][1];
if (findType( nChildY,nChildX , &nType))
{
m_arNode[i][j].arChild[nType].push_back(&m_arNode[nChildY][nChildX]);
}
}
m_listNode.push_back(&m_arNode[i][j]);
auto iter = m_listNode.end();
iter--;
(*iter)->iterNodeList = iter;
}
}
}
bool C_ISLAND::findType(int nY, int nX, int* pType)
{
if (nY < 0 || nX < 0 || nY >= 10 || nX >= 10)
return false;
*pType = m_arData[nY][nX];
return true;
}
void C_ISLAND::makeLand()
{
std::list<S_NODE*> listGroup{};
S_NODE* pNode = *m_listNode.begin();
nodeGroup(pNode->nType, pNode, listGroup);
}
void C_ISLAND::nodeGroup(int nType, S_NODE* pNode, std::list<S_NODE*>& listGroup)
{
if (pNode->iterNodeList == m_listNode.end())
return;
listGroup.push_back(pNode);
m_listNode.erase(pNode->iterNodeList);
pNode->iterNodeList = m_listNode.end();
for (S_NODE* pChild : pNode->arChild[nType])
{
nodeGroup(nType, pChild, listGroup);
}
}
void C_ISLAND::makeNode()
{
linkNode();
makeLand();
}
void C_ISLAND::print()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
printf("%d ", m_arNode[i][j].nType );
}
printf("\n");
}
}
#include <iostream>
#include "island.h"
int main()
{
C_ISLAND cIsland{};
cIsland.makeNode();
cIsland.print();
}