알고리즘 공부

백준 점프점프

컴퓨터과학 2023. 8. 6. 14:29

https://www.acmicpc.net/problem/11060

 

11060번: 점프 점프

재환이가 1×N 크기의 미로에 갇혀있다. 미로는 1×1 크기의 칸으로 이루어져 있고, 각 칸에는 정수가 하나 쓰여 있다. i번째 칸에 쓰여 있는 수를 Ai라고 했을 때, 재환이는 Ai이하만큼 오른쪽으로

www.acmicpc.net

dp의 메모라이제이션과 bfs 섞인 문제

주의점 ) 최단 경로로 가야함 

#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<stack>
using namespace std;

struct Point {
	int s, e, t, cnt, power;
	char c;
};

void Prints(vector<vector<int>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<bool>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<char>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<vector<char>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<vector<bool>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<int>line, int n) {
	for (int i = 0; i < n; i++) {
		cout << line[i] << ",";
	}
	cout << endl;
}
void Prints(string str, int n) {
	for (int i = 0; i < n; i++) {
		cout << str[i] << ",";
	}
	cout << endl;
}

bool checkmaps(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m) {
	bool bCheck = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maps[i][j] != anw[i][j]) {
				bCheck = true;
				i = n + 1;
				j = m + 1;
				continue;
			}
		}

	}
	return bCheck;

}
bool findstart(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m, int) {
	bool bCheck = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maps[i][j] != anw[i][j]) {
				bCheck = true;
				i = n + 1;
				j = m + 1;
				continue;
			}
		}

	}
	return bCheck;

}

struct Data {
	int index;
	int value;
	int cnt;

};
struct GDatas {
	int change;
	int index;
	
	string times;

};

long long n, m;
int k, x;
vector<vector<int>>glistmaps;
vector<bool>bVisited;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//
	int n;
	cin >> n;
	vector<int>lists;
	for (int i = 0; i < n; i++) {
		int tmp;
		cin >> tmp;
		lists.push_back(tmp);
	}
	int start = lists[0];
	int index = 0;
	int gcnt = 0;
	if (lists.size() == 1) {
		cout << 0;
		return 0;
	}
	queue<Data>q;
	Data d;
	d.index = 0;
	d.value = start;
	d.cnt = 0;
	q.push(d);//1
	vector<bool>bVisited(n, false);
	bool bEnd = false;

	while (!q.empty()) {
		Data start = q.front();
		q.pop();
		bVisited[start.index] = true;
		if (bEnd == true) {
			break;
		}
		for (int i =1; i <= start.value; i++) {
			if (start.index + i>=n) {
				gcnt = start.cnt + 1;
				i = start.value + 1;
				bEnd = true;
				continue;
			}
			
			
			if (bVisited[start.index + i] == true) {
				continue;
			}
			int index = start.index+i; //2+0
			int value = lists[start.index + i];//
			bVisited[start.index + i] = true;
			Data tmpd;
			tmpd.index = index;
			tmpd.value = value;
			tmpd.cnt = start.cnt + 1;

			q.push(tmpd);
			if (index == n-1) {
				gcnt = start.cnt+1;
				i = start.value + 1;
				bEnd = true;
				continue;
			}
		}

	}

	if (bEnd == false) {
		cout << -1;
	}
	else {
		cout << gcnt;
	}
	
	return 0;
}