题意是给你一个字符矩阵,‘*’是障碍物,给你起点r,c,和最多能向左走的步数x,最多能向右走的步数y
问你可访问的点的个数是多少
一开始用直接暴力BFS被hack了,后来加上优先队列就能ac
#include<bits/stdc++.h> #define ll long long using namespace std; char data[2005][2005]; int n,m,r,c,x,y; struct node { int x,y,l,r; bool operator <(node a)const { if(l==a.l) return r>a.r; else return l>a.l; } }; int ans=0; bool isok(int x,int y) { return x>=0&&x<n&&y>=0&&y<m&&data[x][y]!='*'; } int vis[2005][2005]; int dir[4][2]= {-1,0,0,1,1,0,0,-1}; void bfs() { priority_queue<node>q; q.push(node{r-1,c-1,0,0}); vis[r-1][c-1]=1; // data[r-1][c-1]='+'; ans++; while(!q.empty()) { node u = q.top(); q.pop(); // printf("%d %d ",u.x,u.y); for (int i=0; i<4 ; i++ ) { int newx=u.x+dir[i][0]; int newy=u.y+dir[i][1]; if(isok(newx,newy) && !vis[newx][newy] && (u.l+(i==3?1:0)<=x) && (u.r+(i==1?1:0)<=y) ) { ans++; // data[newx][newy]='+'; vis[newx][newy]=1; q.push(node{newx,newy,u.l+(i==3?1:0),u.r+(i==1?1:0)}); } } } } int main() { ans=0; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); scanf("%d%d",&r,&c); scanf("%d%d",&x,&y); getchar(); for (int i=0; i<n ; i++ ) { for (int j=0; j<m ; j++ ) { data[i][j]=getchar(); } getchar(); } bfs(); printf("%d",ans); }