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 # (1≤iH,1≤jW).
  • 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:

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[][] = {,,,,,-,-,};
int h,w,c,flag = ;
char mp[][];
int vis[][];
int main()
{
cin>>h>>w;
for(int i = ;i < h;i ++)
{
cin.get();
for(int j = ;j < w;j ++)
{
cin>>mp[i][j];
if(mp[i][j] == '.')c ++;
}
}
if(mp[][] == '.' && mp[h - ][w - ] == '.')
{
queue<times> q;
q.push(times(,,));
vis[][] = ;
while(!q.empty())
{
if(flag)break;
temp = q.front();
q.pop();
for(int i = ;i < ;i ++)
{
int tx = temp.x + dir[i][];
int ty = temp.y + dir[i][];
if(tx < || ty < || tx >= h || ty >= w || vis[tx][ty] || mp[tx][ty] == '#')continue;
vis[tx][ty] = ;
if(tx == h - && ty == w - )flag = temp.t + ;
q.push(times(tx,ty,temp.t + ));
}
}
}
if(flag)cout<<c - flag;
else cout<<-;
}

AtCoder Beginner Contest 088 D Grid Repainting的更多相关文章

  1. AtCoder Beginner Contest 088 (ABC)

    A - Infinite Coins 题目链接:https://abc088.contest.atcoder.jp/tasks/abc088_a Time limit : 2sec / Memory ...

  2. AtCoder Beginner Contest 088 C Takahashi's Information

    Problem Statement We have a 3×3 grid. A number ci,j is written in the square (i,j), where (i,j) deno ...

  3. AtCoder Beginner Contest 224

    AtCoder Beginner Contest 224 A - Tires 思路分析: 判断最后一个字符即可. 代码如下: #include <bits/stdc++.h> using ...

  4. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  5. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  6. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  7. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  8. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  9. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

随机推荐

  1. Linux_文件系统、磁盘分区_RHEL7

    目录 目录 前言 文件系统 目录结构 文件的类型 文件系统损坏后的修复 磁盘分区 分区的类型 分区最小存储单元 查看当前分区的block的大小 分区格式 MBR格式 GPT格式 mount挂载指令 挂 ...

  2. Linux_LVM、RAID_RHEL7

    目录 目录 LVM逻辑卷管理 把物理分区初始化为物理卷 创建卷组 建立逻辑卷 格式化 挂载 vg拓展操作 lv扩展操作 RAID RAID 类型 RAID0条带化 RAID1镜像 RAID5条带冗余 ...

  3. Jmeter之HTTP常用配置元件(默认、头信息和cookies)

    在进行HTTP接口的测试时,会用到部分常用的配置元件,现在简单的说明: 一.HTTP请求默认值 在测试同一个项目的HTTP请求接口时,会存在部分相同的信息,可以将这些相同的信息提取出来,使用HTTP请 ...

  4. robot framework UI自动化之登录

    前面已写环境的搭建,接下来就可以直接进行UI自动化的编写工作了 目录 1.准备工作 2.了解定位 3.一个登录案例 1.准备工作 第一步:需要使用chrome浏览器来测试,因此首先要有一个驱动,下载好 ...

  5. div动画旋转效果

    animation: spin 10s linear infinite;

  6. Mysql-5.7 x64安装

    首先在官网下载Mysql:https://dev.mysql.com/downloads/mysql/ 选择ZIP Archive下载. 下载安装之后配置环境变量: 编辑现有环境变量Path: PS: ...

  7. 第三方app抽奖发送微信红包实现

    1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...

  8. RabbitMQ使用(上)

    1. 说明 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的但是执行起来比较较耗时的地方, ...

  9. 应用安全 - 无文件式攻击 - 工具型攻击 - PowerShell - 汇总

    PowerShell 使用 | 命令 win+r ->powershell #启动Powershell窗口 get-host #查看版本 Get-Host | Select-Object Ver ...

  10. 20191106 Spring Boot官方文档学习(1-2)

    学习内容相关信息 最新版本:2.2.0 CURRENT GA 官网地址 官方文档地址 单页版文档地址 代码生成网址 2.入门 Spring Boot的主要目标是: 为所有Spring开发提供更快且入门 ...