标签:des style http color os io java ar strong




2 2 1 1 1 1 1 3 3 1 19 14 20 12 15 18 13 14 16 4 4 2 14 15 14 15 14 15 14 15 14 15 14 15 14 15 14 15 0 0 0
1.00 1.00 1.00 1.00 2.00 30.00 17.00 25.00 7.00 13.00 14.00 0.00 35.00 1.00 27.00 2.00 28.00 21.00 12.00 17.00 8.00 21.00 12.00 17.00 8.00 1.00 27.00 2.00 28.00HintThe Manhattan Distance (sometimes called the Taxicab distance) between two points is the sum of the (absolute) difference of their coordinates. The grid on the lower right illustrates the Manhattan distances from the grayed cell.
题解及代码:
这是一个double精度的高斯消元,方程建立直接暴力枚举每个点与当前点的距离就可以了,注意一下题目中给出的输入 先是列数,后是行数T^T
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const double eps=0.00000001;
double a[110][110];
double x[110];
int equ,var;
void Debug()
{
for(int i=0;i<equ;i++)
{
for(int j=0;j<=var;j++)
printf("%.2lf ",a[i][j]);
puts("");
}
puts("");
}
void Gauss()
{
int k,col,max_r;
for(k=0,col=0;k<equ&&col<var;k++,col++)
{
max_r=k;
for(int i=k+1;i<equ;i++)
if(fabs(a[i][col])-fabs(a[max_r][col])>eps) max_r=i;
if(max_r!=k)
for(int i=0;i<=var;i++)
{
swap(a[max_r][i],a[k][i]);
}
if(fabs(a[k][col])<eps)
{
k--;
continue;
}
for(int i=k+1;i<equ;i++)
{
if(fabs(a[i][col])>eps)
{
double t=a[i][col]/a[k][col];
for(int j=col;j<=var;j++)
{
a[i][j]=a[i][j]-a[k][j]*t;
}
}
}
}
//Debug();
for(int j=var-1;j>=0;j--)
{
if(fabs(a[j][j])<eps) continue;
double temp=a[j][var];
for(int r=j+1;r<var;r++)
temp=temp-a[j][r]*x[r];
x[j]=temp/a[j][j];
}
}
void init(int m,int n,int d) //系数矩阵初始化
{
var=equ=m*n;
int D,B,L,R;
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
int pos=0;
double k=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
k=0;
pos=i*n+j;
for(int l=0;l<m;l++)
for(int r=0;r<n;r++)
if(abs(l-i)+abs(r-j)<=d)
{
k+=1.0;
a[pos][l*n+r]=1.0;
}
a[pos][var]=k;
}
//Debug();
}
int main()
{
int n,m,d;
double X;
bool fis=true;
while(scanf("%d%d%d",&n,&m,&d))
{
if(n==0&&m==0&&d==0) break;
if(!fis) puts("");
else fis=false;
init(m,n,d);
for(int i=0;i<equ;i++)
{
scanf("%lf",&X);
a[i][var]*=X;
}
//Debug();
Gauss();
for(int i=0;i<equ;i++)
{
printf("%8.2lf",x[i]);
if((i+1)%n==0) printf("\n");
}
}
return 0;
}标签:des style http color os io java ar strong
原文地址:http://blog.csdn.net/knight_kaka/article/details/39159085