개발자 톡
[cpp] 테케 거의다 틀립니다
- 등록일
- 2024-08-13 21:07:01
- 조회수
- 168
- 작성자
- mun9769
```
#include<iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cassert>
using namespace std;
bool chk[10'030];
bool seated[10'030];
pair<int, int> pos[10'030];
int n, m, q;
int id;
string cmd;
vector<pair<int, int>> seq;
vector<pair<int, int>>::iterator cur;
int __b[22][22];
vector<pair<int, int>> d = {
{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {0, 0}
};
void OutId(int id)
{
if(seated[id] == false and !chk[id]) // 들어온적이 없어.
{
cout << id << " didn't eat lunch.\n";
return;
}
if(seated[id] == false and chk[id]) // 이미 먹고 떠났어.
{
cout << id << " already left seat.\n";
return;
}
assert(cur != seq.begin());
seated[id] = false;
chk[id] = true;
auto [i, j] = pos[id];
cur--;
cout << id << " leaves from the seat (" << i << ", " << j << ").\n";
}
void InId(int id)
{
if(seated[id])
{
cout << id << " already seated.\n";
return;
}
if(chk[id])
{
cout << id << " already ate lunch.\n";
return;
}
if(cur == seq.end())
{
cout << "There are no more seats.\n";
return;
}
auto [i, j] = *cur;
cur++;
pos[id] = {i, j};
seated[id] = true;
cout << id << " gets the seat (" << i << ", " << j << ").\n";
}
double norm(double i, double j, double x, double y)
{
return sqrt(pow(i-x, 2) + pow(j-y, 2));
}
void mkSequence()
{
seq.push_back({1, 1});
__b[1][1] = 1;
__b[1][2] = 1;
__b[2][1] = 1;
while(true)
{
double mx = -1;
int ci, cj;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(__b[i][j] > 0)
continue;
auto it = min_element(seq.begin(), seq.end(), // <algorithm>에 있다는 것
[i, j](auto &p1, auto &p2) { // structed binding이 안된다는 것
auto [x1, y1] = p1;
auto [x2, y2] = p2;
return norm(i, j, x1, y1) < norm(i, j, x2, y2);});
double Dxy = norm(i, j, it->first, it->second);
if(mx < Dxy)
{
mx = Dxy;
ci = i, cj = j;
}
}
}
if(mx == -1)
break;
seq.push_back({ci, cj});
for(auto &[di, dj]: d)
{
__b[ci + di][cj + dj] = 1;
}
}
}
int main(int argc, char** argv)
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> q;
mkSequence();
cur = seq.begin();
while(q-- > 0)
{
cin >> cmd;
cin >> id;
if(cmd == "Out") OutId(id);
else InId(id);
}
return 0;
}
```
bool chk[10'030]; // {id}가 다 먹고 나가면 true
bool seated[10'030]; // {id}가 앉았다면 true
pair<int, int> pos[10'030]; // {id}가 앉은 자리의 위치
int n, m, q;
int id;
string cmd;
vector<pair<int, int>> seq; // 높은 안전도로 정렬한 배열 (ex. {1, 1}, {4, 4}, {1, 4}, {4, 1}, {2, 2}, {3, 3} )
vector<pair<int, int>>::iterator cur; {id}가 들어오면 앉을 자리 위치
어디에서 문제가 생기는 걸까요?