在一行上输入两个整数
,代表网格的行数与列数。
在一行上输入四个整数
,代表起点与终点的坐标。
此后
行,第
行输入一个长度为
的字符串
,其中
若
,表示第
行第
列为障碍物;
若
,表示该格子可通行。
保证起点所在格子可通行。
输出一个整数,表示最少移动次数;若无法到达,输出
。
5 5 1 1 5 5 ..... ****. ..... **.** .....
12
5 5 1 1 4 5 ..... ****. ..... **.** .....
-1
5 5 1 1 5 5 ..... ****. ..... ***** .....
-1
auto ifok = [&que, &mp](int x, int y)->void{que.push(pos(x, y)); mp[x][y] = true;}; import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static int[][] direction = new int[][] {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}
};
private static boolean[][] visited;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] line = in.nextLine().split(" ");
int[] position = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int n = Integer.parseInt(line[0]);
int m = Integer.parseInt(line[1]);
char[][] matrix = new char[n][m];
visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
matrix[i] = in.nextLine().toCharArray();
}
int startX = position[0] - 1, startY = position[1] - 1;
int endX = position[2] - 1, endY = position[3] - 1;
Queue<Point> queue = new LinkedList<>();
queue.add(new Point(startX, startY, 0));
visited[startX][startY] = true;
Point cur = null;
boolean isArrive = false;
while (!queue.isEmpty()) {
cur = queue.poll();
int curX = cur.x;
int curY = cur.y;
int curStep = cur.step;
if (curX == endX && curY == endY) {
System.out.println(curStep);
isArrive = true;
break;
}
for (int i = 0; i < direction.length; i++) {
int dx = cur.x + direction[i][0];
int dy = cur.y + direction[i][1];
if (dx >= 0 && dx < n && dy >= 0 && dy < m && !visited[dx][dy] && matrix[dx][dy] == '.') {
visited[dx][dy] = true;
queue.add(new Point(dx, dy, curStep + 1));
}
}
}
if (!isArrive) {
System.out.println(-1);
}
}
}
}
class Point {
public int x;
public int y;
public int step;
public Point(int x, int y, int step) {
this.x = x;
this.y = y;
this.step = step;
}
} #include <iostream>
#include <queue>
#include <vector>
using namespace std;
//bfs使用队列
int main() {
int n, m;
cin >> n >> m;
int start_X, start_Y, end_X, end_Y;
cin >> start_X >> start_Y >> end_X >> end_Y;
vector<string> gi(n);
for (int i = 0; i < n; i ++) {
cin >> gi[i];
}
//如果开始或者结束存在障碍,直接返回
if (gi[start_X - 1][start_Y - 1] == '*' || gi[end_X - 1][end_Y - 1] == '*') {
cout << "-1" << endl;
return 0;
}
//距离数组标记,同时记录距离起点的距离
vector<vector<int>> visited(n, vector<int>(m, -1));
//初始化方向数组
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
//初始化队列
queue<pair<int, int>> q;
q.push({start_X - 1, start_Y - 1});
visited[start_X - 1][start_Y - 1] = 0;
int count_num = 0;
while (!q.empty()) {
auto[x, y] = q.front();
q.pop();
if(x == end_X - 1 && y == end_Y - 1) {
cout << visited[x][y] << endl;
return 0;
}
//遍历方向数组
for (int i = 0; i < 4; i ++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m &&
visited[nx][ny] == -1 && gi[nx][ny] == '.') {
q.push({nx, ny});
//新的位置的距离等于旧的位置距离加1
visited[nx][ny] = visited[x][y] + 1;
}
}
}
cout << "-1" << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
from collections import deque
class Maze:
def __init__(self):
self.n, self.m = list(map(int, input().split(" ")))
self.start_y, self.start_x, self.end_y, self.end_x = list(
map(lambda x: int(x)-1, input().split(" ")))
self.grid = [['\0' for _ in range(self.m)]
for _ in range(self.n)]
for i in range(self.n):
self.grid[i] = list(str(input()))
self.visited = [[False for _ in range(self.m)]
for _ in range(self.n)]
self.direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def bfs(self):
queue = deque([(self.start_x, self.start_y, 0)])
while queue:
curr_x, curr_y, curr_len = queue.popleft()
if self.visited[curr_y][curr_x]:
continue
self.visited[curr_y][curr_x] = True
if (curr_y, curr_x) == (self.end_y, self.end_x):
return curr_len
for dx, dy in self.direction:
nx = curr_x + dx
ny = curr_y + dy
if ((0 <= nx < self.m) and
(0 <= ny < self.n) and
self.grid[ny][nx] == "." and
not self.visited[ny][nx]):
nl = curr_len+1
queue.append((nx, ny, nl))
return -1
if __name__ == "__main__":
m = Maze()
print(m.bfs())
//AI是真牛逼啊,直接帮你把bug改好了,然后回顾你一下你的问题点在哪把
//错就错在你没把struct position用指针的别名来用,导致没有用指针的指针
typedef struct position { //要有名字才能识别next
int x;
int y;
int bushu;
struct position *next;
} position,*position_link; //为什么上面的next没有识别到这个position是因为这个position是在后面的
// 入队列函数 - 修正版
void queue_push(position** queue, position temp) {
position* new_node = (position*)malloc(sizeof(position)); //不可以直接赋值局部变量会消失
new_node->x = temp.x;
new_node->y = temp.y;
new_node->bushu = temp.bushu;
new_node->next = NULL;
if (*queue == NULL) {
*queue = new_node;
} else {
position* p = *queue;
while (p->next != NULL) { // 找到最后一个节点
p = p->next;
}
p->next = new_node; // 在末尾添加新节点
}
}
// 出队列函数 - 修正版
position queue_pop(position** queue) { //指针的指针才能修改传递的值
if (*queue == NULL) {
position empty = {-1, -1, -1, NULL};
return empty;
}
position* temp = *queue;
position result = *temp;
*queue = (*queue)->next;
free(temp);
return result;
}
// 检查队列是否为空
int is_queue_empty(position* queue) {
return queue == NULL;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
int start_x, start_y;
int end_x, end_y;
scanf("%d %d %d %d", &start_x, &start_y, &end_x, &end_y);
// 初始化迷宫地图
char **migong_map = malloc(sizeof(char*) * (n + 1));
for(int i = 0; i <= n; i++) {
migong_map[i] = malloc(sizeof(char) * (m + 1));
}
// 读取迷宫
for(int i = 1; i <= n; i++) {
scanf("%s", migong_map[i]);
// 边界处理
for(int j = m; j > 0; j--) {
migong_map[i][j] = migong_map[i][j - 1];
}
migong_map[i][0] = '*';
}
// 第0行设置为墙
for(int i = 0; i <= m; i++) {
migong_map[0][i] = '*';
}
// BFS开始
position* queue = NULL; // 初始化队列
position d1 = {
.x = start_x,
.y = start_y,
.bushu = 0,
.next = NULL
};
queue_push(&queue, d1);
int min_bushu = n * m;
while (!is_queue_empty(queue)) { // 使用队列空判断
position front = queue_pop(&queue);
// 检查是否到达终点
if (front.x == end_x && front.y == end_y) { // 修正:使用 &&
if (front.bushu < min_bushu) {
min_bushu = front.bushu;
}
// 找到终点后可以继续搜索更短路径,或者直接break
}
// 检查是否为空标记(如果队列真的空了,循环条件会处理)
// 上下左右移动
// 注意:边界检查修正
if (front.x - 1 >= 1 && migong_map[front.x - 1][front.y] == '.') { // 往上
position temp = {
.x = front.x - 1,
.y = front.y,
.bushu = front.bushu + 1,
.next = NULL
};
queue_push(&queue, temp);
migong_map[temp.x][temp.y] = '*'; // 立即标记为已访问
}
if (front.y - 1 >= 1 && migong_map[front.x][front.y - 1] == '.') { // 往左
position temp = {
.x = front.x,
.y = front.y - 1,
.bushu = front.bushu + 1,
.next = NULL
};
queue_push(&queue, temp);
migong_map[temp.x][temp.y] = '*';
}
if (front.x + 1 <= n && migong_map[front.x + 1][front.y] == '.') { // 往下
position temp = {
.x = front.x + 1,
.y = front.y,
.bushu = front.bushu + 1,
.next = NULL
};
queue_push(&queue, temp);
migong_map[temp.x][temp.y] = '*';
}
if (front.y + 1 <= m && migong_map[front.x][front.y + 1] == '.') { // 往右
position temp = {
.x = front.x,
.y = front.y + 1,
.bushu = front.bushu + 1,
.next = NULL
};
queue_push(&queue, temp);
migong_map[temp.x][temp.y] = '*';
}
}
// 输出结果
if (min_bushu == n * m) {
printf("%d", -1);
} else {
printf("%d", min_bushu);
}
// 释放内存
for(int i = 0; i <= n; i++) {
free(migong_map[i]);
}
free(migong_map);
return 0;
} const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
// Write your code here
while ((line = await readline())) {
const [n, m] = line.split(" ").map(Number);
const [xs, ys, xt, yt] = (await readline()).split(" ").map(Number);
const mtx = Array(n);
if (xs === xt && ys === yt) {
console.log(1);
continue;
}
for (let i = 0; i < n; i++) {
mtx[i] = (await readline()).split("");
}
const visited = Array.from({ length: n }, () => Array(m).fill(false));
visited[xs-1][ys-1] = true;
const queue = [[xs - 1, ys - 1]];
let steps = 0;
let reachable = false;
let front = 0
while (front < queue.length) {
const levelSize = queue.length - front;
for (let i = 0; i < levelSize; i++) {
let [x, y] = queue[front++];
if (x - 1 >= 0 && mtx[x - 1][y] === "." && !visited[x - 1][y]) {
if (x - 1 === xt - 1 && y === yt - 1) {
reachable = true;
steps++;
break;
}
queue.push([x - 1, y]);
visited[x-1][y] = true;
}
if (x + 1 < n && mtx[x + 1][y] === "." && !visited[x + 1][y]) {
if (x + 1 === xt - 1 && y === yt - 1) {
reachable = true;
steps++;
break;
}
queue.push([x + 1, y]);
visited[x+1][y] = true;
}
if (y - 1 >= 0 && mtx[x][y - 1] === "." && !visited[x][y - 1]) {
if (x === xt - 1 && y - 1 === yt - 1) {
reachable = true;
steps++;
break;
}
queue.push([x, y - 1]);
visited[x][y-1] = true;
}
if (y + 1 < m && mtx[x][y + 1] === "." && !visited[x][y + 1]) {
if (x === xt - 1 && y + 1 === yt - 1) {
reachable = true;
steps++;
break;
}
queue.push([x, y + 1]);
visited[x][y+1] = true;
}
}
if (reachable) {
break;
}
steps++;
}
console.log(reachable ? steps : -1);
}
})();
边界条件放前面
import java.util.*;
public class Main {
public static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sizeX = in.nextInt();
int sizeY = in.nextInt();
int startX = in.nextInt() - 1;
int startY = in.nextInt() - 1;
int endX = in.nextInt() - 1;
int endY = in.nextInt() - 1;
in.nextLine();
char[][] graph = new char[sizeX][sizeY];
for(int i = 0; i < sizeX; i++) {
String tmp = in.nextLine();
for(int j = 0; j < sizeY; j++) {
graph[i][j] = tmp.charAt(j);
}
}
int result = bfs(startX, startY, endX, endY, graph, sizeX, sizeY);
System.out.println(result);
}
private static int bfs(int startX, int startY, int endX, int endY, char[][] graph, int sizeX, int sizeY) {
if(graph[startX][startY] == '*' || graph[endX][endY] == '*') {
return -1;
}
boolean[][] visited = new boolean[sizeX][sizeY];
Queue<int[]> queue = new LinkedList<>();
int step = 0;
queue.offer(new int[]{startX, startY, step});
visited[startX][startY] = true;
while(!queue.isEmpty()) {
int[] cur = queue.poll();
int curX = cur[0];
int curY = cur[1];
int curStep = cur[2];
if(curX == endX && curY == endY) {
return curStep;
}
for(int[] temp : DIRECTIONS) {
int newX = curX + temp[0];
int newY = curY + temp[1];
if(newX < sizeX && newX >= 0 && newY < sizeY && newY >=0 && visited[newX][newY] == false && graph[newX][newY] != '*') {
queue.offer(new int[]{newX, newY, curStep + 1});
visited[newX][newY] = true;
step++;
}
}
}
return -1;
}
}
from collections import deque n,m = map(int , input().split()) xs,ys,xt,yt = map(int, input().split()) xs,ys,xt,yt = xs-1,ys-1,xt-1,yt-1 maze = [] for _ in range(n): row = list(input()) maze.append(row) def play_maze(n,m,xs,ys,xt,yt,maze): visited= [[False]*m for _ in range(n)] visited[xs][ys]=True q = deque() q.append((xs,ys,0)) # x,y,steps directions = [(1,0),(-1,0),(0,1),(0,-1)] while q: x,y,steps = q.popleft() if (x,y)==(xt,yt): return steps for dx,dy in directions: nx ,ny = x+dx, y+dy if 0<=nx<n and 0<=ny<m and maze[nx][ny]=='.' and not visited[nx][ny]: visited[nx][ny]=True # 注意这里不能写steps +=1,再append(steps),会导致不同方向之间的初始steps会不同 q.append((nx,ny,steps+1)) return -1 print(play_maze(n,m,xs,ys,xt,yt,maze))
import sys from collections import deque def main(): n, m = map(int, sys.stdin.readline().split()) xs, ys, xt, yt = map(int, sys.stdin.readline().split()) grid = [sys.stdin.readline().strip() for _ in range(n)] # 转换为 0-based 或保持 1-based,根据题目要求调整 xs -= 1 ys -= 1 xt -= 1 yt -= 1 if grid[xs][ys] == "*"&nbs***bsp;grid[xt][yt] == "*": print(-1) return directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] visited = [[-1 for _ in range(m)] for __ in range(n)] q = deque() q.append((xs, ys)) visited[xs][ys] = 0 while q: x, y = q.popleft() if x == xt and y == yt: print(visited[x][y]) return for dx, dy in directions: nx, ny = x + dx, y + dy if ( 0 <= nx < n and 0 <= ny < m and grid[nx][ny] == "." and visited[nx][ny] == -1 ): visited[nx][ny] = visited[x][y] + 1 q.append((nx, ny)) print(-1) if __name__ == "__main__": main()
import sys from collections import deque try: while True: n, m = map(int, sys.stdin.readline().split()) start_x, start_y, end_x, end_y = map(int, sys.stdin.readline().split()) grid = [] for _ in range(n): line = sys.stdin.readline().strip() grid.append(line) direction = [(1,0), (0, 1), (-1, 0), (0 , -1)] visited = [[False] * m for _ in range(n)] queue = deque() queue.append((start_x - 1, start_y - 1, 0)) visited[start_x - 1][start_y - 1] = True find_result = False while queue: x, y, step = queue.popleft() if x == end_x - 1 and y == end_y - 1: print(step) find_result = True else: for dx, dy in direction: next_x, next_y = dx + x, dy + y if 0 <= next_x < n and 0 <= next_y < m and grid[next_x][next_y] != "*" and not visited[next_x][next_y]: visited[next_x][next_y] = True queue.append((next_x, next_y, step + 1)) if not find_result: print(-1) except: pass
from copy import copy import copy def bfs(a1,b1,a2,b2,pathmap): queue=[[a1,b1]] step=0 while queue: queue1=[] for item in queue: for i, j in [[0, 1], [0, -1], [1, 0], [-1, 0]]: if (0<=item[0]+i<=len(pathmap)-1)&(0<=item[1]+j<=len(pathmap[0])-1): a=item[0]+i;b=item[1]+j if (pathmap[a][b]!=1)&(pathmap[a][b]!=-1): queue1.append([a,b]) pathmap[a][b]=1 if (a==a2)&(b==b2): return step+1 step+=1 queue=copy.deepcopy(queue1) return -1 n,m=map(int,input().split()) a1,b1,a2,b2=map(int,input().split()) map=[] for _ in range(n): map.append(list(input())) pathmap=[] for i in range(n): temp=[] for j in range(m): if map[i][j]=='.': temp.append(0) else: temp.append(-1) pathmap.append(temp) if (pathmap[a2-1][b2-1]==-1)|(pathmap[a1-1][b1-1]==-1): short=-1 else: pathmap[a1 - 1][b1 - 1] = 1 short=bfs(a1-1,b1-1,a2-1,b2-1,pathmap) print(short)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
}node; //坐标系
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int bfs(char** map,int xs,int ys,int xt, int yt,int n,int m){
int f=0,r=0;
int dist[n+1][m+1];
node q[(n+1)*(m+1)];
for (int i=0;i<n+1;i++) {
for(int j=0;j<m+1;j++) {
dist[i][j]=-1;
}
}
q[r].x=xs,q[r].y=ys;
dist[q[r].x][q[r].y]=0,r++;
while (f<r) {
int x=q[f].x,y=q[f].y;
if (x==xt&&y==yt) {
return dist[q[f].x][q[f].y];
}
else{
for (int i=0; i<4; i++) {
int nx=x+dx[i],ny=y+dy[i];
if (nx<=n&&ny<=m&&nx>=1&&ny>=1&&map[nx][ny]=='.'&&dist[nx][ny]==-1) {
dist[nx][ny]=dist[q[f].x][q[f].y]+1;
q[r].x=nx,q[r].y=ny,r++;
}
}
}
f++;
}
return -1;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
int xs, ys, xt, yt;
scanf("%d %d %d %d", &xs, &ys, &xt, &yt);
getchar();
char** map=(char**)malloc(sizeof(char*)*(n+1));
for (int i=0; i<n+1; i++) {
map[i]=(char*)malloc(sizeof(char)*(m+1));
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
scanf("%c",&map[i][j]);
}
getchar();
}
if (map[xt][yt]=='*') {
printf("-1");
}
else {
printf("%d",bfs(map,xs,ys,xt,yt,n,m));
}
return 0;
} #include <iostream>
using namespace std;
int p,q,m,n,mmin=9999999;
string a[1000][1000];//.表示空地,*表示障碍物
int v[1000][1000];//0表示未访问,1表示访问
void dfs(int x, int y, int step) {
if (x==p && y==q){
if(step < mmin){
mmin = step;
}
return;
}
//顺时针试探
//右
if(a[x][y+1] == "." && v[x][y+1]==0){
v[x][y+1]=1;
dfs(x, y+1, step+1);
v[x][y+1]=0;
}
//下
if(a[x+1][y]=="." && v[x+1][y]==0){
v[x+1][y]=1;
dfs(x+1, y, step+1);
v[x+1][y]=0;
}
//左
if(a[x][y-1]=="." && v[x][y-1]==0){
v[x][y-1]=1;
dfs(x, y-1, step+1);
v[x][y-1]=0;
}
//上
if(a[x-1][y]=="." && v[x-1][y]==0){
v[x-1][y]=1;
dfs(x-1, y, step+1);
v[x-1][y]=0;
}
return;
}
int main(){
int startx, starty;
cin >> n >> m;
cin >> startx >> starty >> p >> q;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> a[i][j];
}
}
v[startx][starty] = 1;
dfs(startx, starty, 0);
if (mmin == 9999999) {
cout << -1;
} else {
cout << mmin;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
//邻接矩阵 + bfs
typedef struct graph{
int n;
int m;
int** datas;
}graph;
void init_graph(graph* g,int n,int m){
g->n = n;
g->m = m;
g->datas = malloc(sizeof(int *)*n);
for(int i = 0 ; i < n;i++){
g->datas[i] = malloc(sizeof(int)*m);
}
}
int add_edge(graph* g,int start,int end,char c){
if('.' == c){
g->datas[start][end] = 0;
} else if('*' == c) {
g->datas[start][end] = 1;
}
return 1;
}
typedef struct node{
int x;
int y;
int layer;
struct node* next;
}node;
typedef struct queue{ //head头节点,tail指向最后一个节点,如果超时,可以尝试使用顺序表来定义队列
node* head;
node* tail;
}queue;
void init_queue(queue* q){
q->head = malloc(sizeof(node));
q->head->next = NULL;
q->tail = q->head;
}
int empty_queue(queue* q){
if(q->head == q->tail){
return 1;
} else {
return 0;
}
}
void in_queue(queue* q,int x,int y,int layer){
node* no = malloc(sizeof(node));
no->x = x;
no->y = y;
no->layer = layer;
no->next = NULL;
q->tail->next = no;
q->tail = no;
}
int de_queue(queue* q,node** ret){
if(q->head->next != NULL){
*ret = q->head->next;
q->head->next = (*ret)->next;
if(*ret == q->tail){
q->tail = q->head;
}
return 1;
} else {
return 0;
}
}
int bfs(graph* g,int startx,int starty,int endx,int endy){
//将当前位置入栈
queue q;
node* cur;
init_queue(&q);
in_queue(&q,startx,starty, 0);
//标记cur节点为已经访问,-1表示该位置已经访问
g->datas[startx][starty] = -1;
while(!empty_queue(&q)){
de_queue(&q, &cur);
//判断是否是目标节点
if(cur->x == endx && cur->y == endy){
return cur->layer;
}
//将该位置上下左右没有障碍物的路径加入栈
if((cur->x - 1) >= 0 && g->datas[cur->x-1][cur->y] == 0){
g->datas[cur->x - 1][cur->y] = -1;
in_queue(&q, cur->x - 1, cur->y, cur->layer + 1);
}
if((cur->x + 1) < g->n && g->datas[cur->x + 1][cur->y] == 0){
g->datas[cur->x + 1][cur->y] = -1;
in_queue(&q, cur->x + 1, cur->y, cur->layer + 1);
}
if((cur->y - 1) >= 0 && g->datas[cur->x][cur->y - 1] == 0){
g->datas[cur->x][cur->y-1] = -1;
in_queue(&q, cur->x, cur->y-1, cur->layer + 1);
}
if((cur->y + 1) < g->m && g->datas[cur->x][cur->y + 1] == 0){
g->datas[cur->x][cur->y+1] = -1;
in_queue(&q, cur->x, cur->y+1, cur->layer + 1);
}
free(cur);
}
return -1;
}
int main() {
int n,m;
scanf("%d%d",&n,&m);
graph g;
init_graph(&g, n,m);
char c = 0;
int startx,starty,endx,endy;
scanf("%d%d%d%d",&startx,&starty,&endx,&endy);
scanf("%c",&c); //取换行符
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < m;j++){
scanf("%c",&c);
add_edge(&g, i, j, c);
}
scanf("%c",&c);//取换行符
}
int result = bfs(&g, startx - 1, starty-1, endx-1, endy -1);
printf("%d",result);
return 0;
}