Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-dhlee777 #47

Merged
merged 1 commit into from
May 19, 2024
Merged

10-dhlee777 #47

merged 1 commit into from
May 19, 2024

Conversation

dhlee777
Copy link
Contributor

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ† ๋งˆํ† 

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

ํ•˜๋ฃจ

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ธฐ์กด์˜ 2์ฐจ์›๋ฐฐ์—ด์˜ ํ† ๋งˆํ† ๋ฌธ์ œ๋Š” ํ’€์–ด๋ดค์ง€๋งŒ ์ด๋ฒˆ์—๋Š” 3์ฐจ์›๋ฐฐ์—ด์˜ ํ† ๋งˆํ†  ๋ฌธ์ œ๋ฅผ ํ’€๊ฒŒ๋˜์—ˆ๋‹ค. ๋‹ฌ๋ผ์ง„์ ์€ ํƒ์ƒ‰๋ฒ”์œ„๊ฐ€ ๊ธฐ์กด์—๋Š” ์ƒ,ํ•˜,์ขŒ,์šฐ 4๊ตฐ๋ฐ ์˜€๋‹ค๋ฉด 3์ฐจ์›๋ฐฐ์—ด์—์„œ๋Š” ์ƒ,ํ•˜,์ขŒ,์šฐ,์œ„,์•„๋ž˜ ์ด 6๊ตฐ๋ฐ๋ฅผ ํƒ์ƒ‰ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ขŒํ‘œ์ •๋ณด๋ฅผ ์ €์žฅํ• 
tomato ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•ด์ฃผ์—ˆ๊ณ  ํ† ๋งˆํ† ์˜ ์ƒํƒœ๋ฅผ์ €์žฅํ•  tom[][][]๋ฐฐ์—ด์„ ์„ ์–ธํ•ด์ฃผ์—ˆ๋‹ค.

์ž…๋ ฅ๋ฐ›๋Š” ์ฝ”๋“œ

cin >> col >> row >> height;

	for (int i = 0; i < height; i++) {
		for (int j = 0; j < row; j++) {
			for (int k = 0; k < col; k++) {
				cin >> tom[i][j][k];
				if (tom[i][j][k] == 1)         //์ต์€ํ† ๋งˆํ† ๊ฐ€ ๋“ค์–ด์žˆ์œผ๋ฉด
					q.push(tomato(k, j, i));   //ํƒ์ƒ‰์‹œ์ž‘์ ์„์œ„ํ•ด ๊ทธ ์ขŒํ‘œ๋ฅผ ํ์— ๋„ฃ๋Š”๋‹ค.
			}
		}
	}

tom๋ฐฐ์—ด์— ์ƒํƒœ๋ฅผ ์ €์žฅํ• ๋•Œ๋Š” ๋†’์ด,ํ–‰,์—ด ์ˆœ์œผ๋กœ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ ํ† ๋งˆํ† ์˜ ์ƒํƒœ๊ฐ€ 1์ด๋ผ๋ฉด ์ต์€ ํ† ๋งˆํ†  ์ด๋ฏ€๋กœ ํƒ์ƒ‰ ์‹œ์ž‘์ ์„ ์œ„ํ•ด ๊ทธ ์ขŒํ‘œ๋ฅผ ํ์— ๋„ฃ์–ด์ค€๋‹ค.

bfs

void bfs() {
	while (!q.empty()) {
		tomato t = q.front();
		q.pop();
		for (int i = 0; i < 6; i++) {      //ํƒ์ƒ‰๋ฒ”์œ„(์ƒ,ํ•˜,์ขŒ,์šฐ,์œ„,์•„๋ž˜)
			int _x = t.x1 + x[i];
			int _y = t.y1 + y[i];
			int _z = t.z1 + z[i];
			if (_x < col && _x >= 0 && _y < row && _y >= 0 && _z < height && _z >= 0 && tom[_z][_y][_x] == 0) {
				tom[_z][_y][_x] = tom[t.z1][t.y1][t.x1] + 1;
				q.push(tomato(_x, _y, _z));

			}
		}


	}

}

ํƒ์ƒ‰๋ฒ”์œ„๋Š” ์ƒ,ํ•˜,์ขŒ,์šฐ,์œ„,์•„๋ž˜ 6๊ตฐ๋ฐ ์ด๊ณ  ํƒ์ƒ‰์กฐ๊ฑด์€ ๊ฐํ–‰๊ณผ ์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋งŒ์กฑํ•˜๊ณ  tom[][][]==0์œผ๋กœ ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ์—ฌ์•ผ ํ•œ๋‹ค. ์ด๋•Œ tom๋ฐฐ์—ด์€ ์›๋ž˜ ํ† ๋งˆํ† ์˜ ์ƒํƒœ๋ฅผ ์ €์žฅํ•ด์ฃผ๋Š” ๋ฐฐ์—ด์ด์—ˆ์œผ๋‚˜ bfs์—์„œ๋Š” ํ† ๋งˆํ† ๊ฐ€์ต๋Š”๋ฐ ๊ฑธ๋ฆฐ ์ผ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค. ํƒ์ƒ‰์— ์„ฑ๊ณตํ•˜์˜€๋‹ค๋ฉด tom๋ฐฐ์—ด์— ๊ทธ์ „์ผ์ˆ˜+1 ์„ํ•ด์ค€๋‹ค. ๊ทธ ํ›„ ํ์— ์ขŒํ‘œ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

๊ฑธ๋ฆฐ์ผ์ˆ˜ ์ฐพ๊ธฐ

int sum = 0;
	for (int i = 0; i < height; i++) {
		for (int j = 0; j < row; j++) {
			for (int k = 0; k < col; k++) {
				if (tom[i][j][k] == 0)     //bfsํ›„ ์•ˆ์ต์€ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ชจ๋‘์ต์ง€๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด๋ฏ€๋กœ  -1์„ ์ถœ๋ ฅ
				{
					cout << -1;
					return 0;
				}
				if (sum < tom[i][j][k]) sum = tom[i][j][k]; //bfs ํ›„ ๊ฑธ๋ฆฐ์‹œ๊ฐ„์„ ์ฐพ๊ธฐ์œ„ํ•ด tom๋ฐฐ์—ด์˜ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„ sum์— ์ €์žฅํ•˜๋Š”๊ณผ์ •

			}
		}
	}

	sum == 1 ? cout << 0 : cout << sum - 1;     

bfs๋ฅผ ํ•ด์ค€ํ›„ ์•ˆ ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋‘ ์ต์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฑธ๋ฆฐ์ผ์ˆ˜๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด sum์„ ํ†ตํ•ด tom๋ฐฐ์—ด์˜ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„์ค€๋‹ค. ๋งŒ์•ฝ sum์ด 1์ด๋ผ๋ฉด ์• ์ดˆ์— ๋ชจ๋‘ ์ต์–ด์žˆ๋Š”์ƒํƒœ์ด๋ฏ€๋กœ 0์„ ์ถœ๋ ฅ ์•„๋‹ˆ๋ฉด ์‹ค์ œ ๊ฑธ๋ฆฐ์ผ์ˆ˜์ธ sum-1์„ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

๊ธฐ์กด 2์ฐจ์›ํ† ๋งˆํ†  ๋ฌธ์ œ์™€ ๋ณ„ ๋‹ค๋ฅผ๊ฒŒ ์—†์—ˆ์ง€๋งŒ ํ’€์–ด๋ณธ์ง€ ์ข€ ์˜ค๋ž˜๋ผ์„œ ๋‹ค์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ƒ๊ฐํ•˜๋Š๋ผ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋˜ ๊ฑฐ ๊ฐ™๋‹ค.

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ตฌ์กฐ์ฒด๋กœ x,y,z 3๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ž˜ ์ฐพ์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
c++์—๋Š” tuple์ด๋ž€ ๊ธฐ๋Šฅ๋„ ์žˆ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ๊ณ„์‹œ๋‚˜์š”? python์˜ tuple๊ณผ ๋‹ฌ๋ฆฌ c++ tuple์€ 3๊ฐœ์˜ ์ธ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pair๋Š” 2๊ฐœ, tuple์€ 3๊ฐœ

#include <tuple>
queue<tuple<int, int, int>> q;
while (!q.empty())
	{
		int x, y, z;

		x = get<2>(q.front());
		y = get<1>(q.front());
		z = get<0>(q.front());
}

์—„์ฒญ ์˜›๋‚ ์— ํ’€์—ˆ๋˜๊ฑฐ๋ผ ์ด๋Ÿฐ ์†Œ์†Œํ•œ ์žฌ๋ฏธ๋ฅผ ๋ณด์•˜๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”.
์ถ”๊ฐ€์ ์œผ๋กœ day๋ฅผ ํƒ์ƒ‰ํ• ๋•Œ ์ €๋Š” bfs์—์„œ ๋น„๊ต๋ฅผ ํ•ด์„œ ๋ฐ”๋กœ ๊ตฌํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ชจ๋“  ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•œ๋‹ค๋ฉด ๋˜‘๊ฐ™๊ฒ ์ง€๋งŒ ์ผ๋ถ€๋งŒ ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ๋ชจ๋“  ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•ด์„œ day๋ฅผ ์ฐพ๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์‹œ๊ฐ„์„ ์กฐ๊ธˆ ์ค„์ผ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?

for (int i = 0; i < 6; i++)
		{
			int nx, ny, nz;
			nx = x + dx[i];
			ny = y + dy[i];
			nz = z + dz[i];

			if (IsOutRange(nx, ny, nz) && tomato[nz][ny][nx] == 0)
			{
				tomato[nz][ny][nx] = tomato[z][y][x] + 1;
				q.push(make_tuple(nz, ny, nx));
				if (tomato[nz][ny][nx] > day)
					day = tomato[nz][ny][nx];
			}
		}

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ‘ผ ์ง€ ์–ผ๋งˆ ์•ˆ ๋œ ๋ฌธ์ œ๋ผ ๊ธฐ์–ต์— ๋‚จ๋„ค์š”. ์ €๋„ ๋น„์Šทํ•˜๊ฒŒ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด ํƒ์ƒ‰ํ•ด ํ’€์—ˆ์—ˆ๋Š”๋ฐ์š”. ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋‹ค๋ณด๋‹ˆ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํฌ๊ฒŒ ๋‚˜์˜ค๋”๋ผ๊ณ ์š”. ํ๋ฅผ ์ด์šฉํ•œ ๊น”๋”ํ•œ ํ’€์ด ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฐฐ์—ด์— 1, 0๋งŒ ๋„ฃ์„ ์ƒ๊ฐ์„ ํ–ˆ๋Š”๋ฐ ๋งค๋ฒˆ 1์”ฉ ๋”ํ•ด์ค˜์„œ ๋ฐฐ์—ด์˜ ์ตœ๋Œ€ ๊ฐ’์— ๋”ฐ๋ผ ๋‚  ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ตฐ์š”! ์ด๋Ÿฐ ๋ฐฉ์‹๋„ ์žˆ์—ˆ๋„ค์š”... ๊ทธ๋ž˜๋„ ๋‹ต์„ ์ฐพ๊ธฐ ์œ„ํ•ด ํ•œ๋ฒˆ ๋” ํƒ์ƒ‰์„ ํ•ด์•ผํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ˆ ๋ณ„๋„ ํƒ์ƒ‰ ์—†์ด ์ตœ๋Œ€ ๋‚ ์งœ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์œผ๋ฉด ์ฐพ์•„๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!

์ด์ œ ๊ตญ๋ฃฐ ์ฝ”์Šค ํ•˜์ดํผ ํ‹ˆ๋ฉ”์ด๋Ÿฌ ํ’€๋Ÿฌ ๊ฐ€์•ผ๊ฒ ์ฃ ?

}
}

sum == 1 ? cout << 0 : cout << sum - 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ถ€๋ถ„ ์ƒ๊น€์ƒˆ๊ฐ€ ๊ธฐ๋ฌ˜ํ•ด์„œ ์กฐ๊ธˆ ์ดํ•ดํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋„ค์š”... ์ด๋Ÿฐ ๋ถ€๋ถ„์€ ์‹คํ–‰ํ•  ํ•จ์ˆ˜๋ฅผ ๋ฐ–์œผ๋กœ ๋นผ๊ณ  ๊ฐ’์— ๋Œ€ํ•ด์„œ๋งŒ ์‚ผํ•ญ์—ฐ์‚ฐ์„ ํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์งˆ ๊ฒƒ ๊ฐ™์•„์š”!

cout << sum == 1 ? 0 : sum - 1; 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„ ํ•จ์ˆ˜๋งŒ ๋ฐ–์œผ๋กœ ๋บ„์ˆ˜๋„ ์žˆ์—ˆ๊ตฐ์š”.. ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ๊น”๋”ํ•ด์ง€๋Š”๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž˜์จ๋จน๊ฒ ์Šต๋‹ˆ๋‹ค !

@dhlee777
Copy link
Contributor Author

๊ตฌ์กฐ์ฒด๋กœ x,y,z 3๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ž˜ ์ฐพ์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. c++์—๋Š” tuple์ด๋ž€ ๊ธฐ๋Šฅ๋„ ์žˆ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ๊ณ„์‹œ๋‚˜์š”? python์˜ tuple๊ณผ ๋‹ฌ๋ฆฌ c++ tuple์€ 3๊ฐœ์˜ ์ธ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pair๋Š” 2๊ฐœ, tuple์€ 3๊ฐœ

#include <tuple>
queue<tuple<int, int, int>> q;
while (!q.empty())
	{
		int x, y, z;

		x = get<2>(q.front());
		y = get<1>(q.front());
		z = get<0>(q.front());
}

์—„์ฒญ ์˜›๋‚ ์— ํ’€์—ˆ๋˜๊ฑฐ๋ผ ์ด๋Ÿฐ ์†Œ์†Œํ•œ ์žฌ๋ฏธ๋ฅผ ๋ณด์•˜๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”. ์ถ”๊ฐ€์ ์œผ๋กœ day๋ฅผ ํƒ์ƒ‰ํ• ๋•Œ ์ €๋Š” bfs์—์„œ ๋น„๊ต๋ฅผ ํ•ด์„œ ๋ฐ”๋กœ ๊ตฌํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ชจ๋“  ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•œ๋‹ค๋ฉด ๋˜‘๊ฐ™๊ฒ ์ง€๋งŒ ์ผ๋ถ€๋งŒ ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ๋ชจ๋“  ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•ด์„œ day๋ฅผ ์ฐพ๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์‹œ๊ฐ„์„ ์กฐ๊ธˆ ์ค„์ผ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?

for (int i = 0; i < 6; i++)
		{
			int nx, ny, nz;
			nx = x + dx[i];
			ny = y + dy[i];
			nz = z + dz[i];

			if (IsOutRange(nx, ny, nz) && tomato[nz][ny][nx] == 0)
			{
				tomato[nz][ny][nx] = tomato[z][y][x] + 1;
				q.push(make_tuple(nz, ny, nx));
				if (tomato[nz][ny][nx] > day)
					day = tomato[nz][ny][nx];
			}
		}

ํ—‰.. ํ•ญ์ƒ ์ธ์ž๊ฐ€ 3๊ฐœ์ผ๋•Œ ๊ตฌ์กฐ์ฒด๋ฅผ ์“ธ์ง€ pair<int,pair<int,int>>๋ฅผ ์“ธ์ง€ ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ tuple ์ด๋ผ๋Š” ์ข‹์€ ๋„๊ตฌ๊ฐ€ ์žˆ์—ˆ๊ตฐ์š”...! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค...! bfs์—์„œ ๋ฐ”๋กœ ๋น„๊ต๋ฅผ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์€ ์ƒ๊ฐ๋„ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค...

@dhlee777 dhlee777 merged commit 3ded618 into main May 19, 2024
1 check passed
@dhlee777 dhlee777 deleted the 10-dhlee777 branch May 19, 2024 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants