当前位置:网站首页>Luogu p1535 [usaco08mar]cow traveling s problem solution
Luogu p1535 [usaco08mar]cow traveling s problem solution
2022-06-21 22:08:00 【q779】
Luogu P1535 [USACO08MAR]Cow Travelling S Answer key
Topic link :P1535 [USACO08MAR]Cow Travelling S
The question :
Cows are being divided into N N N That's ok M M M Column ( 2 ≤ N , M ≤ 100 2 \leq N,M \leq 100 2≤N,M≤100) Walk on the grass , Trying to find the most delicious grass in the whole grassland .
Farmer John At some point I saw Bessie in position ( R 1 , C 1 ) (R_1, C_1) (R1,C1), just T T T( 0 < T ≤ 15 0 \lt T \leq 15 0<T≤15) Seconds later ,FJ In position again ( R 2 , C 2 ) (R_2, C_2) (R2,C2) Hit Bessie .FJ I don't know here T T T Has Bessie ever been there in seconds ( R 2 , C 2 ) (R_2, C_2) (R2,C2), All he can be sure of is , Now Bessie is there .
set up S S S For cows in T T T Seconds from ( R 1 , C 1 ) (R_1, C_1) (R1,C1) Go to the ( R 2 , C 2 ) (R_2, C_2) (R2,C2) The total number of paths that can be selected ,FJ Hope to have A program to help him calculate this value . Every second , Cows move horizontally or vertically 1 1 1 Unit distance ( Cows are always moving , It won't stop at the point of the last second in a second ). There are trees in some places on the grass , natural , Cows can't go where the tree is , Will not go out of the grass .
Now you have got a topographic map of the whole grassland , among
.It means flat grass ,*A tree in the way . Your task is to figure out , At the end of T T T Seconds from ( R 1 , C 1 ) (R_1, C_1) (R1,C1) Move to ( R 2 , C 2 ) (R_2, C_2) (R2,C2) What are the possible paths that cows of .
At a glance, can a combined number be saved
Notice that this is a count dp
set up f i , j , k f_{i,j,k} fi,j,k To express with k k k Step just to ( i , j ) (i,j) (i,j) Number of alternatives
Here we need to use search to calculate dp, Brush table method update ( That is to use the answer of the current node to update other nodes )
Why use memory search ? Because the simple cycle cannot guarantee dp The order of calculation
The relaxed upper bound of time complexity is O ( n m t ) O(nmt) O(nmt)
The data range of this question is relatively small, so it's said that you can pass the storm search and pruning
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(115)
int n,m,t,xa,ya,xb,yb,f[N][N][25];
char s[N][N];
int dx[4]={
0,0,1,-1};
int dy[4]={
1,-1,0,0};
struct node
{
int x,y,step;
};
queue<node> q;
bool safe(int x,int y)
{
return 1<=x&&x<=n&&1<=y&&y<=m&&s[x][y]!='*';
}
void bfs()
{
q.push({
xa,ya,0});
f[xa][ya][0]=1;
while(!q.empty())
{
node tmp=q.front();q.pop();
for(int i=0; i<4; i++)
{
int tx=tmp.x+dx[i];
int ty=tmp.y+dy[i];
int ts=tmp.step+1;
if(!safe(tx,ty)||ts>t)continue;
if(f[tx][ty][ts])
{
f[tx][ty][ts]+=f[tmp.x][tmp.y][tmp.step];
continue;
}
f[tx][ty][ts]+=f[tmp.x][tmp.y][tmp.step];
q.push({
tx,ty,ts});
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n >> m >> t;
for(int i=1; i<=n; i++)
cin >> (s[i]+1);
cin >> xa >> ya >> xb >> yb;
bfs();
cout << f[xb][yb][t] << '\n';
return 0;
}
Reprint please explain the source
边栏推荐
- 央企国电集团上海翔伟机电和中外海达成战略合作,捐赠2亿
- 一文彻底搞懂MySQL基础:B树和B+树的区别
- 自制C#编译器
- 数商云纸业集团供应商管理系统:推进企业信息化建设,全面提升供应商管理效率
- Enzo高灵敏度检测——Arg8-Vasopressin ELISA kit
- Wechat applet JS converts numbers into letters
- Go language unit test simulates service request and interface return
- Precautions for Jerry's external radio [chapter]
- DateGridView首列排序
- Guangdong CDC reminds: the summer vacation is coming, so the returned college students can "return home" safely
猜你喜欢

B2B mall website helps enterprises speed up distribution and build an efficient and intelligent B2B online distribution platform

中国工程院院士郑纬民:我对中国在下一个 IT 时代拥有一席之地很乐观

大型语言模型教会智能体进化,OpenAI这项研究揭示了二者的互补关系

solidity实现智能合约教程(4)-ERC1155合约

C language array and pointer exercises (original question + analysis + original code)

15 iterator

What is the execution order of JS asynchronism

Phpmailer sends mails through SMTP. Some of the same sending contents succeed and some fail

赋·新生,链·未来!城链科技产业赋能创新发展大会隆重举行

数商云纸业集团供应商管理系统:推进企业信息化建设,全面提升供应商管理效率
随机推荐
Tkinter drawing component (29) -- Radio Group Control
Which iPad apps can read English literature well?
做自媒体视频,如何写出热门爆款标题
先进封装,一个大周期的开始——“迎风国潮”半导体设备研讨会
Qx2308 high efficiency PFM Synchronous Boost dc/dc converter
Jerry's SD card reuse IIC precautions [chapter]
自制C#编译器
DateGridView首列排序
企业数据防泄漏解决方案分享
Ruiji housekeeper, a century old classic, is waiting for your elegant journey to start again
New energy industry commercial procurement collaboration system: enable new energy industry procurement business and enhance industrial collaboration
Build Eureka server cluster
对“XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们
What is EGFP, green fluorescent protein
洛谷P1378 油滴扩展 题解
AWS CloudWatch
Chess and card games
Eureka console accesses the info endpoint exposed by the microservice
2022佛山潭洲陶瓷展召开新闻发布会 推出展会十大重点
British teddy bear joins the pubg mobile game