题目1 : Demo Day
时间限制:
10000ms
单点时限:
1000ms
内存限制:
256MB
描述
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.
样例输入
-
4 8
....bb..
........
.....b..
...bb...
样例输出
-
1
本题大意:有一个机器人只能向右,向下走且只有碰到b后才会改变方向,如,开始向右碰b后向下走,开始向下走碰b后向右走,要求将迷宫改动可以将.改为b也可以将b改为.,使这个机器人可以到达右下角,输出改变次数的最小值.
题目1 : Demo Day
-
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.
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#define INF 0x3fffffff
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char a[110][110];
int b[110][110][2];//b[i][j][0]表示向右b[i][j][1]表示向下
memset(b,0,sizeof(b));
int n,m;
scanf("%d%d\n",&n,&m);
for(int i=1;i<=n;i++)
{
a[i][0]='b';
b[i][0][0]=11000;b[i][0][1]=11000;//初始化迷宫的边界
for(int j=1;j<=m;j++)
cin>>a[i][j];
a[i][m+1]='b';
}
for(int i=0;i<=m;i++)
a[0][i]='b',a[n+1][i]='b',b[0][i][1]=11000,b[0][i][0]=11000;//初始化迷宫边界
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(i==1&&j==1)
b[i][j][0]=0,b[i][j][1]=11000;
else
{
int b1,b2;
if(a[i-1][j+1]=='.')//判断是否需要改变a[i-1][j+1];
b1=b[i-1][j][0]+1;
else
b1=b[i-1][j][0];
if(a[i+1][j-1]=='.')//判断是否需要改变a[i+1][j-1];
b2=b[i][j-1][1]+1;
else
b2=b[i][j-1][1];
b[i][j][0]=min(b[i][j-1][0],b2);//取最小
b[i][j][1]=min(b1,b[i-1][j][1]);
b[0][j][1]=11000;
}
if(a[i][j]=='b')
{
b[i][j][0]++;
b[i][j][1]++;
}
//printf(" %c %d %d-%d %d\n",a[i][j],i,j,b[i][j][0],b[i][j][1]);
}
}
printf("%d\n",min(b[n][m][0],b[n][m][1]));
return 0;
}