結果

問題 No.340 雪の足跡
ユーザー startcpp
提出日時 2016-01-30 00:54:30
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 360 ms / 1,000 ms
コード長 3,096 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 71,524 KB
実行使用メモリ 50,560 KB
最終ジャッジ日時 2024-09-21 18:54:52
合計ジャッジ時間 6,082 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void make()’:
main.cpp:43:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 |         scanf("%d%d%d", &w, &h, &n);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:48:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |                         scanf("%d", &t);
      |                         ~~~~~^~~~~~~~~~

ソースコード

diff #
プレゼンテーションモードにする

//使h * w2h * 2w
//2h * 2w10
//m-m-m
//|
//m-m-m
// |
//m-m-m
//
//2h * 2w()0⇔
//!
//
//cin, cout
//
//rx, ry
//(bx, by10001001
#include <iostream>
#include <cstdio>
#include <queue>
#include <tuple>
using namespace std;
int w, h, n;
int m;
int by[1001], bx[1001];
int rx[2002][2002];
int ry[2002][2002];
int dp[2002][2002];
const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
bool isRange(int r, int c){
return (r >= 0 && r < 2 * h - 1 && c >= 0 && c < 2 * w - 1 );
}
void init(){
for( int i = 0; i < 2002; i++ )
for( int j = 0; j < 2002; j++ )
dp[i][j] = 114514194;
}
void make(){
scanf("%d%d%d", &w, &h, &n);
for( int i = 0; i < n; i++ ){
cin >> m; m++;
for( int j = 0; j < m; j++ ){
int t;
scanf("%d", &t);
by[j] = t / w; by[j] *= 2;
bx[j] = t % w; bx[j] *= 2;
}
for( int j = 1; j < m; j++ ){
if( by[j-1] == by[j] ){
//
rx[by[j-1]][min(bx[j-1], bx[j])]++;
rx[by[j-1]][max(bx[j-1], bx[j]) + 1]--;
} else {
//
ry[min(by[j-1], by[j])][bx[j-1]]++;
ry[max(by[j-1], by[j]) + 1][bx[j-1]]--;
}
}
}
for( int i = 0; i < 2*h; i++ )
for( int j = 1; j < 2*w; j++ )
rx[i][j] += rx[i][j-1];
for( int j = 0; j < 2*w; j++ )
for( int i = 1; i < 2*h; i++ )
ry[i][j] += ry[i-1][j];
}
int bfs(){
typedef tuple<int, int, int> T; //, y, x
queue<T> que;
if( ry[0][0] || rx[0][0] || (h == 1 && w == 1) ){
que.push(T(0, 0, 0));
dp[0][0] = 0;
}
while( !que.empty() ){
T now = que.front();
int dist = get<0>(now);
int y = get<1>(now);
int x = get<2>(now);
que.pop();
for( int i = 0; i < 4; i++ ){
int ny = y + dy[i];
int nx = x + dx[i];
if( isRange(ny, nx) && (ry[ny][nx] || rx[ny][nx]) && dp[ny][nx] > dist + 1 ){
que.push(T(dist + 1, ny, nx));
dp[ny][nx] = dist + 1;
}
}
}
if( dp[2*h-2][2*w-2] == 114514194 ) return -1;
return dp[2*h-2][2*w-2] / 2;
}
int main(){
init();
make();
int res = bfs();
if( res == -1 && !(h == 1 && w == 1) ) cout << "Odekakedekinai.." << endl;
else cout << res << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0