马达的妈妈16

RRRRRRrrrrrrr 2023-11-25 10:22:20

#include <bits/stdc++.h>
using namespace std;
#define int long long

int n,m;
char a[1005][1005];
int dp[1005][1005];

struct faq {
	int x,y;
};

void ddd() {
	queue <faq> q;
	q.push({1,1});
	dp[1][1] = 0;
	while(!q.empty()) {
		faq f = q.front();
		q.pop();
		int xx[] = {0,1,0,-1};
		int yy[] = {1,0,-1,0};
		for (int i = 0; i < 4; i++) {
			int nx = f.x,ny = f.y;
			while(nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
				nx += xx[i];
				ny += yy[i];
				if (dp[nx][ny] == -1) {
					if (a[nx][ny] != '.') break;
					q.push({nx,ny});
					dp[nx][ny] = dp[f.x][f.y]+1;
				}
			}
		}
	}
}

signed main() {
	std::ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			dp[i][j] = -1;
		}
	}
	ddd();
	cout << dp[n][m];
	return 0;
}

共 23 条回复

RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr
RRRRRRrrrrrrr