帮 大一学弟 AC一道题
❥题面
❥思路
每个格子都可以作为开始节点,遍历找到最大数,可以直接用优先队列去模拟,或者用dfs+回溯
❥代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct Node {
int val,zx,zy;
bool operator<(const Node&e) const{return val < e.val;}
};
int answer;
int main() {
int number,time;
cin>>number>>time;
priority_queue<Node> ok;
for(int i=1; i<=number; i++) {
for(int j=1; j<=number; j++) {
int val; cin>>val;
ok.push({val,i,j});
}
}
int tmpx=1, tmpy=1;
while(time && ok.size()) {
Node cur = ok.top(); ok.pop();
int val = cur.val, zx=cur.zx, zy=cur.zy;
int get_need = abs(zx-tmpx) + abs(zy-tmpy) + val;
if(get_need<=time) {
answer += val; time -= get_need; tmpx=zx,tmpy=zy;
}
else break;
}
cout<<answer;
return 0;
}