Guest User

Untitled

a guest
Jan 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Solution {
  4. public static boolean canWin(int leap, int[] game) {
  5.  
  6. int pos = 0;
  7.  
  8. while (pos < game.length) {
  9.  
  10. if (pos >= game.length || pos + leap >= game.length)
  11. return true;
  12.  
  13. if (canMoveOneForward(pos, game)) {
  14. pos++;
  15. continue;
  16. }
  17.  
  18. if (canLeap(leap, pos, game)) {
  19. pos += leap;
  20. continue;
  21. }
  22.  
  23. if(canBack(pos, game)){
  24.  
  25. while (canBack(pos, game)) {
  26. game[pos] = 1;
  27. pos--;
  28.  
  29. if (canLeap(leap, pos, game)) {
  30. pos += leap;
  31. break;
  32. }
  33. }
  34.  
  35. continue;
  36. }
  37.  
  38. return false;
  39.  
  40. }
  41.  
  42. return false;
  43.  
  44. }
  45.  
  46. public static boolean canMoveOneForward(int pos, int[] game){
  47. if (pos+1 < game.length)
  48. return game[pos+1] == 0;
  49.  
  50. return false;
  51. }
  52.  
  53. public static boolean canLeap(int leap, int pos, int[] game) {
  54. if (game[pos + leap] == 0)
  55. return true;
  56. return false;
  57. }
  58.  
  59. public static boolean canBack(int pos, int[] game) {
  60. if (pos > 0)
  61. if (game[pos - 1] == 0)
  62. return true;
  63. return false;
  64. }
  65.  
  66.  
  67. public static void main(String[] args) {
  68. Scanner scan = new Scanner(System.in);
  69. int q = scan.nextInt();
  70. while (q-- > 0) {
  71. int n = scan.nextInt();
  72. int leap = scan.nextInt();
  73.  
  74. int[] game = new int[n];
  75. for (int i = 0; i < n; i++) {
  76. game[i] = scan.nextInt();
  77. }
  78.  
  79. System.out.println( (canWin(leap, game)) ? "YES" : "NO" );
  80. }
  81. scan.close();
  82. }
  83. }
Add Comment
Please, Sign In to add comment