Icy Land CFGym 102001D

11 篇文章 0 订阅

You are given a board of R rows and C

columns where each cell is either a dry land ('#') or an icy land ('.'). You can move to either one of the four directions (north, south, east, or west) in the board.

You can walk or standstill on any dry land without any problem; however, an icy land is very slippery. If you stand at cell (r,c)

and decide to move on a particular direction, then you will move on that direction continuously until you reach a dry land or the border of the board.

For example, consider the following board of 6

rows and 7

column.

   #......
   .#.....
   ..##...
   ..##...
   .......
   .#.....
  • From (1,3)

, the 1st row and 3rd column, if you move to the south, then you will end up at (3,3)

  • .
  • From (4,4)
  • , if you move to the north, then you will end up at (3,4)
  • .
  • From (1,1)
  • , if you move to the east, then you will end up at (1,7)
  • .
  • From (6,5)
  • , if you move to the west, then you will end up at (6,2)

    Supposed your initial position is at cell (3,7)

    and your moves are west, west, west, south, east, north, west, and west, respectively. Then your positions are (3,7) → (3,4) → (3,3) → (3,1) → (6,1) → (6,2) → (2,2) → (2,1) → (2,1), thus, visiting 8 different cells. Note that cell (2,1) is visited twice. Cells in which you only passed through are also not considered as visited, e.g., in the example above, if you move south from (3,1), then you will pass through (4,1) and (5,1), and arrive at (6,1); thus, (4,1) and (5,1) are not considered as visited from that particular move, only (3,1) and (6,1)

    are.

    You are allowed to change any icy land into a dry land, and your goal is to make sure that you can always visit all the cells in the board regardless of your initial starting position; in other words, you do not know your starting position yet, but given the board, you want to make sure that you can achieve your goal. What is the minimum number of cells you need to change to ensure that your goal can be achieved?

    Input

    Input begins with a line containing two integers: R

    C (1≤R,C≤500) representing the number of rows and columns of the board, respectively. The next R lines, each contains C

    characters representing the given board. Each character is either '#' which represents a dry land or '.' which represents an icy land.

    Output

    Output contains an integer in a line representing the minimum number of cells you need to change to ensure that you can visit all the cells in the board regardless of your initial starting position.

    Sample Input

    Input

    4 4
    ....
    .###
    ##..
    ###.
    

    Output

    1
    

    Hint

    Explanation for the sample input/output

    We only need to change cell (3,3)

    .

       ....
       .###
       ###.
       ###.
    

    Let 'N' be north, 'S' be south, 'W' be west, and 'E' be east.

    , then the movement "SSENNWENSESSEWNENWNE" will visit all the cells. The corresponding positions are: (1,1) → (3,1) → (4,1) → (4,2) → (3,2) → (2,2) → (2,1) → (2,2) → (1,2) → (2,2) → (2,3) → (3,3) → (4,3) → (4,4) → (4,3) → (3,3) → (3,4) → (2,4) → (2,3) → (1,3) → (1,4)
    • .
    • If you start at cell (1,1)
  • .
  • If you start at cell (1,2)
  • , then the movement "W" followed by the movement in the first example above ("SSENNWENSESSEWNENWNE") will visit all the cells. The corresponding positions are: (1,2) → (1,1) → …
  •  
  • If you start at cell (1,3)
  • , then the movement "W" followed by the movement in the first example above will visit all the cells. The corresponding positions are: (1,3) → (1,1) → …
  •  
  •  
  • If you start at cell (4,3)
  • , then the movement "NNNW" followed by the movement in the first example above will visit all the cells. The corresponding positions are: (4,3) → (3,3) → (2,3) → (1,3) → (1,1) → …
  •  
  • If you start at cell (4,4)

, then the movement "NNW" followed by the movement in the first example above will visit all the cells. The corresponding positions are: (4,4) → (2,4) → (1,4) → (1,1) → …

题意:给你一个由R行和C列组成的板,其中每个单元格要么是一块陆地('#'),要么是一块冰地('.')。你可以在黑板的四个方向(北、南、东、西)中任选一个。你可以在任何干燥的土地上行走或静止不动,没有任何问题;然而,冰地很滑。如果你站在单元格(r,c)处,并决定沿着一个特定的方向移动,那么你将一直沿着那个方向移动,直到你到达一块陆地或棋盘的边界。例如,考虑下面这个由6行7列组成的板。#……。# .....…# #……# #….......。# .....从(1,3)到第1行和第3列,如果向南移动,就会得到(3,3)从(4,4)开始,如果你向北移动,你会得到(3,4)从(1,1)开始,如果你往东移动,你会得到(1,7)从(6,5)开始,如果你向西移动,你会得到(6,2)假设你的初始位置在单元格(3,7),你的移动方向分别是西、西、西、南、东、北、西、西。那么你的位置是(3,7)(3,4)(3,3)(3,1)(6,1)(6,2)(2,2)(2,1)(2,1)(2,1),因此,访问8个不同的单元格。注意,单元格(2,1)被访问了两次。只经过的单元也不视为已访问,例如,在上面的例子中,如果您从(3,1)向南移动,那么您将通过(4,1)和(5,1),并到达(6,1);因此,(4,1)和(5,1)不被认为是从特定的移动访问,只有(3,1)和(6,1)是。你可以把任何一块冰冷的土地变成干燥的土地,你的目标是确保无论你的起始位置如何,你都可以访问板上的所有细胞;换句话说,你还不知道你的起始位置,但是给定板,你要确保你能实现你的目标。为了确保您的目标能够实现,您需要更改的最小单元数是多少

思路:如果格子的长或宽小于3,特判(具体看代码吧),如果都大于2,那么直接数除周围一圈外,中间冰地数x,如果最外面一圈(除了四个角)都是冰块,那么答案是x+1,否则就是x

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char s[510][510];
int main()
{
    int r,c;
    scanf("%d %d",&r,&c);
    for(int i=1; i<=r; i++)
    {
        scanf("%s",s[i]+1);
    }
    int sum=0;
    if(r==1)
    {
        for(int i=2;i<=c-1;i++)
        {
            if(s[1][i]=='.') sum++;
        }
    }
    else if(r==2)
    {
        for(int i=2;i<=c-1;i++)
        {
            if(s[1][i]=='.'&&s[2][i]=='.') sum++;
        }
    }
    else if(c==1)
    {
        for(int i=2;i<=r-1;i++)
        {
            if(s[i][1]=='.') sum++;
        }
    }
    else if(c==2)
    {
        for(int i=2;i<=r-1;i++)
        {
            if(s[i][1]=='.'&&s[i][2]=='.') sum++;
        }
    }
    else
    {
        for(int i=2; i<=r-1; i++)
        {
            for(int j=2; j<=c-1; j++)
            {
                if(s[i][j]=='.') sum++;
            }
        }
        int flag=0;
        for(int i=2;i<=c-1;i++) if(s[1][i]=='#'||s[r][i]=='#') flag=1;
        for(int i=2;i<=r-1;i++) if(s[i][1]=='#'||s[i][c]=='#') flag=1;
        sum+=(!flag);
    }
    printf("%d\n",sum);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值