POJ 4115:鸣人和佐助

#include 
using namespace std;
int M, N, T;
char m[210][210];
int used[210][210][15];
struct Pos{
	int r, c, ck, steps;
	
	Pos(int rr, int cc, int cck, int ss):r(rr), c(cc), ck(cck), steps(ss) {};
	Pos() {}
}; 
queue q;
Pos s, e;
struct mov{
	int mr, mc;
} mov[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int main()
{
	#ifdef ONLINE_JUDGE
	#else
	freopen("in.txt", "r", stdin);
	#endif
	
	int i, j, t;
	scanf("%d%d%d", &M, &N, &T);
	getchar();
	memset(used, 1, sizeof(used));
	for(i = 1; i <= M; i++){
		for(j = 1; j <= N; j++){
			
			scanf("%c", &m[i][j]);
			
			if(m[i][j] == '@') 	s = Pos(i, j, T, 0);
			else if(m[i][j] == '+'){
				m[i][j] = '*';
				e = Pos(i, j, -1, -1);
			}
			for(t = 0; t <= T; t++){
				used[i][j][t] = 0;
			} 
			
		}
		
		getchar(); 
	}
	used[s.r][s.c][T] = 1;
	q.push(s);
	
	while(!q.empty()){
		Pos p = q.front();
		if(p.r == e.r && p.c == e.c){
			printf("%d\n", p.steps);
			return 0;
		} 
		q.pop();
		
		for(i = 0; i < 4; i++){
			int newR = p.r + mov[i].mr;
			int newC = p.c + mov[i].mc;
			
			if(m[newR][newC] == '*' && !used[newR][newC][p.ck]){
				used[newR][newC][p.ck] = 1;
				q.push(Pos(newR, newC, p.ck, p.steps + 1));
			}
			if(m[newR][newC] == '#' && p.ck > 0 && !used[newR][newC][p.ck - 1]){
				used[newR][newC][p.ck - 1] = 1;
				q.push(Pos(newR, newC, p.ck - 1, p.steps + 1));
			}
		}
	}
	
	printf("%d", -1);
	return 0;
}

你可能感兴趣的:(POJ)