红黑地板问题
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.
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.
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 != 0 && m != 0) { char a[105][105]; memset(a,0,sizeof(a)); queue<int>p; int t1, t2; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { cin >> a[i][j]; if (a[i][j] == '@') { t1 = i; t2 = j; p.push(t1); p.push(t2); } } int num = 0, x, y; while (p.empty() == false) { x = p.front(); p.pop(); y = p.front(); p.pop(); num++; //cout << num << endl; if (0 <= x - 1 < m && 0 <= y < n && a[x - 1][y] == '.')//上 { p.push(x - 1); p.push(y); a[x - 1][y] = '#'; } if (0 <= x + 1 < m && 0 <= y < n && a[x + 1][y] == '.')//下 { p.push(x + 1); p.push(y); a[x + 1][y] = '#'; } if (0 <= x < m && 0 <= y - 1 < n && a[x][y - 1] == '.')//左 { p.push(x); p.push(y - 1); a[x][y - 1] = '#'; } if (0 <= x < m && 0 <= y + 1 < n && a[x][y + 1] == '.')//右 { p.push(x); p.push(y + 1); a[x][y + 1] = '#'; } } cout << num << endl; } return 0; }
呜呜呜`~实在不知道哪错了
下面是一种采用标记方法的做法
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[25][25]; int visit[105][105], n, m; queue<point>p; int direct[4][2] = { {-1,0},{1,0},{0,-1},{0,1} }; bool check(int x, int y) { if (0 <= x && x < m && 0 <= y && y < n && visit[x][y]==0 && a[x][y] == '.') return true; else return false; } int bfs(int x, int y) { visit[x][y] = 1; int sum = 0; while (!p.empty()) { s1 = p.front(); int t1 = s1.x; int t2 = s1.y; sum++; p.pop(); for (int i = 0; i < 4; i++) { s1.x = t1 + direct[i][0]; s1.y = t2 + direct[i][1]; if (check(s1.x, s1.y)) { visit[s1.x][s1.y] = 1; p.push(s1); } } } return sum; } int main() { while (cin >> n >> m && n != 0 && m != 0) { memset(visit, 0, sizeof(visit)); memset(a, 0, sizeof(a)); int num = 1; for (int i = 0; i < m; i++) for (int j = 0; 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 0; }
目前还没想明白哪不一样,哎,呜呜呜