Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
10
Indexable
#include <iostream>
using namespace std;
/////Tim nha k bi phu song
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int main (){

	freopen("input.txt", "r", stdin);
	int T;
	cin >> T;
	for(int tc = 1; tc <= T; tc++) {
		int size;
		cin >> size;
		char arr[100][100];
		for(int i = 0; i < size; i++) {
			for(int j = 0; j < size; j++) {
				cin >> arr[i][j];
			}
		}
		
		for(int i = 0; i < size; i++) 
		{
			for(int j = 0; j < size; j++) 
			{
				if(arr[i][j] == 'A' || arr[i][j] == 'B' || arr[i][j] == 'C') 
				{
					for(int k = 0; k < 4; k++) 
					{
						int u = i+dx[k];
						int v = j+dy[k];
						if(u >= 0 && v>=0 && u<=size && v<=size && (arr[u][v] == 'H' || arr[u][v] == 'X')) 
						{
							arr[u][v] = 'O';
						}
					}
				}
				if(arr[i][j] == 'B' || arr[i][j] == 'C') 
				{
					for(int k = 0; k < 4; k++) 
					{
						int u = i+dx[k]*2;
						int v = j+dy[k]*2;
						if(u >= 0 && v>=0 && u<=size && v<=size && (arr[u][v] == 'H' || arr[u][v] == 'X')) 
						{
							arr[u][v] = 'O';
						}
					}
				}
				if(arr[i][j] == 'C') {
					for(int k = 0; k < 4; k++) 
					{
						int u = i+dx[k]*3;
						int v = j+dy[k]*3;
						if(u >= 0 && v>=0 && u<=size && v<=size && (arr[u][v] == 'H' || arr[u][v] == 'X')) 
						{
							arr[u][v] = 'O';
						}
					}
				}
			}
		}
		int count = 0;
		for(int i = 0; i < size; i++) 
		{
			for(int j = 0; j < size; j++) 
			{
				if(arr[i][j] == 'H') count ++;
			}
		}
		cout << "#" << tc << " " << count << endl;
	}
	return 0;
}