[정답 코드] c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef struct {
int speed; //속도
int d; //방향 1:상,2:하,3:우,4:좌
int z; //크기
}info; //상어 정보
int R, C; //R - 세로y, C - 가로x
int M; //초기 상어 수
vector<info> map[101][101]; //어장~!~!
vector<info> temp[101][101];
int answer = 0; //잡은 물고기의 합.
void print() {
cout << "map\n";
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
if (map[i][j].size() == 0)continue;
cout << "위치:" << i << " " << j << "\n";
cout << "s,d,z:" << map[i][j].front().speed << " "<< map[i][j].front().d << " " << map[i][j].front().z << "\n";
}
}
}
void print_temp() {
cout << "temp_map\n";
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
if (temp[i][j].size() == 0)continue;
for(int k=0; i < map[i][j].size(); k++) {
cout << "위치:" << i << " " << j << "\n";
cout << "s,d,z:" << temp[i][j][k].speed << " " << temp[i][j][k].d << " " << temp[i][j][k].z << "\n";
}
}
}
}
void move() {
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
temp[i][j].clear();
}
}
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
if (map[i][j].size() == 0)continue;
int c_speed, c_d, c_z;
//현재 상어 정보
c_speed = map[i][j].front().speed;
c_d = map[i][j].front().d;
c_z = map[i][j].front().z;
//상어 움직일거니까 map에서 지워
map[i][j].pop_back();
int n_d = c_d; //바뀐 방향
int ny, nx;
ny = i; nx = j;
for (int speed = 0; speed < c_speed; speed++) {
if (n_d == 1) {//위
if (ny - 1 < 1) {//반대방향으로 이동
n_d = 2;
ny += 1;
}
//위로 이동
else ny -= 1;
}
else if (n_d == 2) {//아래
if (ny + 1 > R) {//반대방향으로 이동
n_d = 1;
ny -= 1;
}
//아래로 이동
else ny += 1;
}
else if (n_d == 3) {//오른쪽
if (nx + 1 > C) {//반대방향으로 이동
n_d = 4;
nx -= 1;
}
//right로 이동
else nx += 1;
}
else if (n_d == 4) {//왼쪽
if (nx - 1 < 1) {//반대방향으로 이동
n_d = 3;
nx += 1;
}
//left 이동
else nx -= 1;
}
}
temp[ny][nx].push_back({ c_speed,n_d,c_z });
}
}
}
void eat() {
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
if (temp[i][j].size() == 0)continue;
for (int k = 0; k < temp[i][j].size();k++) {
if (map[i][j].size() != 0) {
//다른 상어가 자리 잡았으
if (map[i][j].front().z <= temp[i][j][k].z) {
//cout << "왜!" << "map:" << map[i][j].front().z<< "temp:"<< temp[i][j].back().z << "\n";
map[i][j].pop_back();
map[i][j].push_back(temp[i][j][k]);
//temp[i][j].pop_back();
}
}
else {
map[i][j].push_back(temp[i][j][k]);
}
//cout << "#" << i << "," << j <<", "<<k<<"\n";
//print();
//print_temp();
}
temp[i][j].clear();
}
}
}
void fishing(int p) {
for (int i = 1; i <= R; i++) {
if (map[i][p].size() != 0) {
//잡을 수 있는 물고기가 있다.
answer += map[i][p].front().z;
map[i][p].pop_back();
break;
}
}
}
void solve() {
//바로 오른쪽으로 한 칸 이동시킴
for (int i = 1; i <= C; i++) {
fishing(i); //낚시왕이 물고기 잡는다.
//cout << "where\n";
move(); //상어가 움직인다.
//cout << "where\n";
eat(); //더 큰 상어가 작은 상어 먹는다.
//cout << "#" << i << "\n";
//print();
}
}
int main() {
cin >> R >> C >> M;
//map.clear();
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
map[i][j].clear();
}
}
while (M--) {
int ty, tx, ts, td, tz;
cin >> ty >> tx >> ts >> td >> tz;
map[ty][tx].push_back({ ts,td,tz });
}
//print();
if (M == 0) {
cout << 0 << "\n";
}
else {
solve();
cout << answer << "\n";
}
return 0;
}
'알고리즘 > 백준' 카테고리의 다른 글
BOJ 18406 : 럭키 스트레이트 (0) | 2021.05.02 |
---|---|
BOJ 16234 : 인구이동 (0) | 2021.04.19 |
BOJ 15686 : 치킨 배달 (0) | 2021.04.18 |
BOJ 14890 : 경사로 (0) | 2021.04.15 |
BOJ 14502 : 연구소 (0) | 2021.04.14 |
[정답 코드] c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef struct {
int speed; //속도
int d; //방향 1:상,2:하,3:우,4:좌
int z; //크기
}info; //상어 정보
int R, C; //R - 세로y, C - 가로x
int M; //초기 상어 수
vector<info> map[101][101]; //어장~!~!
vector<info> temp[101][101];
int answer = 0; //잡은 물고기의 합.
void print() {
cout << "map\n";
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
if (map[i][j].size() == 0)continue;
cout << "위치:" << i << " " << j << "\n";
cout << "s,d,z:" << map[i][j].front().speed << " "<< map[i][j].front().d << " " << map[i][j].front().z << "\n";
}
}
}
void print_temp() {
cout << "temp_map\n";
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
if (temp[i][j].size() == 0)continue;
for(int k=0; i < map[i][j].size(); k++) {
cout << "위치:" << i << " " << j << "\n";
cout << "s,d,z:" << temp[i][j][k].speed << " " << temp[i][j][k].d << " " << temp[i][j][k].z << "\n";
}
}
}
}
void move() {
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
temp[i][j].clear();
}
}
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
if (map[i][j].size() == 0)continue;
int c_speed, c_d, c_z;
//현재 상어 정보
c_speed = map[i][j].front().speed;
c_d = map[i][j].front().d;
c_z = map[i][j].front().z;
//상어 움직일거니까 map에서 지워
map[i][j].pop_back();
int n_d = c_d; //바뀐 방향
int ny, nx;
ny = i; nx = j;
for (int speed = 0; speed < c_speed; speed++) {
if (n_d == 1) {//위
if (ny - 1 < 1) {//반대방향으로 이동
n_d = 2;
ny += 1;
}
//위로 이동
else ny -= 1;
}
else if (n_d == 2) {//아래
if (ny + 1 > R) {//반대방향으로 이동
n_d = 1;
ny -= 1;
}
//아래로 이동
else ny += 1;
}
else if (n_d == 3) {//오른쪽
if (nx + 1 > C) {//반대방향으로 이동
n_d = 4;
nx -= 1;
}
//right로 이동
else nx += 1;
}
else if (n_d == 4) {//왼쪽
if (nx - 1 < 1) {//반대방향으로 이동
n_d = 3;
nx += 1;
}
//left 이동
else nx -= 1;
}
}
temp[ny][nx].push_back({ c_speed,n_d,c_z });
}
}
}
void eat() {
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
if (temp[i][j].size() == 0)continue;
for (int k = 0; k < temp[i][j].size();k++) {
if (map[i][j].size() != 0) {
//다른 상어가 자리 잡았으
if (map[i][j].front().z <= temp[i][j][k].z) {
//cout << "왜!" << "map:" << map[i][j].front().z<< "temp:"<< temp[i][j].back().z << "\n";
map[i][j].pop_back();
map[i][j].push_back(temp[i][j][k]);
//temp[i][j].pop_back();
}
}
else {
map[i][j].push_back(temp[i][j][k]);
}
//cout << "#" << i << "," << j <<", "<<k<<"\n";
//print();
//print_temp();
}
temp[i][j].clear();
}
}
}
void fishing(int p) {
for (int i = 1; i <= R; i++) {
if (map[i][p].size() != 0) {
//잡을 수 있는 물고기가 있다.
answer += map[i][p].front().z;
map[i][p].pop_back();
break;
}
}
}
void solve() {
//바로 오른쪽으로 한 칸 이동시킴
for (int i = 1; i <= C; i++) {
fishing(i); //낚시왕이 물고기 잡는다.
//cout << "where\n";
move(); //상어가 움직인다.
//cout << "where\n";
eat(); //더 큰 상어가 작은 상어 먹는다.
//cout << "#" << i << "\n";
//print();
}
}
int main() {
cin >> R >> C >> M;
//map.clear();
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
map[i][j].clear();
}
}
while (M--) {
int ty, tx, ts, td, tz;
cin >> ty >> tx >> ts >> td >> tz;
map[ty][tx].push_back({ ts,td,tz });
}
//print();
if (M == 0) {
cout << 0 << "\n";
}
else {
solve();
cout << answer << "\n";
}
return 0;
}
'알고리즘 > 백준' 카테고리의 다른 글
BOJ 18406 : 럭키 스트레이트 (0) | 2021.05.02 |
---|---|
BOJ 16234 : 인구이동 (0) | 2021.04.19 |
BOJ 15686 : 치킨 배달 (0) | 2021.04.18 |
BOJ 14890 : 경사로 (0) | 2021.04.15 |
BOJ 14502 : 연구소 (0) | 2021.04.14 |