Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(params) {
- var rows = parseInt(params[0]),
- cols = parseInt(params[1]),
- tests = parseInt(params[rows + 2]), i, j, move,
- board = [],
- answer = [];
- for (var j = 0; j < rows; j += 1) {
- board[j] = params[j + 2].split('');
- }
- for (i = 0; i < tests; i++) {
- move = params[rows + 3 + i].split(' ');
- var currentRow = rows - move[0][1] * 1,
- currentCol = move[0][0].charCodeAt() - 97,
- targetRow = rows - move[1][1] * 1,
- targetCol = move[1][0].charCodeAt() - 97;
- var figure = board[currentRow][currentCol];
- if (currentRow === targetRow && currentCol === targetCol) {
- answer.push('no');
- continue;
- }
- while (true) {
- if (figure === 'Q') {
- if (targetRow !== currentRow && targetCol !== currentCol) {
- if (Math.abs(targetRow - currentRow) !== Math.abs(targetCol - currentCol)) {
- answer.push('no');
- break;
- }
- }
- if (Math.abs(targetRow - currentRow) === Math.abs(targetCol - currentCol)) {
- moveDiagonally();
- } else {
- moveStraight()
- }
- } else if (figure === 'B') {
- if (targetRow === currentRow || targetCol === currentCol) {
- answer.push('no');
- break;
- }
- if (Math.abs(targetRow - currentRow) !== Math.abs(targetCol - currentCol)) {
- answer.push('no');
- break;
- }
- moveDiagonally();
- } else if (figure === 'R') {
- if (targetRow !== currentRow && targetCol != currentCol) {
- answer.push('no');
- break;
- }
- moveStraight()
- } else {
- answer.push('no');
- break;
- }
- if (board[currentRow][currentCol] === 'R' || board[currentRow][currentCol] === 'B' || board[currentRow][currentCol] === 'Q') { // check '-'
- answer.push('no');
- break;
- }
- if (targetRow === currentRow && targetCol === currentCol) {
- answer.push('yes');
- break;
- }
- }
- }
- function moveDiagonally() {
- if (targetRow < currentRow) {
- if (targetCol < currentCol) {
- currentRow--;
- currentCol--;
- } else {
- currentRow--;
- currentCol++;
- }
- } else {
- if (targetCol < currentCol) {
- currentRow++;
- currentCol--;
- } else {
- currentRow++;
- currentCol++;
- }
- }
- }
- function moveStraight() {
- if (targetRow < currentRow) {
- currentRow--;
- } else if (targetRow > currentRow) {
- currentRow++;
- } else if (targetCol < currentCol) {
- currentCol--
- } else if (targetCol > currentCol) {
- currentCol++;
- }
- }
- return answer.join('\n').trim();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement