Created
September 18, 2017 16:06
-
-
Save mi6112ogit/fbee62bee1c46c63732904490a7aded7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <utility> | |
#include <map> | |
#include <queue> | |
#include <string> | |
#include <vector> | |
#include <cstdio> | |
#include <cstring> | |
#include <cstdlib> | |
#include <ctime> | |
#include <cmath> | |
using namespace std; | |
#define FOR(i, j, k) for(int i = j; i < k; ++i) | |
#define rep(i, j) FOR(i, 0, j) | |
#define INF (1 << 30) | |
#define fi first | |
#define se second | |
typedef long long ll; | |
typedef unsigned long long ull; | |
typedef pair<int, int> P; | |
typedef pair<P, int> Pi; | |
typedef pair<P, P> PP; | |
const int MOD = 1e9 + 7; | |
const int dy[]={0, 0, 1, -1}; | |
const int dx[]={1, -1, 0, 0}; | |
template <class T> void chmin(T& a, const T& b) { a = min(a, b); } | |
template <class T> void chmax(T& a, const T& b) { a = max(a, b); } | |
int field[400][400]; | |
bool went[400][400]; | |
int bfs(int n) { | |
queue<Pi> que; | |
que.push(Pi(P(0, 0), n)); | |
while(!que.empty()) { | |
Pi p = que.front(); que.pop(); | |
if(field[p.fi.se][p.fi.fi] == INF) return p.se; | |
if(went[p.fi.se][p.fi.fi]) continue; | |
went[p.fi.se][p.fi.fi] = true; | |
rep(i, 4) { | |
int nx = p.fi.fi + dx[i], ny = p.fi.se + dy[i]; | |
if(nx < 0 || ny < 0) continue; | |
if(field[ny][nx] <= p.se + 1) continue; | |
que.push(Pi(P(nx, ny), p.se + 1)); | |
} | |
} | |
return INF; | |
} | |
signed main() { | |
cin.tie(0); | |
ios::sync_with_stdio(false); | |
int m; | |
scanf("%d", &m); | |
rep(i, 400) rep(j, 400) field[i][j] = INF; | |
rep(i, m) { | |
int x, y, t; | |
scanf("%d %d %d", &x, &y, &t); | |
chmin(field[y][x], t); | |
rep(j, 4) { | |
int hx = x + dx[j], hy = y + dy[j]; | |
if(hx < 0 || hy < 0) continue; | |
chmin(field[hy][hx], t); | |
} | |
} | |
int res = bfs(0); | |
printf("%d\n", (res == INF) ? -1 : res); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment