【动态规划】Vijos P1121 马拦过河卒

题目链接:

  https://vijos.org/p/1616

题目大意:

  卒从(0,0)走到(n,m),只能向下或向右,不能被马一步碰到或走到马,有几种走法。

题目思路:

  【动态规划】

  把马控制的地方全部标记,接下来DP,f[i][j]=f[i-1][j]+f[i][j-1];

 

 

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define MAX 0x7f7f7f7f
26 #define PI 3.14159265358979323
27 #define N 24
28 using namespace std;
29 typedef long long LL;
30 int cas,cass;
31 int n,m,lll,ans;
32 int f[N][N];
33 int main()
34 {
35     #ifndef ONLINE_JUDGE
36 //    freopen("1.txt","r",stdin);
37 //    freopen("2.txt","w",stdout);
38     #endif
39     int i,j,hx,hy;
40 //    for(scanf("%d",&cas);cas;cas--)
41 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
42     while(~scanf("%d",&n))
43 //    while(~scanf("%d",&n))
44     {
45         scanf("%d%d%d",&m,&hx,&hy);
46         n+=2,m+=2,hx+=2,hy+=2;
47         f[hx][hy]=f[hx-2][hy-1]=f[hx-2][hy+1]=f[hx-1][hy-2]=f[hx-1][hy+2]=f[hx+1][hy-2]=f[hx+1][hy+2]=f[hx+2][hy-1]=f[hx+2][hy+1]=-MAX;
48         f[2][1]=1,f[1][2]=0;
49         for(i=2;i<=n;i++)
50         {
51             for(j=2;j<=m;j++)
52             {
53                 if(f[i][j]==-MAX)continue;
54                 if(f[i-1][j]!=-MAX)f[i][j]+=f[i-1][j];
55                 if(f[i][j-1]!=-MAX)f[i][j]+=f[i][j-1];
56             }
57         }
58         printf("%d\n",f[n][m]);
59     }
60     return 0;
61 }
62 /*
63 //
64 
65 //
66 */
View Code

 

posted @ 2016-08-16 19:42  Cool639zhu  阅读(256)  评论(0编辑  收藏  举报