AtCoder Beginner Contest 088 D Grid Repainting 【BFS】
Time limit : 2sec / Memory limit : 256MB
Score: 400 points
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≤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:
Explanation of Sample 1
Sample Input 2
10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................
Sample Output 2
209
題意: 給你個包含.#
的地圖,.
代表白色方塊,#
代表黑色方塊,在保證可以從左上角走到右下角情況下,求最多可以把多少白塊變成黑塊
分析:我們直接先列舉下黑塊的數量,然後再減去最短路,即可,這裡的最短路可以用DFS,或者是BFS都行,我用的BFS。
參考程式碼
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
const ll maxn = 1e3 + 10;
const int Move[4][2] = {-1,0,1,0,0,1,0,-1};
char mp[maxn][maxn];
int step[maxn][maxn];
bool vis[maxn][maxn];
int n,m;
bool check(int x,int y) {
if(x < 0 || y < 0|| x >= n || y >= m || vis[x][y] || mp[x][y] == '#') return false;
return true;
}
int BFS() {
queue<pair<int ,int> > q;
q.push(make_pair(0,0));
step[0][0] = 1;
vis[0][0] = true;
while (!q.empty()) {
pair<int ,int> t = q.front();
q.pop();
for(int i = 0;i < 4;i++) {
int tox = t.fi + Move[i][0];
int toy = t.se + Move[i][1];
if(tox == n - 1 && toy == m - 1) return step[t.fi][t.se] + 1;
if(check(tox,toy)) {
vis[tox][toy] = true;
step[tox][toy] = step[t.fi][t.se] + 1;
q.push(make_pair(tox,toy));
}
}
}
return -1;
}
int main(){
init();
cin>>n>>m;
int cnt = 0;
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++) {
cin>>mp[i][j];
if(mp[i][j] == '#') cnt++;
}
int res = BFS();
if(res == -1) {
cout<<"-1"<<endl;
} else {
cout<<n*m - res - cnt<<endl;
}
return 0;
}
- 如有錯誤或遺漏,請私聊下UP,thx