-
4 8 ....bb.. ........ .....b.. ...bb...
样例输出
-
1
描述
You work as an intern at a robotics startup. Today is your company's demo day. During the demo your company's robot will be put in a maze and without any information about the maze, it should be able to find a way out.
The maze consists of N * M grids. Each grid is either empty(represented by '.') or blocked by an obstacle(represented by 'b'). The robot will be release at the top left corner and the exit is at the bottom right corner.
Unfortunately some sensors on the robot go crazy just before the demo starts. As a result, the robot can only repeats two operations alternatively: keep moving to the right until it can't and keep moving to the bottom until it can't. At the beginning, the robot keeps moving to the right.
rrrrbb.. ...r.... ====> The robot route with broken sensors is marked by 'r'. ...rrb.. ...bb...
While the FTEs(full-time employees) are busy working on the sensors, you try to save the demo day by rearranging the maze in such a way that even with the broken sensors the robot can reach the exit successfully. You can change a grid from empty to blocked and vice versa. So as not to arouse suspision, you want to change as few grids as possible. What is the mininum number?
输入
Line 1: N, M.
Line 2-N+1: the N * M maze.
For 20% of the data, N * M <= 16.
For 50% of the data, 1 <= N, M <= 8.
For 100% of the data, 1<= N, M <= 100.
输出
The minimum number of grids to be changed.
题目链接:http://hihocoder.com/problemset/problem/1290
题目大意:一个机器人从左上要走到右下,每次先一直往右走,被挡了就一直往下走,'b'为障碍,每个点可以把'b'变成'.',也可以把'.'变成'b',问使得机器人按规则走到终点最少要改变几个点的状态
题目分析:dp[i][j][k]表示走到点(i, j)方向为k (0为右,1为下)时所要改变的最小次数,状态转移的时候如果本来就有b挡着那可以直接转,否则要转的话次数要加1,因为相当于改变一个'.'的状态来让它转向,初始状态dp[1][1][0] = 0
#include <cstdio>
#include <algorithm>
using namespace std;
int const MAX = 1e2 + 5;
int n, m;
int dp[MAX][MAX][2]; //0 -> 右, 1 -> 下
char s[MAX][MAX];
int main()
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%s", s[i] + 1);
for(int i = 0; i <= n; i++)
for(int j = 0; j <= m; j++)
dp[i][j][0] = dp[i][j][1] = 0x3fffffff;
dp[1][1][0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(i == 1 && j == 1)
continue;
dp[i][j][1] = dp[i - 1][j][1];
if(s[i - 1][j + 1] == 'b' || j == m)
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0]);
else
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0] + 1);
dp[i][j][0] = dp[i][j - 1][0];
if(s[i + 1][j - 1] == 'b' || i == n)
dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][1]);
else
dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][1] + 1);
if(s[i][j] == 'b')
{
dp[i][j][0] ++;
dp[i][j][1] ++;
}
}
}
printf("%d\n", min(dp[n][m][0], dp[n][m][1]));
}

本文介绍了一种通过调整迷宫布局使机器人能成功找到出口的方法。机器人只能左右或上下移动,文章提出使用动态规划算法计算最少需要改变多少个网格状态。

被折叠的 条评论
为什么被折叠?



