Created
November 7, 2015 18:06
-
-
Save trha/4d5135e9a18f139e7e9e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function num_at(n, r, c) { //trả về số tại ô (r, c) của ma trận xoắn ốc nxn | |
| var square = Math.min(r, c, n - r - 1, n - c - 1) | |
| var num = 4 * square * (n - square) //số ngay trước hình vuông này | |
| var top = square | |
| var right = n - square - 1; | |
| var bottom = n - square - 1; | |
| var side = n - 2 * square //độ dài cạnh | |
| if (r === top) { //nằm ở cạnh trên | |
| return num + (c - square) + 1 | |
| } | |
| num += side // + thêm cạnh trên | |
| if (c === right) { //nằm ở cạnh phải | |
| return num + r - square | |
| } | |
| num += side - 1 // + thêm cạnh phải | |
| if (r === bottom) { //nằm ở cạnh dưới | |
| return num + (n - c - 1) - square | |
| } | |
| num += side - 1 // + thêm cạnh dưới | |
| //nằm ở cạnh trái | |
| return num + (n - r - 1 - square) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment