题目 Tractor.
代码很好写,就是在经典bfs基础上加一个贪心的思想
从起点开始,往四个方向走,碰到有草的且没有走过的就放在最前面优先搜索(可以用双端队列或者优先队列实现);
到原点就输出草的数量;
优先队列代码
#include<bits/stdc++.h>
#define ll long long
#define T int t;scanf("%d", &t);while(t--)
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
const ll mod = 1e9+7;
const int maxn = 2e5+5;
int vis[1005][1005]={0}; //标记草
int book[1005][1005]={0}; //标记是否走过
int n;
int s, t;
int main(){
scanf("%d %d %d", &n, &s, &t);
for(int i = 0; i < n; i++){
int x,y;
scanf("%d %d", &x, &y);
vis[x][y] = 1; //有草标记1
}
//优先队列 要删去草的数量少的前面先搜,小顶堆
priority_queue<pair<int,pair<int,int> >, vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > q;
q.push({0,{s,t}});
book[s][t] = 1; //走过标记1
while(!q.empty()){
int x = q.top().second.first;
int y = q.top().second.second;
int step = q.top().first;
q.pop();
if(x == 0 && y == 0){
cout<<step<<endl;
return 0;
}
for(int i = 0; i < 4;i ++){
int tx = x + dx[i];
int ty = y + dy[i];
if(tx >= 0 && tx <= 1003 && ty >= 0 && ty <= 1003 && book[tx][ty] == 0){
book[tx][ty] = 1;
if(vis[tx][ty]) 如果格中有草,step+1;
q.push({step+1,{tx,ty}});
else
q.push({step,{tx,ty}});
}
}
}
return 0;
}
双端队列代码
#include<bits/stdc++.h>
#define ll long long
#define T int t;scanf("%d", &t);while(t--)
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
const ll mod = 1e9+7;
const int maxn = 2e5+5;
int vis[1005][1005]={0}; //记录有没有草
int book[1005][1005]={0};
int ans[1005][1005]={0}; //记录到一格要删去多少草
int n;
int s, t;
int main(){
scanf("%d %d %d", &n, &s, &t);
for(int i = 0; i < n; i++){
int x,y;
scanf("%d %d", &x, &y);
vis[x][y] = 1;
}
deque<pair<int,int> > q;
q.push_back({s,t});
book[s][t] = 1;
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop_front();
if(x == 0 && y == 0){
cout<<ans[0][0]<<endl;
return 0;
}
for(int i = 0; i < 4;i ++){
int tx = x + dx[i];
int ty = y + dy[i];
if(tx >= 0 && tx <= 1003 && ty >= 0 && ty <= 1003 && book[tx][ty] == 0){
book[tx][ty] = 1;
ans[tx][ty] = ans[x][y] + vis[tx][ty]; //更新要删草
if(vis[tx][ty])
q.push_back({tx,ty}); //如果有草,放到队尾搜
else
q.push_front({tx,ty}); //如果没草,放到队头搜
}
}
}
return 0;
}