Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贡献一份AC代码

Posted by 675121663 at 2017-07-30 14:52:02 on Problem 3669
#include<cstdio>
#include<algorithm>
#include<queue>
#define MAX_M 50005
#define MAX_SIZE 303
#define MAX_TIME 9999
using namespace std;

int M;
int X, Y, T;

int map[MAX_SIZE][MAX_SIZE];

int dist[MAX_SIZE][MAX_SIZE];

int dirction[5][2] = {0, -1, -1, 0, 0, 1, 1, 0, 0, 0};

typedef pair<int, int> P;

struct Meteor {
	int X;
	int Y;
	int T;
} meteors[MAX_M];

bool cmp (Meteor a, Meteor b) {
	if (a.T < b.T) return true;
	else return false;
}

bool isLegal(int x, int y) {
	if (x >= 0 && x < MAX_SIZE && y >= 0 && y < MAX_SIZE) return true;
	else return false;
}

int bfs() {
	queue<P> que;
	if (map[0][0] == 0) return -1;
	else if (map[0][0] == MAX_TIME) return 0;
	que.push(P(0, 0));
	while (que.size()) {
		P p = que.front();
		que.pop();
		int px = p.first, py = p.second;
		if (px >= MAX_SIZE || py >= MAX_SIZE) continue;
		for(int i = 0; i < 4; i ++) {
			int tx = px + dirction[i][0], ty = py + dirction[i][1];
			if (isLegal(tx, ty)) {
				if (map[tx][ty]  == MAX_TIME) {
					return ++ dist[px][py];
				}
				if (dist[px][py] + 1 < map[tx][ty] && !dist[tx][ty]) {
					que.push(P(tx, ty));
					dist[tx][ty] = dist[px][py] + 1;
				}
			}
		}
	}
	return -1;
}

void solve() {
	for (int i = 0; i < MAX_SIZE; i ++) {
		for (int j = 0; j < MAX_SIZE; j ++) {
			map[i][j] = MAX_TIME;
		}
	} 
	for (int i = 0; i < M; i ++) {
		scanf("%d %d %d", &meteors[i].X, &meteors[i].Y, &meteors[i].T);
	}
	sort(meteors, meteors + M, cmp);
	for (int i = 0; i < M; i ++) {
		for (int j = 0; j < 5; j ++) {
			int tx = meteors[i].X, ty = meteors[i].Y;
			tx += dirction[j][0];
			ty += dirction[j][1];
			if (isLegal(tx, ty) && map[tx][ty] > meteors[i].T) {
				map[tx][ty] = meteors[i].T;
			}
		}
	}
	printf("%d\n", bfs());
}

int main() {
	while (~scanf("%d", &M)) {
		solve();
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator