【UVa】1600 – Patrol Robot
来源:互联网 发布:ubuntu 14.04 selinux 编辑:程序博客网 时间:2024/05/20 03:37
Problem
A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j)denotes the cell in rowi and column j in the grid. At each step, the robot can only move from one cell to an adjacent cell, i.e. from (x, y) to (x + 1, y), (x, y + 1), (x – 1, y) or (x, y – 1). 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 k cells containing obstacles.
Your task is to write a program to find the shortest path (with the minimum number of cells) from cell (1, 1) to cell (m, n). It is assumed that both these cells do not contain obstacles.
Input
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.
For each data set, the first line contains two positive integer numbers m and n separated by space (1
Output
For each data set, if there exists a way for the robot to reach the cell (m, n), write in one line the integer number s, which is the number of moves the robot has to make; -1 otherwise.
Sample Input
3
2 5
0
0 1 0 0 0
0 0 0 1 0
4 6
1
0 1 1 0 0 0
0 0 1 0 1 1
0 1 1 1 1 0
0 1 1 1 0 0
2 2
0
0 1
1 0
Sample Output
7
10
-1
Solution
bfs
#include <iostream>#include <queue>#include <vector>#include "memory.h"using namespace std;int map[30][30];bool visit[30][30][30];int n, m;int ans;int goToX[] = {0, 0, 1, -1};int goToY[] = {1, -1, 0, 0};struct Node{ int x; int y; int k; int step;};void bfs(int k){ Node ns; queue<Node> nodes; ns.x = 0; ns.y = 0; ns.k = k; ns.step = 0; nodes.push(ns); while(!nodes.empty()){ ns = nodes.front(); nodes.pop(); if(ns.x == m-1 && ns.y == n-1){ ans = ns.step; return; } Node visited; if(ns.k >= 0){ for(int i = 0; i < 4; i++){ visited.x = ns.x + goToX[i]; visited.y = ns.y + goToY[i]; visited.step = ns.step+1; if(map[visited.x][visited.y]){ visited.k = ns.k-1; }else{ visited.k = k; } if(visited.x >= 0 && visited.x < m && visited.y >= 0 && visited.y < n && !visit[visited.x][visited.y][visited.k]){ if(visited.k >= 0){ nodes.push(visited); visit[visited.x][visited.y][visited.k] = true; } } } } } ans = -1 ; }int main(){ int dataSetSize; while(cin >> dataSetSize){ while(dataSetSize --){ memset(map, 0, sizeof(map)); memset(visit, false, sizeof(visit)); int k; cin >> m >> n; cin >> k; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ cin >> map[i][j]; } } bfs(k); cout << ans << endl; } } return 0;}
- 【UVa】1600 – Patrol Robot
- 【uva】1600 - Patrol Robot
- UVa 1600 - Patrol Robot
- UVA 1600 Patrol Robot
- UVa 1600 Patrol Robot
- UVA 1600 Patrol Robot
- Uva - 1600 - Patrol Robot
- uva 1600 Patrol Robot
- UVA - 1600 Patrol Robot
- UVA 1600 Patrol Robot
- UVa 1600 Patrol Robot
- UVA 1600 Patrol Robot
- UVA 1600 Patrol Robot
- uva 1600Patrol Robot
- UVa 1600-Patrol Robot题解
- UVA 1600 Patrol Robot bfs
- UVA 1600 - Patrol Robot( BFS & DFS)
- UVa 1600:Patrol Robot(IDA*)
- C++11以前的序列点
- Java数据结构----树--红黑树
- oracle 11g归档日志研究_1
- java web文件上传功能实现
- .NET(C#)连接各类数据库-集锦
- 【UVa】1600 – Patrol Robot
- 无监督学习与维度约减 机器学习基础(7)
- Win7系统32位Oracle11g客户端安装详述
- Eclipse 无法启动Tomcat原因总结
- 【UVa】11727 – Cost Cutting
- javascript:void(0)
- Eclipse快捷键 10个最有用的快捷键
- 超越线程池:Java并发并没有你想的那么糟糕 http://developer.51cto.com/art/201503/469648.htm
- java 位运算符