BFS problem, but there is a very interesting condition: can not continuously cross the K barrier, as if more than one skill, I use the pre "" "" Array to record the current K value;
#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 ()); 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.s econd]==-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.se Cond]=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;}
1600-patrol Robot (BFS)