uva1600 Patrol Robot 巡逻机器人(BFS)
来源:互联网 发布:windows安装apache ab 编辑:程序博客网 时间:2024/05/18 09:17
</pre>Description<div class="textBG" style="border-width: 2px; border-style: dotted; padding: 10px; font-family: 'times new roman'; font-size: 17px; background-color: rgb(234, 235, 255);"><span style="float: right;"><a target=_blank style="color: blue; text-decoration: none;" href="http://7xjob4.com1.z0.glb.clouddn.com/1e64a3999d53f31d7746044a838fd900" target="_blank"><img width="100" height="26" title="Download as PDF" alt="Download as PDF" src="http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png" border="0" /></a></span> <div style="clear: both;"></div><p style="margin-top: 0px; margin-bottom: 0pt;">A robot has to patrol around a rectangular area which is in a form of <span class="MATH"><em>m</em><tt>x</tt><em>n</em></span> grid (<span class="MATH"><em>m</em></span> rows and <span class="MATH"><em>n</em></span> columns). The rows are labeled from 1 to <span class="MATH"><em>m</em></span>. The columns are labeled from 1 to <span class="MATH"><em>n</em></span>. A cell <span class="MATH">(<em>i</em>, <em>j</em>)</span> denotes the cell in row <span class="MATH"><em>i</em></span> and column <span class="MATH"><em>j</em></span> in the grid. At each step, the robot can only move from one cell to an adjacent cell, i.e. from <span class="MATH">(<em>x</em>, <em>y</em>)</span> to <span class="MATH">(<em>x</em> + 1, <em>y</em>)</span>, <span class="MATH">(<em>x</em>, <em>y</em> + 1)</span>, <span class="MATH">(<em>x</em> - 1, <em>y</em>)</span> or <span class="MATH">(<em>x</em>, <em>y</em> - 1)</span>. Some of the cells in the grid contain obstacles. In order to move to a cell containing obstacle, the robot has to switch to turbo mode. Therefore, the robot cannot move continuously to more than <span class="MATH"><em>k</em></span> cells containing obstacles.</p><p style="margin-top: 0px; margin-bottom: 0pt;">Your task is to write a program to find the shortest path (with the minimum number of cells) from cell (1, 1) to cell <span class="MATH">(<em>m</em>, <em>n</em>)</span>. It is assumed that both these cells do not contain obstacles.</p><p style="margin-top: 0px; margin-bottom: 0pt;"></p><h2 style="margin-bottom: 0px;"><a target=_blank name="t0"></a><span style="color: rgb(255, 0, 0); font-size: 18px;"><a target=_blank name="SECTION0001001000000000000000" style="color: blue; text-decoration: none;" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49764" target="_blank">Input</a> </span></h2><p style="margin-top: 0px; margin-bottom: 0pt;">The input consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.</p><p style="margin-top: 0px; margin-bottom: 0pt;">For each data set, the first line contains two positive integer numbers <span class="MATH"><em>m</em></span> and <span class="MATH"><em>n</em></span> separated by space <span class="MATH">(1<img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" /><em>m</em>, <em>n</em><img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" />20)</span>. The second line contains an integer number <span class="MATH"><em>k</em></span><span class="MATH">(0<img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" /><em>k</em><img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" />20)</span>. The <span class="MATH"><em>i</em><sup>th</sup></span> line of the next <span class="MATH"><em>m</em></span> lines contains <span class="MATH"><em>n</em></span> integer <span class="MATH"><em>a</em><sub>ij</sub></span> separated by space <span class="MATH">(<em>i</em> = 1, 2,..., <em>m</em>;<em>j</em> = 1, 2,..., <em>n</em>)</span>. The value of <span class="MATH"><em>a</em><sub>ij</sub></span> is <tt>1</tt> if there is an obstacle on the cell <span class="MATH">(<em>i</em>, <em>j</em>)</span>, and is <tt>0</tt> otherwise.</p><p style="margin-top: 0px; margin-bottom: 0pt;"></p><h2 style="margin-bottom: 0px;"><a target=_blank name="t1"></a><span style="color: rgb(255, 0, 0); font-size: 18px;"><a target=_blank name="SECTION0001002000000000000000" style="color: blue; text-decoration: none;" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49764" target="_blank">Output</a> </span></h2><p style="margin-top: 0px; margin-bottom: 0pt;">For each data set, if there exists a way for the robot to reach the cell <span class="MATH">(<em>m</em>, <em>n</em>)</span>, write in one line the integer number <span class="MATH"><em>s</em></span>, which is the number of moves the robot has to make; <tt>-1</tt> otherwise.</p><p style="margin-top: 0px; margin-bottom: 0pt;"></p><h2 style="margin-bottom: 0px;"><a target=_blank name="t2"></a><span style="color: rgb(255, 0, 0); font-size: 18px;"><a target=_blank name="SECTION0001003000000000000000" style="color: blue; text-decoration: none;" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49764" target="_blank">Sample Input</a> </span></h2><p style="margin-top: 0px; margin-bottom: 0pt;"></p><pre style="white-space: pre-wrap; -ms-word-wrap: break-word;">3 2 5 0 0 1 0 0 0 0 0 0 1 0 4 6 1 0 1 1 0 0 00 0 1 0 1 10 1 1 1 1 00 1 1 1 0 02 2 0 0 1 1 0
Sample Output
7 10 -1
代码如下 :(不要直接复制 有些许不一样)
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;int maze[22][22];int n,m,ax,ay,gx,gy,k,T;struct node{ int x; int y; int num;}a,b;int j;int dx[]={0,1,0,-1};int dy[]={1,0,-1,0};int d[22][22];int vis[22][22];int bfs(){j=0;memset(d,0,sizeof(d));memset(vis,0,sizeof(vis)); a.x=1;a.y=1;a.num=0; queue<node>q; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); if(a.x==m&&a.y==n) return d[a.x][a.y]; for(int i=0;i<4;i++) { b.x=a.x+dx[i]; b.y=a.y+dy[i]; b.num=a.num; if(maze[b.x][b.y]==1) { a.num++; b.num=a.num; } if(maze[b.x][b.y]==0) b.num=0; if(b.x>0&&b.x<=m&&b.y>0&&b.y<=n&&vis[b.x][b.y]==0&&b.num<=k) { d[b.x][b.y]=d[a.x][a.y]+1; vis[b.x][b.y]=-1; q.push(b); j++; if(j>m*n) return -1; } } } return -1;}int main(){while(~scanf("%d",&t)&&t){ while(t--){ scanf("%d%d",&m,&n); scanf("%d",&k); for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { scanf("%d",&maze[i][j]); } } int t=bfs(); cout<<t<<endl; } } return 0;}
0 0
- uva1600 Patrol Robot 巡逻机器人(BFS)
- Patrol Robot UVa1600巡逻机器人
- Patrol Robot UVa1600巡逻机器人
- 巡逻机器人(Patrol Roboot,UVa1600)
- uva1600-Patrol Robot(bfs)
- UVA 1600 Patrol Robot 巡逻机器人(BFS)
- 算法竞赛入门经典 第二版 习题6-5 巡逻机器人 Patrol Robot uva1600
- UVa1600 习题6-5 巡逻机器人 (Patrol Robot,ACM/ICPC Hanoi 2006)
- 习题6-5 巡逻机器人(Patrol Robot, ACM/ICPC Hanoi 2006, UVa1600)
- Uva1600 - Patrol Robot(bfs解法)
- 习题6-5 UVa1600 Patrol Robot(BFS)
- Uva1600 Patrol Robot 【bfs】【习题6-5】
- uva1600 Patrol Robot —— bfs
- Patrol Robot 巡逻的机器人 UVA
- uva1600 - Patrol Robot
- UVA1600 - Patrol Robot
- uva1600 Patrol Robot
- uva1600 -- Patrol Robot
- Lucene token解析
- Spring(AbstractRoutingDataSource)实现动态数据源切换
- 手动配置 hibernate 项目
- Retrofit的简单使用
- 1022 D进制的A+B
- uva1600 Patrol Robot 巡逻机器人(BFS)
- 计算字符串最后一个单词的长度,单词以空格隔开
- 三次握手 resend
- LeetCode(349)Intersection of Two Arrays
- JDK版本转换Unsupported major.minor version 52.0
- SQL执行顺序
- RasGetCustomAuthData
- 表关系(一对一,一对多,多对多)
- 通过git shell 向github上传文献