用曼哈顿距离模式填充2d阵列

时间:2015-07-31 09:48:26

标签: java arrays algorithm for-loop multidimensional-array

我正在尝试为算法做这个功课,他们让我填写这样的二维int数组:

4 3 2 3 4
3 2 1 2 3
2 1 0 1 2
3 2 1 2 3
4 3 2 3 4

我在java中试过这个:

int[][] array = new int[5][5];
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[i].length; j++) {
        array[i][j] = Math.abs(i - j);
    }
}

但它给了我这样的东西:

0 1 2 3 4
1 0 1 2 3
2 1 0 1 3
3 2 1 0 1
4 3 2 1 0

它并不是一回事,但它是我发现的最接近的东西。我用java编写代码,但它可以使用任何其他语言......重要的是我认为的“公式”。所以,如果你可以帮我解决这个问题,那就太好了,我试着在网上寻找代码,但我没找到任何东西......谢谢。

2 个答案:

答案 0 :(得分:6)

看起来你正在寻找到中心的距离。所以你首先必须计算这一点:

public class ChangeEmail extends BaseActivity implements View.OnClickListener {

EditText Password, CurrentEmail, NewEmail;
String FirebaseUserNewEmail, FirebasePassword, FirebaseUserCurrentEmail;
Button buttonDone;
public static final String DEFAULT = "N/A";
String username;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /**
     * Adding our layout to parent class relative layout.
     */
    getLayoutInflater().inflate(R.layout.changeemail, RelativeLayout);

    CurrentEmail = (EditText) findViewById(R.id.CurrentEmail);
    NewEmail = (EditText) findViewById(R.id.NewEmail);
    buttonDone = (Button) findViewById(R.id.buttonDone);
    buttonDone.setOnClickListener(this);
    // Password - Edit Text
    Password = (EditText) findViewById(R.id.Password);
    // Make text style stay the same / as default
    Password.setTypeface(Typeface.DEFAULT);
    // Make password confidential.
    Password.setTransformationMethod(new PasswordTransformationMethod());

    SharedPreferences prefs = getSharedPreferences("project", 0);
    username = prefs.getString("keyusername", DEFAULT);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ChangeEmail.this);
    username = settings.getString("keyusername", DEFAULT);

    setTitle("Settings");

}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onStop() {
    super.onStop();
}

public void onDestroy() {
    super.onDestroy();
}

public void ChangeEmail()
{
    FirebaseUserCurrentEmail = CurrentEmail.getText().toString();
    FirebaseUserNewEmail = NewEmail.getText().toString();
    FirebasePassword = Password.getText().toString();
    System.out.println("Current Email :" + FirebaseUserCurrentEmail);
    System.out.println("New Email :" + FirebaseUserNewEmail);
    System.out.println("Password :" + FirebasePassword);
    Firebase ref = new Firebase("https://tapamp.firebaseio.com");
    ref.changeEmail(FirebaseUserCurrentEmail, FirebaseUserNewEmail, FirebasePassword, new Firebase.ResultHandler() {
        @Override
        public void onSuccess() {
            Toast.makeText(getApplicationContext(), "Welcome " + FirebaseUserNewEmail, Toast.LENGTH_SHORT).show();
            Intent a = new Intent(ChangeEmail.this, HomePage.class);
            startActivity(a);
        }
        @Override
        public void onError(FirebaseError firebaseError) {
            Toast.makeText(getApplicationContext(), "" + firebaseError, Toast.LENGTH_LONG).show();
            switch (firebaseError.getCode()) {
                case FirebaseError.USER_DOES_NOT_EXIST:
                    // handle a non existing user
                    break;
                case FirebaseError.INVALID_PASSWORD:
                    // handle an invalid password
                    break;
                case FirebaseError.INVALID_EMAIL:
                    Toast.makeText(getApplicationContext(), "Invalid Email" , Toast.LENGTH_SHORT).show();
                    break;
                default:
                    // handle other errors
                    break;
            }
        }
    });
}

@Override
public void onClick(View view) {
    if (view == buttonDone) {
        ChangeEmail();
    }
    else if (view == buttonLogout)
    {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

        alertDialogBuilder.setIcon(R.drawable.trunk);
        alertDialogBuilder.setTitle("Log out"); // your dialog title
        // set dialog message
        alertDialogBuilder
                .setMessage("Are you sure?")
                .setCancelable(true)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent a = new Intent(ChangeEmail.this, LoginPage.class);
                        a.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        a.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        finish();
                        startActivity(a);
                        Firebase ref = new Firebase("https://tapamp.firebaseio.com");
                        ref.unauth();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
}

然后,计算距离非常简单:

int center = array.length / 2; //assuming a quadratic array

答案 1 :(得分:-1)

这也可以。

int p=N-1;
for(i=0,l=N-1;i<=l;i++,l--)
{
    for(j=0,k=N-1;j<=k;j++,k--)
    {
        arr[i][j]=p;
        arr[i][k]=p;
        arr[l][j]=p;
        arr[l][k]=p;
        p--;
    }
    p=N-i-2;
}