## Problem Links:

HackerRank: Coin on the Table## Problem:

You have a rectangular board consisting of n rows , numbered from 1 to n and m columns , numbered from 1 to m . Initially - at time 0 - there is a coin on the top-left cell of your board. Each cell of your board contains one of these letters:

*, exactly one of your cells has letter ‘*’.

U, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘U’, the coin will be on the cell(i-1,j) in time t+1 if i > 1. otherwise there is no coin on your board at time t+1.

L, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘L’, the coin will be on the cell(i,j-1) in time t+1 if j > 1. otherwise there is no coin on your board at time t+1.

D, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘D’, the coin will be on the cell(i+1,j) in time t+1 if i < n. otherwise there is no coin on your board at time t+1.

R, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘R’, the coin will be on the cell(i,j+1) in time t+1 if j < m. otherwise there is no coin on your board at time t+1.

*, exactly one of your cells has letter ‘*’.

U, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘U’, the coin will be on the cell(i-1,j) in time t+1 if i > 1. otherwise there is no coin on your board at time t+1.

L, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘L’, the coin will be on the cell(i,j-1) in time t+1 if j > 1. otherwise there is no coin on your board at time t+1.

D, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘D’, the coin will be on the cell(i+1,j) in time t+1 if i < n. otherwise there is no coin on your board at time t+1.

R, If at time t, the coin is on the cell(i,j) and cell(i,j) has letter ‘R’, the coin will be on the cell(i,j+1) in time t+1 if j < m. otherwise there is no coin on your board at time t+1.

When the coin reaches the cell that has letter ‘*’ it will be there permanently. When you punch on your board, your timer starts and the coin moves between cells. before doing that you want to do some operation so that you could be sure that at time k the coin will be on the cell having letter ‘*’. In each operation you can select a cell with some letter other than ‘*’ and change the letter to ‘U’, ‘L’, ‘R’ or ‘D’. You want to do as few operations as possible in order to achieve your goal. Your task is to find the minimum number of operations.

**Input:**

The first line of input contains three integers n and m and k.

Next n lines contains m letters which describe you board.

n and m are integers less than 51.

k is less than 1001.

**Output:**

on the only line of the output print an integer being the answer to the test.

If you cannot achieve your goal, output -1 please.

**Sample input :**

2 2 3

RD

*L

RD

*L

**Sample output :**

0

**Sample input :**

2 2 1

RD

*L

RD

*L

**Sample output :**

1

**Explanation :**

In the first example you don’t have to change any letters, but in the second example you should change the letter of cell (1,1) to ‘D’.

## Solution:

Obviously, the problem ask you to find the shortest path from (0, 0) to the location of '*', where there is weight 1 cost if you do not have the right corresponding direction character. Like if you goes up, it will cost you 0 if you have 'U' at the corresponding location, otherwise, you just have to pay for 1 unit cost.Here, I use Breadth-First-Search to find the shortest path from (0, 0) to '*', and use memorization(int[][] cost) to reduce redundant calculation.

PS: the problem statement shows at K moment, the coin has to share the same cell of '*', this could also happen if the coin reach there earlier, which means the coin could just sit and wait there, this also could be the solution if this results few costs.

## Source Code:

import java.util.Arrays; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; public class Solution { String[] board; int[][] cost; int N; int M; static final int[] dx = { 1, 0, -1, 0 }; static final int[] dy = { 0, -1, 0, 1 }; static final char[] ch = { 'D', 'L', 'U', 'R' }; public static void main(String[] args) { Solution main = new Solution(); main.run(); System.exit(0); } private void run() { Scanner in = new Scanner(System.in); N = in.nextInt(); M = in.nextInt(); int k = in.nextInt(); board = new String[N]; for (int i = 0; i < N; i++) { board[i] = in.next(); } cost = new int[N][M]; for (int i = 0; i < N; i++) // for (int j = 0; j < M; j++) Arrays.fill(cost[i], Integer.MAX_VALUE); int ret = bfs(k); if (ret == Integer.MAX_VALUE) ret = -1; System.out.println(ret); } private int bfs(int K) { Queue<Node> queue = new PriorityQueue<Node>(); queue.add(new Node(0, 0, K, 0)); while (!queue.isEmpty()) { Node node = queue.poll(); int x = node.x; int y = node.y; int k = node.k; int cost = node.cost; this.cost[x][y] = Math.min(this.cost[x][y], cost); if (board[x].charAt(y) == '*' && k >= 0) return this.cost[x][y]; else if (k < 0) continue; for (int i = 0; i < 4; i++) { int xx = x + dx[i]; int yy = y + dy[i]; if (inBoard(xx, yy)) { int c = board[x].charAt(y) == ch[i] ? 0 : 1; if (c + cost < this.cost[xx][yy]) queue.add(new Node(xx, yy, k - 1, c + cost)); } } } return -1; } private boolean inBoard(int x, int y) { return x >= 0 && x < N && y >= 0 && y < M; } } class Node implements Comparable<Node> { public int x; public int y; public int k; public int cost; public Node(int x, int y, int k, int c) { this.x = x; this.y = y; this.k = k; this.cost = c; } public int compareTo(Node node) { // if (this.cost != node.cost) return this.cost - node.cost; // if (this.k != node.k) // return this.k - node.k; // return this.x + this.y - node.x - node.y; } }

## No comments :

Post a Comment