UVA 1600 Patrol Robot

带状态的bfs

用一个数(ks)来表示状态-当前连续穿越的障碍数;

step表示当前走过的步数;

visit数组也加一个状态;

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; const int maxn = ; struct node {
int x,y;
int step;
int ks;
void init (int nx,int ny,int nstep,int nks){
x=nx;y=ny;step=nstep;ks=nks;
}
}; int n,m,k;
int map[maxn][maxn],visit[maxn][maxn][maxn];
int dir[][]={,,-,,,,,-}; int bfs (){
queue<node> q;
while (!q.empty ())
q.pop ();
node a,b;
a.init (,,,);
q.push (a);
while (!q.empty ()){
a=q.front ();
q.pop (); int xx,yy,sstep,kss;
for (int i=;i<;i++){
xx=a.x;yy=a.y;sstep=a.step;kss=a.ks;
xx+=dir[i][];
yy+=dir[i][];
sstep+=;
if (xx<||xx>n||yy<||yy>m)
continue ;
if (map[xx][yy])
kss++;
else kss=;
if (kss>k)
continue ;
if (visit[kss][xx][yy])
continue ;
if (xx==n&&yy==m)
return sstep ;
b.init (xx,yy,sstep,kss);
q.push (b);
visit[kss][xx][yy]=;
}
}
return -;
} int main (){
int t;
cin>>t;
while (t--){
cin>>n>>m>>k;
if (n==m&&n==){
cout<<<<"error"<<endl;
continue ;
}
memset (visit,,sizeof visit);
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
cin>>map[i][j];
int ans=bfs ();
cout<<ans<<endl;
}
return ;
}
上一篇:beta冲刺总结-咸鱼


下一篇:Beta 冲刺(6/7)