weizhuwyzc000的专栏

BFS题,不过多了一个很有意思的条件:不能连续穿越K个障碍,,就好像多了一个技能一样,我用pre【】【】数组来记录目前的k值;

#include<bits/stdc++.h>using namespace std;int a[30][30],T,m,n,k,d[30][30],pre[30][30],air[10]={1,0,-1,0},air2[10]={0,1,0,-1};;typedef pair<int,int> P;P s[450];int bfs() {queue<P> q;memset(d,-1,sizeof(d));memset(pre,-1,sizeof(pre));d[1][1]=0; pre[1][1]=k;q.push(P(1,1));while(!q.empty()) {P u=q.front(); q.pop();if(u.first==m&&u.second==n) return d[u.first][u.second];for(int i=0;i<4;i++){P v=P(u.first+air[i],u.second+air2[i]);if(v.first>=1&&v.first<=m&&v.second>=1&&v.second<=n){if(d[v.first][v.second]==-1&&a[v.first][v.second]==0) {d[v.first][v.second]=d[u.first][u.second]+1;pre[v.first][v.second]=k;q.push(v);}else if(a[v.first][v.second]==1&&pre[u.first][u.second]>0){d[v.first][v.second]=d[u.first][u.second]+1;pre[v.first][v.second]=pre[u.first][u.second]-1;q.push(v);}}}}return -1;}int main() {scanf("%d",&T);while(T–) {scanf("%d%d%d",&m,&n,&k);for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)scanf("%d",&a[i][j]);int maxn=bfs();printf("%d\n",maxn);}return 0;}

穷则思变,差则思勤!没有比人更高的山没有比脚更长的路。

weizhuwyzc000的专栏

相关文章:

你感兴趣的文章:

标签云: