Link: coin on the table
At the beginning, I tried to use DFS for a long time.
After reading the problem, you can understand that dynamic planning is required.
Set a three-dimensional array DP, where DP [I] [J] [k] indicates the minimum modification required to arrive at the time K (I, j). The recursive formula is as follows:
Image Source: Editorial, where Delta = 0 when the surrounding lattice can be directly moved to (I, j); otherwise, the direction symbol of the surrounding lattice needs to be changed, Delta = 1.
That is, the K-1 moment is in the four grids around (I,. J), and then moves to (I, j) at K moment ). Also, check whether the direction symbols in the four grids can be moved directly. Otherwise, the direction symbols of the four grids will be changed.
The Code is as follows:
1 import java.util.*; 2 3 public class Solution { 4 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 int n = in.nextInt(); 8 int m = in.nextInt(); 9 int K = in.nextInt();10 11 char[][] map= new char[n][m];12 int[][][] dp = new int[n][m][K+1];13 14 int star_x = 0;15 int star_y = 0;16 17 for(int i = 0;i < n;i++){18 String s = in.next();19 if(s.contains("*"))20 {21 star_x = i;22 star_y = s.indexOf("*");23 }24 map[i]=s.toCharArray();25 }26 27 for(int k=0;k <= K;k++){ 28 for(int i = 0;i < n;i++){29 for(int j = 0;j < m;j++){30 if(k==0)31 dp[i][j][k] = (i==0&&j==0? 0:Integer.MAX_VALUE-1);32 else{33 dp[i][j][k] = CalcuMin(i,j,k,dp,map);34 }35 }36 }37 }38 39 int answer = Integer.MAX_VALUE-1;40 for(int k = 0;k <= K;k++){41 answer = Math.min(answer, dp[star_x][star_y][k]);42 }43 44 System.out.println(answer==Integer.MAX_VALUE-1?-1:answer);45 46 }47 48 private static int CalcuMin(int i, int j, int k,int[][][] dp, char[][] map) {49 // TODO Auto-generated method stub50 int mini = Integer.MAX_VALUE-1;51 int n = map.length;52 int m = map[0].length;53 54 if(i-1>=0){55 if(dp[i-1][j][k-1]+(map[i-1][j]==‘D‘?0:1) < mini )56 mini = Math.min(mini,dp[i-1][j][k-1]+(map[i-1][j]==‘D‘?0:1));57 }58 59 if(i+1<n){60 if(dp[i+1][j][k-1]+(map[i+1][j]==‘U‘?0:1) < mini )61 mini = Math.min(mini,dp[i+1][j][k-1]+(map[i+1][j]==‘U‘?0:1));62 }63 64 if(j-1>=0){65 if(dp[i][j-1][k-1]+(map[i][j-1]==‘R‘?0:1) < mini )66 mini = Math.min(mini,dp[i][j-1][k-1]+(map[i][j-1]==‘R‘?0:1));67 }68 69 if(j+1<m){70 if(dp[i][j+1][k-1]+(map[i][j+1]==‘L‘?0:1) < mini )71 mini = Math.min(mini,dp[i][j+1][k-1]+(map[i][j+1]==‘L‘?0:1));72 }73 return mini;74 }75 }