-
#1220. [S/W 문제해결 기본] 5일차 - MagneticCode/swea 2020. 3. 12. 18:23728x90반응형
출처:https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14hwZqABsCFAYD
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
각 열마다 N극인 1들을 내릴 수 있을만큼 끝까지 내려준다. 그다음 S극인 2들을 올릴수 있을만큼 올려준다.
사실 문제를 풀고나서 안 사실은 그냥 바깥에 있는 1,2들만 내리고 올려주기(없애주기)만 하면 된다는 것을 알았다.
이렇게 다 이동시키고 난수 교착상태 갯수를 구해주면된다.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters// #s1220 Magnetic 1h 14min #include <iostream> #include <cstring> #include <vector> #include <algorithm> using namespace std; //N극:1, S극: 2 int dir[3] = { 0,1,-1 }; int map[101][101]; int deadlock; int n;//맵 사이즈 // 1을 먼저 내린다. // 2를 그다음 올려준다. // 1,2 과정에서 내리고 올리는 과정 중 장애물이 있다면 이동 x // 해당 동작 과정에서 movecnt 쟤고 하나라도 움직이지 않았다면 변동이 없는 상태-> 다음 열로 넘어간다. void Input(); void simul(int y) { //cout << "y " << y << endl; // 1을 먼저 끝까지 다 내려준다. for (int x = n - 1; x >= 0; x--) { int color = map[x][y]; if (color == 1) { int nx = x; while (true) { nx += dir[color]; if (nx >= n || map[nx][y]) { map[x][y] = 0; if (map[nx][y]) { map[nx-dir[color]][y] = 1; } break; } } } } // 2를 그다음 끝까지 올려준다. for (int x = 0; x < n; x++) { int color = map[x][y]; if (color == 2) { int nx = x; while (true) { nx += dir[color]; if (nx <0 || map[nx][y]) { map[x][y] = 0; if (map[nx][y]) { map[nx - dir[color]][y] = 2; } break; } } } } int flag = 0; for (int i = 0; i < n; i++){ if (map[i][y] == 1){ if (flag == 0)flag = 1; } else if (map[i][y] == 2){ if (flag == 1){ flag = 0; deadlock += 1; } } } return; } int main() { for (int t = 1; t <= 10; t++) { Input(); for (int j = 0; j < n; j++) { simul(j); } //for (int i = 0; i < n; i++) { // for (int j = 0; j < n; j++) { // cout << map[i][j] << " "; // } // cout << endl; //} //cout << endl; cout << "#" << t << " " << deadlock << endl; } } void Input() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> map[i][j]; } } deadlock = 0; } 728x90반응형'Code > swea' 카테고리의 다른 글
#1767. [SW Test 샘플문제] 프로세서 연결하기 (0) 2020.03.12 #2477. [모의 SW 역량테스트] 차량 정비소 (0) 2020.03.12 #2383. [모의 SW 역량테스트] 점심 식사시간 (0) 2020.02.09 #5644. [모의 SW 역량테스트] 무선 충전 (0) 2020.01.03 #3752. 가능한 시험 점수 (0) 2019.12.31