您的位置:首页 > 其它

Why Did the Cow Cross the Road - UPCOJ 3437 - BFS + DP

2017-07-28 17:49 721 查看

题目:

题目描述

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 2
30 92 36 10
38 85 60 16
41 13 5 68
20 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 107
int 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)
4000
;
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs dp 题解 acm