You are viewing a single comment's thread. Return to all comments →
public static int queensAttack(int n, int k, int r_q, int c_q, List<List<Integer>> obstacles) { // Write your code here List<Integer> list=new ArrayList<>(); int count=0; //----------- // 1. forward if(r_q<n){ int j=r_q; for(int i=c_q+1;i<=n;i++){ list.add(j); list.add(i); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } } } //---------------------------- // 2. backword if(r_q >1){ int j=r_q; for(int i=c_q-1;i>=1;i--){ list.add(j); list.add(i); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } } } //----------------------------------- // 3.up if(c_q<n){ int j=c_q; for(int i=r_q+1;i<=n;i++){ list.add(i); list.add(j); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } } } //------------------------------- // 4.down if(c_q >1){ int j=c_q; for(int i=r_q-1;i>=1;i--){ list.add(i); list.add(j); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } } } // digonal (backword -up) _| // 5. int x=r_q+1; int y=c_q-1; while(x<=n && y>=1){ list.add(x); list.add(y); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } x++; y--; }
// digonal (forward -down) |-- // 6. int xx=r_q-1; int yy=c_q+1; while(xx>=1 && yy<=n){ list.add(xx); list.add(yy); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } xx--; yy++; }
// digonal (forward -up) |\-- // 7. int xxx=r_q+1; int yyy=c_q+1; while(xxx<=n && yyy<=n){ list.add(xxx); list.add(yyy); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } xxx++; yyy++; } // digonal (backword -down) |\-- // 8. int xxxx=r_q-1; int yyyy=c_q-1; while(xxxx>=1 && yyyy>=1){ list.add(xxxx); list.add(yyyy); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } xxxx--; yyyy--; } return count; }
Seems like cookies are disabled on this browser, please enable them to open this website
Queen's Attack II
You are viewing a single comment's thread. Return to all comments →
Java
// digonal (forward -down) |-- // 6. int xx=r_q-1; int yy=c_q+1; while(xx>=1 && yy<=n){ list.add(xx); list.add(yy); if(obstacles.stream().anyMatch(e->e.equals(list))){ list.clear(); break; }else{ count++; list.clear(); } xx--; yy++; }