B - Red and Black 问题思考

时间:2023-03-09 19:48:35
B - Red and Black 问题思考
红黑地板问题
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) 
The end of the input is indicated by a line consisting of two zeros. 

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
题目大意就是给定一个矩阵图形,初始位置为@,只能走. 不能走#,问最多能走多少.
这是一道基础的dfs,bfs水题,然而,刚接触dfs和bfs的萌新敲得就很难受,看到这题,首先反应过来的就是用bfs解决,上下左右四个方向遍历,走过的就不能再走,好吧,写了两个bfs,第一个不知道哪错了,先放上WR代码吧
WR:把走过的点直接变为#,把要走的位置存入队列,计数,输出
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#pragma warning(disable:4996)
using namespace std;
//char a[105][105];
int main()
{
int n, m;
while (cin >> n >> m && n != && m != )
{
char a[][];
memset(a,,sizeof(a));
queue<int>p;
int t1, t2;
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
{
cin >> a[i][j];
if (a[i][j] == '@')
{
t1 = i;
t2 = j;
p.push(t1);
p.push(t2); }
}
int num = , x, y;
while (p.empty() == false)
{
x = p.front();
p.pop();
y = p.front();
p.pop();
num++;
//cout << num << endl;
if ( <= x - < m && <= y < n && a[x - ][y] == '.')//上
{
p.push(x - );
p.push(y);
a[x - ][y] = '#';
}
if ( <= x + < m && <= y < n && a[x + ][y] == '.')//下
{
p.push(x + );
p.push(y);
a[x + ][y] = '#';
}
if ( <= x < m && <= y - < n && a[x][y - ] == '.')//左
{
p.push(x);
p.push(y - );
a[x][y - ] = '#';
}
if ( <= x < m && <= y + < n && a[x][y + ] == '.')//右
{
p.push(x);
p.push(y + );
a[x][y + ] = '#';
}
}
cout << num << endl;
}
return ;
}

呜呜呜`~实在不知道哪错了

下面是一种采用标记方法的做法

AC:对走过的点进行标记,用visit数组储存访问状态,结构体储存位置存入队列(结构体在bfs中非常实用),bfs基础写法,,,

#include "pch.h"
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#pragma warning(disable:4996)
using namespace std;
int bfs(int x, int y);
struct point
{
int x, y;
}s, s1;
char a[][];
int visit[][], n, m;
queue<point>p;
int direct[][] = { {-,},{,},{,-},{,} }; bool check(int x, int y)
{
if ( <= x && x < m && <= y && y < n && visit[x][y]== && a[x][y] == '.')
return true;
else
return false;
}
int bfs(int x, int y)
{
visit[x][y] = ;
int sum = ;
while (!p.empty())
{
s1 = p.front();
int t1 = s1.x;
int t2 = s1.y;
sum++;
p.pop();
for (int i = ; i < ; i++)
{
s1.x = t1 + direct[i][];
s1.y = t2 + direct[i][]; if (check(s1.x, s1.y))
{
visit[s1.x][s1.y] = ;
p.push(s1); } }
}
return sum;
}
int main()
{
while (cin >> n >> m && n != && m != )
{
memset(visit, , sizeof(visit));
memset(a, , sizeof(a));
int num = ;
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
{
cin >> a[i][j];
if (a[i][j] == '@')
{
s.x = i;
s.y = j;
p.push(s); }
}
num = bfs(s.x, s.y);
cout << num << endl; }
return ;
}

目前还没想明白哪不一样,哎,呜呜呜