ann8497

CHESS samsung

Sep 16th, 2019
2,686
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. /* CHESS BOARD
  2.  
  3. 2
  4. 9 9
  5. 3 5 2 8
  6. 20 20
  7. 2 3 7 9
  8.  
  9. */
  10.  
  11. #include<bits/stdc++.h>
  12. using namespace std;
  13.  
  14. struct node{
  15.     int x;
  16.     int y;
  17.     int l;
  18. };
  19.  
  20. node q[100000];
  21. int front,back;
  22.  
  23. void init(){
  24.     front = back = 0;
  25. }
  26.  
  27. void push(int x, int y, int l){
  28.     q[back].x = x;
  29.     q[back].y = y;
  30.     q[back].l = l;
  31.     back++;
  32. }
  33.  
  34. node pop(){
  35.     return q[front++];
  36. }
  37.  
  38. bool empty(){
  39.     return front == back;
  40. }
  41.  
  42. int n,m;
  43. int sx,sy,dx,dy;
  44. bool vis[100][100];
  45.  
  46. int dex[] = {-1, -2, -2, -1, 1, 2, 2, 1};
  47. int dey[] = {-2, -1, 1, 2, 2, 1, -1, -2};
  48.  
  49. bool valid(int x, int y){
  50.     return (x>=0 && x<n && y>=0 && y<m);
  51. }
  52.  
  53. int solve(){
  54.    
  55.     vis[sx][sy] = true;
  56.     push(sx,sy,0);
  57.    
  58.     while(!empty()){
  59.        
  60.         node temp = pop();
  61.         int x = temp.x;
  62.         int y = temp.y;
  63.         int l = temp.l;
  64.        
  65.         if(x == dx && y == dy)return l;
  66.        
  67.         for(int i = 0; i<8; i++){
  68.            
  69.             int xx = x + dex[i];
  70.             int yy = y + dey[i];
  71.            
  72.             if(valid(xx,yy) && !vis[xx][yy]){
  73.                 vis[xx][yy] = true;
  74.                 push(xx,yy,l+1);
  75.             }
  76.         }
  77.        
  78.     }
  79.    
  80. }
  81.  
  82. int main(){
  83.    
  84.    
  85.     int t; cin>>t;
  86.     while(t--){
  87.     cin>>n>>m;
  88.     cin>>sx>>sy>>dx>>dy;
  89.    
  90.     memset(vis, 0, sizeof(vis));
  91.     init();
  92.     cout<<solve()<<endl;
  93.     }
  94.     return 0;
  95. }
Comments
  • ranjan_him212
    2 years
    # text 0.12 KB | 1 0
    1. Can I get the problems as in maximum solutions questions are not given
    2. It would be helpful if provide the questions as well
    3.  
Add Comment
Please, Sign In to add comment