【題解】ZeroJudge b554: 5.貪吃龍遊戲

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=b554

#include <iostream>
using namespace std;

int n, g[10][10], ans, d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
string s;

int dfs(int x, int y){
    int ret = 0;
    g[x][y] = 0;
    for (int i = 0; i < 4; i++){
        if (g[x+d[i][0]][y+d[i][1]]){
            ret = max(ret, dfs(x+d[i][0], y+d[i][1]));
        }
    }
    g[x][y] = 1;
    return ret+1;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> s;
        for (int j = 0; j < n; j++){
            g[i][j+1] = s[j]-'0';
        }
    }
    if (g[1][1]) ans = dfs(1, 1);
    cout << ans << "\n";
}
分享本文 Share with friends