NC206693. 冒险者
描述
输入描述
第一行一个整数T(1≤T≤10),表示共有T组测试数据。
对于每组测试数据,第一行有三个整数。
w-冒险者的初始战斗力
d-d点经验提升1点战斗力
k-分身数量
第二行有两个整数n,m(1≤n,m≤500)。
n-迷宫行数m-迷宫列数接下来 n 行,每行 m 个数字 a i,j( 0 ≤ a i,j ≤ 1e9 )
输出描述
如果冒险者不能完成任务,输出第一行"NO";否则输出第一行"YES",并在第二行输出冒险者战斗力提升了多少。
示例1
输入:
2 2 1 2 1 5 0 1 1 1 0 2 1 1 2 2 0 2 2 1
输出:
YES 3 NO
C++14(g++5.4) 解法, 执行用时: 560ms, 内存消耗: 9708K, 提交时间: 2020-06-07 15:14:14
#include <bits/stdc++.h> using namespace std; int dx[] = {0,1,0,-1,0}; int dy[] = {0,0,1,0,-1}; const int N = 1010; int p[N][N]; long long a[N][N]; struct Node { int x, y; long long v; Node(int xx,int yy,long long vv) { x = xx; y = yy; v = vv; } }; bool operator>(Node a, Node b) { return a.v > b.v; } struct indoor { /* data */ int x,y; }in[1010]; int main() { ios::sync_with_stdio(false); int T; cin >> T; for (;T--;) { int inum = 0; long long w, d, k, n, m; cin >> w >> d >> k; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> a[i][j]; p[i][j] = 0; if (!a[i][j]) { in[++inum].x = i;in[inum].y = j;p[i][j] = 1;} } priority_queue<Node,vector<Node>,greater<Node> > q; for (int i = 1; i <= inum; i++) q.push(Node(in[i].x,in[i].y,0)); long long now = w * d; long long ans = 0; for (;!q.empty();) { int x = q.top().x; int y = q.top().y; int v = q.top().v; q.pop(); if (now/d <= v) { ans = -1; break; } else now += v; for (int i = 1; i <= 4; i++) { int nx = x+dx[i]; int ny = y+dy[i]; if (nx > 0 && ny > 0 && nx <= n && ny <= m && !p[nx][ny]) { p[nx][ny] = 1; q.push(Node(nx, ny, a[nx][ny])); } } } if (ans != -1) { cout << "YES" << endl; cout << now/d-w << endl; } else cout << "NO" << endl; } }
C++11(clang++ 3.9) 解法, 执行用时: 786ms, 内存消耗: 5980K, 提交时间: 2020-06-08 17:00:32
#include<bits/stdc++.h> using namespace std; typedef struct{ int x, y; long long value; }Moster; bool operator<(Moster a,Moster b){ return a.value>b.value; } long long maps[505][505]; bool maps1[505][505]; int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1}; int main(){ int T; scanf("%d",&T); while(T--){ priority_queue<Moster> que; memset(maps1,true,sizeof(maps1)); long long n, m, w, d, k; scanf("%lld%lld%lld%lld%lld",&w,&d,&k,&n,&m); for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ scanf("%lld",&maps[i][j]); if(maps[i][j]==0){ Moster M; maps1[i][j]=false; M.x=i;M.y=j;M.value=0; que.push(M); } } } long long EXP = 0; while(!que.empty()&&w+EXP/d>que.top().value){ Moster M = que.top();que.pop(); EXP+=M.value; int x = M.x, y = M.y; for(int i = 0;i < 4;i++){ int nx = x + dx[i], ny = y + dy[i]; if(nx>=0&&ny>=0&&nx<n&&ny<m&&maps1[nx][ny]){ maps1[nx][ny]=false; M.x=nx;M.y=ny;M.value=maps[nx][ny]; que.push(M); } } } if(que.empty()) printf("YES\n%lld\n",EXP/d); else printf("NO\n"); while(!que.empty()) que.pop(); } return 0; }