알고리즘/백준
[C++] 백준 8972 미친 아두이노
이영재의오른발
2023. 1. 11. 16:28
반응형
시뮬레이션 + 구현 문제
이전 배열의 상태를 저장해 주기만 하면 쉽게 풀수있다.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cmath> using namespace std; string arr; string str; vector<int> moving; vector<pair<int,int> > mad; int dx[] = {1,1,1,0,0,0,-1,-1,-1}; int dy[] = {-1,0,1,-1,0,1,-1,0,1}; int n,m,x,y; int cnt = 0; // 1 == 아두이노, 2 == 미친 아두이노 int v[101][101]; int rem[101][101]; void input() { cin >> n >> m; for(int i=0; i<n; i++) { cin >> arr; for(int j=0; j<arr.length(); j++) { if(arr[j] == 'I') { v[i][j] = 1; x = i; y = j; } if(arr[j] == 'R') { v[i][j] = 2; mad.push_back({i,j}); } } } cin >> str; for(int i=0; i<str.length(); i++) { moving.push_back(str[i] - '0'); } } void print() { for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(v[i][j] == 1) { cout << "I"; } else if(v[i][j] == 2) { cout << "R"; } else { cout << "."; } } cout << '\n'; } } void copy() { for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(rem[i][j] == 1 || rem[i][j] == 2) { v[i][j] = rem[i][j]; } else { v[i][j] = 0; } rem[i][j] = 0; } } } void solve() { // 종수 움직임 for(int i : moving) { cnt++; int kx = dx[i-1] + x; int ky = dy[i-1] + y; if(v[kx][ky] == 2) { cout << "kraj " << cnt; return; } rem[kx][ky] = 1; x = kx; y = ky; for(int j=0; j<n; j++) { for(int k=0; k<m; k++) { if(v[j][k] == 2) { int min_tmp = 99999999; int d; for(int q=0; q<9; q++) { int nx = j + dx[q]; int ny = k + dy[q]; int tmp; tmp = fabs(x - nx) + fabs(y - ny); if(tmp < min_tmp) { min_tmp = tmp; d = q; } } int nx = dx[d] + j; int ny = dy[d] + k; if(x == nx && y == ny) { cout << "kraj " << cnt; return; } rem[nx][ny] += 2; } } } copy(); } print(); } int main() { input(); solve(); } | cs |
728x90
반응형