View Code of Problem 62

#include <iostream>
#include <string>
#include<vector>
using namespace std;
int main() {
	int n, k;
	while (cin >> n >> k) {
		if (n == 0 && k == 0)break;
		int glass[120][120],court=0;
		string row;
		getline(cin, row);//接收回车
		for (int i = 0; i < n; i++)
		{
			
			getline(cin, row);
			for (int j = 0; j < k; j++)//接收数据
			{
				glass[i][j] = row[j]-'0';
			}
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < k; j++)
			{
				if (glass[i][j] == 1) {
					vector <int>queue;
					int m, l;
						queue.push_back(i);//行列放入链表
						queue.push_back(j);
						glass[i][j] = 0;
						while (!queue.empty()) {//将该羊群的所有羊设为0
							l = queue.back();
							queue.pop_back();
							m = queue.back();
							queue.pop_back();
							if (l + 1 < k && glass[m][l + 1] == 1) {//该点右
								queue.push_back(m);//行列放入链表
								queue.push_back(l + 1);
								glass[m][l + 1] = 0;
							}	
							if (l -1 >= 0 && glass[m][l-1] == 1) {//该点左
								queue.push_back(m);//行列放入链表
								queue.push_back(l-1);
								glass[m][l-1] = 0;
							}
							if (m + 1 < n && glass[m +1][l] == 1) {//该点下
								queue.push_back(m+1 );//行列放入链表
								queue.push_back(l);
								glass[m+1 ][l] = 0;
							}
							if (m - 1 >= 0 && glass[m-1 ][l] == 1) {//该点上
								queue.push_back(m -1);//行列放入链表
								queue.push_back(l);
								glass[m-1][l] = 0;
							}
							if (m + 1 < n && l + 1 < k && glass[m + 1][l + 1] == 1) {//该点右下
								queue.push_back(m + 1);//行列放入链表
								queue.push_back(l + 1);
								glass[m + 1][l + 1] = 0;
							}
							if (m - 1 >= 0 && l + 1 <k&& glass[m -1][l +1] == 1) {//该点右上
								queue.push_back(m - 1);//行列放入链表
								queue.push_back(l +1);
								glass[m - 1][l + 1] = 0;
							}
							if (m - 1 >= 0 && l - 1 >= 0 && glass[m - 1][l - 1] == 1) {//该点左上
								queue.push_back(m - 1);//行列放入链表
								queue.push_back(l - 1);
								glass[m - 1][l - 1] = 0;
							}
							if (m + 1 < n && l - 1 >= 0 && glass[m + 1][l - 1] == 1) {//该点左下
								queue.push_back(m + 1);//行列放入链表
								queue.push_back(l - 1);
								glass[m + 1][l - 1] = 0;
							}
						}
						court++;
					
				}	
				
			}
		}
		cout << court<<endl;
	}
}

Double click to view unformatted code.


Back to problem 62