結果
| 問題 | No.124 門松列(3) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-09-17 05:51:15 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,901 bytes |
| 記録 | |
| コンパイル時間 | 801 ms |
| コンパイル使用メモリ | 92,548 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-19 06:50:41 |
| 合計ジャッジ時間 | 1,661 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 WA * 1 |
| other | AC * 12 WA * 14 |
ソースコード
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
using namespace std;
int inputValue(){
int a;
cin >> a;
return a;
};
void inputArray(int * p, int a){
rep(i, a){
cin >> p[i];
}
};
void inputVector(vector<int> * p, int a){
rep(i, a){
int input;
cin >> input;
p -> push_back(input);
}
}
template <typename T>
void output(T a, int precision) {
if(precision > 0){
cout << setprecision(precision) << a << "\n";
}
else{
cout << a << "\n";
}
}
bool isKado(int a, int b, int c){
if ((c < a && a < b) || (b < a && a < c) || (a < c && c < b) || (b < c && c < a)) {
return true;
}
else{
return false;
}
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main(int argc, const char * argv[]) {
// source code
int W = inputValue(); // 2以上
int H = inputValue(); // 2以上
vector<vector<int> > M(W, vector<int>(H));
vector<vector<int> > C(W, vector<int>(H)); //最短
//vector<vector<bool> > V(W, vector<bool>(H)); //通ったか
// bool V[200][200];
rep(i, W){
rep(j, H){
M[i][j] = inputValue();
// V[i][j] = false;
C[i][j] = 0;
}
}
queue<pair<pair<int, int>, int>> q;
if (H >= 3) {
if (isKado(M[0][0], M[0][1], M[0][2])) {
q.push(make_pair(make_pair(0, 2), M[0][1]));
C[0][2] = 2;
}
}
if (isKado(M[0][0], M[0][1], M[1][1])) {
q.push(make_pair(make_pair(1, 1), M[0][1]));
C[1][1] = 2;
}
if (isKado(M[0][0], M[1][0], M[1][1])) {
q.push(make_pair(make_pair(1, 1), M[1][0]));
C[1][1] = 2;
}
if (W >= 3) {
if (isKado(M[0][0], M[1][0], M[2][0])) {
q.push(make_pair(make_pair(2, 0), M[1][0]));
C[2][0] = 2;
}
}
int count = 0;
while (!q.empty() && count < 100000) {
int x = q.front().first.first;
int y = q.front().first.second;
int prevM = q.front().second;
q.pop();
// V[x][y] = true;
rep(i, 4){
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= W || ny < 0 || ny >= H) {
continue;
}
if (isKado(prevM, M[x][y], M[nx][ny])) {
q.push(make_pair(make_pair(nx, ny), M[x][y]));
C[nx][ny] = max(C[nx][ny], C[x][y] + 1);
}
}
count++;
}
output((C[W - 1][H - 1] != 0) ? C[W - 1][H - 1] : -1, 0);
return 0;
}