ZJNU 1901 Why Did the Cow Cross the Road bfs

Why Did the Cow Cross the Road

Time Limit: 3000MS Memory Limit: 3000K
Total Submissions: 40 Accepted: 7
Description

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−1 east-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.

Input

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.

Output

Print the minimum amount of time required for Bessie to travel to FJ’s house.

Sample Input

4 2
30 92 36 10
38 85 60 16
41 13 5 68
20 97 13 80
Sample Output

31
Hint

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*n的矩阵,每走一格我们要消耗k,每走三格我们要消耗st[i][j],即这个矩阵里第i行第j列的值,问我们从(1,1)走向(n,n)最少需要消耗多少。

解题思路:我听到用dfs,用最短路,用dp的都有…而且好像都能对,然而我是用bfs的,因为每格最多只有三格状态,即步数为1,2,3状态,因此总共的状态应该为100*100*3个,因此我选择的是bfs直接暴力过,不过听说dp简单…算了我这种渣渣哈哈哈哈。最简单的bfs即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,k,step,s[105][105],sum[105][105][3],ans;
int a[4][2]={1,0,0,1,0,-1,-1,0};
struct node{
    int x,y,step,sum1;
}ss,sa;
queue<node>p;
bool check(int x,int y){
    if(x<=n&&x>=1&&y<=n&&y>=1)  return true;
    return false;
}
void bfs(int x,int y){
    ss.x=x;
    ss.y=y;
    ss.step=0;
    ss.sum1=0;
    sum[1][1][0]=0;
    p.push(ss);
    while(!p.empty()){
        ss=p.front();
        for(int i=0;i<4;i++){
            sa.x=ss.x+a[i][0];
            sa.y=ss.y+a[i][1];
            if(check(sa.x,sa.y)){
                sa.step=(ss.step+1)%3;
                if(sa.step==0)  sa.sum1=ss.sum1+k+s[sa.x][sa.y];
                else sa.sum1=ss.sum1+k;
                if(sum[sa.x][sa.y][sa.step]==-1||sum[sa.x][sa.y][sa.step]>sa.sum1){
                    sum[sa.x][sa.y][sa.step]=sa.sum1;
                    p.push(sa);
                }
            }
        }
        p.pop();
    }
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)   scanf("%d",&s[i][j]);
    memset(sum,-1,sizeof(sum));
    bfs(1,1);
    ans=min(sum[n][n][0],min(sum[n][n][1],sum[n][n][2]));
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值