알고리즘 문제풀이/swea
[swea 5648] 원자 소멸 시뮬레이션
홍시홍
2020. 4. 22. 00:42
분류 : 구현
요구사항
원자는 이동간에 만나면 파괴된다. 파괴된 원자의 에너지 총량 구하기
풀이
1. 범위의 명확성을 위해서 +1000한다
2. 0.5 구간에서 만날 수 없으니깐 *2를 한다.
3. 이동한다 범위 넘어서면 더이상 할 필요없이 alive 0으로 설정
4. 이동한 것들 중 2개 이상겹친다면 원자 삭제
41개에서 오류가 났는데 이동 확인에서
if (atom[i].alive == 0) continue; 를 확인안해서 해멧다..
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
struct go {
int x;
int y;
int dir;
int power;
int alive;
int index;
};
go atom[1100];
int dr[4] = { 0,0,-1,1 };
int dc[4] = { 1,-1,0,0 };
int n;
int map[4400][4400];
int ans = 0;
bool check() {
for (int i = 0; i < n; i++) {
if (atom[i].alive == 1) return false;
}
return true;
}
void solve() {
int cnt = 0;
while (true) {
if (check()==true) break;
for (int i = 0; i < n; i++) {
if (atom[i].alive == 0) continue;
atom[i].x = atom[i].x + dr[atom[i].dir];
atom[i].y = atom[i].y + dc[atom[i].dir];
if (atom[i].x < 0 || atom[i].y < 0 || atom[i].x >4000 || atom[i].y>4000) {
atom[i].alive = 0;
continue;
}
int x = atom[i].x;
int y = atom[i].y;
map[x][y]++;
}
//이동한것들 검사
for (int i = 0; i < n; i++) {
int x = atom[i].x;
int y = atom[i].y;
if (atom[i].alive == 0) continue;
if (map[x][y] == 0) continue;
if (map[x][y] == 1) {
map[x][y]--;
continue;
}
if (map[x][y] >= 2) {
for (int j = 0; j < n; j++) {
if (atom[j].alive == 0) continue;
int nx = atom[j].x;
int ny = atom[j].y;
if (x == nx && ny == y) {
ans += atom[j].power;
atom[j].alive = 0;
}
}
atom[i].alive = 0;
map[x][y]= 0;
}
}
}
}
int main() {
int t;
scanf("%d", &t);
for (int tc = 1; tc <= t; tc++) {
ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x, y, dir, power;
scanf("%d%d%d%d", &x, &y, &dir, &power);
atom[i].x = (x+1000)*2;
atom[i].y = (y+1000)*2;
atom[i].dir = dir;
atom[i].power = power;
atom[i].alive = 1;
atom[i].index = i;
}
solve();
if(n==0 || n==1) printf("#%d %d\n", tc, 0);
printf("#%d %d\n", tc, ans);
}
return 0;
}