当前位置: 代码迷 >> 综合 >> Shortest path counting
  详细解决方案

Shortest path counting

热度:58   发布时间:2023-12-14 20:33:20.0

A chess rook can move horizontally o r vertically to any square in the same row o r in the same column of a chessboard. Find the number of shortest paths by which a rook can move from one corner of a chessboard to the diagonally opposite corner。

输入


  

a interger number n is row and column of chessboard. 0 < n <=16

输出


  

the number of shortest paths.

样例输入

4

样例输出

20
#include <iostream>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include<stack>  
#include<iostream>  
#include<string.h>  
#include<math.h>  
using namespace std;int main()
{int a[20][20];int n;while (cin >> n){for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){if (i != 1&&j!=1) {//状态转移,将来来源坐标的值转移到当前坐标a[i][j] = a[i-1][j]+a[i][j - 1];}else if (i != 1) {a[i][j] =a[i-1][j];}else if (j != 1) {a[i][j] = a[i][j-1];}else a[i][j] = 1;}}cout << a[n][n] << endl;}return 0;
}

  相关解决方案