AtCoder Beginner Contest 088 D Grid Repainting
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≤i≤H,1≤j≤W). - s1,1 and sH,W are
..
Input
Input is given from Standard Input in the following format:
H W
s1,1s1,2s1,3…s1,W
s2,1s2,2s2,3…s2,W
: :
sH,1sH,2sH,3…sH,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的更多相关文章
- AtCoder Beginner Contest 088 (ABC)
A - Infinite Coins 题目链接:https://abc088.contest.atcoder.jp/tasks/abc088_a Time limit : 2sec / Memory ...
- 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 ...
- AtCoder Beginner Contest 224
AtCoder Beginner Contest 224 A - Tires 思路分析: 判断最后一个字符即可. 代码如下: #include <bits/stdc++.h> using ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
随机推荐
- Failed to read artifact descriptor for xxx:jar
在MyEclipse中执行Maven的install命令时,报“Failed to read artifact descriptor for xxx:jar ”的错误.这可能是在下载过程中文件出现错误 ...
- 第十周java学习总结
目录 第十周java学习总结 学习内容 代码上传截图 代码链接 第十周java学习总结 学习内容 第12章 Java多线程机制 主要内容 Java中的线程 Thread类与线程的创建 线程的常用方法 ...
- 1、node-webkit 的概念,node-webkit 的下载
node-webkit是一个基于node.js和chromium的应用程序运行环境,通过它我们可以把建立在chrome浏览器和node.js上的web应用打包成桌面应用,而且还可以跨平台的哦.很显然比 ...
- Jquery Ajax调用asmx出错问题
1.//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释. [System.Web.Script.Services.ScriptService] 这个 ...
- add_prefix()函数
对于series,是给索引列加前缀. 对于Dataframe,是给列名加前缀. 参考:https://www.cjavapy.com/article/276/
- 2.转发。基于itchat的微信消息同步机器人
原文:https://www.jianshu.com/p/7aeadca0c9bd# 看到了该网址有基于itchat的微信消息同步机器人,转过来继续研究.以下是转过来的: 最近 全栈数据工程师养成攻略 ...
- 【Qt开发】【Linux开发】Qt程序在嵌入式设备(arm) 上运行,鼠标擦除界面的解决方案
笔者最近想在arm开发板上,开发一个应用程序,经过网上查询发现qt作为跨平台开发软件很不错,于是便选择了qt开发,笔者的qt版本是4.8.6的.由于arm的主频太低,在arm上进行开发编译,效率会大大 ...
- 2019 java学习 第二周总结
新学期,新气象,新老师,不同的语言学习. 我已经荒废了大一,感觉自己在大一根本没学啥,可能是自己太贪玩了,导致自己学的不精,自己对其他的见解很少. 也有自大的原因,导致自己一直浮在水平面,有实力,有耐 ...
- Hive-生成一个大文件(小文件合并)
set hive.execution.engine=mr; --在 map-reduce 作业结束时合并小文件.如启用,将创建 map-only 作业以合并目标表/分区中的文件. set hive.m ...
- 解决org.apache.subversion.javahl.ClientException的方法【】
重新刷新项目,配置项目,总是报“The project was not built due to "org.apache.subversion.javahl.ClientException” ...