2019年初の筆記試験--ファン

2071 ワード

問題の説明:
1.1つの球場CのファンスタンドはM*Nのファンを収容することができる.公式は全部でどれだけのファンがいるか、最大のファンがどれだけいるかを統計したいと思っています.ファンの座席選択特性:1.1.同じファングループは隣接席を選択し、異なるファングループは隣接しない席を選択する.(隣接は前後隣接、左右隣接、斜め対角隣接を含む);2.M*Nの2人の球場を与えて、0はこの位置を代表して誰もいないで、1はこの位置を代表して人がいて、チームのグループの個数Pを出力することを望んで、最大のチームのグループの人数Q.入力:第1行、2個の数字、MN、英語のカンマを使って次のM行を隔てて、各行のN個の数字、英語のカンマを使って出力を隔てます:1行、2数字、P Q例:
入力:
10,10
0,0,0,0,0,0,0,0,0,0
0,0,0,1,1,0,1,0,0,0
0,1,0,0,0,0,0,1,0,1
1,0,0,0,0,0,0,0,1,1
0,0,0,1,1,1,0,0,0,1
0,0,0,0,0,0,1,0,1,1
0,1,1,0,0,0,0,0,0,0
0,0,0,1,0,1,0,0,0,0
0,0,1,0,0,1,0,0,0,0
0,1,0,0,0,0,0,0,0,0
出力:6,8
解析:再帰的な思想は1の周囲を遍歴する
#include
#include
#include
using namespace std;

void dfs(int ** fans, int a, int b,int row,int column,int& num)
{
	if (a < 0 || a >= row || b < 0 || b >= column)
		return;
	if (!fans[a][b])
		return;
	fans[a][b] = 0;// 1    0,      
	num++;//    +1;
	dfs(fans, a - 1, b - 1, row, column, num);
	dfs(fans, a - 1, b , row, column, num);
	dfs(fans, a - 1, b +1, row, column, num);
	dfs(fans, a , b - 1, row, column, num);
	//dfs(fans, a , b , row, column, num);
	dfs(fans, a , b +1, row, column, num);
	dfs(fans, a + 1, b - 1, row, column, num);
	dfs(fans, a + 1, b , row, column, num);
	dfs(fans, a +1, b + 1, row, column, num);
	return;
}

int main()
{
	int m, n;
	char a;
	cin >> m >>a>> n;
	int **position = new int*[m];
	for (int i = 0; i < m; i++)
		position[i] = new int[n];
	int p;
	for (int i = 0; i < m; i++){
		for (int j = 0; j < n;){
			cin >> p; 
			cin.ignore(1, ',');
			position[i][j] = p;
			j++;
		
		}
	}
	int team = 0;
	int max_num = 0;
	for (int i = 0; i < m; i++){
		for (int j = 0; j < n; j++){
			if (position[i][j]){
				team++;
				int num = 0;
				dfs(position, i, j, m, n, num);
				if (num > max_num)
					max_num = num;
			}
		}
	}
	cout <

参照先:http://www.cnblogs.com/Semora-2004/p/9487782.html