3 条题解

  • 0
    @ 2025-12-6 1:02:57

    Java题解:

    
    import java.util.*;
    import java.io.*;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String[] first = br.readLine().split(" ");
            int r = Integer.parseInt(first[0]);
            int c = Integer.parseInt(first[1]);
            int n = Integer.parseInt(first[2]);
            
            char[][] g = new char[r + 2][c + 2];
            char[][] robotPos = new char[r + 2][c + 2];
            
            // 读取网格
            for (int i = 1; i <= r; i++) {
                String line = br.readLine();
                for (int j = 1; j <= c; j++) {
                    g[i][j] = line.charAt(j - 1);
                    robotPos[i][j] = '.';
                }
            }
            
            // 机器人数组
            int[] robotR = new int[n + 1];
            int[] robotC = new int[n + 1];
            int[] robotDir = new int[n + 1];
            boolean[] hasItem = new boolean[n + 1];
            char[] holding = new char[n + 1];
            
            for (int i = 1; i <= n; i++) {
                String[] parts = br.readLine().split(" ");
                robotR[i] = Integer.parseInt(parts[0]);
                robotC[i] = Integer.parseInt(parts[1]);
                robotDir[i] = Integer.parseInt(parts[2]);
                robotPos[robotR[i]][robotC[i]] = '@';
            }
            
            int k = Integer.parseInt(br.readLine());
            int ans = 0;
            int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
            
            for (int t = 0; t < k; t++) {
                String[] parts = br.readLine().split(" ");
                int id = Integer.parseInt(parts[0]);
                char op = parts[1].charAt(0);
                
                int cr = robotR[id];
                int cc = robotC[id];
                int cd = robotDir[id];
                
                if (op == 'F') {
                    int nr = cr + dirs[cd][0];
                    int nc = cc + dirs[cd][1];
                    if (nr >= 1 && nr <= r && nc >= 1 && nc <= c && 
                        g[nr][nc] != '#' && robotPos[nr][nc] != '@') {
                        robotPos[cr][cc] = '.';
                        robotPos[nr][nc] = '@';
                        robotR[id] = nr;
                        robotC[id] = nc;
                        ans++;
                    }
                } else if (op == 'L') {
                    robotDir[id] = (cd + 3) % 4;
                    ans++;
                } else if (op == 'R') {
                    robotDir[id] = (cd + 1) % 4;
                    ans++;
                } else if (op == 'P') {
                    if (!hasItem[id] && g[cr][cc] >= 'A' && g[cr][cc] <= 'Z') {
                        hasItem[id] = true;
                        holding[id] = g[cr][cc];
                        g[cr][cc] = '.';
                        ans++;
                    }
                } else if (op == 'D') {
                    if (hasItem[id] && g[cr][cc] == '.') {
                        hasItem[id] = false;
                        g[cr][cc] = holding[id];
                        holding[id] = ' ';
                        ans++;
                    }
                }
            }
            
            System.out.println(ans);
            for (int i = 1; i <= r; i++) {
                for (int j = 1; j <= c; j++) {
                    System.out.print(g[i][j]);
                }
                System.out.println();
            }
        }
    }
    
    

    信息

    ID
    5628
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    14
    已通过
    2
    上传者