1. 程式人生 > 實用技巧 >《3432: Meteor Shower》

《3432: Meteor Shower》

一開始以為只能在第一象限。。

這題主要就是可能0~300的點全都不是安全的,所以bfs的判斷邊界要到305才行。

然後,可能開始的點會被旁邊的點炸到,這樣開始就死了。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e4+5;
const int M = 1e6+5;
const LL Mod = 1e9 + 7;
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define
IO ios::sync_with_stdio(false) #define dbg(ax) cout << "now this num is " << ax << endl; namespace FASTIO{ inline LL read(){ LL x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();} while(c >= '0' && c <= '
9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();} return x * f; } } using namespace FASTIO; int a[305][305],MI[305][305],b[4][2] = {1,0,-1,0,0,1,0,-1}; bool vis[305][305]; struct Node{int x,y,tim;}; int bfs(int x,int y) { queue<Node> Q; Q.push(Node{x,y,0}); vis[x][y]
= 1; while(!Q.empty()) { Node q = Q.front(); Q.pop(); if(a[q.x][q.y] != -1) return q.tim; for(int i = 0;i < 4;++i) { int px = q.x + b[i][0]; int py = q.y + b[i][1]; if(px >= 0 && px < 305 && py >= 0 && py < 305 && !vis[px][py] && q.tim + 1 < MI[px][py]) { vis[px][py] = 1; Q.push(Node{px,py,q.tim + 1}); } } } return -1; } int main() { int m; while(~scanf("%d",&m)) { memset(a,0,sizeof(a)); memset(vis,0,sizeof(vis)); memset(MI,0x3f,sizeof(MI)); for(int i = 1;i <= m;++i) { int x,y,t;x = read(),y = read(),t = read(); a[x][y] = -1; MI[x][y] = min(MI[x][y],t); for(int j = 0;j < 4;++j) { int px = x + b[j][0]; int py = y + b[j][1]; if(px >= 0 && py >= 0) { a[px][py] = -1; MI[px][py] = min(MI[px][py],t); } } } if(MI[0][0] <= 0) printf("-1\n"); else printf("%d\n",bfs(0,0)); } //system("pause"); return 0; }
View Code