标签:space amp efi lse str a* define def arp
随便写的,有些错误。
#include<iostream>
#include<algorithm>
#include<vector>
#define inf 0x7fffff
using namespace std;
vector<int>cur[50000],dist[50000];
int n,m;
int a[201][201],dp[201][201],b[201][201];
int x1,y1,x2,y2;
int stem=1,las=1;
int dx[9]={0,0,0,1,-1,-1,-1,1,1};
int dy[9]={0,1,-1,0,0,-1,1,1,-1};
bool Astar(int x,int y)
{
if(b[201][201]==0)
if(x>0&&x<=n&&y>0&&y<=m)
{
if(dp[x][y]+(n-x)+(n-y)>dp[n][m]||dp[x][y]+(x2-x)+(y2-y)>dp[x2][y2]) return false;
if(dp[x][y]==stem&&dp[n][m]==inf) stem*=2,las=stem/2;
}
return true;
}
void dfs(int x,int y,int step)
{
dp[x][y]=min(dp[x][y],step);
if(x==n&&y==m||x==x2&&y==y2&&a[x][y]==2) return ;
if(step==dp[x][y])
{
if(Astar(x,y)==true)
{
if(a[x][y]>2)
{
for(int j=0;j<cur[a[x][y]].size();j++)
{
int xp=x+cur[a[x][y]][j];
int yp=y+dist[a[x][y]][j];
b[x][y]=1;
dfs(xp,yp,step+1);
b[x][y]=0;
}
}
for(int i=1;i<=8;i++)
{
int xp=x+dx[i];
int yp=y+dy[i];
b[x][y]=1;
dfs(xp,yp,step+1);
b[x][y]=0;
}
}
}
}
void solve()
{
cin>>n>>m;
cin>>x1>>y1>>x2>>y2;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
char x;
cin>>x;
a[i][j]=x-‘0‘;
if(i==x2&&j==y2)
{
if(a[i][j]!=2) a[i][j]=1;
}
if(x>‘2‘)
{
cur[x-‘0‘].push_back(i);
dist[x-‘0‘].push_back(j);
}
dp[i][j]=inf;
}
}
dfs(x1,y1,0);
cout<<min(dp[n][m],dp[x2][y2]);
}
int main()
{
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
solve();
return 0;
}
标签:space amp efi lse str a* define def arp
原文地址:https://www.cnblogs.com/ct666rp/p/12436185.html