当前位置:网站首页>[noip2002 popularization group] cross the river pawn
[noip2002 popularization group] cross the river pawn
2022-06-28 04:46:00 【Pandaoxi】
Go through all the hardships , I finally A C AC AC La !
[NOIP2002 Popularization group ] River crossing pawn
Title Description
On the chessboard A A A There is a river crossing pawn , Need to get to the goal B B B spot . The rules of pawn walking : You can go down 、 Or to the right . At the same time on the chessboard C C C
There's a horse on the other side , The point where the horse is located and all the points that can be reached in one step of jumping are called the control points of the opposing horse . So it's called “ A pawn crossing the river ”.The chessboard is represented by coordinates , A A A spot ( 0 , 0 ) (0, 0) (0,0)、 B B B spot ( n , m ) (n, m) (n,m), The same coordinates of the horse's position need to be given .
Now I ask you to calculate the number of soldiers from A A A Point can reach B B B The number of paths of points , Suppose the horse's position is fixed , It's not a step by step .
Input format
Four positive integers in a row , respectively B B B Point coordinates and horse coordinates .
Output format
An integer , Indicates the number of paths .
Examples #1
The sample input #1
6 6 3 3Sample output #1
6Tips
about 100 % 100 \% 100% The data of , 1 ≤ n , m ≤ 20 1 \le n, m \le 20 1≤n,m≤20, 0 ≤ 0 \le 0≤ Horse coordinates ≤ 20 \le 20 ≤20.
【 Title source 】
NOIP 2002 Question 4 of the popularization group
Let's look at this problem together , yes NOIP The last question of ( For my kind of konjaku , It's already very difficult ), The algorithmic label given by Luogu official is dynamic programming !
Let's think about it , Simplify if you can .
Before that , Let's think about one thing first , For example, this grid , We from S S S Go to the E E E, How many different ways to walk ?
It's simple , Elementary school mathematics filling method .
alike , Let's think about , Simulate their position , Find the recurrence relation :
Obvious , 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) Is the relation of this question , It is also the relation of this big problem .
Now? , Let's think about this problem .
alike , We can be sure , Show the coordinates where the horse can move , Then mark 0.
( Pay attention to this question l o n g l o n g long\ long long long Ha , Otherwise it's out of range )
// input data
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;
}
}
// Represents the coordinates of the horse
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;

Judge whether you crossed the line , If you cross the line , You can't move .
Recursion of the core , Here it is :
i = 0 i=0 i=0 It's the first line , Except for control points , Only from the left , a ( i , j ) = a(i,j)= a(i,j)= The value on the left side
j = 0 j=0 j=0 It's the first column , Except for control points , Only from above , a ( i , j ) = a(i,j)= a(i,j)= The value above
The rest , Except for control points , Sum according to the previous relation 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];
}
}
Finally, simply , The output is over .
cout<<a[n][m];
/* // Output the number of each position for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ cout<<a[i][j]<<" "; } cout<<endl; } */
A complete map :
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
The complete code is as follows :
// 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;
}
Luogu A C AC AC .
边栏推荐
- mysql导入文本文件时的pager
- 视频直播系统源码,倒计时显示,商品秒杀倒计时
- 多线程实现 重写run(),怎么注入使用mapper文件操作数据库
- UI自动化测试框架搭建 —— 编写一个APP自动化
- Matlab exercises -- routine operation of matrix
- Code understanding: implementing volume models for hangwriten text recognition
- Learning about DC-DC step-down chip of sy8120i (12V reduced to 3.3V)
- Web3来临时的风口浪尖
- June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 and the number of 0 in the two intervals are equal. The two intervals can intersect, but not c
- 【Linux】【Mysql】ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
猜你喜欢

leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】

2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。

灵活的IP网络测试工具——— X-Launch

抖音实战~关注博主

Severe tire damage: the first rock band in the world to broadcast live on the Internet

Google Earth Engine(GEE)——全球洪水数据库 v1 (2000-2018年)

TACo:一种关于文字识别的数据增强技术

Multithreading and high concurrency III: AQS underlying source code analysis and implementation classes

How do I get the STW (pause) time of a GC (garbage collector)?

测试/开发程序员真的是青春饭吗?世界是公平的,咱们都凭实力说话......
随机推荐
Multi project design and development · introduction to class library project
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
Pinda general permission system (day 5~day 6)
After launching the MES system, these changes have taken place in the enterprise
有人用cdc同步到mysql发生过死锁吗?
【Matlab红绿灯识别】红绿灯识别【含GUI源码 1908期】
Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)
C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项
Has anyone ever used CDC to synchronize to MySQL with a deadlock?
Sword finger offer 47 Maximum gift value (DP)
在线直播源码,JS动态效果之,侧边栏滚动固定效果
Matlab exercises -- routine operation of matrix
Multithreading and high concurrency six: source code analysis of thread pool
Role of native keyword
Pager when importing text files from MySQL
Resolve cross domain
Establishment of SSH Framework (Part 2)
Tiktok actual battle ~ take off the blogger
UI automation test framework construction - write an app automation
Difference between curdate() and now()




