KNIGHT MOVE

Правка en1, от shreyas_171, 2021-06-09 09:14:27

Can anyone just tell me Whats's wrong with my Code ??

Problem : Daniel is a chess player. At his free time, he usually plays chess with his family or his friends. But, sometimes they have their own activities, so Daniel can't play chess. He spends his time to learn about knight's move. He has 1,000 x 1,000 chessboard, numbered from 1 to 1,000. He wants to move his knight from (a,b) to (1,1) with minimum movement. Help Daniel to solve it.

KNMOVE

MY CODE :

#include <bits/stdc++.h>
using namespace std;

#define fast                    ios_base::sync_with_stdio(false);  cin.tie(NULL);
#define time                    cerr<<"Time taken : " <<(float)clock()/CLOCKS_PER_SEC <<" secs"<<"\n"  ;
#define F                       first
#define S                       second
#define pb                      push_back
typedef long long int           ll  ;
#define INF                     1e15
#define MOD                     (int)1e9+7

// #define TRACE
// #ifdef TRACE
// #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
// template <typename Arg1>
// void __f(const char* name, Arg1&& arg1) {
//     cerr << name << " : " << arg1 << std::endl;
// }
// template <typename Arg1, typename... Args>
// void __f(const char* names, Arg1&& arg1, Args&&... args) {
//     const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
// }
// #else
// #define trace(...)
// #endif



ll  visit[1004][1004]  ;

ll  chess[1004][1004] ;


ll dx[] = {2, 2, -2, -2, 1, 1, -1, -1};
ll dy[] = { -1, 1, -1, 1, 2, -2, 2, -2};


bool VALID( ll x , ll  y) {


    return (x >= 0  && x < 1000 && y >= 0 && y < 1000)  ;

}
void BFS(ll a , ll b) {

    memset(chess , 0 , sizeof (chess))  ;
    memset(visit , 0 , sizeof(visit))  ;

    visit[a][b] = 1;

    chess[a][b] = 0;

    queue<pair<ll, ll>>q ;

    q.push({a , b}) ;

    while (!q.empty()) {


        pair<ll, ll>curr  =  q.front()  ;
        q.pop()  ;

        for (int i = 0 ; i < 8 ; i++) {

            ll  curr_x =  curr.F + dx[i];
            ll  curr_y = curr.S + dy[i];

            if (VALID(curr_x , curr_y) && !visit[curr_x][curr_y]) {

                visit[curr_x][curr_y] = 1 ;
                chess[curr_x][curr_y] = chess[curr.F][curr.S] + 1;
                q.push({curr_x , curr_y})   ;
            }
        }
    }

}



int32_t main() {

    fast ; time;
    BFS(1, 1)  ;

#ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin)  ;
    freopen("output.txt" , "w" , stdout)  ;
#endif

    ll t = 1;
    cin >> t;

    while (t--) {


        ll x, y;
        cin >> x >> y;

        cout << chess[x][y] << "\n"  ;


    }


    return 0  ;
}

Теги #codeforces, #cp, #spoj, #knightmove

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский shreyas_171 2021-06-09 09:14:27 2868 Initial revision (published)