zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 088 D Grid Repainting

    Problem statement

    We have an H×W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i,j).
    Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1,1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H,W) passing only white squares.
    Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1,1) and (H,W). Also, changes of color must all be carried out before the beginning of the game.
    When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print −1 if the game cannot be completed, that is, Kenus can never reach square (H,W) regardless of how Snuke changes the color of the squares.

    The color of the squares are given to you as characters si,j. If square (i,j) is initially painted by white, si,j is .; if square (i,j) is initially painted by black, si,j is #.

    Constraints
    • H is an integer between 2 and 50 (inclusive).
    • W is an integer between 2 and 50 (inclusive).
    • si,j is . or # (1iH,1jW).
    • s1,1 and sH,W are ..
    Input

    Input is given from Standard Input in the following format:

    H W
    s1,1s1,2s1,3s1,W
    s2,1s2,2s2,3s2,W
     :   :
    sH,1sH,2sH,3sH,W
    
    Output

    Print the maximum possible score that Snuke can achieve, or print −1 if the game cannot be completed.

    Sample Input 1
    3 3
    ..#
    #..
    ...
    
    Sample Output 1
    2
    

    The score 2 can be achieved by changing the color of squares as follows:

    Explanation of Sample 1

    Sample Input 2
    10 37
    .....................................
    ...#...####...####..###...###...###..
    ..#.#..#...#.##....#...#.#...#.#...#.
    ..#.#..#...#.#.....#...#.#...#.#...#.
    .#...#.#..##.#.....#...#.#.###.#.###.
    .#####.####..#.....#...#..##....##...
    .#...#.#...#.#.....#...#.#...#.#...#.
    .#...#.#...#.##....#...#.#...#.#...#.
    .#...#.####...####..###...###...###..
    .....................................
    
    Sample Output 2
    209
    
    只能走白色的格子('.'),找到一条最短路,用广搜,然后记录步数,剩下的白色格子可以变成黑色。
    代码:
    #include <bits/stdc++.h>
    using namespace std;
    struct times
    {
        int x,y,t;
        times(){}
        times(int x,int y,int t)
        {
            this -> x = x;
            this -> y = y;
            this -> t = t;
        }
    }temp;
    int dir[4][2] = {0,1,1,0,0,-1,-1,0};
    int h,w,c,flag = 0;
    char mp[51][51];
    int vis[51][51];
    int main()
    {
        cin>>h>>w;
        for(int i = 0;i < h;i ++)
        {
            cin.get();
            for(int j = 0;j < w;j ++)
            {
                cin>>mp[i][j];
                if(mp[i][j] == '.')c ++;
            }
        }
        if(mp[0][0] == '.' && mp[h - 1][w - 1] == '.')
        {
            queue<times> q;
            q.push(times(0,0,1));
            vis[0][0] = 1;
            while(!q.empty())
            {
                if(flag)break;
                temp = q.front();
                q.pop();
                for(int i = 0;i < 4;i ++)
                {
                    int tx = temp.x + dir[i][0];
                    int ty = temp.y + dir[i][1];
                    if(tx < 0 || ty < 0 || tx >= h || ty >= w || vis[tx][ty] || mp[tx][ty] == '#')continue;
                    vis[tx][ty] = 1;
                    if(tx == h - 1 && ty == w - 1)flag = temp.t + 1;
                    q.push(times(tx,ty,temp.t + 1));
                }
            }
        }
        if(flag)cout<<c - flag;
        else cout<<-1;
    }
  • 相关阅读:
    Tensorflow的认识
    机器学习中的重点数学知识
    深度学习TensorFlow常用函数
    18、OpenCV Python 简单实现一个图片生成(类似抖音生成字母人像)
    17、OpenCV Python 数字验证码识别
    django contenttype 表应用
    contentType 应用,(表中数据大量存在外键时使用)
    django 组件拾遗
    rest_framework 的验证,权限,频率
    restframework CBV试图的4种方式
  • 原文地址:https://www.cnblogs.com/8023spz/p/8551956.html
Copyright © 2011-2022 走看看