UVa——1600(巡逻机器人)
迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值。比较简单的三维bfs。写搜索题的话一定要注意细节。这个题花了好长的时间。因为k的原因,一开始用了k的原因,dfs好想一些,因为可以回溯,k的值搜完一个方向,然后回溯。那样就很简单。而且数据是20*20.
但是最后dfs还是T了。
AC的bfs
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 #include <fstream> 8 #include <cstdio> 9 #include <cmath> 10 #include <stack> 11 #include <queue> 12 using namespace std; 13 const double Pi=3.14159265358979323846; 14 typedef long long ll; 15 const int MAXN=5000+5; 16 int dx[4]={-1,1,0,0}; 17 int dy[4]={0,0,1,-1}; 18 const int INF = 0x3f3f3f3f; 19 const int NINF = 0xc0c0c0c0; 20 const ll mod=1e9+7; 21 int vis[25][25][25]; 22 int M[25][25]; 23 int m,n; 24 struct node{ 25 int x,y,step,k; 26 node (int x=0,int y=0,int step=0,int k=0) 27 { 28 this->x=x; 29 this->y=y; 30 this->step=step; 31 this->k=k; 32 } 33 }tim,now; 34 int bfs(int x,int y,int k) 35 { 36 queue <node> Q; 37 Q.push(node(1,1,0,k)); 38 vis[1][1][k]=1; 39 while(!Q.empty()) 40 { 41 tim=Q.front();Q.pop(); 42 if(tim.x==m&&tim.y==n) 43 { 44 return tim.step; 45 } 46 for(int i=0;i<4;i++) 47 { 48 now.x=dx[i]+tim.x; 49 now.y=dy[i]+tim.y; 50 if(M[now.x][now.y]==1) now.k=tim.k-1; 51 else now.k=k; 52 if(now.x>=1&&now.x<=m&&now.y>=1&&now.y<=n&&(M[now.x][now.y]==0||now.k>=0)&&!vis[now.x][now.y][now.k]) 53 { 54 now.step=tim.step+1; 55 Q.push(now); 56 //cout << now.x<<" "<< now.y<< " "<< now.step<<endl; 57 vis[now.x][now.y][now.k]=1; 58 } 59 } 60 } 61 return -1; 62 } 63 int main() 64 { 65 int t;cin>>t; 66 while(t--) 67 { 68 cin>>m>>n; 69 int k;cin>>k; 70 memset(vis,0,sizeof(vis)); 71 for(int i=1;i<=m;i++) 72 for(int j=1;j<=n;j++) 73 cin>>M[i][j]; 74 cout <<bfs(1,1,k)<<endl; 75 } 76 return 0; 77 }
TLE的dfs
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 #include <fstream> 8 #include <cstdio> 9 #include <cmath> 10 #include <stack> 11 #include <queue> 12 using namespace std; 13 const double Pi=3.14159265358979323846; 14 typedef long long ll; 15 const int MAXN=5000+5; 16 int dx[4]={-1,1,0,0}; 17 int dy[4]={0,0,1,-1}; 18 const int INF = 0x3f3f3f3f; 19 const int NINF = 0xc0c0c0c0; 20 const ll mod=1e9+7; 21 int m,n; 22 int vis[25][25]; 23 int M[25][25]; 24 int step[25][25]; 25 int ans=INF; 26 int tk; 27 void dfs(int x,int y,int k) 28 { 29 if(x==m&&y==n) 30 { 31 if(ans>step[x][y]) 32 ans=step[x][y]; 33 return; 34 } 35 for(int i=0;i<4;i++) 36 { 37 int sx=dx[i]+x; 38 int sy=dy[i]+y; 39 if(sx>=1&&sx<=m&&sy>=1&&sy<=n&&!vis[sx][sy]&&(k>0||M[sx][sy]==0)) 40 { 41 int pk=k; 42 if(M[sx][sy]==1) pk--; 43 else pk=tk; 44 vis[sx][sy]=1; 45 step[sx][sy]=step[x][y]+1; 46 dfs(sx,sy,pk); 47 vis[sx][sy]=0; 48 } 49 } 50 } 51 int main() 52 { 53 int t;cin>>t; 54 while(t--) 55 { 56 ans=INF; 57 memset(vis,0,sizeof(vis)); 58 memset(step,-1,sizeof(step)); 59 cin>>m>>n;cin>>tk; 60 for(int i=1;i<=m;i++) 61 for(int j=1;j<=n;j++) 62 cin>>M[i][j]; 63 step[1][1]=0; 64 vis[1][1]=1; 65 dfs(1,1,tk); 66 if(step[m][n]!=-1) cout <<ans<<endl; 67 else cout <<-1<<endl; 68 } 69 return 0; 70 }