Why Did the Cow Cross the Road
来源:互联网 发布:淘宝客服每天接待量 编辑:程序博客网 时间:2024/06/09 00:09
题目:
题目描述
Why did the cow cross the road? Well, one reason is that Farmer John’s farm simply has a lot of roads, making it impossible for his cows to travel around without crossing many of them.
FJ’s farm is arranged as an N×N square grid of fields (3≤N≤100), with a set of N−1 north-south roads and N−1east-west roads running through the interior of the farm serving as dividers between the fields. A tall fence runs around the external perimeter, preventing cows from leaving the farm. Bessie the cow can move freely from any field to any other adjacent field (north, east, south, or west), as long as she carefully looks both ways before crossing the road separating the two fields. It takes her T units of time to cross a road (0≤T≤1,000,000).
One day, FJ invites Bessie to visit his house for a friendly game of chess. Bessie starts out in the north-west corner field and FJ’s house is in the south-east corner field, so Bessie has quite a walk ahead of her. Since she gets hungry along the way, she stops at every third field she visits to eat grass (not including her starting field, but including possibly the final field in which FJ’s house resides). Some fields are grassier than others, so the amount of time required for stopping to eat depends on the field in which she stops.
Please help Bessie determine the minimum amount of time it will take to reach FJ’s house.
输入
The first line of input contains N and T. The next N lines each contain N positive integers (each at most 100,000) describing the amount of time required to eat grass in each field. The first number of the first line is the north-west corner.
输出
Print the minimum amount of time required for Bessie to travel to FJ’s house.
样例输入
4 230 92 36 1038 85 60 1641 13 5 6820 97 13 80
样例输出
31
提示
The optimal solution for this example involves moving east 3 squares (eating the “10”), then moving south twice and west once (eating the “5”), and finally moving south and east to the goal.
题意:
给你n
和t
,然后是一张n*n
的图,每走一步权都+=t
,每走三步权就+=w[i][j]
,也就是加上你所在的那个点的权。问你从左上角走到右下角怎样走权最少。
思路:
用DFS写超时,用BFS写的话每个点只遍历一遍不能得到正确答案。然后借鉴了一下学姐说的用三维DP,走到每个点时枚举一下在这个点可能的步数走到下一个点的权,如果下一个点的已有权大于当前点的状态走过去的权的话,就更新下一个点的权。
所以最后的正确思路是去掉vis[][]
数组的BFS和三维DP。
实现:
#include <iostream>#include <algorithm>#include <set>#include <string>#include <vector>#include <queue>#include <map>#include <stack>#include <iomanip>#include <cstdio>#include <cstring>#include <cmath>#include <climits>#include <cctype>using namespace std;#define maxn 107int mp[maxn][maxn],n,t,to[4][2]={1,0,-1,0,0,1,0,-1};long long bfs() { queue<pair<int, int> > que; long long dp[maxn][maxn][4]; memset(dp,0x3f3f3f3f,sizeof dp); dp[0][0][0] = 0; pair<int, int> now; int x,y,h,l; que.push(make_pair(0,0)); while(!que.empty()) { now = que.front(); que.pop(); x = now.first, y = now.second; for(int k=0 ; k<4 ; k++) { h = x + to[k][0], l = y + to[k][1]; bool flag = false; if(h<n && h>=0 && l<n && l>=0) { if(dp[x][y][0]+t < dp[h][l][1]) { dp[h][l][1] = dp[x][y][0]+t; flag = true; } if(dp[x][y][1]+t < dp[h][l][2]) { dp[h][l][2] = dp[x][y][1]+t; flag = true; } if(dp[x][y][2]+t+mp[h][l] < dp[h][l][0]) { dp[h][l][0] = dp[x][y][2]+t+mp[h][l]; flag = true; } if(flag) que.push(make_pair(h,l)); } } } long long ans = dp[n-1][n-1][3]; for(int i=0 ; i<3 ; i++) ans = min(ans,dp[n-1][n-1][i]); return ans;}int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif ios_base::sync_with_stdio(false);cin.tie(nullptr); while(cin >> n >> t) { for(int i=0 ; i<n ; i++) for(int j=0 ; j<n ; j++) cin >> mp[i][j]; cout << bfs() << '\n'; } return 0;}
- Why Did the Cow Cross the Road
- ZJNU 1901 Why Did the Cow Cross the Road bfs
- Why Did the Cow Cross the Road II
- Why Did the Cow Cross the Road III
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- [USACO17FEB]Why Did the Cow Cross the Road I P
- [USACO17FEB]Why Did the Cow Cross the Road II P
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- ZJNU 1902 Why Did the Cow Cross the Road II dp
- ZJNU 1903 Why Did the Cow Cross the Road III 树状数组
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road II
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road III
- [RK3288][Android6.0] 调试笔记 --- 电池电量一直显示100%
- is-a和has-a
- 取消一个本地svn目录与svn的联系
- sqlserver2008R2数据库出现“不支持此服务器版本。目标服务器必须是 SQL Server 2000 或更高版本解决”
- HBuilder 入门(5) / 编写一个登录页面
- Why Did the Cow Cross the Road
- Unity 面试题
- JS和JSP的区别
- compass安装
- 规范的StringBuilder类用法
- 如何阅读oracle数据块data block dump文件
- centos6.X 安装lnmp环境
- 细说HTTPS
- Jquery实现确定取消对话框