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. centos64位编译32位程序

    test.c #include <stdio.h> int main() { printf("sizeof long is %d\n",sizeof(long)); ; ...

  2. 2014过去了,正式步入职场了,.net

    一.第一家公司(北京XXXXXXX) 从2014年7月1号拿到学位证,到7月15号到北京,努力找工作,用了两个多礼拜,终于找到了一个只有三个人的公司,愿意要我,薪资是实习三千,转正四千. 2014年7 ...

  3. leetcode 27. 移除元素(python)

    1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外 ...

  4. 【flask】项目集成Sentry收集线上错误日志

    flask集成sentry分为4个步骤: 首先在sentry官网注册1个账号 然后创建1个新的项目,这里我选择的是flask,这会得到一些关于sdk的使用说明 接下来创建一个简单的flask项目使用s ...

  5. Vue知识整理12:事件绑定

    采用v-on命令进行事件的绑定操作,通过单击按钮,实现按钮文字上数值的增加 带参数的事件过程 可以添加$event事件,实现事件信息的获取

  6. OpenStack Nova Placement API 统一资源管理接口的未来

    目录 目录 Placement API 为何称之为 "未来" 操作对象基本概念 数据库操作样例 Placement API 在创建虚拟机时的调度过程 Placement REST ...

  7. Linux_PXE服务器_RHEL7

    目录 目录 前言 PXE原理 搭建PXE服务器 关闭SELinux和防火墙 配置DHCP 配置TFTP 配置FTP 配置Kickstart 前言 PXE(preboot execute environ ...

  8. Linux监控命令之==>sar

    一.使用说明 sar 是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情况.磁盘I/O.CPU效率.内存使用状况.进程活动及 ...

  9. 【ABAP系列】SAP 面试 ABAPer的一些感想

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 面试 ABAPer的一些 ...

  10. 【bzoj4710】[Jsoi2011]分特产

    JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同学,一共有多少种不同的分法?当然,JYY 不希望任何一个同学因为没有拿到 ...