結果
| 問題 |
No.1323 うしらずSwap
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-20 23:17:49 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,124 bytes |
| コンパイル時間 | 840 ms |
| コンパイル使用メモリ | 77,728 KB |
| 実行使用メモリ | 13,076 KB |
| 最終ジャッジ日時 | 2024-09-21 12:22:44 |
| 合計ジャッジ時間 | 5,675 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 WA * 30 |
コンパイルメッセージ
main.cpp:8:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
8 | main()
| ^~~~
ソースコード
#include<iostream>
#include<queue>
using namespace std;
int H,W,sx,sy,gx,gy;
string s[1333];
int dist[1333][1333];
int d[5]={0,1,0,-1};
main()
{
cin>>H>>W>>sx>>sy>>gx>>gy;
for(int i=0;i<H;i++)cin>>s[i];
for(int i=0;i<H;i++)for(int j=0;j<W;j++)dist[i][j]=1e9;
sx--,sy--;
gx--,gy--;
dist[sx][sy]=0;
queue<pair<int,int> >P;
P.push(make_pair(sx,sy));
while(!P.empty())
{
int x=P.front().first,y=P.front().second;
P.pop();
for(int r=0;r<4;r++)
{
int tx=x+d[r],ty=y+d[r+1];
if(s[tx][ty]=='#'||dist[tx][ty]<=dist[x][y]+1)continue;
dist[tx][ty]=dist[x][y]+1;
P.push(make_pair(tx,ty));
}
}
if(dist[gx][gy]==(int)1e9)
{
cout<<-1<<endl;
return 0;
}
bool fn=false,two=false;
int x=gx,y=gy;
while(x!=sx||y!=sy)
{
int nx=-1,ny=-1;
int cnt=0;
for(int r=0;r<4;r++)
{
int tx=x+d[r],ty=y+d[r+1];
if(s[tx][ty]=='#')continue;
if(dist[tx][ty]==dist[x][y]-1)
{
if(nx<0)nx=tx,ny=ty;
else two=true;
}
cnt++;
}
if((x!=gx||y!=gy)&&cnt>=3)fn=true;
x=nx;
y=ny;
}
if(two)cout<<dist[gx][gy]*2<<endl;
else if(fn)cout<<dist[gx][gy]*2+2<<endl;
else cout<<-1<<endl;
}