当前位置:网站首页>【codeforces 1695C Zero Path】DP
【codeforces 1695C Zero Path】DP
2022-07-27 05:30:00 【Yuzhibo one dozen seven~】
Topic link
The question :
To give you one n*m Matrix , The value of each number of this matrix is 1 Or is it -1, Ask whether there is a path so that the sum on the path is 0, Each point can only go down one grid or go right one grid
analysis :
First of all, the length of this path must be n+m-1, So if the length is an odd number, there must be no solution , Because it's all 1 Or is it -1, When the length is even , use mx[i][j] From (1,1) To (i,j) The maximum value of the path ,mi[i][j] From (1,1) To (i,j) The minimum value of the path , And then if 0 stay mx[n][m] and mi[n][m] Then there must be a solution , because mx[n][m] and mi[n][m] It must be an even number , Then the path can be transformed to make mx[n][m] and mi[n][m] To change in real time , And the range of change is 2, So as long as it's 0 Within the range, it must be able to obtain , Now look at the code :
#include<bits/stdc++.h>
using namespace std;
const int N = 1010,inf = 1e7;
int g[N][N];
int mx[N][N],mi[N][N];
typedef pair<int,int> PII;
int main(){
int T;
cin>>T;
while(T--){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&g[i][j]);
mx[i][j] = -1e7;
mi[i][j] = 1e7;
}
if((n+m-1)%2){
cout<<"NO"<<endl;
continue;
}
mx[1][1] = mi[1][1] = g[1][1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i > 1){
mx[i][j] = max(mx[i][j],mx[i-1][j]+g[i][j]);
mi[i][j] = min(mi[i][j],mi[i-1][j]+g[i][j]);
}
if(j > 1){
mx[i][j] = max(mx[i][j],mx[i][j-1]+g[i][j]);
mi[i][j] = min(mi[i][j],mi[i][j-1]+g[i][j]);
}
}
}
if(mx[n][m] >= 0 && mi[n][m] <= 0) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
边栏推荐
- 订单系统功能实现
- 创建项目 实现登录注册,生成jwt,发送验证码
- 李宏毅机器学习组队学习打卡活动day01---机器学习介绍
- Day6 --- SQLAlchemy进阶
- 后台实现spu管理
- torch中乘法整理,*&torch.mul()&torch.mv()&torch.mm()&torch.dot()&@&torch.mutmal()
- 原生token生成加密、解密
- JVM Part 1: memory and garbage collection part 12 -- stringtable
- How to view the evaluation of tutors
- pytorch 数据类型 和 numpy 数据 相互转化
猜你喜欢
随机推荐
Student management system
小米商城项目_注册
String class
Pinball games
redis锁
实用小工具: Kotlin 代码片段
Sparse array → saving and continuation of Gobang
创建项目 实现登录注册,生成jwt,发送验证码
稀疏数组→五子棋的存盘续盘等操作
B1027 print hourglass
封装JWT
接收方设置并发量和限流
mq设置过期时间、优先级、死信队列、延迟队列
用户管理-分页
Day5 --- Flask-RESTful请求响应与SQLAlchemy基础
秒杀系统设计
李宏毅机器学习组队学习打卡活动day02---回归
Pytorch data type and numpy data are mutually transformed
李宏毅机器学习组队学习打卡活动day06---卷积神经网络
Database connection pool & Druid usage









