Submission #1166048
Source Code Expand
//全探索 + 貪欲
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#define int long long
using namespace std;
typedef pair<int, int> Pos;
typedef pair<int, vector<Pos>> Piece;
//入力
int h, w, k;
int s[50][50];
void input() {
string str;
cin >> h >> w >> k;
for (int i = 0; i < h; i++) {
cin >> str;
for (int j = 0; j < w; j++) {
s[i][j] = str[j] - '0';
}
}
}
//サブ関数
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
bool is_range(int r, int c) {
return 0 <= r && r < h && 0 <= c && c < w;
}
//ピース全列挙
bool marked[50][50] = {false};
vector<Piece> pieces;
//(r, c)…今から見る場所がr行c列目, path…今までの経路 (葉で列挙する系)
void search(int r, int c, vector<Pos> &path) {
marked[r][c] = true;
path.push_back(Pos(r, c));
if (path.size() == k) {
int score = 1;
for (int i = 0; i < k; i++) {
score *= s[path[i].first][path[i].second];
}
pieces.push_back(Piece(score, path));
path.pop_back();
marked[r][c] = false;
return;
}
for (int i = 0; i < 4; i++) {
int nr = r + dy[i];
int nc = c + dx[i];
if (is_range(nr, nc) && !marked[nr][nc] && s[nr][nc] > 0) {
search(nr, nc, path);
}
}
path.pop_back();
marked[r][c] = false;
}
//貪欲
vector<Pos> ans;
void greedy() {
for (int r = 0; r < h; r++) {
for (int c = 0; c < w; c++) {
vector<Pos> path;
if (s[r][c] > 0) {
search(r, c, path);
}
}
}
sort(pieces.begin(), pieces.end(), greater<Piece>());
for (int i = 0; i < pieces.size(); i++) {
vector<Pos> path = pieces[i].second;
bool can_put = true;
for (int j = 0; j < path.size(); j++) {
if (marked[path[j].first][path[j].second]) {
can_put = false;
break;
}
}
if (can_put) {
for (int j = 0; j < path.size(); j++) {
ans.push_back(Pos(path[j].first, path[j].second));
marked[path[j].first][path[j].second] = true;
}
}
}
}
signed main() {
input();
greedy();
cout << ans.size() / k << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
}
return 0;
}
Submission Info
Submission Time |
|
Task |
A - Multiple Pieces |
User |
startcpp |
Language |
C++14 (GCC 5.4.1) |
Score |
815748 |
Code Size |
2262 Byte |
Status |
AC |
Exec Time |
1750 ms |
Memory |
428252 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 |
85008 / 1343058 |
76415 / 1343058 |
85074 / 1343058 |
74798 / 1343058 |
86812 / 1343058 |
76123 / 1343058 |
85905 / 1343058 |
76438 / 1343058 |
86011 / 1343058 |
83164 / 1343058 |
Status |
|
|
|
|
|
|
|
|
|
|
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 |
1405 ms |
320476 KB |
subtask_01_02.txt |
AC |
1511 ms |
346460 KB |
subtask_01_03.txt |
AC |
1636 ms |
427612 KB |
subtask_01_04.txt |
AC |
1725 ms |
428124 KB |
subtask_01_05.txt |
AC |
1750 ms |
427868 KB |
subtask_01_06.txt |
AC |
1527 ms |
347100 KB |
subtask_01_07.txt |
AC |
1561 ms |
355420 KB |
subtask_01_08.txt |
AC |
1591 ms |
357084 KB |
subtask_01_09.txt |
AC |
1680 ms |
428252 KB |
subtask_01_10.txt |
AC |
1472 ms |
335452 KB |