About wide search

In fact, a simple broad search, then essentially routine. But what should be noted, that is, when the heavy sentence, with the dynamic regulation is the same reason, the state is represented by a parameter k k-dimensional array, you can not use three parameters of state of the two-dimensional array would not be re-sentenced a. For example:
one hundred training 4115: Naruto and Sasuke
I started to do a rescue operation that question, is the method of Guo God to do, namely get another killed a Boolean variable (in fact, is to step directly into the t + 2 two steps), but I only had visited [maxn] [maxn] arrays are too, so do not try to be smart to consider this chakra on the heavy sentence and think of anyway to point this chakra will consume 1, morning till night to are the same, but obviously this idea is wrong, because maybe the first few strange little long route, after more than a few strange short route, general line a few more strange than the previous, play a few strange little short behind this route case wrong.
Recent always more silly ...... alas. But proficiency is one of the factors of it. The key is not to gauge what move mechanically in isolation, but to correctly understand the description of the method and of the state and the state extended to all of the algorithms.
No nonsense, AC codes:

//POJ 4115 鸣人和佐助
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 202;
const int MAXT = 12;
struct state {
	int x, y;
	int chakra;
	int t;
	friend bool operator < (const state & a, const state & b) {
		//if (a.t == b.t) return (a.chakra < b.chakra);
		return (a.t > b.t);
	}
	state(int _x, int _y, int _chakra, int _t): x(_x), y(_y), chakra(_chakra), t(_t) {}
};
int visited[MAXN][MAXN][MAXT];
//int visited[MAXN][MAXN];
int M, N, T;
char map[200][201];
int sx, sy;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int legal(int x, int y, int chakra) {
	if (x < 0 || x >= M || y < 0 || y >= N) return 0;
	if (map[x][y] == '#' && chakra == 0) return 0;
	if (visited[x][y][chakra]) return 0;
	//if (visited[x][y]) return 0;
	return 1;
}
int main() {
	cin >> M >> N >> T;
	for (int i = 0; i < M; ++i) {
		for (int j = 0; j < N; ++j) {
			cin >> map[i][j];
			if (map[i][j] == '@') {
				sx = i;
				sy = j;
			}
		}
	}
	priority_queue <state> Q;
	state init(sx, sy, T, 0);
	Q.push(init);
	while(!Q.empty()) {
		init = Q.top();
		Q.pop();
		if (map[init.x][init.y] == '+') {
			cout << init.t << endl;
			return 0;
		}
		for (int i = 0; i < 4; ++i) {
			if (!legal(init.x + dx[i], init.y + dy[i], init.chakra)) continue;
			if (map[init.x + dx[i]][init.y + dy[i]] == '#') {
				//visited[init.x + dx[i]][init.y + dy[i]] = 1;
				visited[init.x + dx[i]][init.y + dy[i]][init.chakra - 1] = 1;
				Q.push(state(init.x + dx[i], init.y + dy[i], init.chakra - 1, init.t + 1));
			}
			else {
				//visited[init.x + dx[i]][init.y + dy[i]] = 1;
				visited[init.x + dx[i]][init.y + dy[i]][init.chakra] = 1;
				Q.push(state(init.x + dx[i], init.y + dy[i], init.chakra, init.t + 1));
			}
		}
	}
	cout << -1 << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44288817/article/details/90942470