Submission #2078665


Source Code Expand

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = { 0, 1, 0,-1};

int H, W, K;
vector<string> grid;
int board[50][50];
bool visit[50][50];

void disp_board(void){
	rep (i, H){
		rep (j, W){
			cerr << board[i][j];
		} // end rep
		cerr << endl;
	} // end rep
}

vector<int> solve(int cr, int cc){
	vector<int> res; res.clear();
	queue<int> que;
	que.push(cr * W + cc);
	while(!que.empty() && (int)res.size() < K){
		int curr = que.front(); que.pop();
		cr = curr / W;
		cc = curr % W;
		if (!visit[cr][cc]){
			visit[cr][cc] |= true;
			res.push_back(cr * W + cc);
		} // end if
		for (int k = 0; k < 4; ++k){
			int nr = curr / W + dr[k];
			int nc = curr % W + dc[k];
			if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
			if (!visit[nr][nc]){
				que.push(nr * W + nc);
			} // end if
		} // end for
	} // end while
	return ((int)res.size() == K ? res : vector<int>());
}			

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	memset(board, 0, sizeof(board));
	memset(visit, false, sizeof(visit));
	grid.clear();
	cin >> H >> W >> K;
	grid.resize(H, string(W, '0'));
	rep (i, H){
		cin >> grid[i];
	} // end rep
	rep (i, H) rep (j, W) board[i][j] = (int)(grid[i][j] - '0');
	rep (i, H) rep (j, W) visit[i][j] = (bool)(board[i][j] == 0);

//	disp_board();
	vector<vector<int> > res; res.clear();
	rep (i, H) rep (j, W){
		if (!visit[i][j]){
			vector<int> ans = solve(i,j);
			if (!ans.empty()){
				res.push_back(ans);
			} // end if
		} // end if
	} // end rep
	cout << (int)res.size() << endl;
	rep (i, (int)res.size()){
		rep (j, (int)res[i].size()){
			int x = res[i][j] % W;
			int y = res[i][j] / W;
			cout << y + 1 << ' ' << x + 1 << endl;
		} // end rep
	} // end rep
	
	return 0;
}

Submission Info

Submission Time
Task A - Multiple Pieces
User ty70
Language C++14 (GCC 5.4.1)
Score 102292
Code Size 1961 Byte
Status AC
Exec Time 5 ms
Memory 384 KB

Judge Result

Set Name test_01 test_02 test_03 test_04 test_05 test_06 test_07 test_08 test_09 test_10
Score / Max Score 11203 / 1343058 11249 / 1343058 10954 / 1343058 8621 / 1343058 10118 / 1343058 9806 / 1343058 9309 / 1343058 9406 / 1343058 9916 / 1343058 11710 / 1343058
Status
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
Set Name Test Cases
test_01 subtask_01_01.txt
test_02 subtask_01_02.txt
test_03 subtask_01_03.txt
test_04 subtask_01_04.txt
test_05 subtask_01_05.txt
test_06 subtask_01_06.txt
test_07 subtask_01_07.txt
test_08 subtask_01_08.txt
test_09 subtask_01_09.txt
test_10 subtask_01_10.txt
Case Name Status Exec Time Memory
subtask_01_01.txt AC 5 ms 256 KB
subtask_01_02.txt AC 5 ms 256 KB
subtask_01_03.txt AC 5 ms 256 KB
subtask_01_04.txt AC 5 ms 384 KB
subtask_01_05.txt AC 5 ms 384 KB
subtask_01_06.txt AC 5 ms 256 KB
subtask_01_07.txt AC 5 ms 384 KB
subtask_01_08.txt AC 5 ms 256 KB
subtask_01_09.txt AC 5 ms 256 KB
subtask_01_10.txt AC 5 ms 256 KB