微軟2016校園招聘4月線上筆試 hihocoder 1290 Demo Day (dp)
描述
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
題目連結: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]));
}
相關文章
- 微軟2016校園招聘4月線上筆試 hihocoder 1289 403 Forbidden微軟筆試ORB
- 微軟2016校園招聘4月線上筆試 hihocoder 1288 Font Size (模擬)微軟筆試
- 各大IT公司2012校園招聘筆試面試整理筆試面試
- 華為2016校園招聘上機筆試題:簡單錯誤記錄 [python]筆試Python
- 校園招聘的思考
- 2016微軟4月線上筆試題微軟筆試
- 【JAVA】【華為校園招聘筆試-軟體】2020-09-09Java筆試
- 【校園招聘】一道筆試題看c++中virtual覆蓋筆試C++
- 百度2011.10.16校園招聘會筆試題筆試
- 【JAVA】騰訊2021校園招聘-後臺&綜合-第二次筆試Java筆試
- 百度2013校園招聘筆試題[軟體研發]及答案筆試
- 微軟windows 7推向校園微軟Windows
- 【小米集團校園招聘】射頻測試工程師 (急招)工程師
- [小米集團校園招聘] 射頻測試工程師 (急招)工程師
- [廣州、成都、武漢] 多益網路22屆校園招聘 程式類10+崗位(直通筆試!)筆試
- 2014百度校園招聘筆試題(上海軟體研發工程師)筆試工程師
- 華為2018屆校園招聘筆試題目以及相應程式碼分享 軟體開發崗位筆試
- 【資料庫】網易校園招聘筆試題--資料庫事務的定義,屬性,以及隔離級別資料庫筆試
- 【演算法】2013創新工場校園招聘筆試題--如何求出帶環連結串列的入口節點演算法筆試
- 【小米集團校園招聘】IoT 軟體專案經理
- [小米集團校園招聘] IoT 軟體專案經理
- 【小米集團校園招聘】iOS 開發工程師 (急招)iOS工程師
- 【小米集團校園招聘】嵌入式工程師 (急招)工程師
- [小米集團校園招聘] 嵌入式工程師 (急招)工程師
- [小米集團校園招聘] iOS 開發工程師 (急招)iOS工程師
- 校園網-真實的校園網路社群
- 【小米集團校園招聘】Android 開發工程師 (急招)Android工程師
- [小米集團校園招聘] Android 開發工程師 (急招)Android工程師
- 百度2015校園招聘面試題(成功拿到offer)面試題
- 【小米集團校園招聘】顯示演算法工程師 (急招)演算法工程師
- [小米集團校園招聘] 顯示演算法工程師 (急招)演算法工程師
- 參加技術校園招聘,你最好先了解這五件事
- 2017年高頻率的網際網路校園招聘面試題面試題
- EMC校招筆試題目筆試
- 【小米集團校園招聘】軟體開放工程師-Java 方向 (急招)工程師Java
- [小米集團校園招聘] 軟體開發工程師-Java 方向 (急招)工程師Java
- 百度2015校園招聘面試題回憶錄(成功拿到offer)面試題
- 大學校園IP網路廣播-基於校園區域網的大學校園IP廣播方案設計指南