package algorithm;
import java.util.ArrayList;
import java.util.List;
//https://blog.csdn.net/CSDN_PatrickStar/article/details/84933887
/*
Problem:曼哈顿距离
Description:
在平面上,坐标(x1, y1)的点P1与坐标(x2, y2)的点P2的曼哈顿距离为:|x1-x2|+|y1-y2|。 现在有一个nXn (1<=n<=1000)的矩阵,一开始矩阵中每个元素的值都为零。 对这个矩阵进行m (1<=m<=100000)次操作,每次操作定义如下:
(x1, y1), (x2, y2): 对于矩阵中满足x1<=x<=x2, y1<=y<=y2的所有元素(x, y)加上(x, y)与(x1, y1)的曼哈顿距离|x1-x|+|y1-y|
例如,当n=5,m=2,两组操作分别为(1, 1), (4, 4)和(2, 2), (5, 5):
第一行两个整数n, m,分别表示正方形网格的边长和操作次数;
接下来m行,每行4个整数x1 y1 x2 y2;
Output:
输出一个整数代表答案。
Sample Input:
5 2
1 1 4 4
2 2 5 5
Sample Output:
853543927
*/
public class ManhadunDistance {
/*
输入5 ,2 即此为(5+1)*(5+1)元素,做2此变换(1,1)、(4,4) 和(2,2)、(5,5)
第一横行和第一纵行 无用
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 2 3 0
0 1 2 3 4 0
0 2 3 4 5 0
0 3 4 5 6 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 2 3 0
0 1 2 4 6 3
0 2 4 6 8 4
0 3 6 8 10 5
0 0 3 4 5 6
*/
//public boolean isFlag(){
//}
public void solution(int[][] ints,List<Ntree> nt){
int n=ints.length;
for(int m=0;m<nt.size();m++){
Ntree nt1=nt.get(m);
int index=nt1.getIndex();
int x1=nt1.getX1();
int y1=nt1.getY1();
int x2=nt1.getX2();
int y2=nt1.getY2();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i>=x1&&i<=x2&&j>=y1&&j<=y2){
ints[i][j]+=Math.abs(i-x1)+Math.abs(j-y1);
}
}
}
printArray(ints);
}
}
class Ntree{
int index;
int x1;
int y1;
int x2;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
int y2;
Ntree(int index,int x1,int y1,int x2,int y2){
this.index=index;
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
}
public static void printArray(int[][] ints){
System.out.println();
for(int i=0;i<ints.length;i++){
int[] tmp=ints[i];
System.out.println();
for(int j=0;j<tmp.length;j++){
System.out.print("\t"+tmp[j]);
}
}
}
public static long getValue(int[][] ints){
long result=0;
for(int i=0;i<ints.length;i++){
int[] tmp=ints[i];
// System.out.println();
for(int j=0;j<tmp.length;j++){
int tt=tmp[j];
if(tt!=0){
if(result==0){
result=tt;
}else{
result*=tt;
}
}
}
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=5;
int m=2;
int[][] intN=new int[n+1][n+1];
printArray(intN);
ManhadunDistance md=new ManhadunDistance();
List<Ntree> an=new ArrayList<Ntree>();
for(int i=0;i<m;i++){
Ntree nt=md.new Ntree(0,1,1,4,4);
an.add(nt);
//}
Ntree nt1=md.new Ntree(1,2,2,5,5);
an.add(nt1);
md.solution(intN,an);
long result=getValue(intN);
System.out.println("\n"+result);
System.out.println("\n"+result%1000000007);
}
}