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. 每日踩坑 2019-08-23 button 元素点击后刷新页面

    button标签按钮会提交表单. 解决方案: <button class="btn btn-primary" type="button" id=" ...

  2. Android NDK的生命周期JNI_OnLoad与JNI_OnUnload(转)

    摘要 NDK的生命周期 //当动态库被加载时这个函数被系统调用 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { LOGI ...

  3. java 重写和重载的区别

    重写:重写是子类的方法覆盖父类的方法,有两个重要特点 1.子类方法名和参数类型.个数必须和父类一样. 2.如果有返回值,则返回值类型也必须和父类一样. //父类 public class Bird() ...

  4. JavaScript对象的常用属性及使用

    什么是浏览器对象模型? 浏览器对象模型(BOM Browser Object Model)是JavaScript的组成之一,它提供了独立于内容和浏览器窗口进行交互的对象,使用浏览器对象模型可以实现与H ...

  5. 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_07.环境搭建的注意事项

    2 resources下面创建目录要一级一级的创建,下面这个创建的就是一级目录而不是三级 在文件夹下看到的目录也是一级的 因此这里创建目录需要一个个的去创建 配置文件和dao类这两个目录要保持一致,这 ...

  6. 【HANA系列】SAP HANA数据处理的理解与分析一

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA数据处理的理解与 ...

  7. DedeCMS调取其他织梦CMS站点数据库数据方法

    第1步:打开网站include\taglib文件夹中找到sql.lib.php文件,并直接复制一些此文件出来,并把复制出来的这个文件重命名为mysql.lib.php.注:mysql.lib.php, ...

  8. 你知道e.g.和i.e.的区别吗?

    见 i.e. 是对前面的完全举例,特指 e.g. 则是不完全举例,有可能是...也有可能是...还可能是其他. 注意,i.e. 和 e.g. 第二个点后面都常跟一个逗号.

  9. finereport 带多参数查询

    1.sql语句 ${,""," and dt.货主地区='"+comboBox0+"'")} ${,""," ...

  10. [转帖]OEM、ODM、OBM分别是什么?

    OEM.ODM.OBM分别是什么? https://blog.csdn.net/liangtianmeng/article/details/83215500 感觉很多地方说明的都不对 OEM 别人的产 ...