搜索 HOJ 1273 Peg Game

Peg Game

My Tags   (Edit)
  Source : ACM ICPC Greater New York 2003
  Time limit : 1 sec   Memory limit : 32 M

Submitted : 73, Accepted : 31

You are given a 7-by-7 board of holes. Some holes are filled with pegs, and some are not. You may jump a peg over an adjacent peg, as long as the hole the jumping peg lands in is unoccupied. The jumped peg is removed. Your goal is to leave the board with only one peg in it, and the peg must end up in the specified location.

The board is specified as a 7-by-7 array of characters, with the following meanings:

x: this hole may never be occupied by a peg
e: this hole is initially empty
o: this hole is initially occupied by a peg
E: this hole is initially empty, and the last peg should end here
O: this hole is initially occupied, and the last peg should end here

For example, consider the following board:

x x e e e x x
x x o e e x x
e e o e e e e
e e o O e e e
e e e e e e e
x x e e e x x
x x e e e x x

You can see that there are initially 4 pegs in the board, and the last peg should end up in the middle of the board. The winning sequence of moves is:

1. (4, 4) to (2, 4)
2. (3, 2) to (3, 4)
3. (2, 4) to (4, 4)

Where coordinates are given as (x, y).


Input

The first line of input is the number of datasets to follow. Each dataset should be processed the same.

The input for each dataset consists of 7 lines; each line consists of 7 characters from the set {x, e, o, E, O} with blanks between them. You are guaranteed that exactly one 'E' or 'O' will appear, and that two or more 'o' or 'O' will appear.


Output

For each dataset, output a line containing the data set number. If a sequence of valid moves exists that leaves only one peg on the board, and leaves that peg in the desired location, print out the sequence of moves, as shown in the above example. If no sequence exists, print "No solution". Leave a blank line between datasets.


Sample Input

2
x x e e e x x
x x o e e x x
e e o e e e e
e e o O e e e
e e e e e e e
x x e e e x x
x x e e e x x
x x e E e e e
x e e e e e e
e e e o o e e
e e e x e e e
e e e e e e e
e e e e e e e
e e e e e e e


Sample Output

Dataset 1:
1. (4, 4) to (2, 4)
2. (3, 2) to (3, 4)
3. (2, 4) to (4, 4)

Dataset 2:
No solution.

题意:就是一个跳棋的游戏,一个棋子可以越过一个且仅一个棋子跳到对面没有棋子的地方,被跨越的棋子就会去掉,问怎么玩才能最后只剩下一个棋子,并且这个棋子要在指定位置。

思路:对所有情况进行搜索就行了,如果一个棋子能够移动,就把棋盘状态改变一下搜下去,如果发现搜下去不行,就把棋盘状态恢复,然后换一个棋子搜,其实就是回溯。

代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<cstdio>
#include<string.h>
#include<cstring>
using namespace std;
#define mp make_pair
char G[15][15];
char buffer[4];
int Move[2][4] = { {0,1,0,-1} , {1,0,-1,0} };
int dest_r , dest_c , cnt , Cas = 0;
typedef pair<int,int> node;
node stk[1000];
int top;
void init()
{
	top = cnt = 0;
	for (int i = 0 ; i < 15 ; ++i)
		for (int j = 0 ; j < 15 ; ++j) 
			G[i][j] = 'x';
}

void input()
{
	for (int i = 2 ; i < 2+7 ; ++i)
	{
		for (int j = 2 ; j < 9 ; ++j)
		{
			scanf("%s",buffer);
			G[i][j] = buffer[0];
		}
	}
	for (int i = 2 ; i < 9 ; ++i) 
	{
		for (int j = 2 ; j < 9 ; ++j)
		{
			if (G[i][j]=='E' || G[i][j]=='O')
				dest_r = i , dest_c = j;
			if (G[i][j]=='o' || G[i][j]=='O') 
				++cnt , G[i][j] = 'o';
			if (G[i][j]=='E') G[i][j] = 'e';
		}
	}
}

bool dfs(int rest)
{
	if (rest==1)
	{
		if (G[dest_r][dest_c]=='o') return true;
		return false;
	}
	for (int i = 2 ; i < 9 ; ++i)
	{
		for (int j = 2 ; j < 9 ; ++j) if (G[i][j]=='o')
		{
			for (int k = 0 ; k < 4 ; ++k)
			{
				int r = i+Move[0][k];
				int c = j+Move[1][k];
				if (G[r][c]!='o') continue;
				int rr = r+Move[0][k];
				int cc = c+Move[1][k];
				if (G[rr][cc]!='e') continue;
				G[i][j] = 'e';
				G[r][c] = 'e';
				G[rr][cc] = 'o';
				if (dfs(rest-1))
				{
					stk[top++] = mp(rr-1,cc-1);
					stk[top++] = mp(i-1,j-1);
					return true;
				}
				G[rr][cc] = 'e';
				G[r][c] = 'o';
				G[i][j] = 'o';
			}
		}
	}
	return false;
}

void solve()
{
	if (!dfs(cnt))
		printf("No solution.\n");
	else 
	{
		int step = 1;
		for (int i = top-1 ; i >= 1 ; i -= 2)
			printf("%d. (%d, %d) to (%d, %d)\n",step,stk[i].second,stk[i].first,stk[i-1].second,stk[i-1].first);
	}
}

int main()
{
	int T;
	cin>>T;
	while (T--)
	{
		++Cas;
		printf("Dataset %d:\n",Cas);
		init();
		input();
		solve();
		if (T) printf("\n");
	}
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值