Hledání nejkratší cesty jezdcem

#include <iostream>
using namespace std;
 
const int N = 8;
 
int p [N+1] [N+1];
 
void tisk ()
{
    for (int r = 1; r <= N; r++)
    {
        for (int s = 1; s <= N; s++)
        {
            if (p [r][s] == -1)
                cout << ' ';
            else
                cout << p [r][s];
            cout << ' ';
        }
        cout << endl;
    } 
    cout << endl;
}
 
void jump (int r, int s, int v = 0)
{
    if (r >= 1 && r <= N && s >= 1 && s <= N)
    {
        if (p[r][s] == -1 || p[r][s] > v)
        {
            p [r][s] = v;
 
            jump (r+1, s+2, v+1);
            jump (r+1, s-2, v+1);
            jump (r-1, s+2, v+1);
            jump (r-1, s-2, v+1);
 
            jump (r+2, s+1, v+1);
            jump (r+2, s-1, v+1);
            jump (r-2, s+1, v+1);
            jump (r-2, s-1, v+1);
        }
    }
}
 
int main()
{
    for (int r = 0; r <= N; r++)
        for (int s = 0; s <= N; s++)
            p [r][s] = -1; // nenavstivena policka
 
    jump (1, 1);
    tisk ();
 
    cout << "O.K." << endl;
}
 
zalg/jezdec_ct.txt · Last modified: 2021/03/18 15:16 by 147.32.9.2
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki