結果
問題 |
No.340 雪の足跡
|
ユーザー |
|
提出日時 | 2016-01-29 23:07:03 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 469 ms / 1,000 ms |
コード長 | 2,847 bytes |
コンパイル時間 | 1,284 ms |
コンパイル使用メモリ | 108,996 KB |
実行使用メモリ | 16,000 KB |
最終ジャッジ日時 | 2024-09-21 18:36:29 |
合計ジャッジ時間 | 7,398 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 32 |
ソースコード
#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> using namespace std; int minDist(const vector<string>& plane, char wall, int y1, int x1, int y2, int x2) { int dy[] = {0, 0, -1, 1}; int dx[] = {-1, 1, 0, 0}; int h = plane.size(); int w = plane[0].size(); queue<pair<int, int> > q; vector<vector<bool> > check(h, vector<bool>(w, false)); q.push(make_pair(y1, x1)); check[y1][x1] = true; int ret = 0; while(!q.empty()){ queue<pair<int, int> > q1; while(!q.empty()){ int y = q.front().first; int x = q.front().second; q.pop(); if(y == y2 && x == x2) return ret; for(int i=0; i<4; ++i){ int y0 = y + dy[i]; int x0 = x + dx[i]; if(plane[y0][x0] != wall && !check[y0][x0]){ q1.push(make_pair(y0, x0)); check[y0][x0] = true; } } } ++ ret; q.swap(q1); } return -1; } int main() { int w, h, n; cin >> w >> h >> n; vector<string> s(2*h+1, string(2*w+1, '#')); for(int y=0; y<h; ++y){ for(int x=0; x<w; ++x){ s[2*y+1][2*x+1] = '.'; } } vector<vector<int> > a(h, vector<int>(w, 0)); vector<vector<int> > b(h, vector<int>(w, 0)); for(int i=0; i<n; ++i){ int m, z1; cin >> m >> z1; for(int j=0; j<m; ++j){ int z2; cin >> z2; int y1 = z1 / w; int x1 = z1 % w; int y2 = z2 / w; int x2 = z2 % w; if(y1 == y2){ if(x2 < x1) swap(x1, x2); ++ a[y1][x1]; -- a[y1][x2]; } else{ if(y2 < y1) swap(y1, y2); ++ b[y1][x1]; -- b[y2][x1]; } z1 = z2; } } for(int y=0; y<h; ++y){ int sum = 0; for(int x=0; x<w; ++x){ sum += a[y][x]; if(sum > 0) s[2*y+1][2*x+2] = '.'; } } for(int x=0; x<w; ++x){ int sum = 0; for(int y=0; y<h; ++y){ sum += b[y][x]; if(sum > 0) s[2*y+2][2*x+1] = '.'; } } int ans = minDist(s, '#', 1, 1, 2 * h - 1, 2 * w - 1); if(ans == -1) cout << "Odekakedekinai.." << endl; else cout << (ans / 2) << endl; return 0; }