본문 바로가기
Algorithm/문제 풀이 (Problem Solving)

[C++,구현] 백준 17143번 낚시왕 문제풀이

by matters_ 2019. 4. 28.
문제 링크

코테에서 2문제 "완벽히" 풀 자신이 없다면 선택적으로 한문제 완벽히 맞추고 넘어가야 한다! 명심 또 명심!

 

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
128
129
130
131
132
#include <cstdio>
#include <cmath> 
int r,c,m,ans;
int fish[10000][5],check[10000];
int map[100][100];
 
void erase(int x){
    for(int i=0;i<m;i++)
        if(fish[i][4]==x)check[i]=1;
}
 
void getfish(int x){
    for(int i=0;i<r;i++){
        if(map[i][x]>0){erase(map[i][x]);ans+=map[i][x];
        //printf("\n(%d)%d\n",x,map[i][x]);
        map[i][x]=0;break;}
    }
}
 
void movefish(){
    for(int i=0;i<m;i++){
        if(check[i])continue;
        map[fish[i][0]-1][fish[i][1]-1]=0;
        switch(fish[i][3]){
            case 1:
                fish[i][2]%=(2*r-2);
                if(fish[i][0]-fish[i][2]<=0){
                    fish[i][0]=abs(fish[i][0]-fish[i][2])+2;
                    fish[i][3]=2;
                    if(fish[i][0]>r){
                    fish[i][3]=1;
                     fish[i][0]=(r-(fish[i][0]-r));}
                }
                else
                    fish[i][0]-=fish[i][2];
                
                break;
            case 2:
                fish[i][2]%=(2*r-2);
                if(fish[i][0]+fish[i][2]>r){
                
                    fish[i][0]=(r)-(fish[i][0]+fish[i][2]-(r));
                    fish[i][3]=1;
                    if(fish[i][0]<=0){
                    fish[i][3]=2;
                    fish[i][0]=abs(fish[i][0])+2;}
                }
                else
                    fish[i][0]+=fish[i][2];
                
                break;
                
                
            case 3:
                fish[i][2]%=(2*c-2);
                if(fish[i][1]+fish[i][2]>c){
                
                    fish[i][1]=(c)-(fish[i][1]+fish[i][2]-(c));
                    fish[i][3]=4;
                    if(fish[i][1]<=0){
                            fish[i][3]=3;
                    fish[i][1]=abs(fish[i][1])+2;}
                }
                else
                    fish[i][1]+=fish[i][2];
                
                break;
                
            case 4:
                fish[i][2]%=(2*c-2);
                if(fish[i][1]-fish[i][2]<=0){
                    fish[i][1]=abs(fish[i][1]-fish[i][2])+2;
                    fish[i][3]=3;
                    if(fish[i][1]>c){
                    fish[i][3]=4;
                     fish[i][1]=(c-(fish[i][1]-c));}
                }
                else
                    fish[i][1]-=fish[i][2];
                
                break;
        }
    }
}
 
void setfish(){
    for(int i=0;i<m;i++){
    if(check[i])continue;
    
        if(map[fish[i][0]-1][fish[i][1]-1]>0){
            if(map[fish[i][0]-1][fish[i][1]-1]<fish[i][4]){    
                erase(map[fish[i][0]-1][fish[i][1]-1]);
                map[fish[i][0]-1][fish[i][1]-1]=fish[i][4];
            
            }
            else{
                erase(fish[i][4]);
            }
        }
        else
            map[fish[i][0]-1][fish[i][1]-1]=fish[i][4];
    }
    
}
void printmap(){
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            printf("%d ",map[i][j]);
        }
        printf("\n");
    }
        printf("\n");
}
 
void solve(){
 
    for(int i=0;i<c;i++){
        setfish();
        //printmap();
        getfish(i);
        movefish();
    }
    printf("%d",ans);
}
 
main(){
    scanf("%d%d%d",&r,&c,&m);
    for(int i=0;i<m;i++)
        for(int j=0;j<5;j++)
            scanf("%d",&fish[i][j]);
    solve();
}
cs

입력으로 물고기의 상태가 주어진다. 나는 물고기의 현재 위치를 map에다가 입력시키는 setfish함수, 낚시꾼이 물고기를 잡는 getfish함수, 물고기가 움직이는 movefish함수로 나누어서 각각 구현을 하였다. 실제 시험장에서는 movefish함수 구현하다가 40분을 날려먹었다.. 결국 디버깅도 못하고 제출했다..ㅜ 지금 다시 풀어보니 알고리즘이 조잡한 느낌도 있다. 이후에 시간을 내 최적해야겠다.

 


20.05.11 재작성

 

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

int R, C, M;

typedef struct shark{
	int x, y, speed, dir, size,val;
};

int map[100][100],tmap[100][100];
vector <shark> svec;

int shark_catch(int t){
	int idx;
	for (int i = 0; i < R; i++){
		if (map[i][t]){
			idx = map[i][t] - 1;
			svec[idx].val = 0;
			map[i][t] = 0;
			return svec[idx].size;
		}
	}
	return 0;
}

void shark_move(){
	memset(tmap, 0, sizeof(tmap));
	for (int i = 0; i < M; i++){
		if (svec[i].val){
			if (svec[i].dir == 0){
				svec[i].speed %= 2*R - 2;
				svec[i].x -= svec[i].speed;
				if (svec[i].x < 0){
					svec[i].x *= -1;
					svec[i].dir = 1;
					if (svec[i].x > R-1){
						svec[i].x = (R - 1) - (svec[i].x - (R - 1));
						svec[i].dir = 0;
					}
				}
			}
			else if (svec[i].dir == 1){
				svec[i].speed %= 2*R - 2;
				svec[i].x += svec[i].speed;
				if (svec[i].x > R-1){
					svec[i].x = (R - 1) - (svec[i].x - (R - 1));
					svec[i].dir = 0;
					if (svec[i].x < 0){
						svec[i].x *= -1;
						svec[i].dir = 1;
					}
				}

			}
			else if (svec[i].dir == 2){
				svec[i].speed %= 2*C - 2;
				svec[i].y += svec[i].speed;
				if (svec[i].y > C - 1){
					svec[i].y = (C - 1) - (svec[i].y - (C - 1));
					svec[i].dir = 3;
					if (svec[i].y < 0){
						svec[i].y *= -1;
						svec[i].dir = 2;
					}
				}

			}
			else if (svec[i].dir == 3){
				svec[i].speed %= 2*C - 2;
				svec[i].y -= svec[i].speed;
				if (svec[i].y < 0){
					svec[i].y *= -1;
					svec[i].dir = 2;
					if (svec[i].y > C - 1){
						svec[i].y = (C - 1) - (svec[i].y - (C - 1));
						svec[i].dir = 3;
					}
				}

			}
			if (!tmap[svec[i].x][svec[i].y])
				tmap[svec[i].x][svec[i].y] = i+1;
			else{
				int tidx = tmap[svec[i].x][svec[i].y]-1;

				if (svec[i].size < svec[tidx].size){
					svec[i].val = 0;
				}
				else{
					svec[tidx].val = 0;
					tmap[svec[i].x][svec[i].y] = i+1;
				}

			}

		}

	}

}

void print_map(){

	cout << "\n";
	for (int i = 0; i < R; i++){
		for (int j = 0; j < C; j++){

			cout << map[i][j] << " ";
		}
		cout << "\n";
	}
	
}

int sol(){
	int gram = 0;
	for (int i = 0; i < C; i++){
		//print_map();
		gram += shark_catch(i);
		//print_map();
		shark_move();
		memcpy(map, tmap, sizeof(map));
	}
	return gram;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> R >> C >> M;
	for (int i = 0; i < M; i++)
	{
		int x, y, s, d, z;
		cin >> x >> y >> s >> d >> z;
		svec.push_back({ x-1, y-1, s, d-1, z,1 });
		map[x - 1][y - 1] = i+1;
	}

	cout<<sol();
	return 0;
}

댓글