[ACWing] 844. 미로 로
11268 단어 AC수색 하 다.도 론 과 네트워크알고리즘
https://www.acwing.com/problem/content/846/
주어진 m× n m\times n m×n 2 차원 0 - 1 0 - 1 - 1 매트릭스, 000 은 공 터 를 대표 하고 11 은 장애물 을 대표 한다.(1, 1) (1, 1) (1, 1) 에서 출발 하여 매번 상하 좌우 로 한 걸음 씩 걸 을 수 있 고 장애물 에 올 라 갈 수 없다.(m, n) (m, n) (m, n) 까지 가 려 면 적어도 몇 걸음 은 가 야 하 는 지 물 어 봐 야 한다.
데이터 범위: 1 ≤ n, m ≤ 1000 1 \ \ le n, m \ le 1000 1 ≤ n, m ≤ 1000
BFS 로 하면 됩 니 다.코드 는 다음 과 같 습 니 다:
#include
#include
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int a[N][N];
bool visited[N][N];
int main() {
int m, n;
cin >> m >> n;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
if (m == 1 && n == 1) {
cout << 0 << endl;
return 0;
}
int res = 0;
queue<PII> q;
q.push(make_pair(1, 1));
int d[] = {
1, 0, -1, 0, 1};
while (!q.empty()) {
res++;
int s = q.size();
for (int i = 0; i < s; i++) {
PII pair = q.front();
q.pop();
for (int j = 0; j < 4; j++) {
int nextX = pair.first + d[j], nextY = pair.second + d[j + 1];
if (nextX == m && nextY == n) {
cout << res << endl;
return 0;
}
if (1 <= nextX && nextX <= m && 1 <= nextY && nextY <= n && a[nextX][nextY] == 0 && !visited[nextX][nextY]) {
visited[nextX][nextY] = true;
q.push(make_pair(nextX, nextY));
}
}
}
}
return 0;
}
시공 복잡 도 O (m n) O (mn) O (mn).
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ACCESS 2000 데이터베이스 에 있 는 모든 표 의 이름 가 져 오기void OpenSchemaX(TCHAR *TableName){HRESULT hr = S_OK;::CoInitialize(NULL); //ComIADORecordBinding*picRs=NULL; 초기 화Record...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.