标签:hang cst acl native ring 输出 min star ble
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
看了题解的解释才会怎么做,dp大法吼啊! 遇到这种很多约束条件的,可以采用 dp, 每一个 grid 都有着自己的状态。
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std; const int MAXN = 105; int n, m, ans, dp[MAXN][MAXN][2]; char mp[MAXN][MAXN]; inline bool check(int x, int y){ if(x>0 && x<=n && y>0 && y<=m && mp[x][y]==‘.‘){ return true; }else{ return false; } } void fun(){ memset(dp, 0x3f3f3f3f, sizeof(dp)); dp[1][1][0] = dp[1][1][1] = 0; int obstacle, rt, r, b, lb; for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ obstacle = check(i, j)? 0:1; rt = check(i-1, j+1)?1:0; lb = check(i+1, j-1)?1:0; b = check(i+1, j)?1:0; r = check(i, j+1)?1:0; /// right dp[i][j][0] = min(dp[i][j][0], dp[i-1][j][0] + obstacle + rt + b ); dp[i][j][0] = min(dp[i][j][0], dp[i][j-1][0] + obstacle ); dp[i][j][0] = min(dp[i][j][0], dp[i-1][j][1] + obstacle + b); dp[i][j][0] = min(dp[i][j][0], dp[i][j-1][1] + obstacle + lb); // down dp[i][j][1] = min(dp[i][j][1], dp[i-1][j][0] + obstacle + rt ); dp[i][j][1] = min(dp[i][j][1], dp[i][j-1][0] + obstacle + r ); dp[i][j][1] = min(dp[i][j][1], dp[i-1][j][1] + obstacle ); dp[i][j][1] = min(dp[i][j][1], dp[i][j-1][1] + obstacle + lb + r); } } } int main(){ freopen("in.txt", "r", stdin); while(scanf("%d %d", &n, &m) != EOF){ for(int i=1; i<=n; ++i){ getchar(); scanf("%s", mp[i]+1); } fun(); ans = min( dp[n][m][0], dp[n][m][1] ); printf("%d\n", ans ); } return 0; }
标签:hang cst acl native ring 输出 min star ble
原文地址:http://www.cnblogs.com/zhang-yd/p/6882795.html