반응형
https://www.acmicpc.net/problem/17144
#include <iostream>
#include <string>
#include<vector>
#include <stdio.h>
#include <string>
#include<algorithm>
#include<cstring>
using namespace std;
int map[51][51];
int temp[51][51];
int r, c;
int t;
int res;
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { -1, 1, 0, 0 };
int ccw[4] = { 2, 0, 3, 1 }; // 반시계(counter-clockwise)
int cw[4] = { 2, 1, 3, 0 }; // 시계(clockwise)
int cnt;
vector <pair<int, int>> v;
void dust(int time) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cnt = 0;
if (map[i][j] > 0) {
for (int d = 0; d < 4; d++) {
int cx = i + dx[d];
int cy = j + dy[d];
if (map[cx][cy] != -1 && cx >= 0 && cx < r && cy >= 0 && cy < c) {
temp[cx][cy] += (map[i][j] / 5);
cnt++;
}
}
int a = (map[i][j] - ((map[i][j] / 5) * cnt));
temp[i][j] += a;
}
}
}//먼지 확산
}
void circulate(int cx , int cy, int dir[4]) {
int y = cy;
int x = cx+1;
map[y][x] = 0;
for (int k = 0; k < 4; k++) {
while (1) {
int nx = x + dx[dir[k]];
int ny = y + dy[dir[k]];
if (nx < 0 || ny < 0 || nx >= c || ny >= r) {
break;
}
if (cx == nx && cy == ny) {
break;
}
map[ny][nx] = temp[y][x];
y = ny;
x = nx;
}
}
}
int main() {
int num;
scanf_s("%d%d%d", &r,&c,&t);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf_s("%d", &map[i][j]);
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (map[i][j] == -1) {
v.push_back(make_pair(i, j));
temp[i][j] = -1;
}
}
}
int upy = v[0].first;
int upx = v[0].second;
int downy = v[1].first;
int downx = v[1].second;
for (int i = 0; i < t; i++) {
cnt = 0;
dust(t);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
map[i][j] = temp[i][j];
}
}
circulate(upx, upy, ccw);
circulate(downx, downy,cw);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
// map[i][j] = temp[i][j];
if ((j == upx && i == upy)) {
temp[i][j] = -1;
}
else if ((j == downx && i == downy)) {
temp[i][j] = -1;
}
else {
temp[i][j] = 0;
}
}
}
}
res = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (!((j == upx && i == upy) || (j == downx && i == downy))) {
res = res+ map[i][j];
}
}
}
printf("%d", res);
return 0;
}
코드 정리하기
반응형
'CS > 알고리즘' 카테고리의 다른 글
백준 1600 말이 되고픈 원숭이 (bfs , 사선) (0) | 2019.10.27 |
---|---|
백준 1260 bfs 와 dfs (0) | 2019.10.27 |
백준 1726 로봇 (0) | 2019.10.03 |
백준 5427 불 (bfs) (0) | 2019.10.01 |
지금까지 푼 알고리즘 문제(깃헙) (0) | 2018.11.19 |