Created
July 30, 2014 13:37
-
-
Save mizuhara/82b3e5e075da680b2748 to your computer and use it in GitHub Desktop.
An answer of http://nabetani.sakura.ne.jp/hena/ord23snakemoinc/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#include <climits> | |
using namespace std; | |
constexpr int FIELD_SIZE = 5; | |
int field[FIELD_SIZE][FIELD_SIZE]; | |
int memo[10][FIELD_SIZE][FIELD_SIZE]; | |
typedef pair<int, int> point_t; | |
constexpr point_t points[] = { make_pair(-1, 0), make_pair(1, 0), make_pair(0, -1), make_pair(0, 1), }; | |
constexpr int size = sizeof(points) / sizeof(*points); | |
auto dfs(int d, int i, int j, int n)-> int | |
{ | |
if(memo[d][i][j] != -1) return memo[d][i][j]; | |
int res = INT_MIN; | |
for(size_t x = 0; x < size; ++x) { | |
const int ii = i + points[x].first; | |
const int jj = j + points[x].second; | |
if( (0 <= ii && ii < FIELD_SIZE) && (0 <= jj && jj < FIELD_SIZE) && (field[i][j] < field[ii][jj]) ) { | |
res = max( res, dfs(d + 1, ii, jj, n + 1) ); | |
} | |
} | |
return memo[d][i][j] = max(res, n); | |
} | |
auto solve(const string &src)-> int | |
{ | |
for(size_t i = 0, j = 0; i < src.length(); ++i) { | |
if(src[i] == '/') { | |
j += 1; | |
continue; | |
} | |
field[j][i % 6] = src[i] - '0'; | |
} | |
memset( memo, -1, sizeof(memo) ); | |
int res = INT_MIN; | |
for(size_t j = 0; j < FIELD_SIZE; ++j) { | |
for(size_t i = 0; i < FIELD_SIZE; ++i) { | |
res = max( res, dfs(0, i, j, 1) ); | |
} | |
} | |
return res; | |
} | |
void test(const string &src, int expected) | |
{ | |
const int actual = solve(src); | |
cout << (actual == expected ? "ok" : "***NG***") << endl; | |
} | |
int main() | |
{ | |
pair<string, int> d[] = { | |
make_pair("01224/82925/69076/32298/21065", 6), | |
make_pair("03478/12569/03478/12569/03478", 10), | |
make_pair("09900/28127/87036/76545/87650", 10), | |
// 略 | |
}; | |
for(size_t i = 0; i < sizeof(d) / sizeof(*d); ++i) { | |
test(d[i].first, d[i].second); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment