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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章