L3-018 森森美图

L3-018 森森美图

#include<bits/stdc++.h>

using namespace std;
typedef pair<double,int> P;
#define FI first
#define SE second
int dr[2][4] = {0,0,1,-1,-1,1,0,0};
int dr2[2][4] = {1,1,-1,-1,-1,1,-1,1};
typedef long long LL;
struct  Point
{
	int x,y;
	Point(int xx = 0,int yy = 0):x(xx),y(yy){}	
	bool operator == (const Point &a){
		return a.x == x && a.y == y;
	}
};
const int maxn = 100+10,maxm = maxn*maxn;
int a[maxn][maxn];
double dis[maxn][maxn];
bool done[maxn][maxn];
Point S,T;
int N,M;
int DIR;
int  Multiply(Point &a,Point &b,Point & c){
	int x1 = b.x-a.x;
	int y1 = b.y-a.y;
	int x2 = c.x-a.x;
	int y2 = c.y-a.y;

	return x1*y2-x2*y1;
}
bool judge(int x,int y){
	if( x < 0 ||x >= N || y < 0 || y >= M || done[x][y]) return false;
	Point c(x,y);
	if(c == S ||c == T) return true;
	if(DIR == 0 && Multiply(S,T,c) < 0) return true;
	if(DIR == 1 && Multiply(S,T,c) > 0) return true; 
	// if(DIR) cout<<x<<" "<<y<<endl;
	return false;
}

void Dij(){
	for(int i = 0;i < N; ++i)
		for(int j = 0;j < M; ++j)
			dis[i][j] = 0x3f3f3f3f,done[i][j] = 0;
	priority_queue<P> Q;
	Q.push(P(0,S.x*M+S.y));
	dis[S.x][S.y] = a[S.x][S.y];
	while(!Q.empty()){
		P p = Q.top(); Q.pop();
		int x = p.SE/M,y = p.SE%M;
		if(done[x][y]) continue;
		done[x][y] = true;
		for(int i = 0;i < 4; ++i){
			int xx = x + dr[0][i];
			int yy = y + dr[1][i];
			if(!judge(xx,yy)) continue;
			if(dis[xx][yy] > a[xx][yy] + dis[x][y]) {
				dis[xx][yy] = a[xx][yy] + dis[x][y];
				Q.push(P(-dis[xx][yy],xx*M+yy));
			}
		}
		for(int i = 0;i < 4; ++i){
			int xx = x + dr2[0][i];
			int yy = y + dr2[1][i];
			if(!judge(xx,yy)) continue;
			double e = (sqrt(2)-1)*(a[xx][yy]+a[x][y]);
			if(dis[xx][yy] > a[xx][yy] + e + dis[x][y])
			{
				dis[xx][yy] = a[xx][yy] + e + dis[x][y];
				Q.push(P(-dis[xx][yy],xx*M+yy));
			}
		}
	}
}
int main(void){
		// freopen("input.txt","r",stdin);
		// for(int i =0;i < 4; ++i)
		// 	cout<<dr[0][i]<<" "<<dr[1][i]<<endl;
		// for(int i = 0;i < 4; ++i)
		// 	cout<<dr2[0][i]<<" "<<dr2[1][i]<<endl;
		// freopen("output.txt","w+",stdout);
	cin>>N>>M;
	for(int i = 0;i < N; ++i){
		for(int j = 0;j < M; ++j){
			cin>>a[i][j];
		}
	}
	int x1,y1,x2,y2;
	cin>>y1>>x1>>y2>>x2;
	S = Point(x1,y1),T = Point(x2,y2);
	double ans = 0;
	DIR = 1,Dij(),ans += dis[x2][y2];
	// cout<<ans<<endl;
	// cout<<dis[x2][y2]<<endl;
	DIR = 0,Dij(),ans += dis[x2][y2];
	// cout<<dis[x2][y2]<<endl;
	ans -= a[x2][y2];
	ans -= a[x1][y1];
	printf("%.2f\n",ans+1e-6);
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值