Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 브루트포스
- BFS
- VR
- 백트래킹
- 누적 합
- Unreal Engine 5
- Team Fortress 2
- 재귀
- 백준
- 그리디 알고리즘
- 유니티
- ue5
- 시뮬레이션
- 자료구조
- 수학
- 그래프
- 트리
- 문자열
- DFS
- 다이나믹 프로그래밍
- 우선순위 큐
- XR Interaction Toolkit
- 유니온 파인드
- 투 포인터
- 다익스트라
- 알고리즘
- 스택
- c++
- 정렬
- 구현
Archives
- Today
- Total
1일1알
백준 17265번 나의 인생에는 수학과 함께 C++ 본문
https://www.acmicpc.net/problem/17265
17265번: 나의 인생에는 수학과 함께
세현이의 인생의 목표는 1분 1초 모든 순간 수학과 함께 살아가는 것이다. 그렇기 때문에 매일 수학을 생각하면서 살아가고 있다. 세현이는 밥을 먹을 때도 쌀알의 수를 계산하여 칼로리를 바로
www.acmicpc.net
bfs로 연산자일때와 숫자일때를 구분해서 풀었다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
struct Info {
int row;
int col;
int val;
bool isOp;
char op;
};
int n;
int dRow[2] = { 0,1 };
int dCol[2] = { 1,0 };
vector<vector<char>> board;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
board = vector<vector<char>>(n, vector<char>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
}
}
int maxVal = -INT_MAX;
int minVal = INT_MAX;
queue<Info> q;
q.push({ 0,0,0,false,'+' });
while (!q.empty()) {
auto curr = q.front();
q.pop();
int nextVal = curr.val;
bool nextIsOp = curr.isOp;
char nextOp = curr.op;
if (curr.isOp) {
nextOp = board[curr.row][curr.col];
nextIsOp = false;
}
else {
switch (curr.op) {
case '+':
nextVal += board[curr.row][curr.col] - '0';
break;
case '-':
nextVal -= board[curr.row][curr.col] - '0';
break;
case '*':
nextVal *= board[curr.row][curr.col] - '0';
break;
default:
break;
}
nextIsOp = true;
}
if (curr.row == n - 1 && curr.col == n - 1) {
maxVal = max(maxVal, nextVal);
minVal = min(minVal, nextVal);
}
for (int i = 0; i < 2; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (nextRow >= n || nextCol >= n) continue;
q.push({ nextRow,nextCol,nextVal,nextIsOp,nextOp });
}
}
cout << maxVal << " " << minVal;
}
'알고리즘' 카테고리의 다른 글
백준 5567번 결혼식 C++ (0) | 2022.12.30 |
---|---|
백준 16957번 체스판 위의 공 C++ (0) | 2022.12.29 |
백준 12845번 모두의 마블 C++ (0) | 2022.12.27 |
백준 15558번 점프 게임 C++ (0) | 2022.12.25 |
백준 19640번 화장실의 규칙 C++ (0) | 2022.12.24 |