洛谷 P1958 上学路线-dfs
生活随笔
收集整理的這篇文章主要介紹了
洛谷 P1958 上学路线-dfs
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
輸入:
5 4 3 2 2 2 3 4 2輸出:
5注意點:這題目輸入的時候先輸入列,再輸入行,這點要注意,然后這個圖跟我們二維數(shù)組是反過來的,所以題目說得向北走要變成向南走,注意一下細節(jié)就好了。
代碼如下:
#include <iostream> using namespace std; const int N = 20;int dx[] = {0, 1}; bool vis[N][N]; int mp[N][N];int dy[] = {1, 0}; int ans; int n, m;void dfs(int x, int y) {if (x == n && y == m) {ans++;return ;}for (int i = 0; i < 2; i++) {int xx = x + dx[i], yy = y + dy[i];if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] || mp[xx][yy] == -1)continue;vis[xx][yy] = true;dfs(xx, yy);vis[xx][yy] = false;} }int main() {cin >> m >> n;//n為行,m為列int cnt;cin >> cnt;while (cnt--) {int a, b;cin >> a >> b;//a為列,b為行mp[b][a] = -1;//障礙物標記為-1}dfs(1, 1);cout << ans << endl;return 0; } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的洛谷 P1958 上学路线-dfs的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 该怎么脱离电源电脑电源如何分开
- 下一篇: 叱咤30年的杀毒软件叱咤30年的杀毒软件