Knights of Ni (bfs)

来源:互联网 发布:英语软件哪个好 编辑:程序博客网 时间:2024/04/30 03:50

Description

Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible.

Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000).

The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery.

In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block.

It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.

Input

Line 1: Two space-separated integers: W and H.

Lines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row.

The integers that describe the map come from this set:
0: Square through which Bessie can travel
1: Impassable square that Bessie cannot traverse
2: Bessie's starting location
3: Location of the Knights of Ni
4: Location of a shrubbery

Output

Line 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.

Sample Input

8 44 1 0 0 0 0 1 00 0 0 1 0 1 0 00 2 1 1 3 0 4 00 0 0 4 1 1 1 0

Sample Output

11

Hint

Explanation of the sample:

Width=8, height=4. Bessie starts on the third row, only a few squares away from the Knights.

Bessie can move in this pattern to get a shrubbery for the Knights: N, W, N, S, E, E, N, E, E, S, S. She gets the shrubbery in the northwest corner and then makes her away around the barriers to the east and then south to the Knights.
题意:
给你一个地图从数字2开始走,然后带上4,走到数字3,问你最小能走多少步?
思路:
等走到3的时候标记一下,然后带着4,走到3. 就是开一个结构体,里面有,位置x,y,step,还有重要的一个变量叫它thing吧,如果thing为0,那就表明没有带上4,相反,带上了4这个物品。
这个方法很快写好了,但是。。。。。。一直没有出来结果。为什么呢?
刚刚看了别人的blog才发现的,那就是,要设一个标记数组,来走没走过,当时我也在疑惑啊,如果标记了那么带上东西不就走不回来了?所以找了半天的错,就是在这里。
找了一下午啊,主要还是对于标记数组没有足够的认识啊。。。
自己写的没有带标记数组的代码:


#if 0#include<bits/stdc++.h>using namespace std;#define MAX 2000+50int w,h;int f[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int a[MAX][MAX],sx,sy,ex,ey;bool ok(int i,int j){if(i>=0&&i<h&&j>=0&&j<w){return 1;}return 0;}struct node{int x,y,step;int thing;};void bfs(){queue<node> q;node now;now.step=0;now.x=sx;now.y=sy;now.thing=0;q.push(now);while(!q.empty()) {now=q.front(); q.pop();if(now.x==ex&&now.y==ey)   {if(now.thing==1){cout<<now.step<<endl;return;}}elsefor(int i=0; i<4; i++){node next;next.x=now.x+f[i][0];next.y=now.y+f[i][1];if(ok(next.x,next.y)) {if(a[next.x][next.y]!=1) {if(a[next.x][next.y]==4){next.thing=1;next.step=now.step+1;q.push(next);}else {next.step=now.step+1;q.push(next);}}}}}}int main(){while(cin>>w>>h){memset(a,0,sizeof(a));//memset(b,0,sizeof(b));for(int i=0; i<h; i++){for(int j=0; j<w; j++){cin>>a[i][j];if(a[i][j]==2){sx=i;sy=j;}else if(a[i][j]==3){ex=i;ey=j;}}}bfs();}}#endif


完美的代码:

//////vis开三维判断到4之前和到4之后是否访问过该点# include <stdio.h># include <algorithm># include <string.h># include <queue>using namespace std;int xx[4]={0,0,1,-1};int yy[4]={1,-1,0,0};struct node{    int x;    int y;    int step;    int flag;};int n,m;int a[1010][1010];int vis[1010][1010][2];void bfs(int ax,int ay){    memset(vis,0,sizeof(vis));    node front,next;    front.x=ax;    front.y=ay;    front.step=0;    front.flag=0;    vis[ax][ay][0]=1;    queue<node>q;    q.push(front);    while(!q.empty())    {        front=q.front();        q.pop();        if(a[front.x][front.y]==3&&front.flag==1)        {            printf("%d\n",front.step);            return ;        }        for(int i=0;i<4;i++)        {            next.x=front.x+xx[i];            next.y=front.y+yy[i];            next.step=front.step+1;            if(a[next.x][next.y]==4||front.flag==1)                next.flag=1;            else                next.flag=0;            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&a[next.x][next.y]!=1&&!vis[next.x][next.y][next.flag])            {                vis[next.x][next.y][next.flag]=1;                q.push(next);            }        }    }    return ;}int main(){    int i,j,ax,ay;    while(~scanf("%d%d",&m,&n))    {        for(i=0; i<n; i++)        {            for(j=0; j<m; j++)            {                scanf("%d",&a[i][j]);                if(a[i][j]==2)                {                    ax=i;                    ay=j;                }            }        }        bfs(ax,ay);    }    return 0;}














原创粉丝点击