日期 2021 / 10 / 21
[题目链接]https://acm.dingbacode.com/showproblem.php?pid=1312
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 40902 Accepted Submission(s): 24844
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black > > tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the > numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as > > > follows.
'.' - a black tile '#' - a red tile '@' - a man on a black tile(appears exactly once in a data set)
Output For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
题目大意是寻找人能到达的最多黑砖块的数目,是个bfs模板吧可以说,利用struct存x,y坐标,然后放到queue里面,对于队列中出来的每一个点 判断其位置是否在边界外以及是否碰到了红砖块,还有就是判断这个点是否被用过(这里得重视,写代码时答案错误改了好久就是这里的问题)都满足条件的话就 cnt++,其他的就没什么了,主要是很久没写bfs的题目今天练一下手。
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define rep(x, y, z) for (int x = y; x <= z; x++)
#define dec(x, y, z) for (int x = y; x >= z; x--)
#define format(a) memset (a, 0, sizeof(a))
#define swap(a, b) (a ^= b ^= a ^= b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
const int maxn = 1e6 + 10;
using namespace std;
string Map[21];
int used[21][21];
int sx, sy, cnt, n, m;
int dirx[4] = {0, 1, -1, 0};
int diry[4] = {1, 0, 0, -1};
struct coordinate {
int x;
int y;
};
void BFS() {
coordinate start, next;
start.x = sx;
start.y = sy;
queue<coordinate> q;
q.push(start);
while (!q.empty()) {
start = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
next.x = start.x + dirx[i];
next.y = start.y + diry[i];
if (next.x >= 0 && next.x < m && next.y >= 0 && next.y < n && Map[next.x][next.y] == '.' && !used[next.x][next.y]) {
cnt++;
used[next.x][next.y] = 1;
q.push(next);
}
}
}
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string s1 = "@";
while (cin >> n >> m && n && m) {
format(used);
cnt = 1;
int k;
for (int i = 0; i < m; i++) {
cin >> Map[i];
k = Map[i].find(s1);
if (k < n && k != -1) {
sx = i;
sy = k;
}
}
BFS();
cout << cnt << endl;
}
return 0;
}