poj1101_The Game

原创 2013年12月05日 19:38:29
The Game
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8573   Accepted: 2611

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.

分析:简单广搜, 记录上一个状态的方向。注意:起点和终点相邻时,为一个段。

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

struct Data
{
	int x, y, sum, num;
	int xiang;//记录方向
};

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dd[] = {0, 2, 3, 1};//方向数组
bool visit[80][80];
char map[80][80];
int n, m;
int mmin = 100000;//最少段

queue<Data>q;

int bfs(int x, int y, int a, int b)
{
	if (x == a && y == b)
		return 0;
	Data data, c;
	data.x = x;
	data.y = y;
	data.sum = 0;
	data.num = 0;
	visit[x][y] = 1;
	q.push(data);
	while (!q.empty())
	{
		data = q.front();
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			c.x = data.x + dx[i];
		    c.y = data.y + dy[i];
			c.xiang = dd[i];
			if (c.x == a && c.y == b)
			{
				if (data.sum != 0 && c.xiang != data.xiang)
					c.num = data.num + 1;
				else
					c.num = data.num;
				if (mmin > c.num + 1)
					mmin = c.num + 1;
			}
			if (c.x >= 0 && c.y >= 0 && c.x <= n + 1 && c.y <= m + 1 && map[c.x][c.y] != 'X' && !visit[c.x][c.y])
			{
				if (data.sum != 0 && c.xiang != data.xiang)
					c.num = data.num + 1;
				else 
					c.num = data.num;
				c.sum = data.sum + 1;
				visit[c.x][c.y] = 1;
				q.push(c);
			}
		}
	}			
	if (mmin != 100000)
		return mmin;
	else
		return -1;
}

int main()
{
	int cun = 1;
	while (scanf("%d%d", &m, &n) != EOF && n != 0 && m != 0)
	{
		getchar();
		memset(map, 0, sizeof(map));
		memset(visit, 0, sizeof(visit));
		while (!q.empty())
			q.pop();
		int i, j;
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= m; j++)
				map[i][j] = getchar();
			getchar();
		}
        printf("Board #%d:\n", cun++);
		int x_st, y_st, x_end, y_end;
		int cot = 1;
		while (scanf("%d%d%d%d", &y_st, &x_st, &y_end, &x_end) == 4 && x_st != 0 && y_st != 0)
		{
			mmin = 100000;
			memset(visit, 0, sizeof(visit));
            while (!q.empty())
				q.pop();
			if ((x_st - y_st == 1 || x_st - y_st == -1) && x_end == y_end || (x_end - y_end == 1 || y_end - x_end == -1) && x_st == y_st)
			{
				printf("Pair %d: 1 segments.\n", cot++);
				continue;
			}
			int k = bfs(x_st, y_st, x_end, y_end);
			if (k == -1)
				printf("Pair %d: impossible.\n", cot++);
			else 
				printf("Pair %d: %d segments.\n", cot++, k);
		}
		printf("\n");
	}
	return 0;
}


iOS第三方验证简单接入Game Center

1. 从2016年开始,Game Center已经取消了沙箱机制 2. 要开启Game Center,需要在iTunes Connect的Game Center处,新建一个排行榜或者成就。这个我觉得...
  • qq393830887
  • qq393830887
  • 2017年01月04日 19:59
  • 3486

IOS平台游戏如何对接GameCenter

现在随着手游市场的不断扩大,活跃在IOS平台下的游戏厂商为了增加玩家的粘性,以及为自己的游戏前途考虑都纷纷开始接入苹果国内公司的游戏平台GameCenter;今天呢我就把我对接GameCenter的一...
  • shenjie12345678
  • shenjie12345678
  • 2015年04月13日 17:41
  • 21495

关于博弈基础知识的总结:巴什博弈(Bash Game)、威佐夫博奕(Wythoff Game)、尼姆博奕(Nim Game)

文章PDF文档:http://www.kuaipan.cn/file/id_32020291156705322.html 博弈论是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策略,达到取...
  • chao1983210400
  • chao1983210400
  • 2013年08月24日 20:24
  • 3102

POJ 1101 The Game(BFS+判方向)

The Game Description One morning, you wake up and think: "I am such a good programmer. Why not...
  • u013986860
  • u013986860
  • 2014年07月04日 15:09
  • 1126

poj1101-The Game(BFS)

题目链接:点击打开链接 题意: 有一个矩阵,有的地方为X,求从某一个X,所在位置为(x1, y1)到另一个X,所在位置为(x2, y2)的折线,使得线段最少,如(2, 3)到(5, 3)所连成的折...
  • qq_33279781
  • qq_33279781
  • 2017年03月15日 00:50
  • 132

POJ1101The Game

刚刚接触做这道题时,我也被那副"连连看"吓了一下,不过慢慢地分析才发现这道题也很简单,可能是测试数据太水了吧~~~不过我是一次做对(但不是一次ac,因为有两次PE。。。。。。       不过做这道...
  • zhangmin4235
  • zhangmin4235
  • 2013年05月28日 19:41
  • 592

Poj 1101 The Game(广搜,优先级:优先队列或者其他)

/************************************************************************* The Game Time Limit...
  • u012590854
  • u012590854
  • 2013年12月06日 21:00
  • 725

poj 1101 The Game

/* Name:poj 1101 The Game Author: UnimenSun Date: 05/05/2011 23:15 Description: BFS*//*解题报告:BFS1...
  • Unimen
  • Unimen
  • 2011年06月18日 18:25
  • 672

poj1101The Game(记忆化搜索)

题目请戳这里 题目大意:给一个n*m的地图,由'X'和空格组成,'X'表示物体,'.'表示空地。再给若干组数据,每组数据为2个坐标,问这2个坐标点之间能否连同,如果能,需要多少线段使之联通。可以借助...
  • ophunter
  • ophunter
  • 2013年12月23日 16:13
  • 920

POJ 1101 The Game (bfs)

题目连接: ~( ̄▽ ̄~)(~ ̄▽ ̄)~ 相邻的两个的segments为1,注意格式问题,wa了半天就在这2点上,郁闷 code: #include #include int X1...
  • ulquiorra0cifer
  • ulquiorra0cifer
  • 2012年04月02日 16:24
  • 296
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj1101_The Game
举报原因:
原因补充:

(最多只允许输入30个字)