排序后如何获取数组元素的旧索引 [英] How to get the old index of an element of an array after sorting

查看:65
本文介绍了排序后如何获取数组元素的旧索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在研究alghroitms。所以,我发现了一个传统问题;这是骑士之旅。



基本上,我已经创建了解决方案。但是,我无法格式化它。



23 8 59 62 25 10 29 54

60 63 24 9 52 55 26 11

7 22 61 58 41 28 53 30

64 37 40 51 56 49 12 27

21 6 57 42 39 46 31 48

36 3 38 45 50 43 16 13

5 20 1 34 15 18 47 32

2 35 4 19 44 33 14 17



这是输出样本。我希望看到这样的输出

第一名:G3(第1名)

第二名:H1(第2名)

第三名:F2( 3号)

等等。



最好的方法是什么。我对数组进行了排序,但仍有问题。我正在丢失旧指数。所以我不能重命名它们。



这是我的代码:

Main。

Hello everyone,

I am studying on alghroitms. So, I have found a tradiational problem; which is "Knight's Tour".

Basicly, I have created the solution. However, I cannot format it.

23 8 59 62 25 10 29 54
60 63 24 9 52 55 26 11
7 22 61 58 41 28 53 30
64 37 40 51 56 49 12 27
21 6 57 42 39 46 31 48
36 3 38 45 50 43 16 13
5 20 1 34 15 18 47 32
2 35 4 19 44 33 14 17

This is sample of output. I want to see the output like this
First: G3 (Number 1)
Second: H1 (Number 2)
Third: F2 (Number 3)
and so on.

What is the best way to do this. I sorted the array but, still have problems. I am loosing the old indexes. So I can' t rename them.

Here is my code:
Main.

public static void main(String[] args) {
        At _at =  new At();
        _at.grid = new int[_at.base][_at.base];
        _at.total = (_at.base - 4) * (_at.base - 4);

        for (int r = 0; r < _at.base; r++) {
            for (int c = 0; c < _at.base; c++) {
                if (r < 2 || r > _at.base - 3 || c < 2 || c > _at.base - 3) {
                    _at.grid[r][c] = -1;
                }
            }
        }

        int row = 2 + (int) (Math.random() * (_at.base - 4));
        int col = 2 + (int) (Math.random() * (_at.base - 4));

         _at.grid [row][col] = 1;

        if (_at.solve(row, col, 2)) {
            _at.printResult();
        } else {
            System.out.println("no result");
        }

    }





Horse Class



Horse Class

public class At {

    private final static int TahtaBoyutu = 8;
    public int base = TahtaBoyutu + 4;
    //ArrayList<Integer> hareketler = new ArrayList<>();
    //public final static int[][] aktar = new int[8][8];
    public final static int[][] moves = {{1, -2}, {2, -1}, {2, 1}, {1, 2}, {-1, 2},
    {-2, 1}, {-2, -1}, {-1, -2}};
    public static int[][] grid;
    public int total;

    public boolean solve(int r, int c, int count) {
        if (count > total) {
            return true;
        }

        List<int[]> nbrs = neighbors(r, c);

        if (nbrs.isEmpty() && count != total) {
            return false;
        }

        Collections.sort(nbrs, new Comparator<int[]>() {
            public int compare(int[] a, int[] b) {
                return a[2] - b[2];
            }
        });

        for (int[] nb : nbrs) {
            r = nb[0];
            c = nb[1];
            grid[r][c] = count;
            if (!orphanDetected(count, r, c) && solve(r, c, count + 1)) {
                return true;
            }
            grid[r][c] = 0;
        }

        return false;
    }

    private static List<int[]> neighbors(int r, int c) {
        List<int[]> nbrs = new ArrayList<>();

        for (int[] m : moves) {
            int x = m[0];
            int y = m[1];
            if (grid[r + y][c + x] == 0) {
                int num = countNeighbors(r + y, c + x);
                nbrs.add(new int[]{r + y, c + x, num});
            }
        }
        return nbrs;
    }

    private static int countNeighbors(int r, int c) {
        int num = 0;
        for (int[] m : moves) {
            if (grid[r + m[1]][c + m[0]] == 0) {
                num++;
            }
        }
        return num;
    }

    public boolean orphanDetected(int cnt, int r, int c) {
        if (cnt < total - 1) {
            List<int[]> nbrs = neighbors(r, c);
            for (int[] nb : nbrs) {
                if (countNeighbors(nb[0], nb[1]) == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    public void printResult() {
        for (int[] row : grid) {
            for (int i : row) {
                if (i == -1) {
                    continue;
                }
                System.out.printf("%2d ", i);
            }
            System.out.println();
        }
    }

推荐答案

hmmm



为什么不在网格中运行两个for循环并使用网格值作为线性数组的索引(只要网格值是唯一的)



hmmm

why don't you run two for loops across your grid and use the grid value as the index into a linear array (as long as the grid values are unique)

// pseudo code 

for rows = 0..7
  for cols = 0..7
     linear string array[grid[row, col]] = ascii(65+row) concat ascii(48+col) 
  end-for
end-for

for linear-index 1..64
  print linear string array[linear-index]
end-for 


这篇关于排序后如何获取数组元素的旧索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆