3 条题解
-
0
java题解:
import java.util.*; import java.io.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; public static void main(String[] args) throws IOException { solve(); } static void solve() throws IOException { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); char[][] grid = new char[n][m]; int sx = -1, sy = -1; // 读取网格 for (int i = 0; i < n; i++) { String line = br.readLine(); grid[i] = line.toCharArray(); if (sx == -1) { for (int j = 0; j < m; j++) { if (grid[i][j] == 'S') { sx = i; sy = j; break; } } } } if (sx == -1) { System.out.println(-1); return; } int[] dx = {-1, 0, 1, 0}; int[] dy = {0, 1, 0, -1}; // 使用Set记录访问状态,避免三维数组 Set<Integer> visited = new HashSet<>(); Queue<int[]> q = new LinkedList<>(); // 状态编码: (x << 20) | (y << 10) | can int startState = encode(sx, sy, 1); visited.add(startState); q.offer(new int[]{sx, sy, 1, 0}); // {x, y, can, dist} while (!q.isEmpty()) { int[] cur = q.poll(); int x = cur[0]; int y = cur[1]; int can = cur[2]; int dist = cur[3]; if (grid[x][y] == 'E') { System.out.println(dist); return; } // 正常移动 for (int d = 0; d < 4; d++) { int nx = x + dx[d]; int ny = y + dy[d]; int ncan = 1; if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] != '#') { int state = encode(nx, ny, ncan); if (!visited.contains(state)) { visited.add(state); q.offer(new int[]{nx, ny, ncan, dist + 1}); } } } // 跳跃移动 if (can == 1) { for (int d = 0; d < 4; d++) { int nx = x + dx[d] * 2; int ny = y + dy[d] * 2; int ncan = 0; if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] != '#') { int state = encode(nx, ny, ncan); if (!visited.contains(state)) { visited.add(state); q.offer(new int[]{nx, ny, ncan, dist + 1}); } } } } } System.out.println(-1); } static int encode(int x, int y, int can) { // 假设n,m≤1000,x和y各用10位足够 return (x << 20) | (y << 10) | can; } } -
0
py题解:
import sys from collections import deque def solve(): data = sys.stdin.read().split() if not data: return n, m = map(int, data[:2]) idx = 2 # 只存储必要的字符网格 grid = [] sx = sy = 0 for i in range(n): row = data[idx] idx += 1 if 'S' in row: sx, sy = i, row.index('S') grid.append(row) if sx == -1: # 没有找到起点 print(-1) return # 使用位运算压缩状态 # state = (x << 20) | (y << 10) | can def encode(x, y, can): return (x << 20) | (y << 10) | can dx = [-1, 0, 1, 0] dy = [0, 1, 0, -1] visited = set() q = deque() start_state = encode(sx, sy, 1) visited.add(start_state) q.append((sx, sy, 1, 0)) # (x, y, can, dist) while q: x, y, can, dist = q.popleft() if grid[x][y] == 'E': print(dist) return # 正常移动 for d in range(4): nx, ny = x + dx[d], y + dy[d] ncan = 1 if 0 <= nx < n and 0 <= ny < m and grid[nx][ny] != '#': state = encode(nx, ny, ncan) if state not in visited: visited.add(state) q.append((nx, ny, ncan, dist + 1)) # 跳跃移动 if can == 1: for d in range(4): nx, ny = x + dx[d] * 2, y + dy[d] * 2 ncan = 0 if 0 <= nx < n and 0 <= ny < m and grid[nx][ny] != '#': state = encode(nx, ny, ncan) if state not in visited: visited.add(state) q.append((nx, ny, ncan, dist + 1)) print(-1) if __name__ == "__main__": solve() -
0
#include <bits/stdc++.h> #define int long long // 仅在需要大整数时使用,memset 数组为 0x3f 时去掉 #define INF 0x3f3f3f3f #define PII pair<int, int> #define ULL unsigned long long #define PIII tuple<int, int, int> #define all(v) v.begin(), v.end() #define debug(a) cout << #a << " = " << a << endl; using namespace std; constexpr int M = 1 * 1e3 + 10; char mp[M][M]; bool st[M][M][2]; int n, m; int sx, sy, ex, ey; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; struct node { int x, y; int step; bool flag; }; bool checkin(int x, int y) { return x > 0 && y > 0 && x <= n && y <= m; } int bfs() { queue<node> q; q.push({sx, sy, 0, 0}); st[sx][sy][0] = 1; while (q.size()) { auto [x, y, step, flag] = q.front(); q.pop(); if (x == ex && y == ey) return step; //Walk for (int i = 0; i < 4; i ++) { node ne; ne.x = x + dir[i][0]; ne.y = y + dir[i][1]; if (!checkin(ne.x, ne.y)) continue; if (mp[ne.x][ne.y] == '#') continue; if (st[ne.x][ne.y][0]) continue; st[ne.x][ne.y][0] = 1; ne.step = step + 1; ne.flag = 0; q.push(ne); } //Jump if (!flag) { for (int i = 0; i < 4; i ++) { node ne; ne.x = x + dir[i][0] * 2; ne.y = y + dir[i][1] * 2; if (!checkin(ne.x, ne.y)) continue; if (mp[ne.x][ne.y] == '#') continue; if (st[ne.x][ne.y][1]) continue; st[ne.x][ne.y][1] = 1; ne.step = step + 1; ne.flag = 1; q.push(ne); } } } return -1; } void solve() { cin >> n >> m; for (int i = 1; i <= n; i ++) { for (int j = 1; j <= m; j ++) { cin >> mp[i][j]; if (mp[i][j] == 'S') sx = i, sy = j; if (mp[i][j] == 'E') ex = i, ey = j; } } cout << bfs() << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(nullptr), cout.tie(nullptr); int _ = 1; // cin >> _; while (_--) { solve(); } return 0; } /** * author: Nijika_jia * description: C++17 Algorithm Template for Competitive Programming */
- 1
信息
- ID
- 5629
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 27
- 已通过
- 4
- 上传者