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. Oracle开发:dba和sysdba的区别

    oracle dba和sysdba的区别如下: 1.dba是一种role对应的是对Oracle实例里对象的操作权限的集合,而sysdba是概念上的role是一种登录认证时的身份标识而已.而且,dba是 ...

  2. spring boot 整合saml2

    项目是国外的一位大神发布到githut上,这里只是对项目代码的分析与学习,也算是一种强化记忆 附上 githut地址:https://github.com/OpenConext/Mujina 项目分为 ...

  3. eclipse添加约束文件

    DTD 类型约束文件     1. Window->Preferences->XML->XML Catalog->User Specified Entries窗口中,选择Add ...

  4. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_6 mybatis中的事务原理和自动提交设置

    在实际的开发中,建议使用连接池的形式. JNDI的资料 H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2. ...

  5. 用JS实现移动的窗口

    https://blog.csdn.net/iteye_21064/article/details/81496640 用JS实现移动的窗口 2007年09月06日 23:23:00 阅读数:3 很简单 ...

  6. Java 语言特性之 Annotation 注解

    利用 Java 的反射机制,可以在运行时获取 Java 类的注解信息. 注解 注解的特性 注解是 Java 5 的一个新特性,是插入代码中的一种注释或者说是元数据.注解并不是程序代码,可以对程序作出解 ...

  7. Java的in.nextInt()和in.nextLine()方法的具体内涵

    本人也是刚开始学习java语言,在学习的过程中,老师让我们做一个模拟学生学籍管理系统的小程序.因为刚开始,做的是比较简单的,用switch语句做界面,然后配合Scanner 接收输入的数字进行跳转,完 ...

  8. 精读《Monorepo 的优势》

    1. 引言 本周精读的文章是 The many Benefits of Using a Monorepo. 现在介绍 Monorepo 的文章很多,可以分为如下几类:直接介绍 Lerna API 的: ...

  9. fragment嵌套子fragment

    场景: 需求是在expandablelistview的item长按会弹出一个对话框,对话框顶部一个选项卡,下面是fragment作相应的切换.对话框是用DialogFragment实现的. 问题: j ...

  10. js while循环

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...