더보기
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Set
{
	int num;
	char c;
};

void InsetSort(vector<Set>& set)
{
	int size = set.size();
	for (int i = 1; i < size; i++)
	{
		int numData = set[i].num;
		char cData = set[i].c;

		int j; 
		for ( j = i - 1; j >= 0 ; j--)
		{
			if (set[j].num == numData)
			{
				if (set[j].c > cData)
				{
					set[j + 1].c = set[j].c;
					set[j + 1].num = set[j].num;
				}
				else
					break;
			}
			else if (set[j].num > numData)
			{
				set[j + 1].c = set[j].c;
				set[j + 1].num = set[j].num;
			}
			else
				break;
		}

		set[j+1].num = numData;
		set[j+1].c = cData;
	}
}

int main()
{
	vector<Set> set;
	int a;
	cin >> a;
	for (size_t i = 0; i < a; i++)
	{
		int num;
		char c;

		cin >> num >> c;

		set.push_back({ num , c});
	}

	InsetSort(set);

	//만약 람다를 사용한다면
	/*sort(set.begin(), set.end(), [&](const Set& a, const Set& b)
		{
			if (a.num == b.num) return a.c < b.c;
			return a.num < b.num;
		});*/

	for (const auto& s : set)
	{
		cout << s.num << s.c << endl;
	}

	return 0;
}
더보기
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 4;
const int strMAX = 3;
int arr[4] = { 0,0,0,0 };
string str[strMAX] = { "금", "은", "동" };

void InsertSort(int score)
{
	const int last = MAX - 1;

	if (arr[last] < score)
		arr[last] = score;
	else
		return;

	int data = arr[MAX - 1];

	int j;
	for (j = last - 1; j >= 0; j--)
	{
		if (arr[j] < data)
			arr[j + 1] = arr[j];
		else
			break;
	}

	arr[j + 1] = data;
}

int main()
{
	int N;

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		int score;
		cin >> score;
		InsertSort(score);
	}
	
	for (int i = 0; i < strMAX; i++)
	{
		cout << str[i] << arr[i] << endl;
	}
	
	return 0;
}
더보기
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int first = 1;
const int boom = 3;

int main()
{
	vector<int> v;
	int N;

	cin >> N;

	int count = 0;
	for (int i = 0; i < N; i++)
	{
		int num;
		cin >> num;
		
		if (v.size() != 0 && v.back() != num)
			count = 1;
		else
			count++;

		if (count == boom)
		{
			while (count > first)
			{
				v.pop_back();
				count--;
			}
			count = 0;
		}
		else
		{
			v.push_back(num);
		}
	}
	
	sort(v.begin(), v.end());
    
    for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}

	return 0;
}
더보기
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	vector<vector<int>> v;
	v.resize(5);

	for (int i = 0; i < 5; i++)
	{
		string strNUm;
		cin >> strNUm;

		for (int j = 0; j < strNUm.size(); j++)
		{
			int num = strNUm[j] - '0';
			v[i].push_back(num);
		}
	}

	int line1, line2;
	cin >> line1 >> line2;

	sort(v[line1].begin(), v[line1].end());
	sort(v[line2].begin(), v[line2].end());

	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i][0] << " ";
	}

	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	vector<string> names;
	int N;

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		string name;
		cin >> name;
		names.push_back(name);
	}

	sort(names.begin(), names.end(), [&](const string& name1, const string& name2)
		{
			if (name1.size() == name2.size())
			{
				return name1 < name2;
			}
			return name1.size() < name2.size();
		});

	for (int i = 0; i < names.size(); i++)
	{
		cout << names[i] << endl;
	}

	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Vote
{
	int num;
	string name;
};

int main()
{
	vector<vector<Vote>> info;
	int num;
	int people;

	cin >> num >> people;
	info.resize(num);

	for (int i = 0; i < people; i++)
	{
		int num;
		string name;

		cin >> num >> name;

		info[num].push_back({num, name});
	}
	
	sort(info.begin(), info.end(), [&](const vector<Vote>& a, const vector<Vote>& b)
		{
			return a.size() > b.size();
		});


	for (int i = 0; i < info[0].size(); i++)
	{
		cout << info[0][i].name << " ";
	}

	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	vector<vector<string>> farm(3, vector<string>(3));
	vector<pair<int, int>> xy;
	int crops = 0;

	int N,K;
	cin >> N;

	for (int i = 0; i < N; i++)
	{
		int x;
		int y;
		string amount;

		cin >> x >> y >> amount;
		farm[x][y] = amount;
		xy.push_back(make_pair(x, y));

		crops += amount.size();
	}

	cin >> K;

	for (int i = 0; i < K; i++)
	{
		int pow;
		cin >> pow;

		for (int j = 0; j < xy.size(); j++)
		{
			int x = xy[j].first;
			int y = xy[j].second;

			if (farm[x][y] == "")
				continue;

			int amount = farm[x][y].back() - '0';

			if (pow > amount)
			{
				farm[x][y].pop_back();
				crops--;
			}
			else
			{
				int _amount = stoi(farm[x][y]) - pow;
				farm[x][y] = to_string(_amount);
			}
		}
	}

	cout << crops;

	return 0;
}
더보기

 

#include <iostream>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;

bool IsLowerOnly(const string& str)
{
	for (int i = 0; i < str.size(); i++)
	{
		bool _isupper = isupper(str[i]);
		if (_isupper)
			return false;
	}

	return true;
}

bool IsUpperHeadOnly(const string& str)
{
	for (int i = 1; i < str.size(); i++)
	{
		bool _isupper = isupper(str[i]);
		if (_isupper)
			return false;
	}

	return true;
}

void ToUpperAllIndex(string& str)
{
	for (int i = 0; i < str.size(); i++)
	{
		str[i] = toupper(str[i]);
	}
}

void FixName(vector<string>& names)
{
	int size = static_cast<int>(names.size());

	for (int i = 0; i < size; i++)
	{
		if (IsLowerOnly(names[i]))
			names[i][0] = toupper(names[i][0]);
		else if (!IsUpperHeadOnly(names[i]))
			ToUpperAllIndex(names[i]);
	}
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	vector<char> alpha;
	int select;
	alpha.resize(10);

	for (int i = 0; i < 10; i++)
	{
		cin >> alpha[i];
	}

	cin >> select;

	sort(alpha.begin(), alpha.end(), [&](const char& a, const char& b)
		{
			return a < b;
		});

	const int ASCll = 128;
	int alphaAmount[ASCll] = {};
	int size = alpha.size() - 1;
	for(int i = size; i > (size - select); i--)
	{
		const int toNum = alpha[i];
		alphaAmount[toNum]++;
	}

	int g_alpha = -1;
	char most;
	for (int i = 0; i < ASCll; i++)
	{
		if (alphaAmount[i] > g_alpha)
		{
			g_alpha = alphaAmount[i];
			most = i;
		}
	}

	cout << most;

	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main()
{
	vector<vector<int>> number;
	vector<vector<int>> bit;
	vector<pair<int, int>> xy;
	int n;
	cin >> n;

	number.resize(n);
	bit.resize(n);

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			int num;
			cin >> num;
			number[i].push_back(num);
		}
	}
	
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			int _bit;
			cin >> _bit;
			bit[i].push_back(_bit);
			
			if (_bit == 1)
				xy.push_back(make_pair(i,j));
		}
	}

	const int size = xy.size();
	vector<vector<int>> priority(10);
	for (size_t i = 0; i < size; i++)
	{
		int x = xy[i].first;
		int y = xy[i].second;
		priority[number[x][y]].push_back(number[x][y]);
	}

	sort(priority.begin(), priority.end(), [&](const vector<int>& a, const vector<int>& b)
		{
			if (a.size() == b.size() && !a.empty() && !b.empty())
				return a[0] < b[0];
			else
				return a.size() > b.size();
		});

	for (int i = 0; i < 10; i++)
	{
		const int length = priority[i].size();
		for (int j = 0; j < length; j++)
		{
			cout << priority[i][j] << " ";
		}

		if (priority[i].size() == 0)
			break;
	}
	
	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 6;
string dictionary[MAX] =
{
	"ABCD",
	"ABCE",
	"AGEH",
	"EIEI",
	"FEQE",
	"ABAD"
};

int Search(string& str)
{
	vector<int> index;
	int count = 0;

	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] != '?')
			index.push_back(i);
	}

	for (int i = 0; i < MAX; i++)
	{
		bool success = true;
		string word = dictionary[i];
		for (int j = 0; j < index.size(); j++)
		{
			if (word[index[j]] != str[index[j]])
				success = false;
		}
		if (success)
			count++;
	}

	return count;
}

int main()
{
	string str;
	cin >> str;

	int result = Search(str);
	cout << result;

	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	string str;
	cin >> str;

	int count = 0;

	while (true)
	{
		for (int i = 0; i < str.size(); i++)
		{
			if (str[i] == '_')
			{
				cout << str[i];
				continue;
			}
				
			str[i]--;
			if (str[i] < 'A')
			{
				str[i] = '_';
				count++;
			}
			cout << str[i];
		}

		cout << endl;
		if (count >= str.size())
			break;
	}
	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<char>> tile =
{
	{'A','B','C','E','F','G'},
	{'H','I','J','K','L','M'},
	{'N','O','P','Q','R','S'},
};

vector<vector<char>> cpy_tile =
{
	{'A','B','C','E','F','G'},
	{'H','I','J','K','L','M'},
	{'N','O','P','Q','R','S'},
};

bool Flip(int y, int x)
{
	int dy[] = {-1,1,0,0};
	int dx[] = {0,0,-1,1};
	tile[y][x] = '#'; 

	for (int i = 0; i < 4; i++)
	{
		int ny =  y + dx[i];
		int nx =  x + dy[i];

		if (nx < 0 || ny < 0 || nx > tile[0].size() - 1 || ny > tile.size() - 1)
			continue;

		if (tile[ny][nx] == '#')
			tile[ny][nx] = cpy_tile[ny][nx];
		else
			tile[ny][nx] = '#';
	}

	return true;
}

int main()
{
	string str;
	cin >> str;

	for (int i = 0; i < str.size(); i++)
	{
		char c = str[i];

		for (int j = 0; j < tile.size(); j++)
		{ 
			bool done = false;
			for (int k = 0; k < tile[0].size(); k++)
			{
				if (c == tile[j][k])
				{
					done = Flip(j, k);
					break;
				}
			}

			if (done)
				break;
		}
	}


	for (int i = 0; i < tile.size(); i++)
	{
		for (int j = 0; j < tile[i].size(); j++)
		{
			cout << tile[i][j];
		}
		cout << endl;
	}
	

	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int people;
	int n;
	string updown;

	cin >> people;

	int first = 0;
	int tail = 50;
	int flag = 0;

	for (int i = 0; i < people; i++)
	{
		cin >> n >> updown;

		if ("UP" == updown)
		{
			first = n + 1;
		}
		else if ("DOWN" == updown)
		{
			tail = n - 1;
		}

		if (first == tail)
		{
			flag = 1;
			cout << first;
			break;
		}
		else if(first > tail)
		{
			flag = 1;
			cout << "ERROR!!";
			break;
		}
	}
	
	if(flag == 0)
		cout << first << " ~ " << tail;

	return 0;
}
예시2
예시3
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void Chase(vector<pair<int, string>>& info, int index)
{
	if (info[index].second == "테러범")
	{
		cout << index << "번" << endl;
		return;
	}
		
	int dir = 1;
	if (info[index].second == "<<")
		dir *= -1;

	int nextIndex = index + (info[index].first * dir);
	Chase(info, nextIndex);

	cout << index << "번" << endl;
}

int main()
{
	vector<pair<int, string>> info =
	{
		{3, ">>"}, {2, ">>"}, {1, "<<"}, {3, ">>"}, {2, "<<"}, {0, "테러범"}, {1, "<<"}
	};

	int index;
	cin >> index;

	Chase(info, index);
	return 0;
}
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 10;
int arr[MAX][MAX];
int cpyarr[MAX][MAX];

void Rotat(int num)
{
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < num; j++)
		{
			cpyarr[j][num - 1 - i] = arr[i][j];
		}
	}

	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < num; j++)
		{
			arr[i][j] = cpyarr[i][j];
		}
	}
}

int main()
{
	int n, turn;
	cin >> n >> turn;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{  
			cin >> arr[i][j];
		}
	}

	for (int i = 0; i < turn; i++)
	{
		Rotat(n);
		int a = 0;
	}


	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout<< cpyarr[i][j];
		}
		cout << endl;
	}
	

	return 0;
}
1회 회전
2회 회전
더보기

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int column = 4;
const int row = 8;

int ground[column][row];
bool visited[column][row];

int MakeArea(int y, int x)
{
	int sum = 0;

	int height = column;
	int width = row - x;
	int flag = 0;
	
	for (int i = y; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			if (ground[i][x + j] == 0)
			{
				if (j == 0)//직사각형 아님 
					flag = 1;

				width = j;//넓이 결정
				break;
			}
			sum += ground[i][x + j];
			visited[i][x + j] = true;
		}

		if (flag == 1)
			break;
	}

	return sum;
}


int main()
{
	for (int i = 0; i < column; i++)
	{
		for (int j = 0; j < row; j++)
		{
			cin >> ground[i][j];
		}
	}

	int max = -1;
	int sum = 0;
	for (int i = 0; i < column; i++)
	{
		for (int j = 0; j < row; j++)
		{
			if (ground[i][j] != 0 && visited[i][j] == false)//이미 방문했다면 어떠한 직사각형에 포함이 된 상태기 때문에 계산할 필요x
				sum = MakeArea(i, j);

			max = sum > max ? sum : max;
		}
	}

	cout << max;

	return 0;
}

 

'개발공부 > 기본기 훈련' 카테고리의 다른 글

1주차 문제풀이  (0) 2024.05.25
더보기
#include <iostream>
using namespace std;

int main()
{
	// 알고리즘 1. O(1)
	{
		/*#include <iostream>
		using namespace std;

		int main()
		{
			for (int i = 0; i < 10000; i++) {
				cout << "#";
			}

			return 0;
		}*/
	}

	// 알고리즘 2. O(n)
	{
		/*#include <iostream>
		using namespace std;

		int main()
		{
			int n;
			cin >> n;

			for (int y = 0; y < n; y++) {
				for (int x = 0; x <= y; x++) {
					cout << "#";
				}
			}

			return 0;
		}*/
	}

	// 알고리즘 3. O(n^2)
	{
		/*#include <iostream>
		using namespace std;

		int n;

		void abc()
		{
			for (int i = 0; i < n; i++) {
				cout << "#";
			}
		}

		int main()
		{
			cin >> n;

			for (int y = 0; y < n; y++) {
				abc();
				abc();
				abc();
			}

			return 0;
		}*/
	}

	//알고리즘 4. O(n^2)
	{
		/*#include <iostream>
		using namespace std;

		int main()
		{
			cin >> n;

			for (int y = 0; y < n; y++) {

				for (int x = 0; x < 5; x++) {

					for (int z = 0; z < n; z++) {
						cout << "#";
					}
				}
			}

			return 0;
		}*/
	}

	int arr[5] = { -1, 21, 11, 2, 2 };

	int algo;

	cin >> algo;

	int score = arr[algo];

	return 0;
}
더보기
#include <iostream>
using namespace std;

int main()
{
	const int MAX = 100000;
	int arr[MAX] = { 7, 2, 4, 3, 2, 1, 1, 9, 2 };
	int n = 9;

	while (true)
	{
		cin >> n;

		if (n >= 4 && n <= MAX)
		{
			for (int i = 0; i < n; i++)
			{
				cin >> arr[i];
			}

			break;
		}
	}

	int min = INT_MAX;
	int length = (n - 4) + 1;
	int sum = 0;

	for (int i = 0; i < 4; i++)
	{
		sum += arr[i];
	}

	for (int i = 1; i < length; i++)
	{
		sum = sum - arr[i - 1] + arr[i + 3];
		if (min > sum)
			min = sum;
	}

	return 0;
}
더보기
#include <iostream>
using namespace std;


int main()
{
	// 1번 40 byte
	{
		/*#include <iostream> 
		using namespace std;
		int data[10]; 
		int main()
		{
			return 0;
		}*/
	}

	// 2번 24 + 10 + 40 
	{
		/*#include <iostream> 
		using namespace std;
		double data[3]; 8 24
		char vect[10];  1 10
		int dt[10];     4 40
		int main()
		{
			return 0;
		}*/
	}

	// 3번 800
	{
		/*#include <iostream> 
		using namespace std;
		struct Node 
		{
			int x;  4
			char t; 1 + 3;
		};
		Node vect[100]; 800
		int main()
		{
			return 0;
		}*/
	}

	// 4번 16
	{
		/*#include <iostream> 
		using namespace std;
		struct Node { int x; 	char* next; };
		Node vect;
		int main() { return 0; }*/
	}

	int input = 0;

	cin >> input;

	if (input == 1)
		cout << 40;
	if (input == 2)
		cout << 74;
	if (input == 3)
		cout << 800;
	if (input == 4)
		cout << 16;

	return 0;
}
더보기
#include <iostream>
using namespace std;

const int num = 5;
string str[5];
string MAPOM = "MAPOM";

void SwapChar()
{
	const int line1 = 1;
	const int line3 = 3;

	for (int i = 0; i < num; i++)
	{
		char temp = str[i][line1];
		str[i][line1] = str[i][line3];
		str[i][line3] = temp;
	}
}

void SearchSRT()
{
	for (int i = 0; i < num; i++)
	{
		if (MAPOM == str[i])
		{
			cout << "yes"; 
			return;
		}
			
	}

	cout << "no";
}

int main()
{
	
	for (int i = 0; i < num; i++)
	{
		cin >> str[i];
	}

	SwapChar();
	SearchSRT();
	
	return 0;
}
더보기
#include <iostream>
using namespace std;

const string HITSMUSIC = "HITSMUSIC";
string arr[100];

int main()
{
	int N;

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		cin >> arr[i];
	}

	int count = 0;

	for (int i = 0; i < N; i++)
	{
		for (int j = i + 1; j < N; j++)
		{
			string add = arr[i] + arr[j];
			if (HITSMUSIC == add)
				count++;
		}
	}

	return 0;
}

 

더보기
#include <iostream>
using namespace std;

int main()
{
	int cost[9] = { 1,2,3,3,5,1,0,1,3 };
	int busSize;
	int length;
	int sum = 0; 
	
	cin >> busSize;
	length = 9 - busSize + 1;

	for (int i = 0; i < busSize; i++)
	{
		sum += cost[i];
	}

	int min = INT_MAX;

	for (int i = 1; i < length; i++)
	{
		sum = sum - cost[i - 1] + cost[i + busSize - 1];
		if (min > sum)
			min = sum;
	}

	return 0;
}
더보기
#include <iostream>
using namespace std;

int main()
{
	int n;
	string str[20];

	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> str[i];
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (str[i].length() > str[j].length())
			{
				string temp = str[i];
				str[i] = str[j];
				str[j] = temp;
			}
			else if (str[i].length() == str[j].length())
			{
				if (str[i] > str[j])
				{
					string temp = str[i];
					str[i] = str[j];
					str[j] = temp;
				}
			}
		}
	}


	return 0;
}
더보기
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int P;
	int N;

	cin >> P >> N;

	for (int i = 0; i < N; i++)
	{
		int mult = P * 2;
		string str = to_string(mult);

		int front = 0;
		int tail = static_cast<int>(str.length() - 1);
		for (int j = 0; j < str.length(); j++)
		{
			if ((front + j) >= (tail - j))
			{
				P = stoi(str);
				break;
			}
				
			char temp = str[front + j];
			str[front + j] = str[tail - j];
			str[tail - j] = temp;
		}

	}


	return 0;
}
더보기
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int amount;
	vector<int> cost;

	cin >> amount;

	for (int i = 0; i < amount; i++)
	{
		int a;
		cin >> a;
		cost.push_back(a);
	}

	int startIndex = 0;
	int endIndex = 0;

	int max = -1;
	for (int i = 0; i < amount; i++)
	{
		int sum = 0;
		sum += cost[i];
		for (int j = i + 1; j < amount; j++)
		{
			sum += cost[j];

			if (sum < 0)
				break;

			if (sum > max)
			{
				max = sum;
				startIndex = i;
				endIndex = j;
			}
				
		}
	}

	return 0;
}

'개발공부 > 기본기 훈련' 카테고리의 다른 글

2주차 문제풀이  (0) 2024.05.30

 

\(\vec{v}\) = 방향 , 거리 (위치x)

 

\(\vec{d}\) = \(b-a\)

\( \lVert \vec{d} \rVert \) = \(\sqrt{a^2 + b^2}\) = .lenght()

\( \hat{\vec{d}}\) = \( \vec{d} \over \lVert \vec{d} \rVert \) = 1 = unit vector = .normalize()

 

 

 

 

\(\vec{a} \cdot \vec{b} \) = scalar = 내적

\(\vec{b}\) = unit vector

 

만약 두 벡터가 유닛벡터 = \(\theta\)

 

 

 

 

\(\vec{a} \times \vec{b}\) = vector = 외적

위 그림은 왼손 좌표계. 언리얼엔진과 dx11은 기본 왼손 좌표계이다.

 

\(\lVert \vec{a} \times \vec{b} \rVert \over 2 \) = 삼각형 넓이 

 

\( \vec{a} \cdot ( \vec{a} \times \vec{b}) \) = 0 = 90도

\( \vec{b} \cdot ( \vec{a} \times \vec{b}) \) = 0 = 90도

 

아이디어.

1.기본적으로 탐색은 dfs를 사용한다.
2.각 열에 시추관이 지날 때 마다 앞서 이미 조사한 석유블럭을 다시 조사하는 것을 막기 위해, 이미 조사한 블럭이라면 탐색하지 않고 Cash에 저장된 블럭의 수를 리턴하고 끝낸다.
3.Cash 이중배열 벡터를 만들고,  석유블럭 덩어리에 번호를 붙여 Size에 해당 덩어리의 크기를 저장한다.

예시

 

코드 Ver.1

#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> Cash(501,vector<int>(501, -1));
vector<int> Size(501, 0);
int sizeCount = 0;

int Greater(int a, int b)
{
	if (a >= b)
	{
		return a;
	}
	else if (b >= a)
	{
		return b;
	}
}

void Dfs(vector<vector<int>> land, int row, int column, int& oil, int size)
{
	int dy[] = { 0, 1, 0 , -1 };
	int dx[] = { 1, 0, -1 , 0 };

	for (int i = 0; i < 4; i++)
	{
		int nextY = row + dy[i];
		int nextX = column + dx[i];

		if (nextY < 0 || nextX < 0 || nextY >= land.size() || nextX >= land[0].size() || Cash[nextY][nextX] != -1)
			continue;

		if (land[nextY][nextX] == 1 && Cash[nextY][nextX] == -1)
		{
			oil++;
			Cash[nextY][nextX] = size;
			Dfs(land, nextY, nextX, oil, size);
		}
	}
}

int solution(vector<vector<int>> land)
{
	int row = land.size();
	int column = land[0].size();

	int result = 0;
	int oil = 0;
	int answer = 0;


	for (int i = 0; i < column; i++)
	{
		vector<bool> visited(250, false);
		vector<int> v;//디버깅용

 		for (int j = 0; j < row; j++)
		{
			if (land[j][i] == 1 && Cash[j][i] == -1)//탐색시작
			{
				visited[sizeCount] = true;

				oil++;
				Cash[j][i] = sizeCount;
				Dfs(land,j,i,oil, sizeCount);

				Size[sizeCount] = oil;
				sizeCount++;
				
			}
			else if (land[j][i] == 1 && Cash[j][i] != -1 && visited[Cash[j][i]] == false)
			{
				visited[Cash[j][i]] = true;
				oil = Size[Cash[j][i]];
			}

			result += oil;
			v.push_back(oil);//디버깅용
			oil = 0; 
		}

		answer = Greater(answer, result);
		result = 0;
	}

    return answer;
}

 

 

결과.

 

 마지막 테스트를 실패하고 모든 테스트 케이스에서 시간 초과가 걸려버렸다.

 

 마지막 테스트 실패는 그렇다 치더라도 모든 테스트에서 시간초과가 나올줄은 몰랐다. 접근은 괜찮았다고 생각 했는데....

 

 일단 박제 해놓고 다시 해 봐야겠다.

 

 

  

+ Recent posts