2015计算机学科夏令营上机考试 G:The Game DFS能求出所有解(从而找到其中min_ans)而BFS只能求出第一个出现的解

总时间限制: 1000ms 内存限制: 65536kB题目
描述
One morning, you wake up and think: “I am such a good programmer. Why not make some money?” So you decide to write a computer game.
The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.

One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:

It consists of straight segments, each one being either horizontal or vertical.

It does not cross any other game pieces.

(It is allowed that the path leaves the board temporarily.)

Here is an example:
这里写图片描述

The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.

The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.
输入
The input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a “X” if there is a game piece at this location, and a space if there is no game piece.

Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing “0 0 0 0”.

The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.
输出
For each board, output the line “Board #n:”, where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with “Pair m: “, where m is the number of the pair (starting the count with 1 for each board). Follow this by “ksegments.”, where k is the minimum number of segments for a path connecting the two game pieces, or “impossible.”, if it is not possible to connect the two game pieces as described above.

Output a blank line after each board.
样例输入

5 4
XXXXX
X   X
XXX X
 XXX 
2 3 5 3
1 3 4 4
2 3 3 4
0 0 0 0
0 0

样例输出

Board #1:
Pair 1: 4 segments.
Pair 2: 3 segments.
Pair 3: impossible.

思路:
本题用单源点BFS求解迷宫问题,需要注意的是:
1 迷宫图的输入,横着是x竖着是y,处理的时候for循环需要注意
2 域外的路径可能有多个,怎么从中选择最小的一个?
代码(未ac):

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<stack>
using namespace std;
//1 输入有点小技巧 2 域外的怎么处理也就是多个可能路径怎么找最小值
int tt=1;
struct node
{
    int x,y;
    int label;
    int step;
}S,E,Node;
int X[]={0,1,0,-1};
int Y[]={-1,0,1,0};
const int MAX = 77;
int w,h,ans=0;
char G[MAX][MAX];
bool visit[MAX][MAX]={false};
bool check(int x,int y)
{
    if(x>w+1||x<0||y>h+1||y<0) return 0;
    if(visit[x][y]==1||G[x][y]=='X') return 0;
    return 1;
}

int BFS()
{
    queue<node > q;
    q.push(S);
    visit[S.x][S.y]=1;
    int tag=1;
    while(!q.empty())
    {
        node temp =q.front();
        q.pop();
        if(temp.x==E.x&&temp.y==E.y) return temp.step;
        if(tag)
        {
            tag=0;
            for(int i=0;i<4;i++)
            {
                int newx= temp.x+X[i];
                int newy= temp.y+Y[i];
                if(check(newx,newy))
                {
                    Node.x=newx;Node.y=newy;
                    if(i%2==0) Node.label=0;//纵向
                    else Node.label=1;//横向
                    Node.step=1;
                    visit[newx][newy]=1;
                    q.push(Node);
                    ans++;//从起点出发一定+1
                }
            }
            continue;
        }
        for(int i=0;i<4;i++)
            {
                int newx= temp.x+X[i];
                int newy= temp.y+Y[i];
                if(check(newx,newy))
                {
                    Node.x=newx;Node.y=newy;
                    if(i%2==0) Node.label=0;//横向
                    else Node.label=1;//纵向
                    visit[newx][newy]=1;

                    if(temp.label!=(i%2))
                    {
                        ans++;//转向就+1
                        Node.step=temp.step+1;
                    }
                    else Node.step=temp.step;
                    q.push(Node);
                }
            }
    }
    return -1;
}
int main()
{
    //freopen("input.txt","r",stdin);
    while(cin>>w>>h)
    {
        if(w==0&&h==0) break;
        //输入
        getchar();
        for(int i=1;i<=h;i++)
            {
                string s;
                getline(cin,s);
                int cnt=0;
                for(int j=1;j<=w;j++)
                {
                    G[j][i]=s[cnt++];
                }
            }
            /*
         for(int i=1;i<=h;i++)
         {
             for(int j=1;j<=w;j++)
            {
                cout<<G[j][i];
            }
            cout<<endl;
         }
*/
     //输出
        int x1,y1,x2,y2;
        int ttt=1;
        cout<<"Board #"<<tt++<<":"<<endl;
        while(cin>>x1>>y1>>x2>>y2)
        {

            if(x1==0&&y1==0&&x2==0&&y2==0) break;
            ans=0;
            fill(*visit,*visit+MAX*MAX,false);
            S.x=x1;S.y=y1;E.x=x2;E.y=y2;
            G[x2][y2]=' ';
            int a=BFS();
            G[x2][y2]='X';
            if(a!=-1) cout<<"Pair "<<ttt++<<": "<<a<<" segments."<<endl;
            else cout<<"Pair "<<ttt++<<": impossible."<<endl;
        }
    }
    return 0;
}

原因:
本题要保证最短步数,也就是尽最大可能走直线,这符合DFS的思想,因为DFS在for里每次都是先对某一个方向递归,而BFS的队列中依序排放的是四个方向的node,这样可能导致路径长的先被计算出来。
ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

//int flag=0;//记录两点之间是否有路径
const int INF=0x3f3f3f3f;//表示正无穷
int Min=INF;//用来记录最小拐弯次数
int now=0;//用来记录每条路径拐弯次数

char game1[80][80];//记录矩阵初始状态
int  game2[80][80];//用于记录路径方向

int w,h;//表示矩阵的宽和高
int x2,y2;//表示终点

//深度搜索,搜出所有路径,并记录每个路径拐点的个数
void bfs(int x1,int y1)//(x1,y1)表示开始位置
{
    //递归终止条件【搜索到(x2,y2)处】
    if(now<Min&&x1==x2&&y1==y2)
    {
        Min=now;
        return;
    }

    //大于最优,直接跳出
    if(now>Min) return;

    //分别用1、2、3、4表示来自上、下、左、右
    int dx[4]={-1,1,0,0};
    int dy[4]={0,0,-1,1};

    for(int i=0;i<4;i++)//对四个方向进行遍历
    {
        if((y1+dy[i]>=0&&y1+dy[i]<=h+1&&x1+dx[i]>=0&&x1+dx[i]<=w+1&&game1[y1+dy[i]][x1+dx[i]]!='X'&&game2[y1+dy[i]][x1+dx[i]]==0)||(x1+dx[i]==x2&&y1+dy[i]==y2))
        {
            int rflag=0;//假设此处不是拐点
            game2[y1+dy[i]][x1+dx[i]]=i+1;
            //判断是否出现拐点
            if(game2[y1][x1]!=game2[y1+dy[i]][x1+dx[i]])
            {
                now++;
                rflag=1;
            }

            //继续递归
            bfs(x1+dx[i],y1+dy[i]);
            //now恢复原值
            if(rflag==1) now--;
            game2[y1+dy[i]][x1+dx[i]]=0;
        }
    }
}

int main()
{
    freopen("input.txt","r",stdin);
    int board=1;//表示第board组数据
    for(;;)
    {
        scanf("%d%d",&w,&h);
        gets(game1[0]);
        if(w==0&&h==0) break;
        memset(game2,0,sizeof(game2));

         for (int i=0;i<=h+2;i++)
        {
            for (int j=0;j<=w+2;j++)
            {
                game1[i][j]=' ';
            }
        }
        for (int i=1;i<=h;i++)
        {
            gets(game1[i]);//输入每一行的初始状态
            for (int j=w;j>=0;j--)
            {
                game1[i][j+1]=game1[i][j];
            }
            game1[i][0]=' ';
        }

        printf("Board #%d:\n",board++);
        int x1,y1;
        int pair=1;//表示第pair对数据
        for(;;)
        {
            //初始化全局变量
            Min=INF;
            now=0;
            //flag=0;

            cin>>x1>>y1>>x2>>y2;
            if(x1==0&&x2==0&&y1==0&&y2==0)
            {
                cout<<endl;
                break;
            }
            printf("Pair %d: ",pair++);
            bfs(x1,y1);
            if(Min==INF) printf("impossible.\n");
            else printf("%d segments.\n",Min);
        }
    }
    return 0;
}

没理解的地方:

//now恢复原值
            if(rflag==1) now--;
            game2[y1+dy[i]][x1+dx[i]]=0;

这一步是为什么。//恢复原值以待for的下一个循环

猜你喜欢

转载自blog.csdn.net/zongza/article/details/80209672