회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
c++ 런타임에러..
21608번 - 상어 초등학교
sjh04196
2년 전
0
제눈에는 outofbounds 날 구석이 없는데...한번만 봐주세요 ㅠㅠㅠㅠ
#include <iostream> #include <vector> #include <algorithm> #include <map> #include <math.h> #include <memory.h> using namespace std; // 1. 좋아하는 학생이 인접한 칸 // 2. 인접한 칸 중 비어있는 칸이 많은 칸 // 3. 행번호가 작은 칸 // 4. 열번호가 작은 칸 int N; vector<vector<int>>room(500,vector<int>(500,0)); vector<vector<int>> copy(500,vector<int>(500,0)); // 움직임 int dy[4]={0,1,0,-1}; int dx[4]={1,0,-1,0}; // 만족도 int satisfied = 0; bool compare(pair<int,int>const& a, pair<int,int>const&b){ if(a.first==b.first){ return a.second < b.second; } return a.first < b.first; } // 학생 struct student{ int me; int friends[4]; /** * vector<int> friends; * student() : friends(4){} */ }; // 전체 학생 vector<student> members; // 좌표, 학생정보 map<pair<int,int>,student> posAndinfo; pair<int,int> solution(student st){ // 좋아하는 학생이 인접한 칸에 가장 많은 칸으로 자리를 정한다. int Maxcnt = -1; vector<pair<int,int>> pos[4]; // 아무리 많아도 4칸 for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ if(room[i][j]==0){// 빈 공간에서만 탐색 int cnt=0; for(int d=0;d<4;d++){ int y = dy[d]+i; int x = dx[d]+j; if(y>=1 && x>=1 && y<=N && x<=N){ for(int l=0;l<4;l++){ // 친구가 있으면 cnt++ if(st.friends[l]==room[y][x]){ cnt++; } } } } Maxcnt = max(Maxcnt,cnt); pos[cnt].emplace_back(i,j); } } } // 인접한 칸 중 비어있는 칸이 많은 칸 vector<pair<int,int>> wide[4]; vector<pair<int,int>> place[4]; int MaxEmpty = -1; for(auto it : pos[Maxcnt]){ int cy = it.first; int cx = it.second; int emptycnt = 0; for(int k=0;k<4;k++){ int ny = cy+dy[k]; int nx = cx+dx[k]; if(ny>=1 && nx>=1 && ny<=N && nx<=N && room[ny][nx]==0){ emptycnt++; } } // 특정 위치 주변에 공란이 많은 칸으로 place[emptycnt].emplace_back(cy,cx); MaxEmpty = max(MaxEmpty,emptycnt); } return place[MaxEmpty].front(); } // 계산기 int cal(){ int answer = 0; for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ int s = 0; for(int d=0;d<4;d++){ int ny = i + dy[d]; int nx = j + dx[d]; for(int f=0;f<4;f++){ if(posAndinfo[{i,j}].friends[f]==room[ny][nx]){ s++;// 만족도 } } } answer+= pow(10,s-1); } } return answer; } // 출력 void print(){ for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ cout << room[i][j] << " "; } cout << '\n'; } } void input(){ cin >> N; for(int i=0;i<(N*N);i++){ student s; for(int j=0;j<5;j++){ if(j==0){ cin >> s.me; }else{ cin >> s.friends[j-1]; } } members.emplace_back(s); } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); input(); // 학생들의 정보를 순차적으로 탐색 for(auto it : members){ // 비어있는 칸 중 좋아하는 학생이 가장 많은 칸 pair<int,int> ans = solution(it); room[ans.first][ans.second] = it.me; // 좌표를 키로, 학생정보를 밸류로 posAndinfo[ans]=it; } satisfied = cal(); //print(); cout << satisfied << endl; }
댓글을 작성하려면
로그인
해야 합니다.
sjh04196 2년 전
제눈에는 outofbounds 날 구석이 없는데...한번만 봐주세요 ㅠㅠㅠㅠ