1、问题描述:
给定一个n*n的围棋盘,交叉点上分布着黑,白子或空白(分别用字符b,w和 n表示),
相同颜色的棋子在同行或同列相连属于同一块棋,请遍历整个围棋盘,计算黑白棋各有几块。
输入:第一行只有一个整数n(0<n<20),表示棋盘的大小。
接下来n行中,包含n个字符,分别用字符b,w和 n表示黑,白子或空白。
输出:两个整数,分别表示黑,白棋各有几块。 打印出属于同一区域的棋子坐标(可选择)。
2、样例输入:
6
bbbnww
nbnnwn
bbwwnn
bwwnnw
bbbbww
wwnbbw
3、样例输出:
1 4
4、算法思想:
深搜或广搜均可
5、代码如下:
#include<iostream>
#include<cstdio>
#include <vector>
#include <opencv2/opencv.hpp>
#include <cv.h>
using namespace std;
using namespace cv;
const int MAX = 20;
char m_map[MAX][MAX];
bool lib[MAX][MAX]; //标记该点是否已经来过
int DicX[4] = {0, 1, 0, -1}; //向东南西北四个方向移动,x坐标相当于行坐标
int DicY[4] = {1, 0, -1, 0}; //向东南西北四个方向移动,y坐标相当于列坐标
int n;
void DFS(int x, int y, char c); //x和y分别表示当前行坐标和列坐标,c表示棋子颜色
void BFS(int x, int y, char c, vector<Point>& points); //x和y分别表示当前行坐标和列坐标,c表示棋子颜色
int main()
{
cin >> n;
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
cin >> m_map[i][j];
}
}
vector<Point> outPoints;
int sumB = 0, sumW = 0; //分别记录黑,白棋的块数
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
if (!lib[i][j] && m_map[i][j] != 'n') //该位置未处理过
{
// DFS(i, j, m_map[i][j]);
BFS(i, j, m_map[i][j], outPoints);
if (m_map[i][j] == 'b')
{
sumB++;
cout<<outPoints<<endl;
}
else
{
sumW++;
cout<<outPoints<<endl;
}
}
outPoints.clear();
}
}
cout << sumB << " " << sumW << endl;
return 0;
}
void DFS(int x, int y, char c) //x和y分别表示当前行坐标和列坐标,c表示棋子颜色
{
int nx, ny;
for (int i=0; i<4; i++)
{
nx = x + DicX[i];
ny = y + DicY[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < n && !lib[nx][ny] && m_map[nx][ny] == c)
{
lib[nx][ny] = 1; //走过了就不能再来
DFS(nx, ny, c);
}
}
}
void BFS(int x, int y, char c, vector<Point>& points) //x和y分别表示当前行坐标和列坐标,c表示棋子颜色
{
points.clear();
int Q[MAX*MAX][2]; //用来存储节点坐标的队列
int nx, ny, r = 0, f = 0;
Q[r][0] = x;
Q[r++][1] = y;
while (r > f)
{
x = Q[f][0];
y = Q[f++][1];
for (int i=0; i<4; i++)
{
nx = x + DicX[i];
ny = y + DicY[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < n && !lib[nx][ny] && m_map[nx][ny] == c)
{
lib[nx][ny] = 1; //走过了就不能再来
Q[r][0] = nx;
Q[r++][1] = ny;
Point onepoint;
onepoint.x = nx;
onepoint.y = ny;
points.push_back(onepoint);
}
}
}
}
6、实际输出如下: