UVA 1600 Patrol Robot
2016-02-27 18:17
232 查看
题意:一个机器人从m*n的格子的左上角(1,1)走到右下角(m,n),格子是空地或者障碍,机器人每次可以向四个方向走一步,但是不能穿越连续k个障碍,求最短步数。
思路:bfs求最短路。考虑一种情况,对于一个位置,可能先连续穿越障碍去接近这个点,步数最小。但是如果这个点四周都是障碍,而现在连续穿越的障碍数已经达到上限了,这条路就没法继续走下去了。反之如果一开始绕一下路使得到达该点时连续穿越的障碍数小一些,那么这条路就可以通了。所以考虑一个点能不能走多次,就是看当前到达这个点连续穿越的障碍数是不是比之前的要少,就可以更新一下。
思路:bfs求最短路。考虑一种情况,对于一个位置,可能先连续穿越障碍去接近这个点,步数最小。但是如果这个点四周都是障碍,而现在连续穿越的障碍数已经达到上限了,这条路就没法继续走下去了。反之如果一开始绕一下路使得到达该点时连续穿越的障碍数小一些,那么这条路就可以通了。所以考虑一个点能不能走多次,就是看当前到达这个点连续穿越的障碍数是不是比之前的要少,就可以更新一下。
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <queue> #include <algorithm> using namespace std; #define rep(i,j,k) for(int i = j; i <= k; i++ ) #define Rrep(i,j,k) for(int i = j; i >= k; i-- ) #define Clean(x,y) memset(x,y,sizeof(x)) const int dx[4] = {0,0,1,-1}; const int dy[4] = {1,-1,0,0}; struct node { int x,y,step,now; node(int a = 0,int b = 0,int c = 0,int d = 0):x(a),y(b),step(c),now(d){} }; int T; int k,n,m; int map[30][30]; int t[30][30]; //记录到达当前点需要连续穿过的障碍数 int bfs() { int tx,ty; queue<node> q; q.push( node(1,1,0,0) ); node temp; rep(i,1,n) rep(j,1,m) t[i][j] = 1000; while(!q.empty()) { temp = q.front(); if ( temp.x == n && temp.y == m ) { return temp.step; } q.pop(); rep(i,0,3) { tx = temp.x + dx[i]; ty = temp.y + dy[i]; int p = ( map[tx][ty]==1 ? temp.now+1 : 0 ); if ( tx>=1 && tx<=n && ty>=1 && ty<=m && p<t[tx][ty] ) { t[tx][ty] = p; if ( map[tx][ty] == 1 && temp.now >= k ) continue; q.push( node( tx,ty,temp.step+1,p ) ); } } } return -1; } int main() { cin>>T; while(T--) { cin>>n>>m; cin>>k; rep(i,1,n) rep(j,1,m) scanf("%d",&map[i][j]); cout<<bfs()<<endl; } return 0; }
相关文章推荐
- Android 实现形态各异的双向侧滑菜单 自定义控件来袭
- shell脚本实例-matrix
- 菜鸟在线教你用Unity3D开发VR版的Hello World
- ZOJ 2110 Tempter of the Bone (DFS)
- 企业后台模板
- java设计模式(四)--单例模式
- UVA 439 Knight Moves
- spring学习笔记 - 面试题
- 猴子吃桃子的问题
- FPGA知识大梳理(二)verilogHDL语法入门(1)
- CodeForces 612 D. The Union of k-Segments(水~)
- javascript高级程序设计笔记-第三章(基本概念)
- codeforces 628D 数位dp
- 绘画基础研究(一)-画面的对象组成与形状组成
- 如何获取 yum 安装的 rpm 包位置---详解
- CF 50A Domino piling
- C语言中的类型转换
- 学习《Python核心编程》做一下知识点提要,方便复习(一)
- POJ 2376 Cleaning Shifts (贪心区间最少覆盖)
- Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy settings