您的位置:首页 > Web前端

UVA 825 Walking on the Safe Side 读入输出+DP

2014-07-11 00:05 288 查看
#include <map>
#include <set>
#include <list>
#include <cmath>
#include<cctype>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b)
{
return a % b == 0 ? b : gcd(b, a % b);
}
#define MAXN 105
int G[MAXN][MAXN],dp[MAXN][MAXN];
int W,N;
int dx[]={0,1};
int dy[]={1,0};
bool judge(int x,int y){return (x>=1 && x<=W && y>=1 && y<=N); }
void read()
{
memset(G,0,sizeof(G));
scanf("%d%d",&W,&N);getchar();
char tmp[120];
for (int i=1;i<=W;i++)
{
gets(tmp);
// printf("%s\n",tmp);
int j=0;
while (isdigit(tmp[j])) j++;
for (int k=j+1;k<(int)strlen(tmp);k++)
{
int x=0;
while (isdigit(tmp[k]))
{
x=x*10+tmp[k]-'0';
k++;
}
G[i][x]=1;
}
}
/*for (int i=1;i<=W;i++)
{
for (int j=1;j<=N;j++)
printf("%d",G[i][j]);
putchar('\n');
}*/
}
int  calcu(int x,int y)
{
if (dp[x][y]!=-1) return dp[x][y];
if (x==W && y==N) return 1;
dp[x][y]=0;
for (int i=0;i<2;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if (!G[nx][ny] && judge(nx,ny))
dp[x][y]+=calcu(nx,ny);
}
return dp[x][y];
}
int main()
{
//freopen("smaple.txt","r",stdin);
int T;
scanf("%d",&T);
while (T)
{
read();
memset(dp,-1,sizeof(dp));
printf("%d\n",calcu(1,1));
if (--T) putchar('\n');
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: