样例不过求调
查看原帖
样例不过求调
824800
donnieguo楼主2024/7/4 23:22

rt,用的bfs,样例不过 qwq

#include <iostream>
#include <queue>
#define N 150
using namespace std;

int r, c;
bool vis[N][N];
//bool flag;

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int pass[N][N][3];
struct node {
	int x, y;
};
queue <node> q;

void bfs() {
	q.push({1, 1});
	while (!q.empty()) {
		node now = q.front();
		q.pop();
		int x = now.x, y = now.y;
		if (x == r && y == c) {
//			flag = 1;
			break;
		}
		if (x < 1 || x > r || y < 1 || y > c || vis[x][y])
			continue;
		vis[x][y] = 1;
		for (int i = 0; i < 4; i++) {
			pass[x + dx[i]][y + dy[i]][1] = x;
			pass[x + dx[i]][y + dy[i]][2] = y;
			q.push({x + dx[i], y + dy[i]});
		}

	}
}

void print(int x, int y) {
	if (!(pass[x][y][1] + pass[x][y][2]))
		return;
	print(pass[x][y][1], pass[x][y][2]);
	printf("%d %d\n", x, y);
}

int main() {
	cin >> r >> c;
	for (int i = 1; i <= r; i++)
		for (int j = 1; j <= c; j++) {
			char cc;
			cin >> cc;
			if (cc == '*')
				vis[i][j] = 1;
		}
	bfs();
//	cout << (flag ? "Yes" : "No") << endl;;
	printf("1 1\n");
	print(r, c);
//	cout << pass[r][c][1] << ' ' << pass[r][c][2] << '\n';
	return 0;
}

2024/7/4 23:22
加载中...