POJ 1101

The Game

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12028   Accepted: 3700

Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

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

Source

Mid-Central European Regional Contest 1999

大致题意:

棋盘方块游戏,X代表方块,空格代表空。两个方块不能通过其他方块相连(可通过棋盘外部和空格)

输入:

棋盘大小xy,棋盘内容,以及需要判断相连的两个方块坐标

输出:

若能相连,则输出需要几个步骤

否则输出impossible

解题思路:

BFS

用队列储存路径,用vis[][]储存走到当前位置所需要的步骤数

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
char a[99][99];
int x,y,sx,sy,ex,ey,CNT=0,cnt;
int xxx[]={1,-1,0,0},yyy[]={0,0,1,-1},vis[99][99];
//判断位置是否在范围内且是否进行过标记
bool check(int xx,int yy)
{
    if(xx<=x+1&&yy<=y+1&&xx>=0&&yy>=0&&a[xx][yy]!='X'&&!vis[xx][yy])return 1;
    return 0;
}
//宽搜,返回到达ex,ey所需步数
int bfs(int xx,int yy)
{
    queue <int> aa,bb;//储存路径
    aa.push(xx);bb.push(yy);//压入起点
    //宽搜
    while(!aa.empty())
    {
        int tempa=aa.front(),tempb=bb.front();//弹出当前位置
        aa.pop();bb.pop();
        //若到达终点,返回步数
        if(tempa==ex&&tempb==ey) return vis[tempa][tempb];
        //判断当前位置上下左右是否为空或边缘
        for(int i=0;i<=3;i++)
        {
            int qa=tempa,qb=tempb;
            while(check(qa+xxx[i],qb+yyy[i]))
            {
                qa+=xxx[i];qb+=yyy[i];
                vis[qa][qb]=vis[tempa][tempb]+1;//步数在前一个位置基础上加1
                aa.push(qa);bb.push(qb);//压入符合条件的位置
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d",&y,&x)&&x)
    {  
        memset(a,0,sizeof(a));
        CNT++;//棋盘个数
        cnt=0;//输入需要判断的条数
        printf("Board #%d:\n",CNT);
        for(int i=1;i<=x;i++)
        {
            for(int j=0;j<=y;j++)
            scanf("%c",&a[i][j]);
        }
        while(scanf("%d%d%d%d",&sy,&sx,&ey,&ex)&&sx)
        {
            cnt++;
            memset(vis,0,sizeof(vis)); 
            a[ex][ey]=' ';
            int temp=bfs(sx,sy);
            if(~temp)
                printf("Pair %d: %d segments.\n",cnt,temp);
            else printf("Pair %d: impossible.\n",cnt);
            a[ex][ey]='X';
        }
        printf("\n");
    }
}

深搜

#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()
{
	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;
}
 
发布了158 篇原创文章 · 获赞 34 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_40421671/article/details/95068259