6087번 레이저 통신

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

코딩테스트

목록 보기
27/76

생각이 너무 복잡해서.. 실패했다. 내가 시도한 방식은 이전에 진행해온 방향을 저장하고, 그 방향대로 한칸이동, 거울 방향대로 2가지, 총 3개를 queue에 넣어주었다. 허우적대다가 실패했다.
거울이라는 한정된 정보에 매달리지 말고, 똑같이 상하좌우를 탐색하는게 단순해지고 깔끔하다..
아래는 오류코드이다. 주의!

#include <iostream>
#include <vector>
#include <queue>
#include <stdio.h>

using namespace std;
char map[101][101];
int dist[101][101];
struct Loc {
	int x, y, cnt, where;
	Loc(int a, int b, int c, int d) {
		x = a;
		y = b;
		where = c;
		cnt = d;
	}
	bool operator<(const Loc& b) const{
		return cnt > b.cnt;
	}
};

struct flag {
	int x, y;
	flag(int a, int b) {
		x = a;
		y = b;
	}
};
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
int main() {
	int w, h;
	bool f=true;
	freopen("in1.txt", "rt", stdin);
	flag first = flag(0, 0);
	flag second = flag(0, 0);
	cin >> h>>w;
	for (int i = 0; i < w; i++) {
		cin >> map[i];
		for (int j = 0; j < h; j++) {
			if (map[i][j]=='C') {
				if (f) {
					second.x = i;
					second.y = j;
					f = false;
				}
				else {
					first.x = i;
					first.y = j;
				}
			}
		}
	}
	priority_queue<Loc> Q;
	for (int i = 0; i < 4; i++) {
		Q.push(Loc(first.x+dx[i],first.y+dy[i],0,i));
		//cout << first.x << "," << first.y << "," << "0"<<","<<i << '\n';
	}
	dist[first.x][first.y] = 1;
	while (!Q.empty()) {
		Loc tmp = Q.top();
		Q.pop();
		//cout << "error " << tmp.x << "," << tmp.y <<","<< tmp.cnt<<"," <<tmp.where<<'\n';
		if (tmp.x == second.x && tmp.y == second.y) {
			cout << tmp.cnt + 1 << '\n';
			return 0;
		}
		if (tmp.x >= w || tmp.x < 0 || tmp.y >= h || tmp.y < 0) continue;
		if (map[tmp.x][tmp.y] == '*') continue;
		int xx = tmp.x;
		int yy = tmp.y;
		cout << "error2 " << xx << "," << yy << "," << tmp.cnt << "," << tmp.where << "," << "dx[tmp.where]: " << dx[tmp.where] << " dy[tmp.where]: " << dy[tmp.where] << '\n';

		if (dist[xx + dx[3 - tmp.where]][yy + dy[3 - tmp.where]]==0) {
			dist[xx + dx[3 - tmp.where]][yy + dy[3 - tmp.where]] = 1;
			Q.push(Loc(xx + dx[3 - tmp.where], yy + dy[3 - tmp.where], tmp.cnt + 1, 3 - tmp.where));
		}
		if (dist[xx + dx[(5 - tmp.where) % 4]][yy + dy[(5 - tmp.where) % 4]]==0) {
			dist[xx + dx[(5 - tmp.where) % 4]][yy + dy[(5 - tmp.where) % 4]] = 1;
			Q.push(Loc(xx + dx[(5 - tmp.where) % 4], yy + dy[(5 - tmp.where) % 4], tmp.cnt + 1, (5 - tmp.where) % 4));
		}
		if (dist[xx][yy] == 0) {
			dist[xx][yy] = 1;
			Q.push(Loc(xx, yy, tmp.cnt, tmp.where));
		}
	}
	return 0;
}

아래는 정답코드.. 이제는 오히려 ch배열을 너무 어렵게 생각하고 있는 것 같다. 좀더 단순히!

#include <iostream>
#include <vector>
#include <queue>
#include <stdio.h>

using namespace std;
char map[101][101];
int dist[101][101];
struct Loc {
	int x, y, cnt, where;
	Loc(int a, int b, int c, int d) {
		x = a;
		y = b;
		where = c;
		cnt = d;
	}
	bool operator<(const Loc& b) const {
		return cnt > b.cnt;
	}
};

struct flag {
	int x, y;
	flag(int a, int b) {
		x = a;
		y = b;
	}
};
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
int main() {
	int w, h;
	bool f = true;
	freopen("in1.txt", "rt", stdin);
	flag first = flag(0, 0);
	flag second = flag(0, 0);
	cin >> h >> w;
	for (int i = 0; i < w; i++) {
		cin >> map[i];
		for (int j = 0; j < h; j++) {
			if (map[i][j] == 'C') {
				if (f) {
					second.x = i;
					second.y = j;
					f = false;
				}
				else {
					first.x = i;
					first.y = j;
				}
			}
		}
	}
	queue<flag> Q;
	Q.push(flag(first.x, first.y));
	dist[first.x][first.y] = 1;
	//cout << first.x << " " << first.y << '\n';
	while (!Q.empty()) {
		flag tmp = Q.front();
		Q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = tmp.x + dx[i];
			int yy = tmp.y + dy[i];
			while (xx < w && xx >= 0 && yy < h && yy >= 0) {
				if (map[xx][yy] == '*') break;
				if (dist[xx][yy] == 0) {
					dist[xx][yy] = dist[tmp.x][tmp.y] + 1;
					Q.push(flag(xx, yy));
				}
				xx += dx[i];
				yy += dy[i];
			}
		}
	}
	cout << dist[second.x][second.y] - 2 << '\n';
	return 0;
}
profile
긍정코딩세상

2개의 댓글

comment-user-thumbnail
2021년 8월 20일

저도 허우적거리다 결국 실패했어요ㅠㅠ 내일은 강의듣고 풀어봐야지 넘 지치네용..ㅜㅅㅜ

1개의 답글

관련 채용 정보