17143번 낚시왕

동도리동·2021년 9월 20일
0

코딩테스트

목록 보기
41/76

우선 잘못된 코드이다

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;
struct Loc {
	int s, d, z;
	Loc(int a, int b, int c) {
		s = a;
		d = b;
		z = c;
	}
	bool operator<(const Loc& b)const {
		return z > b.z;
	}
};
vector<Loc> map[101][101];
int n,m,k;
int dx[5] = { 0,-1,1,0,0 };
int dy[5] = { 0,0,0,-1,1 };
void go(int sx, int sy) {
	Loc tmp = map[sx][sy][0];
	int x = sx;
	int y = sy;
	int tmp2 = tmp.s;
	while(tmp.s--) {
		x += dx[tmp.d];
		y += dy[tmp.d];
		if (x > n || x <= 0 || y > m || y <= 0) {
			x -= dx[tmp.d];
			y -= dy[tmp.d];
			if (tmp.d == 1) tmp.d = 2;
			else if (tmp.d == 2) tmp.d = 1;
			else if (tmp.d == 3) tmp.d = 4;
			else if (tmp.d == 4) tmp.d = 3;
			x += dx[tmp.d];
			y += dy[tmp.d];
		}
	}
	if (!(x == sx && y == sy)) {
		map[sx][sy].erase(map[sx][sy].begin(), map[sx][sy].end());
		map[x][y].push_back(Loc(tmp2, tmp.d, tmp.z));
	}
	
	
}
int main() {
	freopen("in1.txt", "rt", stdin);
	int a, b, c, d, e;
	cin >> n >> m >> k;
	int ans = 0;
	if (k != 0) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> a >> b >> c >> d >> e;
				map[a][b].push_back(Loc(c, d, e));
			}
		}
	}
	int q = 1;
	while (q <=m) {
		for (int i = 1; i <= n; i++) { //낚시
			if (map[q][i].size()) {
				ans += map[q][i][0].z;
				//cout << map[q][i][0].z << '\n';
				map[q][i].erase(map[q][i].begin());
				
				break;
			}
		}
		for (int i = 1; i <= n; i++) { //상어 이동
			for (int j = 1; j <= m; j++) {
				if(map[i][j].size()) go(i,j);
			}
		}
		for (int i = 1; i <= n; i++) { //상어 이동
			for (int j = 1; j <= m; j++) {
				if (map[i][j].size() > 1) {
					sort(map[i][j].begin(), map[i][j].end());
					map[i][j].erase(map[i][j].begin()+1 , map[i][j].end());
				}
			}
		}
		q++;
	}
	cout << ans << '\n';
	//cout << map[4][5][0].z << '\n';
	return 0;
}

억지로 vector map[][]에 push_back으로 더 꼬아버렸다. 그냥 int map[][]으로 다시 해야징

문제가 너무 좋다. 미세먼지 안녕!과 이 문제에 공통점은 따로 배열에 확산한 먼지와 이동한 상어를 따로 저장해준다는 것이다. 내일 두 문제 다시 풀어봐야겠다.

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
struct Loc {
	
	int z = 0;
	int s = 0;
	int d = 0;
	
};
int n, m, k;
Loc map[100][100];
Loc nmap[100][100];
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,1,-1 };

tuple<int, int, int> go(int sx, int sy, int speed,int dir) {
	
	for (int i = 0; i < speed; i++) {
		if (dir == 0) {
			if (sx == 0) {
				dir = 1;
				sx = 1;
			}
			else sx -= 1;
		}
		else if (dir == 1) {
			if (sx == n - 1) {
				sx = n - 2;
				dir = 0;
			}
			else sx++;
		}
		else if (dir == 2) {
			if (sy == m-1) {
				sy = m-2;
				dir = 3;
			}
			else sy++;
		}
		else if(dir==3){
			if (sy == 0) {
				sy = 1;
				dir = 2;
			}
			else sy--;
		}
	}
	return make_tuple(sx,sy,dir);
}
int main() {
	freopen("in1.txt", "rt", stdin);
	int mm;
	cin >> n >> m >> mm;
	while (mm--) {
		int x1, x2, x3, x4, x5;
		cin >> x1 >> x2 >> x3 >> x4 >> x5;
		x1--; x2--; x4--;
		map[x1][x2] = Loc({ x5, x3, x4 });
	}
	int ans = 0;
	for (int j = 0; j < m; j++) { //낚시왕 움직임
		//cout << j<<"\n";
		for (int i = 0; i < n; i++) { //땅에 가장 가까운 상어 잡으러 출발
			if (map[i][j].z > 0) {
				ans += map[i][j].z;
				map[i][j].z = 0;
				break;
			}
		}
		for (int i = 0; i < n; i++) {//상어이동
			//cout << j << "\n";
			for (int l2 = 0; l2 < m; l2++) {
				if (map[i][l2].z == 0) continue;
				int x,y,dir;
				tie(x,y,dir) = go(i,l2,map[i][l2].s,map[i][l2].d);
				if (nmap[x][y].z == 0 || nmap[x][y].z < map[i][l2].z) {
					nmap[x][y] = Loc({map[i][l2].z,map[i][l2].s,dir});
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int l2 = 0; l2 < m; l2++) {
				map[i][l2] = nmap[i][l2];
				nmap[i][l2].z = 0;
			}
		}
	}
	cout << ans << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보