1. 程式人生 > 其它 >馬蘭過河卒

馬蘭過河卒

 

 二、設計思想

  首先將所有點設定為可通行,將控制點設定為不可通行。因為只可以向下或者向右行走,所以要想到達目標點只需要到達其左邊的點或者上邊的點,以此類推,計算從起點到達中間任何一個點的路徑條數,並用陣列tripnum[][]儲存到達這個點的路徑條數。

三、程式碼

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int a[9] = { 0,-1,-1,-2,-2,1,1,2,2 };
    int b[9] = { 0,2,-2,1,-1,2,-2,1,-1 };
    
int map[16][16] = { 0 }; long long tripnum[16][16] = { 0 }; int n, m, x, y; scanf_s("%d %d %d %d", &n, &m, &x, &y); for (int i = 0; i < 16; i++) for (int j = 0; j < 16; j++) map[i][j] = 1; for (int i = 0; i < 9; i++) { if (x + a[i] <= 15
&& x + a[i] >= 0 && y + b[i] <= 15 && y + b[i] >= 0) map[x + a[i]][y + b[i]] = 0; } for (int i = 0; i < 16; i++) { if (map[0][i] == 1) { tripnum[0][i] = 1; } else break; } for (int i = 0
; i < 16; i++) { if (map[i][0] == 1) { tripnum[i][0] = 1; } else break; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (map[i][j] == 1) tripnum[i][j] = tripnum[i - 1][j] + tripnum[i][j - 1]; } } printf("%lld", tripnum[n][m]); return 0; }
View Code