RE求助
查看原帖
RE求助
787888
Channel_Choo楼主2023/9/9 10:56
#include <iostream>
#include <stdio.h>
#include <queue>
#include <cctype>
#include <string.h>
using namespace std;
char c[1001][1001] = {'1'};
int map[1001][1001];
int x1, y1, x2, y2;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int n;
int qr()
{
    int k = 0, f = 1 ;
    char c = getchar() ;
    while(!isdigit(c)){if(c == '-') f = -1 ;c = getchar() ;}
    while(isdigit(c)) k = (k << 1) + (k << 3) + c - 48 ,c = getchar() ;
    return k * f ;
}
void write(int x)
{
    if(x<0)
    {
        x=-x;
        putchar('-');
    }
    if(x>9)
    {
        write(x / 10);
    }
    putchar(x % 10+'0');
}
struct Point
{
	int x, y;
};
queue <Point> q;
void bfs(int startx, int starty)
{
	q.push((Point){startx, starty});
	map[startx][starty] = 0;
	c[startx][starty] = '1';
	while (q.size() > 0)
	{
		Point now = q.front();
		q.pop();
		if (now.x == x2 && now.y == y2)
		{
		    write(map[now.x][now.y]);
		    return ;
		}
		for (int i = 0;i < 4;i++)
		{
			int nx = now.x + dx[i];
			int ny = now.y + dy[i];
			if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && c[nx][ny] == '0' && map[nx][ny] == -1)
			{
				map[nx][ny] = map[now.x][now.y] + 1;
				c[nx][ny] = '1';
				q.push((Point){nx, ny});
			}
		}
	}
}
int main()
{
	n = qr();
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			c[i][j] = getchar();
		}
		getchar();
	}
	memset(map, -1, sizeof(map));
	x1 = qr();
	y1 = qr();
	x2 = qr();
	y2 = qr();
	bfs(x1, y1);
	return 0;
}

没加快读快写是可以的,加了快读快写RE,求助大佬qwq

2023/9/9 10:56
加载中...