-
[BaekJoon] ์ผ์ฑ sw ์ญ๋ ํ ์คํธ(์ด์ฐจ์ ๋ฐฐ์ด๊ณผ ์ฐ์ฐ)SW Test/BaekJoon 2020. 5. 4. 00:19๋ฐ์ํ
- ์ด ๋ฌธ์ ๋ c++๋ก ํ์ด๋ฅผ ์์ฑํ์์ต๋๋ค.
๋ฌธ์
์์
ํ์ด
#include <iostream> #include <algorithm> using namespace std; int vec[101][101]={0}; int r,c,k; int n=3; int m=3; struct node{ int n; int cnt; }; // node์ ๋ฐฐ์ด์ ์ ๋ ฌํ๊ธฐ ์ํ ๋น๊ตํจ์ // ์๊ฐ ๋ง์์ง๋ ์์ผ๋ก, ๊ฐ์ผ๋ฉด ์๊ฐ ์ปค์ง๋ ์์ผ๋ก bool cmp(node a,node b){ if(a.cnt==b.cnt){ return a.n<b.n; } return a.cnt<b.cnt; } int main(){ int answer=0; cin>>r>>c>>k; for(int i=1;i<=3;i++){ for(int j=1;j<=3;j++){ cin>>vec[i][j]; } } // answer์ด 100์ด์์ด๊ฑฐ๋ vec[r][c]==k๊ฐ ๋๋ฉด ์ข ๋ฃ while(vec[r][c]!=k && answer<=100){ answer++; if(n>=m){ // ํ ์ ๋ ฌ for(int i=1;i<=n;i++){ int copy[101]={0}; // ํด๋น ์ซ์์ ๊ฐฏ์๋ฅผ ์๊ธฐ ์ํ ์์ for(int j=1;j<=m;j++){ copy[vec[i][j]]++; } // node ๋ฐฐ์ด์ ํ ๋น node nd[101]; int idx=1; for(int j=1;j<=100;j++){ if(copy[j]!=0){ node temp; temp.n=j; temp.cnt=copy[j]; nd[idx++]=temp; } } sort(nd+1,nd+idx,cmp); // ์ ๋ ฌ // ์ถ๊ฐ์ ์ธ ์์ ๋ ์ด int rm=0; for(int j=1;j<idx;j++){ vec[i][++rm]=nd[j].n; vec[i][++rm]=nd[j].cnt; } for(int j=rm+1;j<=100;j++){ vec[i][j]=0; } if(rm>=m){m=rm;} } } else{ // ์ด ์ ๋ ฌ (๋๊ฐ์ด ๊ตฌํ) for(int i=1;i<=m;i++){ int copy[101]={0}; for(int j=1;j<=n;j++){ copy[vec[j][i]]++; } node nd[101]; int idx=1; for(int j=1;j<=100;j++){ if(copy[j]!=0){ node temp; temp.n=j; temp.cnt=copy[j]; nd[idx++]=temp; } } sort(nd+1,nd+idx,cmp); int rn=0; for(int j=1;j<idx;j++){ vec[++rn][i]=nd[j].n; vec[++rn][i]=nd[j].cnt; } for(int j=rn+1;j<=100;j++){ vec[j][i]=0; } if(rn>=n){n=rn;} } } } answer=answer>100?-1:answer; cout<<answer; }
- ์ถ๊ฐ๋ก ๊ถ๊ธํ ์ ์ด๋ ์์ ํ ๋ถ๋ถ ์์ผ๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์.
๋ฐ์ํ'SW Test > BaekJoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BaekJoon] ์ผ์ฑ sw ์ญ๋ ํ ์คํธ(ํ ํธ๋ก๋ฏธ๋ ธ) (0) 2020.05.12 [BaekJoon] ์ผ์ฑ sw ์ญ๋ ํ ์คํธ(๋๋๊ณค ์ปค๋ธ) (0) 2020.05.05 [BaekJoon] ์ผ์ฑ sw ์ญ๋ ํ ์คํธ(๊ฐ์) (0) 2020.04.30 [BaekJoon] ์ผ์ฑ sw ์ญ๋ ํ ์คํธ(์นํจ ๋ฐฐ๋ฌ) (0) 2020.04.30 [BaekJoon] ์ผ์ฑ sw ์ญ๋ ํ ์คํธ(์คํํธ์ ๋งํฌ) (0) 2020.04.26