초기 네이버 기록/알고리즘(C)

백준 마인크래프트 18111번 브루스포스(C언어)

뜨거운 개발자 2023. 1. 3. 23:54
#include<stdio.h>
#include<stdlib.h>

int to_high(int high, int** arr, int width, int length,int inventory)
{
	
	int count = 0;//인벤토리 추가될 블럭
	int time = 0;
	int check;
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < width; j++)
		{
			check = high - arr[i][j];
			if (check > 0)
			{
				//printf("up\n");
				count -= check;
				time+=check;
			}
			else if (check < 0)
			{
				//printf("down\n");
				count -= check;
				time += -2*check;
			}
		}
	}
	if (count + inventory >= 0)
	{
		//printf("inven%d\n", time);
		return (time);
	}
	else
		return -1;
}

int main()
{
	int length, width, inventory;
	scanf("%d %d %d", &length, &width, &inventory);
	int** arr;
	int high = 0;;
	int low = 356;
	arr= (int**) malloc(sizeof(int*) * length);
	for (int i = 0; i < length; i++)
	{
		arr[i] =(int*) malloc(sizeof(int) * width);
		for (int j = 0; j < width; j++)
		{
			scanf("%d", &arr[i][j]);
			if (arr[i][j] > high)
				high = arr[i][j];
			if (arr[i][j] < low)
				low = arr[i][j];
		}
	}
	//printf("high: %d low:%d", high, low);
	int big_high;
	int less_time= 2147483647;
	int time;
	for (int k = high; k >= low - 1&&k>=0; k--)
	{
		//printf("high:%d\n", k);
		time = to_high(k, arr, width, length, inventory);
		if (time>=0&&time < less_time)
		{
			less_time = time;
			big_high = k;
		}

	}
	printf("%d %d", less_time, big_high);
	for (int i = 0; i < length; i++)
		free(arr[i]);
	free(arr);
}

문제를 차근차근 생각해가면서 풀수 있어서 재미있던 문제였다.

수식으로 접근하면 매우 어려운 문제가 되지만 컴퓨터의 장점인 많은 양의 연산이 가능하다는 걸 생각해서 문제를 해결하도록 생각하면 새로운 식을 도출 할 수기 있게된다.

 

​이 글은 코딩 꼬꼬마 시절에 푼 문제를 보관한 글로 네이버에 저장해둔 글을 옮긴 글입니다.
혹시나 참고하시는 부분에 이상한 부분이나 질문이 생긴다면 남겨주시면 친절히 답변 드리겠습니다.

728x90