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}}; void work(int dep,int x,int y){ a[x][y]=1; if(x==b1&&y==b2){ ans=min(ans,dep); a[x][y]=0; return; } for(int i=0;i<=3;i++){ int tx=x+dx[i]; int ty=y+dy[i]; if(a[tx][ty]==0)work(dep+1,tx,ty); } a[x][y]=0; } int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>a1>>a2>>b1>>b2; work(0,a1,a2); cout<<ans; return 0; }
信息
- ID
- 409
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- 递交数
- 40
- 已通过
- 23
- 上传者