• 2022.11.30每日刷题打卡


     Fire Net HDU - 1045 

    Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

    A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.

    Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

    The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.

    The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.

    Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration. 

    Input

    The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.

    Output

    For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.

    Sample

    InputcopyOutputcopy
     
    4 
    .X.. 
    .... 
    XX.. 
    .... 
    2 
    XX 
    .X 
    3 
    .X. 
    X.X 
    .X. 
    3 
    ... 
    .XX 
    .XX 
    4 
    .... 
    .... 
    .... 
    .... 
    0 
     
    5 
    1 
    5 
    2 
    4 

    原题链接:传送门

    题意:给出一张图,图中'X'表示wall,'.'表示空地,可以放置blockhouse同一条直线上只能有一个blockhouse,除非有wall隔开,问在给出的图中最多能放置多少个blockhous。

    思路:开始想着构建二分图匹配,但其实没那么复杂直接深搜即可

    AC代码:

    1. #include
    2. using namespace std;
    3. int n, ans;
    4. char g[15][15];
    5. int main(){
    6. ios::sync_with_stdio(false);
    7. cin.tie(nullptr);
    8. while (cin >> n, n){
    9. for (int i = 0; i < n; i++)
    10. cin >> g[i];
    11. ans = 0;
    12. function<int(int, int)> check = [&](int a, int b){
    13. if (g[a][b] != '.') return 0;
    14. for (int i = a - 1; i >= 0; i--)
    15. if (g[i][b] == '?') return 0;
    16. else if (g[i][b] == 'X') break;
    17. for (int i = b - 1; i >= 0; i--)
    18. if (g[a][i] == '?') return 0;
    19. else if (g[a][i] == 'X') break;
    20. return 1;
    21. };
    22. function<void(int, int)> dfs = [&](int j, int cnt){
    23. if (j == n * n){
    24. ans = max(ans, cnt);
    25. return;
    26. }
    27. int x = j / n, y = j % n;
    28. if (check(x, y)){
    29. g[x][y] = '?';
    30. dfs(j + 1, cnt + 1);
    31. g[x][y] = '.';
    32. }
    33. dfs(j + 1, cnt);
    34. return;
    35. };
    36. dfs(0, 0);
    37. cout << ans << "\n";
    38. }
    39. return 0;
    40. }

     Nightmare HDU - 1072

    Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

    Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.

    Here are some rules:
    1. We can assume the labyrinth is a 2 array.
    2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
    3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
    4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
    5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
    6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.

    Input

    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
    Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
    There are five integers which indicate the different type of area in the labyrinth:
    0: The area is a wall, Ignatius should not walk on it.
    1: The area contains nothing, Ignatius can walk on it.
    2: Ignatius' start position, Ignatius starts his escape from this position.
    3: The exit of the labyrinth, Ignatius' target position.
    4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.

    Output

    For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.

    Sample

    InputcopyOutputcopy
     
    3 
    3 3 
    2 1 1 
    1 1 0 
    1 1 3 
    4 8 
    2 1 1 0 1 1 1 0 
    1 0 4 1 1 0 4 1 
    1 0 0 0 0 0 0 1 
    1 1 1 4 1 1 1 3 
    5 8 
    1 2 1 1 1 1 1 4 
    1 0 0 0 1 0 0 1 
    1 4 1 0 1 1 0 1 
    1 0 0 0 0 3 0 1 
    1 1 4 1 1 1 1 1 
     
    4 
    -1 
    13

    原题链接:传送门

    题意:每个炸弹是6分钟,2是起点3是终点,1什么都没有,0是墙不能走,4能延迟爆炸

    思路:bfs中模拟这5种状态

    1. #include
    2. using namespace std;
    3. const int N = 8;
    4. const int dx[] = {1, -1, 0, 0};
    5. const int dy[] = {0, 0, 1, -1};
    6. struct node{
    7. int x, y, step, ti;
    8. }t, tt;
    9. int n, m, sx, sy, ex, ey;
    10. int g[N][N];
    11. int f[N][N];
    12. int main(){
    13. ios::sync_with_stdio(false);
    14. cin.tie(nullptr);
    15. int test;
    16. cin >> test;
    17. while(test--){
    18. for(int i = 0; i <= N; i++)
    19. for(int j = 0; j <= N; j++)
    20. f[i][j] = 6;
    21. cin >> n >> m;
    22. for(int i = 0; i < n; i++)
    23. for(int j = 0; j < m; j++){
    24. cin >> g[i][j];
    25. if(g[i][j] == 2) sx = i, sy = j;
    26. if(g[i][j] == 3) ex = i, ey = j;
    27. }
    28. int ans = -1;
    29. function<int(int, int)> bfs = [&] (int x, int y){
    30. queue q;
    31. q.push({x, y, 0, 0});
    32. while(!q.empty()){
    33. t = q.front(), q.pop();
    34. if(t.x == ex && t.y == ey) {
    35. ans = t.step;
    36. return 1;
    37. }
    38. for(int i = 0; i < 4; i++){
    39. tt.x = t.x + dx[i], tt.y = t.y + dy[i];
    40. if(tt.x < 0 || tt.x >= n || tt.y < 0 || tt.y >= m || g[tt.x][tt.y] == 0) continue;
    41. int k = g[tt.x][tt.y];
    42. if(k == 1 || k == 3 || k == 4) tt.step = t.step + 1, tt.ti = t.ti + 1;
    43. if(tt.ti == 6) continue;
    44. if(k == 4) tt.ti = 0;
    45. if(tt.ti < f[tt.x][tt.y]) f[tt.x][tt.y] = tt.ti, q.push(tt);
    46. }
    47. }
    48. return 0;
    49. };
    50. int flag = bfs(sx, sy);
    51. if(!flag) cout << "-1\n";
    52. else cout << ans << "\n";
    53. }
    54. }

    Rescue HDU - 1242

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

    Input

    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

    Process to the end of the file.

    Output

    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

    Sample

    InputcopyOutputcopy
     
    7 8 
    #.#####. 
    #.a#..r. 
    #..#x... 
    ..#..#.# 
    #...##.. 
    .#...... 
    ........ 
     
    13 

    原题链接:传送门

    题意:r->a的最短时间,遇到x时间多加1

    1. #include
    2. using namespace std;
    3. const int N = 205;
    4. const int dx[] = {1, -1, 0, 0};
    5. const int dy[] = {0, 0, 1, -1};
    6. struct node{
    7. int x, y, step;
    8. }t, tt;
    9. int n, m, sx, sy, ex, ey;
    10. char g[N][N];
    11. bool vis[N][N];
    12. int f[N][N];
    13. int main(){
    14. ios::sync_with_stdio(false);
    15. cin.tie(nullptr);
    16. while(cin >> n >> m){
    17. for(int i = 0; i < n; i++)
    18. for(int j = 0; j < m; j++){
    19. cin >> g[i][j];
    20. if(g[i][j] == 'r') sx = i, sy = j;
    21. if(g[i][j] == 'a') ex = i, ey = j;
    22. }
    23. memset(f, 0x1f, sizeof f);
    24. function<void(int, int)> bfs = [&] (int x, int y){
    25. queue q;
    26. q.push({x, y, 0});
    27. vis[x][y] = 1;
    28. while(!q.empty()){
    29. t = q.front(), q.pop();
    30. for(int i = 0; i < 4; i++){
    31. tt = {t.x + dx[i], t.y + dy[i], t.step + 1};
    32. if(tt.x < 0 || tt.x >= n || tt.y < 0 || tt.y >= m || g[tt.x][tt.y] == '#') continue;
    33. if(g[tt.x][tt.y] == 'x') tt.step ++;
    34. if(tt.step < f[tt.x][tt.y]) f[tt.x][tt.y] = tt.step, q.push(tt);
    35. }
    36. }
    37. };
    38. bfs(sx, sy);
    39. int ans = f[ex][ey];
    40. if(ans == 0x1f1f1f1f) cout << "Poor ANGEL has to stay in the prison all his life.\n";
    41. else cout << ans << "\n";
    42. }
    43. }

  • 相关阅读:
    小程序开发设计-第一个小程序:创建小程序项目④
    CDH集群使用spark作为hive查询引擎(实时查询)
    动态规划---图像压缩
    使用jdbcTemplate的new BeanPropertyRowMapper<>查不到数据
    【LeetCode】三个无重叠子数组的最大和 [H](动态规划)
    SQL模板-用户留存率计算
    对“Linux中遇到的RAID阵列”好好总结一次
    【C++】map、set,multiset和multimap的使用及底层原理【完整版】
    SpringBoot热部署
    Linux操作系统之操作系统概论以及操作系统设计思想
  • 原文地址:https://blog.csdn.net/weixin_62802134/article/details/128121533