您的位置:首页 > 其它

HDU1978howmanyways

2016-04-23 23:07 162 查看
<h1 style="COLOR: #1a5cc8">How many ways</h1><span size="+0" style=""><strong><span style="font-family: Arial; color: green; font-size: 12px;">Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4447    Accepted Submission(s): 2616
</span></strong></span>

<div class="panel_title" align="left">Problem Description</div><div class="panel_content">这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下:
1.机器人一开始在棋盘的起始点并有起始点所标有的能量。
2.机器人只能向右或者向下走,并且每走一步消耗一单位能量。
3.机器人不能在原地停留。
4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量。
<center><img style="MAX-WIDTH: 100%" src="http://acm.hdu.edu.cn/data/images/C113-1003-1.gif" alt="" /> </center>
如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点是(2,4)

点,当他到达(2,4)点时将拥有1单位的能量,并开始下一次路径选择,直到到达(6,6)点。
我们的问题是机器人有多少种方式从起点走到终点。这可能是一个很大的数,输出的结果对10000取模。</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Input</div><div class="panel_content">第一行输入一个整数T,表示数据的组数。
对于每一组数据第一行输入两个整数n,m(1 <= n,m <= 100)。表示棋盘的大小。接下来输入n行,每行m个整数e(0 <= e < 20)。</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Output</div><div class="panel_content">对于每一组数据输出方式总数对10000取模的结果.</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="FONT-FAMILY: Courier New,Courier,monospace">1
6 6
4 5 6 6 4 3
2 2 3 1 7 2
1 1 4 6 2 7
5 8 4 3 9 5
7 6 6 2 1 5
3 1 1 3 7 2</div>


[align=left]Sample Output[/align]

3948



#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int map[101][101],dir[101][101];
int ans,n,m,s;
int dfs(int x,int y)
{
int ans=0;
if(x==n&&y==m)//bj
return 1;
if(dir[x][y]>=0)
return dir[x][y];
int sum=map[x][y];//别设置全局变量
for( int i=0 ; i<= sum ; i++ )         //搜索所有可以走的路
for( int j=0 ; j<= sum ; j++ )
{
if( (i+j)<= sum && (x+i<= n )&& (y+j<=m )&&(i+j)!= 0 )//使其所能走的所有可能满足情况
{
ans+=dfs( i+x , j+y ) ;    //要求的范围内
ans %=10000 ;
}
//该点能走的路数
}
dir[x][y]=ans;
return ans;
}
int main()
{
int t;
while(scanf("%d",&t)==1)
{
while(t--)
{
cin>>n>>m;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin>>map[i][j];
memset(dir,-1,sizeof(dir));
s=dfs(1,1);
cout<<s<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: