結果
問題 | No.340 雪の足跡 |
ユーザー |
![]() |
提出日時 | 2016-06-17 22:07:59 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 462 ms / 1,000 ms |
コード長 | 2,828 bytes |
コンパイル時間 | 719 ms |
コンパイル使用メモリ | 74,332 KB |
実行使用メモリ | 23,064 KB |
最終ジャッジ日時 | 2024-10-09 16:34:37 |
合計ジャッジ時間 | 9,035 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 32 |
ソースコード
#include <iostream>#include <vector>#include <map>#include <queue>#include <algorithm>using namespace std;#define REP(i,s,e) for (i = s; i <= e; i++)#define rep(i,n) REP (i,0,(int)(n)-1)#define RREP(i,s,e) for (i = s; i >= e; i--)#define rrep(i,n) RREP (i,(int)(n)-1,0)#define INF (int)1e8#define MOD (int)(1e9+7)#define RIGHT 3#define LEFT 1#define UP 0#define DOWN 2typedef long long ll;int pass[1000][1000][4];int dist[1000][1000];int main(void) {int i, j, w, h, n;cin >> w >> h >> n;rep (i,n) {int m, pre;cin >> m >> pre;rep (j,m) {int b;cin >> b;if (pre / w == b / w) {int y = pre / w;int from = pre % w;int to = b % w;if (from < to) {pass[y][from][RIGHT]++;pass[y][to][RIGHT]--;pass[y][to][LEFT]++;pass[y][from][LEFT]--;}else {pass[y][from][LEFT]++;pass[y][to][LEFT]--;pass[y][to][RIGHT]++;pass[y][from][RIGHT]--;}}else {int x = pre % w;int from = pre / w;int to = b / w;if (from < to) {pass[from][x][UP]++;pass[to][x][UP]--;pass[to][x][DOWN]++;pass[from][x][DOWN]--;}else {pass[from][x][DOWN]++;pass[to][x][DOWN]--;pass[to][x][UP]++;pass[from][x][UP]--;}}pre = b;}}rep (i,h) rep (j,w-1) pass[i][j+1][RIGHT] += pass[i][j][RIGHT];rep (i,h) rrep (j,w-1) pass[i][j][LEFT] += pass[i][j+1][LEFT];rep (j,w) rep (i,h-1) pass[i+1][j][UP] += pass[i][j][UP];rep (j,w) rrep (i,h-1) pass[i][j][DOWN] += pass[i+1][j][DOWN];queue<pair<int,int>> q;rep (i,h) rep (j,w) dist[i][j] = INF;dist[0][0] = 0;q.push(make_pair(0,0));while (!q.empty()) {int y = q.front().first;int x = q.front().second;q.pop();int dxy[4] = {1,0,-1,0};rep (i,4) if (pass[y][x][i]) {int ny = y + dxy[i];int nx = x + dxy[(i+1)%4];if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;if (dist[ny][nx] > dist[y][x] + 1) {dist[ny][nx] = dist[y][x] + 1;q.push(make_pair(ny,nx));}}}if (dist[h-1][w-1] == INF)cout << "Odekakedekinai.." << endl;elsecout << dist[h-1][w-1] << endl;return 0;}