About Me

About Me : I have been working as a Software Engineer for various international companies for four years.Currently, I am working as a full stack Javascript developer in Petronas(Malaysia).

Skills

Skills • Javascript •Typescript •Python •C •Java •ReactJs • Redux • VueJs • NestJs • React Testing Library • Django• PostgreSQL • MySQL • NodeJs • Git • Docker • Jira • Visual Studio Code • Slack

রবিবার, ৮ মার্চ, ২০১৫

uva 439 - Knight Moves solution

#include<bits/stdc++.h>
using namespace std;
void BFS(int a,int b);
int cost[100][100];
int check[100][100];
queue<int>Q;
int dr[]= {-2,-2,+2,+2,+1,-1,+1,-1};
int dc[]= {+1,-1,+1,-1,-2,-2,+2,+2};
int start_row, start_col,  end_row, end_col;
int main()
{
    string a,b;
    while(cin>>a>>b)
    {
        start_row=int(a[0]-96);
        start_col=a[1]-'0';
        end_row=int(b[0]-96);
        end_col=b[1]-'0';
        // cout<<start_row<<end_row<<endl;
        memset(cost,0,sizeof(cost));
        memset(check,0,sizeof(check));
        BFS(start_row-1,start_col-1);
        cout<<"To get from "<<a<<" to "<<b<<" takes "<<cost[end_row-1][end_col-1]<<" knight moves."<<endl;

    }

    return 0;
}
void BFS(int r,int c)
{
    check[r][c]=1;
    cost[r][c]=0;
    Q.push(r);
    Q.push(c);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        int v=Q.front();
        Q.pop();
        for(int i=0; i<8; i++)
        {
            int row=dr[i]+u;
            int col=dc[i]+v;
            if(((row>=0&&row<8)&&(col>=0&&col<8))&&check[row][col]==0)
            {
                check[row][col]=1;
                cost[row][col]=cost[u][v]+1;
                Q.push(row);
                Q.push(col);
            }
        }


    }



}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন