[Usaco feb07] bronze lotus pond

Source: Internet
Author: User

169. [usaco feb07] bronze lotus pond

★Input file:bronlily.inOutput file:bronlily.outSimple comparison
Time Limit: 1 s memory limit: 128 MB

Translated by cmykrgb123

Description

Farmer John built a beautiful pond to make his cows aesthetic and exercise. The pool of this rectangle is divided into m rows and n columns (1 ≤ m ≤ 30; 1 ≤ n ≤ 30) square lattice. There are amazing strong lotus on some grids, and some rocks. The rest is beautiful, pure, blue water.

Bessie is practicing ballet. She jumps from a lotus flower to another lotus flower, which is currently located in a lotus flower. She wants to jump one by one on the Lotus, with the goal being another given Lotus. She can skip neither the water nor a rock.

LingLayman'sSurprisingly, every jump of Bessie is like a knight in chess: horizontal M1 (1 ≤ M1 ≤ 30), vertical movement and then m2 (1 ≤ M2 ≤ 30; m1 = m2), or vertical movement and then M1, horizontal movement of M2. Bessie sometimes has up to eight choices.

Given the layout of the pond and the jumping format of Bessie, determine the minimum number of hops for Bessie from her departure location to the final destination. Bessie will be able to jump to the destination with the given test data.

Input

  • Row 1st: Four integers separated by spaces: m, n, M1, M2
  • 2nd .. row M + 1: N integers in line I + 1, indicating the status of the position: 0 indicates water; 1 indicates Lotus; 2 indicates rock; 3 indicates the starting position of Bessie; 4. This is the target location for Bessie.

Output

  • Row 1st: an integer ranging from the starting point to the position to which you want to go. This is the minimum number of hops of Bessie.

Sample Input

4 5 1 21 0 1 0 13 0 2 0 40 1 2 0 00 0 0 1 0

Sample output

2

Input description

Bessie started from the second position in row 2nd, and her goal was at the rightmost of row 1st. Several

Output description

Bessie jumped smartly to the location of 1st rows and then to the destination.

Basic Search.

#include<cstdio>#include<queue>using namespace std;const int maxn =33;int map[maxn][maxn];struct node{    int x,y;    int step;}s_pos;int m,n,m1,m2,step;int sx,sy,ex,ey;bool vis[maxn][maxn];bool check(int x,int y){     return x>=0&&x<m&&y>=0&&y<n;}void bfs(){    int dx[]={-m1,-m1,m1,m1,-m2,-m2,m2,m2};    int dy[]={m2,-m2,m2,-m2,m1,-m1,m1,-m1};    queue<node> q;    s_pos.x=sx; s_pos.y=sy; s_pos.step=0;    q.push(s_pos);    vis[sx][sy]=true;    while(!q.empty()){        node now = q.front();q.pop();        if(now.x==ex&&now.y==ey){           step=now.step;return;        }        for(int i=0;i<8;i++){            int nx=now.x+dx[i];  int ny=now.y+dy[i];            if(check(nx,ny)&&!vis[nx][ny]&&(map[nx][ny]==1||map[nx][ny]==4)){                node next = now;                next.x=nx;  next.y=ny;  next.step++;                q.push(next);                vis[nx][ny]=true;            }        }    }}int main(){    freopen("bronlily.in","r",stdin);    freopen("bronlily.out","w",stdout);    scanf("%d%d%d%d",&m,&n,&m1,&m2);    for(int i=0;i<m;i++) for(int j=0;j<n;j++) scanf("%d",&map[i][j]);    for(int i=0;i<m;i++){        for(int j=0;j<n;j++){           if(map[i][j]==3){               sx=i;sy=j;           }           if(map[i][j]==4){               ex=i;ey=j;           }        }    }    bfs();    printf("%d\n",step);    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.