BOJ 17143
λ¬Έμ
λμμ μ±κ³΅
1 μ΄ 512 MB 38078 11153 6336 25.940% λ¬Έμ
λμμμ΄ μμ΄ λμλ₯Ό νλ κ³³μ ν¬κΈ°κ° R×CμΈ κ²©μνμΌλ‘ λνλΌ μ μλ€. 격μνμ κ° μΉΈμ (r, c)λ‘ λνλΌ μ μλ€. rμ ν, cλ μ΄μ΄κ³ , (R, C)λ μλ κ·Έλ¦Όμμ κ°μ₯ μ€λ₯Έμͺ½ μλμ μλ μΉΈμ΄λ€. μΉΈμλ μμ΄κ° μ΅λ ν λ§λ¦¬ λ€μ΄μμ μ μλ€. μμ΄λ ν¬κΈ°μ μλλ₯Ό κ°μ§κ³ μλ€.
λμμμ μ²μμ 1λ² μ΄μ ν μΉΈ μΌμͺ½μ μλ€. λ€μμ 1μ΄ λμ μΌμ΄λλ μΌμ΄λ©°, μλ μ ν μμλλ‘ μΌμ΄λλ€. λμμμ κ°μ₯ μ€λ₯Έμͺ½ μ΄μ μ€λ₯Έμͺ½ μΉΈμ μ΄λνλ©΄ μ΄λμ λ©μΆλ€.
- λμμμ΄ μ€λ₯Έμͺ½μΌλ‘ ν μΉΈ μ΄λνλ€.
- λμμμ΄ μλ μ΄μ μλ μμ΄ μ€μμ λ κ³Ό μ μΌ κ°κΉμ΄ μμ΄λ₯Ό μ‘λλ€. μμ΄λ₯Ό μ‘μΌλ©΄ 격μνμμ μ‘μ μμ΄κ° μ¬λΌμ§λ€.
- μμ΄κ° μ΄λνλ€.
μμ΄λ μ λ ₯μΌλ‘ μ£Όμ΄μ§ μλλ‘ μ΄λνκ³ , μλμ λ¨μλ μΉΈ/μ΄μ΄λ€. μμ΄κ° μ΄λνλ €κ³ νλ μΉΈμ΄ κ²©μνμ κ²½κ³λ₯Ό λλ κ²½μ°μλ λ°©ν₯μ λ°λλ‘ λ°κΏμ μλ ₯μ μ μ§νμ±λ‘ μ΄λνλ€.
μΌμͺ½ κ·Έλ¦Όμ μνμμ 1μ΄κ° μ§λλ©΄ μ€λ₯Έμͺ½ μνκ° λλ€. μμ΄κ° λ³΄κ³ μλ λ°©ν₯μ΄ μλμ λ°©ν₯, μΌμͺ½ μλμ μ ν μ μλ μλ ₯μ΄λ€. μΌμͺ½ μμ μμ΄λ₯Ό ꡬλΆνκΈ° μν΄ λ¬Έμλ₯Ό μ μλ€.
μμ΄κ° μ΄λμ λ§μΉ νμ ν μΉΈμ μμ΄κ° λ λ§λ¦¬ μ΄μ μμ μ μλ€. μ΄λλ ν¬κΈ°κ° κ°μ₯ ν° μμ΄κ° λλ¨Έμ§ μμ΄λ₯Ό λͺ¨λ μ‘μλ¨Ήλλ€.
λμμμ΄ μμ΄ λμλ₯Ό νλ 격μνμ μνκ° μ£Όμ΄μ‘μ λ, λμμμ΄ μ‘μ μμ΄ ν¬κΈ°μ ν©μ ꡬν΄λ³΄μ.
μ λ ₯
첫째 μ€μ 격μνμ ν¬κΈ° R, Cμ μμ΄μ μ Mμ΄ μ£Όμ΄μ§λ€. (2 ≤ R, C ≤ 100, 0 ≤ M ≤ R×C)
λμ§Έ μ€λΆν° Mκ°μ μ€μ μμ΄μ μ λ³΄κ° μ£Όμ΄μ§λ€. μμ΄μ μ 보λ λ€μ― μ μ r, c, s, d, z (1 ≤ r ≤ R, 1 ≤ c ≤ C, 0 ≤ s ≤ 1000, 1 ≤ d ≤ 4, 1 ≤ z ≤ 10000) λ‘ μ΄λ£¨μ΄μ Έ μλ€. (r, c)λ μμ΄μ μμΉ, sλ μλ ₯, dλ μ΄λ λ°©ν₯, zλ ν¬κΈ°μ΄λ€. dκ° 1μΈ κ²½μ°λ μ, 2μΈ κ²½μ°λ μλ, 3μΈ κ²½μ°λ μ€λ₯Έμͺ½, 4μΈ κ²½μ°λ μΌμͺ½μ μλ―Ένλ€.
λ μμ΄κ° κ°μ ν¬κΈ°λ₯Ό κ°λ κ²½μ°λ μκ³ , νλμ μΉΈμ λ μ΄μμ μμ΄κ° μλ κ²½μ°λ μλ€.
μΆλ ₯
λμμμ΄ μ‘μ μμ΄ ν¬κΈ°μ ν©μ μΆλ ₯νλ€.
μμ μ λ ₯ 1 볡μ¬
4 6 8 4 1 3 3 8 1 3 5 2 9 2 4 8 4 1 4 5 0 1 4 3 3 1 2 7 1 5 8 4 3 3 6 2 1 2 2 2 2 3 5
μμ μΆλ ₯ 1 볡μ¬
22
κ° μΉΈμ μΌμͺ½ μλμ μ ν μλ μλ ₯, μ€λ₯Έμͺ½ μλλ ν¬κΈ°, μΌμͺ½ μλ μμ΄λ₯Ό ꡬλΆνκΈ° μν λ¬Έμμ΄λ€. μ€λ₯Έμͺ½ μμ β€οΈλ λμμμ΄ μ‘μ λ¬Όκ³ κΈ° νμμ΄λ€.
μ΄κΈ° μν
1μ΄
2μ΄ (Eλ² μμ΄λ Bλ²μκ² λ¨Ήνλ€)
3μ΄
4μ΄
5μ΄
6μ΄
νμ΄
빑ꡬνμν€λ©΄μ μκ°μ΄κ³Ό μ λνλ λ¬Έμ μ΄λ€.
νμ΄μ체λ κ°λ¨νλ€. κ·Έλ₯ λ¬Έμ μμ μ€λͺ νλ λλ‘ μ μ§μ£Όλ©΄ λλ€.
λ€λ§ μ λ ₯ λ°°μ΄μ μ¬μ΄μ¦κ° 100 * 100μ΄κ³ , μμ΄μ μλ ₯μ΄ 1000μ΄λΌ μμ΄λ₯Ό μ΄λμν¬λ μμ΄κ° λͺ¨λ μΉΈμ μλ€λ κ°μ νμ
μμ΄μ κ°μ * μ΄ * μμ΄μ μλ ₯μΌλ‘ N*M^2*SλΌλ κ΄΄λν μκ°μ΄ λμ€κ² λλ€.
κ·Έλ¬λ―λ‘ μμ΄λ₯Ό μ΄λνλ λμμμ μ΅λν μκ°μ λΉΌμ£Όμ΄μΌ νλ€.
void move_shark(){
vector<shark>move[102][102];
vector<pair<int,int>>si;
int vis[102][102] = {0};
for(auto point : sh_point){
if(board[point.first][point.second].empty()) continue;
shark cur = board[point.first][point.second][0];
int curx = point.first, cury = point.second;
board[point.first][point.second].pop_back();
int ns = cur.s;
//for(int k=0; k<board[point.first][point.second][0].s; k++){
while(ns){
int s;
if(cur.d == 1)
s = curx-1;
else if(cur.d == 2)
s = R-curx;
else if(cur.d == 3)
s = C-cury;
else if(cur.d == 4)
s = cury-1;
if(ns < s)
s = ns;
ns -= s;
int nx = curx + dx[cur.d-1]*s;
int ny = cury + dy[cur.d-1]*s;
// if((nx == 0 || nx == R || ny == 0 || ny == C)){
// if(cur.d == 1)
// cur.d = 2;
// else if(cur.d == 2)
// cur.d = 1;
// if(cur.d == 3)
// cur.d = 4;
// else if(cur.d == 4)
// cur.d = 3;
// }
if((nx == 1 || nx == R) &&(cur.d == 1 || cur.d ==2)){
if(cur.d == 1)
cur.d = 2;
else if(cur.d == 2)
cur.d = 1;
}else if((ny == 1 || ny == C) && (cur.d == 3 || cur.d == 4) ){
if(cur.d == 3)
cur.d = 4;
else if(cur.d == 4)
cur.d = 3;
}
curx = nx;
cury = ny;
}
if(move[curx][cury].empty())
move[curx][cury].push_back(cur);
else{
shark com = move[curx][cury][0];
move[curx][cury].pop_back();
if(com.z > cur.z)
move[curx][cury].push_back(com);
else
move[curx][cury].push_back(cur);
}
if(vis[curx][cury] == 0){
vis[curx][cury] = 1;
si.push_back({curx,cury});
}
}
for(auto e : si){
if(move[e.first][e.second].empty()) continue;
board[e.first][e.second].push_back(move[e.first][e.second][0]);
}
sh_point = si;
return;
}
μ΄ λΆλΆμ΄ μμ΄λ₯Ό μμ§μ΄κ³ , λ¨Ήλ κ³Όμ κΉμ§ ν μ½λμ΄λ€.
μ¬κΈ°μ μ€μν λΆλΆμ
while(ns){
int s;
if(cur.d == 1)
s = curx-1;
else if(cur.d == 2)
s = R-curx;
else if(cur.d == 3)
s = C-cury;
else if(cur.d == 4)
s = cury-1;
if(ns < s)
s = ns;
ns -= s;
int nx = curx + dx[cur.d-1]*s;
int ny = cury + dy[cur.d-1]*s;
// if((nx == 0 || nx == R || ny == 0 || ny == C)){
// if(cur.d == 1)
// cur.d = 2;
// else if(cur.d == 2)
// cur.d = 1;
// if(cur.d == 3)
// cur.d = 4;
// else if(cur.d == 4)
// cur.d = 3;
// }
if((nx == 1 || nx == R) &&(cur.d == 1 || cur.d ==2)){
if(cur.d == 1)
cur.d = 2;
else if(cur.d == 2)
cur.d = 1;
}else if((ny == 1 || ny == C) && (cur.d == 3 || cur.d == 4) ){
if(cur.d == 3)
cur.d = 4;
else if(cur.d == 4)
cur.d = 3;
}
curx = nx;
cury = ny;
}
μ΄ μμ΄λ₯Ό μμ§μ΄λ λΆλΆμ΄λ€.
μμ΄κ° λ°©ν₯μ λ°κΎΈλ ꡬκ°μ 0λ²μ§Έ, Rλ²μ§Έ νμ μμλ. 0λ²μ§Έ, Cλ²μ§Έ μ΄μ μλ κ²½μ°μ΄λ€.
κ·Έλ¬λ―λ‘ μμ΄κ° 3λ²νμ μλ€ κ°μ νκ³ R == 7μ΄λΌ κ°μ νμ.
μμ΄κ° λ°©ν₯μ΄ λ°λμ§ μκ³ μλ‘ μμ§μ΄λ κ²½μ°λ μ΅λ 3(μμ΄μ νμ μμΉκ°) λ§νΌ μμ§μΌ μ μλ€.
μμ΄κ° λ°©ν₯μ΄ λ°λμ§ μκ³ μλλ‘ μμ§μ΄λ κ²½μ°λ μ΅λ 4(R - μμ΄μ νμ μμΉκ°) λ§νΌ μμ§μΌ μ μλ€.
μ΄λ₯Ό μ΄μ©ν΄μ μμ΄λ₯Ό νλ² μμ§μΌλ μ΅λν λ§μ΄ μμ§μ¬μ£Όλ©΄ λλ€.
ν¬μΈνΈ
μμ΄κ° 0λ²μ΄λ κ°μ₯μ리μ μλ κ²½μ°μλ, λ°©ν₯μ΄ κ³μ λ°λμ΄λ λλ€.
λ¬Όλ‘ μ½κ°μ μκ°μ΄ λ λ€ μλ μμ§λ§, λ°©ν₯μ΄ λ°λμ¬λ μ΄μ²νΌ 1μ΄ νμ λ°©ν₯κ³Ό μμΉλ κ°κΈ° λλ¬Έμ΄λ€.
μ 체 μ½λ
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int R,C,M,dx[4]={-1,1,0,0},dy[4]={0,0,1,-1}, ans;
struct shark{
int s,d,z=0;
};
vector<pair<int,int>>sh_point;
vector<shark>board[102][102];
void catch_shark(int idx){
for(int i=1; i<=R; i++){
if(board[i][idx].empty()) continue;
ans += board[i][idx][0].z;
board[i][idx].pop_back();
return;
}
}
void move_shark(){
vector<shark>move[102][102];
vector<pair<int,int>>si;
int vis[102][102] = {0};
for(auto point : sh_point){
if(board[point.first][point.second].empty()) continue;
shark cur = board[point.first][point.second][0];
int curx = point.first, cury = point.second;
board[point.first][point.second].pop_back();
int ns = cur.s;
//for(int k=0; k<board[point.first][point.second][0].s; k++){
while(ns){
int s;
if(cur.d == 1)
s = curx-1;
else if(cur.d == 2)
s = R-curx;
else if(cur.d == 3)
s = C-cury;
else if(cur.d == 4)
s = cury-1;
if(ns < s)
s = ns;
ns -= s;
int nx = curx + dx[cur.d-1]*s;
int ny = cury + dy[cur.d-1]*s;
// if((nx == 0 || nx == R || ny == 0 || ny == C)){
// if(cur.d == 1)
// cur.d = 2;
// else if(cur.d == 2)
// cur.d = 1;
// if(cur.d == 3)
// cur.d = 4;
// else if(cur.d == 4)
// cur.d = 3;
// }
if((nx == 1 || nx == R) &&(cur.d == 1 || cur.d ==2)){
if(cur.d == 1)
cur.d = 2;
else if(cur.d == 2)
cur.d = 1;
}else if((ny == 1 || ny == C) && (cur.d == 3 || cur.d == 4) ){
if(cur.d == 3)
cur.d = 4;
else if(cur.d == 4)
cur.d = 3;
}
curx = nx;
cury = ny;
}
if(move[curx][cury].empty())
move[curx][cury].push_back(cur);
else{
shark com = move[curx][cury][0];
move[curx][cury].pop_back();
if(com.z > cur.z)
move[curx][cury].push_back(com);
else
move[curx][cury].push_back(cur);
}
if(vis[curx][cury] == 0){
vis[curx][cury] = 1;
si.push_back({curx,cury});
}
}
for(auto e : si){
if(move[e.first][e.second].empty()) continue;
board[e.first][e.second].push_back(move[e.first][e.second][0]);
}
sh_point = si;
return;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>R>>C>>M;
//input
for(int i=0; i<M; i++){
shark ns;
int x, y;
cin>>x>>y>>ns.s>>ns.d>>ns.z;
board[x][y].push_back(ns);
sh_point.push_back({x,y});
}
for(int st=1; st<=C; st++){
catch_shark(st);
move_shark();
}
cout<<ans;
}