当前位置:网站首页>[NOIP2002 普及组] 过河卒
[NOIP2002 普及组] 过河卒
2022-06-28 04:10:00 【潘道熹】
历尽千辛万苦,我终于 A C AC AC啦!
[NOIP2002 普及组] 过河卒
题目描述
棋盘上 A A A 点有一个过河卒,需要走到目标 B B B 点。卒行走的规则:可以向下、或者向右。同时在棋盘上 C C C
点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。棋盘用坐标表示, A A A 点 ( 0 , 0 ) (0, 0) (0,0)、 B B B 点 ( n , m ) (n, m) (n,m),同样马的位置坐标是需要给出的。
现在要求你计算出卒从 A A A 点能够到达 B B B 点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步。
输入格式
一行四个正整数,分别表示 B B B 点坐标和马的坐标。
输出格式
一个整数,表示所有的路径条数。
样例 #1
样例输入 #1
6 6 3 3样例输出 #1
6提示
对于 100 % 100 \% 100% 的数据, 1 ≤ n , m ≤ 20 1 \le n, m \le 20 1≤n,m≤20, 0 ≤ 0 \le 0≤ 马的坐标 ≤ 20 \le 20 ≤20。
【题目来源】
NOIP 2002 普及组第四题
我们一起来看下这道题,是NOIP的最后一题(对于我这种蒟蒻,难度已经很大了),洛谷官方给的算法标签是动态规划!
我们来想想看啊,能简化就简化。
在此之前,我们先来想一个事儿,比如说这个格子,我们从 S S S走到 E E E,有多少种不同的走法?
很简单,小学数学填数法。
同样的,我们想一下,模拟下它们的位置,找出递推关系式:
显而易见, a ( i , j ) = a ( i − 1 , j ) + a ( i , j − 1 ) a(i,j)=a(i-1,j)+a(i,j-1) a(i,j)=a(i−1,j)+a(i,j−1)是这个题的关系式,同样也是这道大题的关系式。
现在,让我们一起来思考一下这道题。
同样的,我们可以确定,把马可以移动的的坐标表示出来,然后标上0。
(注意这个题要开 l o n g l o n g long\ long long long哈,不然就超出范围了)
// 输入数据
long long a[100][100]={
};
int n,m,x,y;
cin>>n>>m>>x>>y;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
a[i][j]=1;
}
}
// 表示马的坐标
a[x][y]=0;
if(x-2>=0&&y-1>=0) a[x-2][y-1]=0;
if(x-2>=0&&y+1<=m) a[x-2][y+1]=0;
if(x-1>=0&&y-2>=0) a[x-1][y-2]=0;
if(x-1>=0&&y+2<=m) a[x-1][y+2]=0;
if(x+1<=n&&y-2>=0) a[x+1][y-2]=0;
if(x+2<=n&&y-1>=0) a[x+2][y-1]=0;
if(x+2<=n&&y+1<=m) a[x+2][y+1]=0;
if(x+1<=n&&y+2<=m) a[x+1][y+2]=0;

判断是否越界,如果越界了,就移动不了了呗。
核心的递推,在这儿:
i = 0 i=0 i=0 是第一行,除了控制点,只能从左侧来, a ( i , j ) = a(i,j)= a(i,j)= 左侧的值
j = 0 j=0 j=0 是第一列,除了控制点,只能从上方来, a ( i , j ) = a(i,j)= a(i,j)= 上方的值
其余点,除了控制点,按照以前的关系式求和 a ( i , j ) = a ( i − 1 , j ) + a ( i , j − 1 ) a(i,j)=a(i-1,j)+a(i,j-1) a(i,j)=a(i−1,j)+a(i,j−1)
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0&&j==0) continue;
if(a[i][j]==0) continue;
if(i==0) a[i][j]=a[i][j-1];
else if(j==0) a[i][j]=a[i-1][j];
else a[i][j]=a[i-1][j]+a[i][j-1];
}
}
最后简简单单,输出就完了。
cout<<a[n][m];
/* // 输出每一个位置的数 for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ cout<<a[i][j]<<" "; } cout<<endl; } */
完整的地图:
1 1 1 1 1 1 1
1 2 0 1 0 1 2
1 0 0 1 1 0 2
1 1 1 0 1 1 3
1 0 1 1 2 0 3
1 1 0 1 0 0 3
1 2 2 3 3 3 6
完整的代码如下:
// Author:PanDaoxi
#include <iostream>
using namespace std;
int main(){
long long a[100][100]={
};
int n,m,x,y;
cin>>n>>m>>x>>y;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
a[i][j]=1;
}
}
a[x][y]=0;
if(x-2>=0&&y-1>=0) a[x-2][y-1]=0;
if(x-2>=0&&y+1<=m) a[x-2][y+1]=0;
if(x-1>=0&&y-2>=0) a[x-1][y-2]=0;
if(x-1>=0&&y+2<=m) a[x-1][y+2]=0;
if(x+1<=n&&y-2>=0) a[x+1][y-2]=0;
if(x+2<=n&&y-1>=0) a[x+2][y-1]=0;
if(x+2<=n&&y+1<=m) a[x+2][y+1]=0;
if(x+1<=n&&y+2<=m) a[x+1][y+2]=0;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0&&j==0) continue;
if(a[i][j]==0) continue;
if(i==0) a[i][j]=a[i][j-1];
else if(j==0) a[i][j]=a[i-1][j];
else a[i][j]=a[i-1][j]+a[i][j-1];
}
}
cout<<a[n][m];
/* for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ cout<<a[i][j]<<" "; } cout<<endl; } */
return 0;
}
洛谷 A C AC AC 。
边栏推荐
- leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
- 有关函数模板的那些小知识-.-
- Why is the frame rate calculated by opencv wrong?
- Web3来临时的风口浪尖
- xml&nbsp; File read / write
- [Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
- 测试/开发程序员真的是青春饭吗?世界是公平的,咱们都凭实力说话......
- AspNetCoreRateLimit 速率限制 接口访问限制 限流控制
- 公司领导说,个人代码超10个Bug就开除,是什么体验?
- 云厂商为什么都在冲这个KPI?
猜你喜欢

抖音實戰~關注博主

Matlab exercises -- exercises related to symbolic operation

From zero to one, I will teach you to build a "search by text and map" search service (I)

Uncover the mystery of SSL and learn how to protect data with SSL

一文详解|增长那些事儿

Tiktok practice ~ pay attention to bloggers

Pager when importing text files from MySQL

Matlab exercises -- routine operation of matrix

Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products

Web3来临时的风口浪尖
随机推荐
If mysqlcdc sets multiple parallelism, will the incremental data repeat?
10:00面试,10:02就出来了 ,问的实在是太...
Matlab exercises -- basic data processing
A bit of knowledge - online resources on Chinese Learning
leetcode - 329. Longest increasing path in matrix
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
浅析搭建视频监控汇聚平台的必要性及场景应用
Reading notes of top performance version 2 (II) -- Performance observation tool
Matlab exercises -- routine operation of matrix
Google Earth engine (GEE) - global flood database V1 (2000-2018)
Excel knowledge and skills summary
How do I get the STW (pause) time of a GC (garbage collector)?
From meeting a big guy to becoming a big guy, shengteng AI developer creation day brings infinite possibilities to developers
政策利好,20多省市开启元宇宙发展规划
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
2022年中國音頻市場年度綜合分析
xml&nbsp; File read / write
短视频平台开发,点击链接、图片自动跳转到新的页面
Another option for ERP upgrade, MES system
Web3来临时的风口浪尖




