25 ❤︎ Bài viết: 28 Tìm chủ đề
3600 15
Link bài:

Thuật toán:


  • Ta dùng thuật toán quay lui để lan đường đi từ viên bi cần di chuyển ra khắp bản đồ. Nếu thuật toán lan được đến viên bi cần tìm, ta sẽ in "YES", không thì in "NO".

Code:

Mã:
#include <bits/stdc++.h>
using namespace std;

int a[11][11], n, sy, dy, sx, dx;

void doit(int i, int j){
    if (a[i][j] == 0){
        a[i][j] = 2;
        doit(i-1, j);
        doit(i+1, j);
        doit(i, j-1);
        doit(i, j+1);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("line.inp", "r", stdin);
    //freopen("line.out", "w", stdout);
    cin >> n >> sy >> sx >> dy >> dx;
    for (int i = 0; i <= n+1; i++) a[0][i] = a[i][0] = a[n+1][i] = a[i][n+1] = -1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> a[i][j];
    a[sy][sx] = 0;
    doit(sy, sx);
    if (a[dy][dx] == 2) cout << "YES"; else cout << "NO";
    
    /*for (int i = 0; i <= n+1; i++){
        cout << endl;
        for (int j = 0; j <= n+1; j++)
            cout << a[i][j] << " ";
    }*/
    
    return 0;
}
 
Từ khóa: Sửa

Những người đang xem chủ đề này

Xu hướng nội dung

Back