結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-06-07 06:11:40 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2,747 ms / 5,000 ms | 
| コード長 | 912 bytes | 
| コンパイル時間 | 2,553 ms | 
| コンパイル使用メモリ | 202,484 KB | 
| 最終ジャッジ日時 | 2025-01-10 23:40:25 | 
| ジャッジサーバーID (参考情報) | judge2 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:12:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |         scanf("%d%d%d%d%d%d",&n,&v,&sx,&sy,&gx,&gy); sx--; sy--; gx--; gy--;
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:13:32: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |         rep(i,n) rep(j,n) scanf("%d",&B[i][j]);
      |                           ~~~~~^~~~~~~~~~~~~~~
            
            ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
const int INF=1<<29;
int main(){
	int n,v,sx,sy,gx,gy,B[100][100];
	scanf("%d%d%d%d%d%d",&n,&v,&sx,&sy,&gx,&gy); sx--; sy--; gx--; gy--;
	rep(i,n) rep(j,n) scanf("%d",&B[i][j]);
	if(v>=1800) return printf("%d\n",abs(sx-gx)+abs(sy-gy)),0;
	static int d[100][100][1800];
	rep(i,n) rep(j,n) rep(k,v+1) d[i][j][k]=INF;
	d[sy][sx][v]=0;
	priority_queue<tuple<int,int,int,int>> Q; Q.emplace(0,sy,sx,v);
	while(!Q.empty()){
		int d0,i,j,k; tie(d0,i,j,k)=Q.top(); Q.pop();
		d0*=-1;
		if(d0>d[i][j][k]) continue;
		if(i==gy && j==gx) return printf("%d\n",d0),0;
		rep(l,4){
			int y=i+dy[l],x=j+dx[l];
			if(0<=y && y<n && 0<=x && x<n && B[y][x]<k && d[y][x][k-B[y][x]]>d0+1){
				d[y][x][k-B[y][x]]=d0+1;
				Q.emplace(-(d0+1),y,x,k-B[y][x]);
			}
		}
	}
	puts("-1");
	return 0;
}
            
            
            
        