[C++] ๋ฐฐ์—ด DAT(Direct Access Table)

leeactยท2023๋…„ 4์›” 18์ผ
1

c++ ์ •๋ฆฌ

๋ชฉ๋ก ๋ณด๊ธฐ
1/13
post-thumbnail

๐Ÿคฆโ€โ™‚๏ธ๋ฐฐ์—ด ์„ ์–ธ

1์ฐจ์› ๋ฐฐ์—ด

int arr[10];			// ๊ธธ์ด๊ฐ€ 10์ธ intํ˜• ๋ฐฐ์—ด
int arr[10] = {0, };	// ๊ธธ์ด๊ฐ€ 10์ธ intํ˜• ๋ฐฐ์—ด ์„ ์–ธ ๋ฐ 10๊ฐœ ๋ชจ๋‘ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
// ์ดˆ๊ธฐํ™”๋Š” {0, }์ผ ๊ฒฝ์šฐ ์ „์ฒด๊ฐ€ ๋ฐ”๋€Œ์ง€๋งŒ ๋‹ค๋ฅธ ๊ฐ’์€ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค.
int arr[10] = {5, };	// ์ฒซ ์›์†Œ๋งŒ 5, ๋‚˜๋จธ์ง€๋Š” 0

2์ฐจ์› ๋ฐฐ์—ด

int arr[10][10];
int arr[10][10] = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}

๐Ÿฑโ€๐Ÿ๋ฐฐ์—ด ์ ‘๊ทผ

๋ฐฐ์—ด์€ ์„ ์–ธํ–ˆ์œผ๋ฉด index๋ฅผ ํ™œ์šฉํ•ด์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

for (int i = 0; i< 10; i++){	// i๋ฅผ index๋กœ ์‚ฌ์šฉ
	int temp;					
    cin >> temp;
    arr[i] = temp;
}

โœจ์ž์ฃผ ๋‚˜์˜ค๋Š” ๋ฐฐ์—ด ๋ฌธ์ œ

  • ๋ฐฐ์—ด์—์„œ ํ•˜๋‚˜์˜ ์›์†Œ๋ฅผ ์„ ํƒํ•˜๊ณ  ์ธ์ ‘ํ•œ ์›์†Œ๋“ค์„ ๊ฐ™์ด ๊ฒ€์‚ฌํ•˜๋Š” ์œ ํ˜•

ex) index๊ฐ€ 5์ผ ๋•Œ, ์•ž ๋’ค์˜ ์›์†Œ๋“ค๋„ ๊ฒ€์‚ฌ

int arr[10] = {0, };

// check, index๊ฐ€ 5์ผ ๋•Œ
int index = 5;
if (arr[index] == 0)
	arr[index] = 1;
if (arr[index - 1] == 0)
	arr[index] = 1;
if (arr[index + 1] == 0)
	arr[index] = 1;

์œ„ ์ฝ”๋“œ๋Š” if๋ฌธ 3๊ฐœ ํ™œ์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฒ€์‚ฌํ•˜๋Š” ๋ฒ”์œ„๊ฐ€ ๊ธธ์–ด์ง„๋‹ค๋ฉด
์œ„ ๋ฐฉ๋ฒ•์€ ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค.๐Ÿค”

arr[index], arr[index - 1], arr[index + 1]์—์„œ ๊ทœ์น™์„ ์ฐพ์•„๋ณด๋ฉด
์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
arr[index + 0], arr[index + (-1)], arr[index + (1)]

int d[3] = {-1, 0, 1};		// ํ™•์ธํ•  ๋ฐฐ์—ด ๋ฒ”์œ„
for (int i = 0; i<3; i++){
	int index;				// index (1~8);
    cin >> index;			// ์ธ๋ฑ์Šค ์ž…๋ ฅ
	nx = d[i] + index;		// ํ™•์ธํ•  ๋ฐฐ์—ด ์›์†Œ
    if (arr[index] == 0)
		arr[index] = 1;
}

for (int i = 0; i<3; i++){
	int index;				// index (0~9);
    // ์ธ๋ฑ์Šค๊ฐ€ ๋งŒ์•ฝ ๋งจ ์•ž, ๋งจ ๋’ค์ผ๋•Œ ํƒ์ƒ‰์„ ํ•œ๋‹ค๋ฉด
    // nx๊ฐ€ -1๋กœ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    cin >> index;			// ์ธ๋ฑ์Šค ์ž…๋ ฅ
	nx = d[i] + index;		// ํ™•์ธํ•  ๋ฐฐ์—ด ์›์†Œ
    if (nx < 0 || nx > 9)	// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
    	continue;
    if (arr[index] == 0)
		arr[index] = 1;
}

์œ„ ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ ๋ฒ”์œ„๋ฅผ ๋”ฐ๋กœ ์ •ํ•œ๋‹ค๋ฉด if๋ฌธ์„ ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


2์ฐจ์› ๋ฐฐ์—ด(์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰)

int arr[10][10] = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 2, 0, 0, 0, 0, 9, 0 },
                    { 0, 2, 0, 0, 3, 2, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 5, 0, 0, 0, 0 },
                    { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
                    { 0, 3, 0, 0, 0, 0, 1, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 5, 0, 0, 8, 0, 0, 0, 0 },
                    { 0, 7, 0, 0, 0, 8, 0, 6, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}
                    
int row[] = {0, 0, 1, -1};
int col[] = {1, -1, 0, 0};

for (int i = 0; i< 4; i++){
	int nextRow, nextCol;
    int row, col;
    cin >> row, col;
    nextRow = row[i] + row;
    nextCol = col[i] + col;
    if (nextRow < 0 || nextRow > 9)		// ํ–‰ ๋ฒ”์œ„ ๋ฒ—์–ด๋‚จ
    	continue;
    if (nextCol < 0 || nextCol > 9)		// ์—ด ๋ฒ”์œ„ ๋ฒ—์–ด๋‚จ
    	continue;
   	if (arr[nextRow][nextCol] != 0) {
    	// ์›์†Œ๊ฐ’๋งŒํผ ์ถœ๋ ฅ
    	for (int i = 0; i < arr[nextRow][nextCol]; i++) {
        	cout << "์—๋ฏธ๋„ด์ž…๋‹ˆ๋‹ค";
        }
    }
}

Thank you (*๏ฟฃโ–ฝ๏ฟฃ*)ใƒ–

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 4์›” 20์ผ

๊ฟ€ํŒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋ฏธ์Šคํ„ฐ๋ฆฌ์•กํŠธ์”จ(โœงร—โœง)

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ