結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2014-11-09 23:17:07 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 363 ms / 5,000 ms |
コード長 | 1,498 bytes |
コンパイル時間 | 659 ms |
コンパイル使用メモリ | 64,012 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-28 10:05:03 |
合計ジャッジ時間 | 2,001 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <map> #include <vector> #include <memory.h> using namespace std; typedef signed long long ll; #define FOR(x,to) for(x=0;x<to;x++) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) int N,V,SX,SY,GX,GY; int Cost[101][101]; int HP[2][101][101]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>V>>SX>>SY>>GX>>GY; FOR(y,N) { FOR(x,N) { cin>>Cost[y][x]; } } SX--,SY--,GX--,GY--; ZERO(HP[0]); HP[0][SY][SX] = V; int dx[] = {-1,0,1,0}; int dy[] = {0,-1,0,1}; for(int i = 0; i < 10001;i++) { int current = i % 2; int next = (i+1) % 2; ZERO(HP[next]); FOR(y, N) { FOR(x, N) { if(HP[current][y][x] > 0) { for(int k = 0;k < 4;k++) { int nextX = x + dx[k]; int nextY = y + dy[k]; if(nextX < 0 || nextY < 0 || nextX >=N || nextY >= N) { continue; } int nextHP = HP[current][y][x] - Cost[nextY][nextX]; if(nextHP <= 0) { continue; } HP[next][nextY][nextX] = max(HP[next][nextY][nextX], nextHP); } } } } if(HP[next][GY][GX] > 0) { printf("%d\n", i+1); return; } } printf("-1\n"); return; } int main() { solve(); return 0; }