본문 바로가기
백준 알고리즘/구현

무선 충전 C++

by paysmile 2022. 4. 25.

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRDL1aeugDFAUo

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX = 10;
vector<int> map[MAX][MAX];
int m;
struct MOVE { int x, y; };
MOVE mv[4] = { {-1,0}, {0,1}, {1,0}, {0,-1} };
struct INFO { int x, y, c, p; }; //좌표, 충전범위, 처리량
vector<INFO> bc;
int a;
vector<int> p1;
vector<int> p2;
int visited[MAX][MAX];
int answer = 0;
void ChooseCharge(pair<int, int> loc,pair<int,int> loc2) {
 int tmp = 0;
 
 if (map[loc.first][loc.second].size() == 0) {
  for (int j = 0; j < map[loc2.first][loc2.second].size(); j++) {
   int tp = 0;
   tp += bc[map[loc2.first][loc2.second][j]].p;
   tmp = max(tmp, tp);
  }
 }
 else if(map[loc2.first][loc2.second].size() == 0) {
  for (int j = 0; j < map[loc.first][loc.second].size(); j++) {
   int tp = 0;
   tp += bc[map[loc.first][loc.second][j]].p;
   tmp = max(tmp, tp);
  }
 }
 else {
  for (int i = 0; i < map[loc.first][loc.second].size(); i++) {
   for (int j = 0; j < map[loc2.first][loc2.second].size(); j++) {
    int tp = 0;
    if (map[loc.first][loc.second][i] == map[loc2.first][loc2.second][j]) {
     tp = bc[map[loc.first][loc.second][i]].p;
    }
    else {
     tp += bc[map[loc.first][loc.second][i]].p;
     tp += bc[map[loc2.first][loc2.second][j]].p;
    }
    tmp = max(tmp, tp);
   }
  }
 }
 answer += tmp;
}
void MoveDfs(int ii, int jj, int index, int count) {
 queue<pair<pair<int, int>,int>> q;
 q.push({ { ii,jj },0 });
 while (!q.empty()) {
  int mi = q.front().first.first;
  int mj = q.front().first.second;
  int count = q.front().second;
  q.pop();
  if (count == bc[index].c) continue;
  for (int k = 0; k < 4; k++) {
   int movei = mi + mv[k].x;
   int movej = mj + mv[k].y;
   if (!(movei >= 0 && movei < 10 && movej >= 0 && movej < 10)) continue;
   if (visited[movei][movej] == 1) continue;
   visited[movei][movej] = 1;
   map[movei][movej].push_back(index);
   q.push({ {movei,movej},count + 1 });
  }
 }
}
int main(void) {
 int testcase;
 cin >> testcase;
 int index = 1;
 for (; testcase > 0; testcase--) {
  answer = 0;
  cin >> m >> a;
  bc.clear();
  bc.resize(a);
  p1.clear();
  p2.clear();
  for (int i = 0; i < 10; i++) {
   for (int j = 0; j < 10; j++) {
    map[i][j].clear();
   }
  }
  for (int i = 0; i < m; i++) {
   int n;
   cin >> n;
   p1.push_back(n);
  }
  for (int i = 0; i < m; i++) {
   int n;
   cin >> n;
   p2.push_back(n);
  }
  for (int i = 0; i < a; i++) {
   int x, y, c, p;
   cin >> x >> y >> c >> p;
   bc[i] = { y-1,x-1,c,p };
  }
  for (int i = 0; i < a; i++) {
   memset(visited, -1, sizeof(visited));
   int ii = bc[i].x;
   int jj = bc[i].y;
   
   map[ii][jj].push_back(i);
   visited[ii][jj] = 1;
   MoveDfs(ii, jj, i, 0);
  }
  pair<int, int> loc1 = { 0,0 };
  pair<int, int> loc2 = { 9,9 };
  ChooseCharge(loc1, loc2);
  for (int i = 0; i < m; i++) {
   if (p1[i] > 0) {
    loc1.first = loc1.first + mv[p1[i] - 1].x;
    loc1.second = loc1.second + mv[p1[i] - 1].y;
   }
   if (p2[i] > 0) {
    loc2.first = loc2.first + mv[p2[i] - 1].x;
    loc2.second = loc2.second + mv[p2[i] - 1].y;
   }
   ChooseCharge(loc1, loc2);
  }
  cout << "#" << index << " " << answer << "\n";
  index++;
 }
}

'백준 알고리즘 > 구현' 카테고리의 다른 글

점심 식사시간 C++  (0) 2022.04.25
미생물 격리 C++  (0) 2022.04.25
보물상자 비밀번호  (0) 2022.04.24
벽돌 깨기 C++  (0) 2022.04.24
특이한 자석 C++  (0) 2022.04.23