SHUOJ 1013 过河卒问题 (递推)

news/2024/7/5 9:55:38 标签: 递推, 过河卒

    题目:SHUOJ 1013

    题目链接:http://202.121.199.212/JudgeOnline/problem.php?id=1013

    题目:

 

1013: 过河卒

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1584  Solved: 464
[Submit][Status][Web Board]

Description

如图,A 点有一个过河卒,需要走到目标 B 点。卒行走规则:可以向下、或者向右。同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点。例如上图 C 点上的马可以控制 9 个点(图中的P1,P2 … P8 和 C)。卒不能通过对方马的控制点。

 

棋盘用坐标表示,A 点(0,0)、B 点(n,m)(n,m 为不超过 20 的整数,并由键盘输入),同样马的位置坐标是需要给出的(约定: C<>A,同时C<>B)。现在要求你计算出卒从 A 点能够到达 B 点的路径的条数。

Input

键盘输入
B点的坐标(n,m)以及对方马的坐标(X,Y){不用判错}

Output

屏幕输出
一个整数(路径的条数)。

Sample Input

6 6 3 2

Sample Output

17

 

 

 

    额,看到这个题我就用了DFS(打脸反省中。。。)。亏我昨天做了一天的递推题。。。。DFS不行会超时,还是递推强啊。

    因为卒只能向下走或者是向右走(也就是说不能返回,所以用DFS就是个错误),所以每一个点的卒要么是从上面来的,要么是从左边来的,那这个点的路径数就是上面那个点的路径数加上左边那个点的路径数,所以递推关系式就是

        f[i][j]=f[i-1][j]+f[i][j-1];

    而我们只需要把第一行和第一列的答案值赋出来就好了,两个单循环就赋好了,遇到控制点之前全是1,控制点及其之后全是0,然后一个递推关系式就行了。当然最开始要先把控制点标记出来。看代码就好了~~~

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=25;
int map[maxn][maxn];
int n,m,x,y;
int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{2,1},{2,-1},{1,2},{1,-2}};        //标记马的八个控制点
long long ans[maxn][maxn];                                                      //int存不下
int main(){
	cin>>n>>m>>x>>y;
	for(int i=0;i<=n;i++)
		for(int j=0;j<=m;j++)
			map[i][j]=0;                                             //地图赋初始值
	map[x][y]=1;
	for(int i=0;i<8;i++){                                                    //标记控制点
		int tempx=x+dir[i][0];
		int tempy=y+dir[i][1];
		if(tempx>=0 && tempx<=n && tempy>=0 && tempy<=m){
			map[tempx][tempy]=1;
		}
	}
	int k=0;
	for(int i=0;i<=m;i++){                                                    //第一行答案赋值
		if(k==1)
			ans[0][i]=0;
		else
			if(map[0][i]==1){
				k=1;
				ans[0][i]=0;
			}
			else
				ans[0][i]=1;
	}
	k=0;
	for(int i=0;i<=n;i++){                                                   //第一列答案赋值
		if(k==1)
			ans[i][0]=0;
		else
			if(map[i][0]==1){
				k=1;
				ans[i][0]=0;
			}
			else
				ans[i][0]=1;
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){                                            //递推,如果遇到控制点就归0
			if(map[i][j]==0)
				ans[i][j]=ans[i-1][j]+ans[i][j-1];
			else
				ans[i][j]=0;
		}
	cout<<ans[n][m];
	return 0;
} 

 

 

    嗯,下面是超时的代码~~~嘿嘿

 

#include<iostream> //超时的代码
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=25;
int map[maxn][maxn];
int n,m,x,y;
int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{2,1},{2,-1},{1,2},{1,-2}};
int dir2[2][2]={{1,0},{0,1}};
long long ans=0;
void dfs(int a,int b){
	if(map[n][m]==2){
		ans++;
		return;
	}
	for(int i=0;i<2;i++){
		int tempx=a+dir2[i][0];
		int tempy=b+dir2[i][1];
		if(tempx>=0 && tempx<=n && tempy>=0 && tempy<=m){
			if(map[tempx][tempy]==0){
				map[tempx][tempy]=2;
				dfs(tempx,tempy);
				map[tempx][tempy]=0;
			}
		}
	}
	return;
}
int main(){
	cin>>n>>m>>x>>y;
	for(int i=0;i<=n;i++)
		for(int j=0;j<=m;j++)
			map[i][j]=0;
	map[x][y]=1;
	for(int i=0;i<8;i++){
		int tempx=x+dir[i][0];
		int tempy=y+dir[i][1];
		if(tempx>=0 && tempy<=n && tempy>=0 && tempy<=m){
			map[tempx][tempy]=1;
		}
	}
	dfs(0,0);
	cout<<ans;
	return 0;
} 


    啦啦啦~~~加油~~

 

 

 

 

 


http://www.niftyadmin.cn/n/1306278.html

相关文章

母函数-以HDU-1398 Square Coins为例

额&#xff0c;其实这个题目不是很难&#xff0c;但因为是母函数的第一篇博客&#xff0c;也是学习母函数的第一次应用&#xff0c;所以就以此题为基础讲一下母函数。 母函数原理 既然是第一道母函数的题目&#xff0c;那就有必要先看一下母函数是什么&#xff0c;原理是什么&a…

WebService之CXF注解报错(三)

WebService之CXF注解 1、具体错误如下 五月 04, 2014 11:29:28 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://service.you.com/}IServiceService from class com.you.service.IService 五月 …

HDU 1028 Ignatius and the Princess III(母函数)

题目&#xff1a;HDU-1028 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1028 题目&#xff1a; Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16806 …

IE9和JPEG-XR:第一印象

One of the new features in IE9 is the support for the JPEG-XR format, which reportedly has a better compression. Is it something we should dive into ASAP? IE9的新功能之一是对JPEG-XR格式的支持&#xff0c;据说该格式具有更好的压缩率。 我们应该尽快涉足吗&…

JavaScript实现获取table中某一列的值

JavaScript实现获取table中某一列的值 1、实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/1999/xhtml"> …

HDU 1085 Holding Bin-Laden Captive!(母函数)

题目&#xff1a;HDU-1085 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1085 题目&#xff1a; Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19057 Accep…

WebService之CXF注解之一(封装类)

Teacher.java&#xff1a; /*** Title:Teacher.java* Package:com.you.model* Description:老师封装类* author:Youhaidong(游海东)* date:2014-5-5 下午11:03:13* version V1.0*/ package com.you.model;import java.io.Serializable;/*** 类功能说明* 类修改者 修改日期* 修改…

编程神书_向编程之神祈祷

编程神书Oh Almighty All-One (01), 全能的哦(01)&#xff0c; Grant me the courage to embrace and early adopt new cool technologies, the serenity to ignore distractions from new technology fads and the wisdom to tell the difference. 让我勇于接受和尽早采…