Last active
August 31, 2018 09:48
-
-
Save goodGid/9bcdf74acf7cd2325a276d96f048baca to your computer and use it in GitHub Desktop.
정답 코드 보기 --> https://goodgid.github.io/SWEA-2382/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<vector> | |
#include<cstring> | |
#include<algorithm> | |
#include<cmath> | |
using namespace std; | |
struct Bug { | |
int x; | |
int y; | |
int cnt; | |
int dir; | |
Bug(int x,int y,int cnt, int dir):x(x),y(y),cnt(cnt),dir(dir){} | |
}; | |
int map[101][101]; | |
int change_dir[] = {0,2,1,4,3}; | |
pair<int,int> max_bug[101][101]; | |
int main(){ | |
int TC; | |
cin >> TC; | |
for (int tc=1; tc<=TC; tc++) { | |
int n,m,k; // 셀의 개수 N, 격리 시간 M, 미생물 군집의 개수 K | |
cin >> n >> m >>k ; | |
memset(max_bug,0,sizeof(max_bug)); | |
vector<Bug> bug; | |
for(int i=0; i<k; i++){ | |
int a,b,c,d; | |
cin >> a >> b >> c >> d; | |
bug.push_back(Bug(a,b,c,d)); | |
} | |
while (m--) { | |
memset(map,-1,sizeof(map)); | |
// Bug Move | |
for(int i=0; i<k; i++){ | |
if(bug[i].cnt == 0) continue; | |
if(bug[i].dir == 1) bug[i].x -= 1; | |
else if(bug[i].dir == 2) bug[i].x += 1; | |
else if(bug[i].dir == 3) bug[i].y -= 1; | |
else if(bug[i].dir == 4) bug[i].y += 1; | |
// 가장 자리일 경우 | |
if( bug[i].x == 0 || bug[i].x == n-1 || bug[i].y == 0 || bug[i].y == n-1){ | |
bug[i].cnt /= 2; | |
bug[i].dir = change_dir[bug[i].dir]; | |
map[ bug[i].x ][ bug[i].y ] = i; | |
} | |
// 이동한 장소에 이미 있을 시 | |
else if(map[ bug[i].x ][ bug[i].y ] != -1){ | |
// 해당 좌표에 존재하는 Bug의 Index | |
int pre_bug_idx = map[ bug[i].x ][ bug[i].y ]; | |
if( max_bug[ bug[i].x ][ bug[i].y ].first > bug[i].cnt){ | |
bug[pre_bug_idx].cnt += bug[i].cnt; | |
bug[i].cnt = 0; | |
} | |
else{ | |
bug[i].cnt += bug[pre_bug_idx].cnt; | |
bug[pre_bug_idx].cnt = 0; | |
map[ bug[i].x ][ bug[i].y ] = i; // map에 새로운 bug index로 초기화 | |
max_bug[ bug[i].x ][ bug[i].y ] = {bug[i].cnt, bug[i].dir}; | |
} | |
} // end of else if | |
else{ | |
map[ bug[i].x ][ bug[i].y ]= i; | |
max_bug[ bug[i].x ][ bug[i].y ] = {bug[i].cnt, bug[i].dir}; | |
} | |
} // end of for i | |
} // end of while | |
int ans = 0; | |
for(int i=0; i<k; i++){ | |
ans += bug[i].cnt; | |
} | |
printf("#%d %d\n",tc, ans); | |
} // end of for tc | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment