练习赛A题

来源:互联网 发布:js 对象访问器 编辑:程序博客网 时间:2024/06/06 19:39

题目:

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.
题目传送门:https://cn.vjudge.net/contest/178899#problem/A

解题思路:

两次BFS

分别从骑士位置和起点向灌木丛位置搜索

刚开始没标记结果一直wa

后来改了好多次还是wa

最后发现是因为方向数组k < 4写成了k <=4

还是要更加细心。。。

/* 0、可以走的地方 1、不可以走的地方 2、起点 3、骑士的位置 4、灌木丛的位置 */#include <iostream>#include <cstring>#include <algorithm>#include <stdio.h>#include <queue>#include <iomanip>#include <string>using namespace std;int dx[4] = {0,1,0,-1};int dy[4] = {1,0,-1,0};int map[1005][1005];bool visit[1005][1005];int flag,W,H,i,j,sum,ii,cnt;struct Point{    int x,y,step;}start_point,end_point,point,point1,pp[1000000];bool pd(int x,int y){    if (x < 1 || x > H || y < 1 || y > W)        return 1;    return 0;}void BFS1(){    queue <Point> q;    visit[start_point.x][start_point.y] = 1;    q.push(start_point);    while (!q.empty())    {        point = q.front();        q.pop();        if (map[point.x][point.y] == 4)        {            for (int q = 1; q <= sum; q++)                if (point.x == pp[q].x && point.y == pp[q].y)                {                    pp[q].step = point.step;                    break;                }            ii++;            if (ii == sum)                return ;            continue;        }        for (int ki = 0; ki < 4; ki++)        {            if (pd(point.x+dx[ki],point.y+dy[ki]))                continue;            if (map[point.x + dx[ki]][point.y + dy[ki]] == 1)                continue;            if (visit[point.x + dx[ki]][point.y+dy[ki]] == 1)                continue;            point1.x = point.x + dx[ki];            point1.y = point.y + dy[ki];            point1.step = point.step + 1;            q.push(point1);            visit[point1.x][point1.y] = 1;        }    }}void BFS2(){    queue <Point> q;    visit[end_point.x][end_point.y] = 1;    end_point.step = 0;    q.push(end_point);    while (!q.empty())    {        point = q.front();        //cout << point.x << " " << point.y << endl;        q.pop();        //cout << 1 << endl;        if (map[point.x][point.y] == 4)        {            int ttt;            for(int r = 1;r <= sum; r++)                if(point.x == pp[r].x && point.y == pp[r].y)                {                    ttt = pp[r].step;                    //cout << ttt << endl;                    break;                }            if(cnt > point.step + ttt)                cnt = point.step + ttt;            //cout << cnt << endl;            ii++;            if(ii == sum)                return;            continue;        }        for (int ki = 0; ki < 4; ki++)        {            if (pd(point.x+dx[ki],point.y+dy[ki]))                continue;            if (map[point.x + dx[ki]][point.y + dy[ki]] == 1)                continue;            if (visit[point.x + dx[ki]][point.y+dy[ki]] == 1)                continue;            point1.x = point.x + dx[ki];            point1.y = point.y + dy[ki];            point1.step = point.step + 1;            q.push(point1);            visit[point1.x][point1.y] = 1;        }    }}int main(){    scanf("%d%d",&W,&H);    sum = 0;    ii = 0;    cnt = 0x7fffffff;    for (i = 1; i <= H; i++)        for (j = 1; j <= W; j++)        {            scanf("%d",&map[i][j]);            if (map[i][j] == 2)            {                start_point.x = i;                start_point.y = j;                start_point.step = 0;            }            if (map[i][j] == 3)            {                end_point.x = i;                end_point.y = j;            }            if (map[i][j] == 4)            {                sum++;                pp[sum].x = i;                pp[sum].y = j;            }        }    memset(visit,0,sizeof(visit));    BFS1();    memset(visit,0,sizeof(visit));    ii = 0;    BFS2();    printf("%d\n",cnt);    return 0;}


原创粉丝点击