Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 寻找骑士所有独特的可能动作_Java_Algorithm - Fatal编程技术网

Java 寻找骑士所有独特的可能动作

Java 寻找骑士所有独特的可能动作,java,algorithm,Java,Algorithm,我们有一个8x8板,单元从一个开始,我的目标是编写这样一个带有签名的函数: 公共静态字符串findPos(int i,int j,int n)它接受参数(i,j)骑士在棋盘上的起始位置,n是它将进行的总移动量(n>=0) 例如: findPos(1,1,0)将仅返回(1,1),因为0步骑士只能停留在自己的位置 FindPO(1,1,1)将返回(1,1)、(2,3)、(3,2) 我写了一个不太漂亮的解决方案,但是它计算了骑士在这种情况下可以采取的重复位置,例如findPos(1,1,2): 公共

我们有一个8x8板,单元从一个开始,我的目标是编写这样一个带有签名的函数:
公共静态字符串findPos(int i,int j,int n)
它接受参数(i,j)骑士在棋盘上的起始位置,n是它将进行的总移动量(n>=0)

例如:

findPos(1,1,0)将仅返回(1,1),因为0步骑士只能停留在自己的位置

FindPO(1,1,1)将返回(1,1)、(2,3)、(3,2)

我写了一个不太漂亮的解决方案,但是它计算了骑士在这种情况下可以采取的重复位置,例如findPos(1,1,2):


公共静态字符串findPos(int i、int j、int n){
如果(n==0){

如果代码中的(i>=1&&j>=1&&i=1&&i,则使用布尔数组来标记访问的位置,并防止再次调用该位置

修改如下:

访问的静态布尔值[][]=新布尔值[9][9];
公共静态字符串findPos(int i、int j、int n){
如果(i8 | | | j8 | | | | visted[i][j])返回“”;
否则{
访问[i][j]=真实;
如果(n==0){

如果(i>=1&&j>=1&&i=1&&i不是一个很好的解决方案,但是如果你想避免重复,你可以使用
集合
,使用集合来跟踪你去过的地方。如果你可以改变棋盘,你可以设置一个访问过的旗子,这样一旦骑士看到一个正方形,就把它标记为已访问过,并且不再访问它。下面给出的答案是什么状态估计?解决还是不解决?

public static String findPos(int i, int j, int n) {

        if (n == 0) {
            if (i >= 1 && j >= 1 && i <= 8 && j <= 8) {
                return "(" + i + ", " + j + " )";;
            } else {
                return "";
            }

        } else {

            if (i >= 1 && j >= 1 && i <= 8 && j <= 8) {
                return "(" + i + ", " + j + " )" + findPos(i - 1, j - 2, n - 1)
                        + findPos(i - 2, j - 1, n - 1)
                        + findPos(i - 2, j + 1, n - 1)
                        + findPos(i - 1, j + 2, n - 1)
                        + findPos(i + 1, j + 2, n - 1)
                        + findPos(i + 2, j + 1, n - 1)
                        + findPos(i + 2, j - 1, n - 1)
                        + findPos(i + 1, j - 2, n - 1);

            } else {

                return findPos(i - 1, j - 2, n - 1)
                        + findPos(i - 2, j - 1, n - 1)
                        + findPos(i - 2, j + 1, n - 1)
                        + findPos(i - 1, j + 2, n - 1)
                        + findPos(i + 1, j + 2, n - 1)
                        + findPos(i + 2, j + 1, n - 1)
                        + findPos(i + 2, j - 1, n - 1)
                        + findPos(i + 1, j - 2, n - 1);

            }

        }

    }
#include<bits/stdc++.h>
using namespace std;
//This array is to check if a position is visited or not
bool visited[8][8];
//Here you're making a direction array for the move of the knight
int row[] = {-2,-1,+1,+2,+2,+1,-1,-2};
int col[] = {+1,+2,+2,+1,-1,-2,-2,-1};
//Returning the next move based on the current position and the move no
pair<int,int> nextMove(pair<int,int> curPos, int moveNo) {
    return {
        curPos.first+row[moveNo],
        curPos.second+col[moveNo]
    };
}
//Checking if the current position is in the board or not
bool validity(pair<int,int> curPos) {
    if(curPos.first<1 || curPos.first>8) return false;
    if(curPos.second<1 || curPos.second>8) return false;
    return true;
}
//BFS algorithm
string bfs(pair<int,int> curPos, int n) {
    //Making the string representation of the visited positions
    string ans = "("+to_string(curPos.first)+","+to_string(curPos.second)+")";
    queue<pair<int,int>> q;
    q.push(curPos);
    //Maintaining level so that it doesn't exceed the given "n" number of moves
    map<pair<int,int>, int> level;
    level[curPos] = 0;
    visited[curPos.first][curPos.second] = true;
    while(!q.empty()) {
        curPos = q.front();
        if(level[curPos]>=n) break;
        q.pop();
        for(int i=0; i<8; i++) {
            pair<int,int> tempPos = nextMove(curPos,i);
            if(validity(tempPos) && !visited[tempPos.first][tempPos.second]) {
                level[tempPos] = level[curPos]+1;
                q.push(tempPos);
                visited[tempPos.first][tempPos.second] = true;
                ans += "("+to_string(tempPos.first)+","+to_string(tempPos.second)+")";
            }
        }
    }
    return ans;
}
int main() {
    ios_base::sync_with_stdio(false);
    int i,j,n;
    cin >> i >> j >> n;
    cout << bfs({i,j},n) << "\n";
}
1 1 2
(1,1)(2,3)(3,2)(1,5)(3,5)(4,4)(4,2)(3,1)(1,3)(2,4)(5,3)(5,1)